Skip to content

Commit

Permalink
Improved intendation parsing. Now it in a same way as Godot
Browse files Browse the repository at this point in the history
  • Loading branch information
elamaunt committed May 5, 2024
1 parent 3c7952c commit 09c8add
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 95 deletions.
5 changes: 4 additions & 1 deletion src/GDShrapt.Reader.Tests/AssertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ internal static void NoInvalidTokens(GDNode node)

messageBuilder.AppendLine();
for (int i = 0; i < invalidTokens.Length; i++)
messageBuilder.AppendLine((i+1) + ". " + invalidTokens[i]);
{
var token = invalidTokens[i];
messageBuilder.AppendLine($"{token.StartLine}.{token.StartColumn}: " + token);
}

Assert.AreEqual(0, invalidTokens.Length, messageBuilder.ToString(), "There are invalid tokens in the code");
}
Expand Down
79 changes: 71 additions & 8 deletions src/GDShrapt.Reader.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,17 +1895,17 @@ func _init(
}
match b:
0:
0:
pass
var t:
for i in range(10) :
pass
var t:
for i in range(10) :
var c = b + a + i + t
var c = b + a + i + t
new_method()
new_method()
print(c)
print(c)
print(""done"")
Expand Down Expand Up @@ -2562,7 +2562,7 @@ public void StatementAtributesTest2()
}

[TestMethod]
public void ParseGetNodeNewSyntax()
public void ParseGetUniqueNodeTest()
{
var reader = new GDScriptReader();

Expand Down Expand Up @@ -2788,5 +2788,68 @@ class_name MyClass extends Node
AssertHelper.CompareCodeStrings(code, @class.ToString());
AssertHelper.NoInvalidTokens(@class);
}

[TestMethod]
public void ParseGetNodeWithOnreadyTest()
{
var reader = new GDScriptReader();

var code = @"@onready var sprite2d = $Sprite2D
@onready var animation_player = $ShieldBar/AnimationPlayer";

var @class = reader.ParseFileContent(code);

AssertHelper.CompareCodeStrings(code, @class.ToString());
AssertHelper.NoInvalidTokens(@class);
}

[TestMethod]
public void ParseDifferentIntendationsTest()
{
var reader = new GDScriptReader();

var code = @"var prop4:
set=_setter,
get = _getter
func _setter(x):
var f = func():
pass
var f2 = func():
var x2 = 10
var f3 = func():
pass
pass
var f5 = func():
pass
pass
func _getter():
return 0
func _init(x):
pass
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
";

var @class = reader.ParseFileContent(code);

Assert.AreEqual(1, @class.Variables.Count());
Assert.AreEqual(4, @class.AllNodes.Where(x => x is GDMethodExpression).Count());
Assert.AreEqual(5, @class.Methods.Count());

AssertHelper.CompareCodeStrings(code, @class.ToString());
AssertHelper.NoInvalidTokens(@class);
}
}
}
4 changes: 2 additions & 2 deletions src/GDShrapt.Reader/Basics/GDSyntaxToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,6 @@ public List<GDIdentifier> ExtractAllMethodScopeVisibleDeclarationsFromParents(in

while (true)
{
node = node.Parent;

if (node == null || node is GDClassDeclaration || node is GDInnerClassDeclaration)
break;

Expand All @@ -500,6 +498,8 @@ public List<GDIdentifier> ExtractAllMethodScopeVisibleDeclarationsFromParents(in

foreach (var item in node.GetMethodScopeDeclarations(beforeLine))
results.Add(item);

node = node.Parent;
}

return results;
Expand Down
10 changes: 5 additions & 5 deletions src/GDShrapt.Reader/Declarations/GDVariableDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ internal override void HandleChar(char c, GDReadingState state)
case State.Comma:
state.PushAndPass(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, this, true, true, Intendation + 1), c);
return;
//case State.SecondAccessorDeclarationNode:
// state.PushAndPass(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, this, Comma != null, false, Intendation + 1), c);
// break;
case State.SecondAccessorDeclarationNode:
state.PushAndPass(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, this, Comma != null, false, Intendation + 1), c);
break;
default:
this.HandleAsInvalidToken(c, state, x => x.IsNewLine());
break;
Expand All @@ -184,11 +184,11 @@ internal override void HandleNewLineChar(GDReadingState state)
return;
}

/*if (_form.State == State.SecondAccessorDeclarationNode)
if (_form.State == State.SecondAccessorDeclarationNode)
{
state.PushAndPassNewLine(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, this, Comma != null, false, Intendation + 1));
return;
}*/
}

state.PopAndPassNewLine();
}
Expand Down
6 changes: 3 additions & 3 deletions src/GDShrapt.Reader/GDReadSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
public class GDReadSettings
{
/// <summary>
/// Read 4 spaces when intendation is calculated into tabs.
/// Default value is TRUE
/// Amount of spaces that equals a single tabulation
/// Default value is 4
/// </summary>
public bool ReadFourSpacesAsIntendation { get; set; } = true;
public int SingleTabSpacesCost { get; set; } = 4;

public int ReadBufferSize { get; set; } = 1024;

Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/GDReadingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ internal class GDReadingState
readonly Stack<GDReader> _readersStack = new Stack<GDReader>(64);
GDReader CurrentReader => _readersStack.PeekOrDefault();

/// <summary>
/// The first intendation in the code is used for the next ones as a pattern like in the Godot's editor.
/// </summary>
public int? IntendationInSpacesCount { get; set; } = null;

public GDReadingState(GDReadSettings settings)
{
Settings = settings;
Expand Down
6 changes: 3 additions & 3 deletions src/GDShrapt.Reader/GDShrapt.Reader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Version>4.2.0-alpha</Version>
<Version>4.3.0-alpha</Version>
<Authors>elamaunt</Authors>
<Product>GDShrapt</Product>
<Description>GDShrapt.Reader is .Net library and object-oriented one-pass parser of GDScript. It can build a lexical tree of GDScript code or generate a new code from scratch.
Expand All @@ -16,8 +16,8 @@ Usage: Just create a GDScriptReader instance and call methods from it.</Descript
<RepositoryUrl>https://github.com/elamaunt/GDShrapt</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>GDShrapt GDScript reader parser codegeneration Godot lexical analyzer</PackageTags>
<AssemblyVersion>4.2.0</AssemblyVersion>
<FileVersion>4.2.0</FileVersion>
<AssemblyVersion>4.3.0</AssemblyVersion>
<FileVersion>4.3.0</FileVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down
14 changes: 7 additions & 7 deletions src/GDShrapt.Reader/Resolvers/GDClassMembersResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private void Complete(GDReadingState state, string sequence)

if (sequence[0] == '@')
{
Owner.HandleReceivedToken(state.Push(new GDCustomAttribute(LineIntendationThreshold)));
Owner.HandleReceivedToken(state.Push(new GDCustomAttribute(CurrentResolvedIntendationInSpaces)));

for (int i = 0; i < sequence.Length; i++)
state.PassChar(sequence[i]);
Expand Down Expand Up @@ -229,14 +229,14 @@ void HandleStaticIfMet(ITokenReceiver<GDSpace> spaceReceiver, Action push, bool
break;
case "signal":
{
var m = new GDSignalDeclaration(LineIntendationThreshold);
var m = new GDSignalDeclaration(CurrentResolvedIntendationInSpaces);
HandleStaticIfMet(m, () => Owner.HandleReceivedToken(state.Push(m)));
m.Add(new GDSignalKeyword());
}
break;
case "enum":
{
var m = new GDEnumDeclaration(LineIntendationThreshold);
var m = new GDEnumDeclaration(CurrentResolvedIntendationInSpaces);
HandleStaticIfMet(m, () => Owner.HandleReceivedToken(state.Push(m)));
m.Add(new GDEnumKeyword());
}
Expand All @@ -262,28 +262,28 @@ void HandleStaticIfMet(ITokenReceiver<GDSpace> spaceReceiver, Action push, bool
break;
case "func":
{
var m = new GDMethodDeclaration(LineIntendationThreshold);
var m = new GDMethodDeclaration(CurrentResolvedIntendationInSpaces);
HandleStaticIfMet(m, () => Owner.HandleReceivedToken(state.Push(m)), false);
m.Add(new GDFuncKeyword());
}
break;
case "const":
{
var m = new GDVariableDeclaration(LineIntendationThreshold);
var m = new GDVariableDeclaration(CurrentResolvedIntendationInSpaces);
HandleStaticIfMet(m, () => Owner.HandleReceivedToken(state.Push(m)));
m.Add(new GDConstKeyword());
}
break;
case "var":
{
var m = new GDVariableDeclaration(LineIntendationThreshold);
var m = new GDVariableDeclaration(CurrentResolvedIntendationInSpaces);
HandleStaticIfMet(m, () => Owner.HandleReceivedToken(state.Push(m)), false);
m.Add(new GDVarKeyword());
}
break;
case "class":
{
var m = new GDInnerClassDeclaration(LineIntendationThreshold);
var m = new GDInnerClassDeclaration(CurrentResolvedIntendationInSpaces);
HandleStaticIfMet(m, () => Owner.HandleReceivedToken(state.Push(m)));
m.Add(new GDClassKeyword());
}
Expand Down
2 changes: 1 addition & 1 deletion src/GDShrapt.Reader/Resolvers/GDElifResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected override void OnFail(GDReadingState state)

protected override void OnMatch(GDReadingState state)
{
var branch = new GDElifBranch(LineIntendationThreshold);
var branch = new GDElifBranch(CurrentResolvedIntendationInSpaces);

branch.Add(new GDElifKeyword());

Expand Down
2 changes: 1 addition & 1 deletion src/GDShrapt.Reader/Resolvers/GDElseResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected override void OnFail(GDReadingState state)

protected override void OnMatch(GDReadingState state)
{
var branch = new GDElseBranch(LineIntendationThreshold);
var branch = new GDElseBranch(CurrentResolvedIntendationInSpaces);

branch.Add(new GDElseKeyword());

Expand Down
Loading

0 comments on commit 09c8add

Please sign in to comment.