Skip to content

Commit

Permalink
Fixed comments. Implemented node paths. Fixed exports reading. Added …
Browse files Browse the repository at this point in the history
…new tests. All tests have been passed.
  • Loading branch information
elamaunt committed Jun 22, 2021
1 parent aaa6898 commit bcb2c7d
Show file tree
Hide file tree
Showing 38 changed files with 1,021 additions and 273 deletions.
2 changes: 1 addition & 1 deletion src/GDShrapt.Reader.Tests/AssertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace GDShrapt.Reader.Tests
{
public static class AssertHelper
{
internal static void CompareStrings(string s1, string s2)
internal static void CompareCodeStrings(string s1, string s2)
{
Assert.AreEqual(
s1.Replace("\r", "").Replace(" ", "\t"),
Expand Down
1 change: 1 addition & 0 deletions src/GDShrapt.Reader.Tests/GDShrapt.Reader.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
Expand Down
338 changes: 295 additions & 43 deletions src/GDShrapt.Reader.Tests/ParsingTests.cs

Large diffs are not rendered by default.

78 changes: 75 additions & 3 deletions src/GDShrapt.Reader.Tests/SyntaxTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace GDShrapt.Reader.Tests
{
Expand Down Expand Up @@ -41,7 +43,36 @@ func get_recognized_extensions(res): # func comment
var @class = reader.ParseFileContent(code);

Assert.IsNotNull(@class);
AssertHelper.CompareStrings(code, @class.ToString());

var comments = @class.AllTokens
.OfType<GDComment>()
.Select(x => x.ToString())
.ToArray();

Assert.AreEqual(17, comments.Length);

comments.Should().BeEquivalentTo(new[]
{
"# before tool comment",
"# tool comment",
"# before class name comment",
"# class name comment",
"# before extends comment",
"# extends comment",
"# before const comment",
"# const comment",
"# before func comment 1",
"# before func comment 2",
"# func comment",
"# before if statement comment",
"# if expression comment",
"# before return statement comment",
"# if true statement comment",
"# end file comment 1",
"# end file comment 2"
});

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

[TestMethod]
Expand All @@ -63,7 +94,48 @@ enum { a, # a comment
var @class = reader.ParseFileContent(code);

Assert.IsNotNull(@class);
AssertHelper.CompareStrings(code, @class.ToString());

var comments = @class.AllTokens
.OfType<GDComment>()
.Select(x => x.ToString())
.ToArray();

Assert.AreEqual(8, comments.Length);

comments.Should().BeEquivalentTo(new[]
{
"# before enum comment",
"# a comment",
"# before b comment",
"# b comment",
"# before c comment",
"# c comment}",
"# after c comment",
"# enum ending comment"
});

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

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

var code = @"
{
a: 0,
""1"": x # : x [1,2,3] + d = l
f + d = lkj :[1,2,3]
# : xa: 0,
}";

var expression = reader.ParseExpression(code);
Assert.IsNotNull(expression);
Assert.IsInstanceOfType(expression, typeof(GDDictionaryInitializerExpression));
Assert.AreEqual(3, ((GDDictionaryInitializerExpression)expression).KeyValues.Count);
Assert.AreEqual(2, expression.AllTokens.OfType<GDComment>().Count());
AssertHelper.CompareCodeStrings(code, "\n"+expression.ToString());
}
}
}
23 changes: 20 additions & 3 deletions src/GDShrapt.Reader/Basics/GDNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GDShrapt.Reader
/// <summary>
/// Basic GDScript node, may contains multiple tokens
/// </summary>
public abstract partial class GDNode : GDSyntaxToken, IStyleTokensReceiver
public abstract class GDNode : GDSyntaxToken, IStyleTokensReceiver
{
internal abstract GDTokensForm Form { get; }

Expand All @@ -17,19 +17,36 @@ public IEnumerable<GDSyntaxToken> AllTokens
{
get
{
foreach (var token in Tokens)
foreach (var token in Form)
{
if (token is GDNode node)
{
foreach (var nodeToken in node.AllTokens)
yield return token;
yield return nodeToken;
}
else
yield return token;
}
}
}

public IEnumerable<GDNode> AllNodes
{
get
{
foreach (var token in Form)
{
if (token is GDNode node)
{
yield return node;

foreach (var nodeToken in node.AllNodes)
yield return nodeToken;
}
}
}
}

/// <summary>
/// Removes child node or does nothing if node is already removed.
/// </summary>
Expand Down
8 changes: 0 additions & 8 deletions src/GDShrapt.Reader/Basics/Receiving/IPathReceiver.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/GDShrapt.Reader/Declarations/GDEnumValueDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ public GDIdentifier Identifier
get => _form.Token0;
set => _form.Token0 = value;
}

internal GDColon Colon
{
get => (GDColon)_form.Token1;
set => _form.Token1 = value;
}

internal GDAssign Assign
{
get => (GDAssign)_form.Token1;
set => _form.Token1 = value;
}

public GDExpression Value
{
get => _form.Token2;
Expand Down
23 changes: 13 additions & 10 deletions src/GDShrapt.Reader/Declarations/GDExportDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,23 @@ public GDExportDeclaration()

internal override void HandleChar(char c, GDReadingState state)
{
if (IsSpace(c))
{
_form.AddBeforeActiveToken(state.Push(new GDSpace()));
state.PassChar(c);
return;
}

switch (_form.State)
{
case State.Export:
this.ResolveKeyword(c, state);
if (!this.ResolveStyleToken(c, state))
this.ResolveKeyword(c, state);
break;
case State.OpenBracket:
this.ResolveOpenBracket(c, state);
if (!this.ResolveStyleToken(c, state))
this.ResolveOpenBracket(c, state);
break;
case State.Parameters:
_form.State = State.CloseBracket;
state.PushAndPass(Parameters, c);
break;
case State.CloseBracket:
this.ResolveCloseBracket(c, state);
if (!this.ResolveStyleToken(c, state))
this.ResolveCloseBracket(c, state);
break;
default:
state.PopAndPass(c);
Expand All @@ -73,6 +69,13 @@ internal override void HandleChar(char c, GDReadingState state)

internal override void HandleNewLineChar(GDReadingState state)
{
if (_form.State == State.Parameters)
{
_form.State = State.CloseBracket;
state.PushAndPassNewLine(Parameters);
return;
}

state.PopAndPassNewLine();
}

Expand Down
Loading

0 comments on commit bcb2c7d

Please sign in to comment.