Skip to content

Commit

Permalink
Fixed dictionary with assignment parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
elamaunt committed Jan 25, 2024
1 parent 8ac3be9 commit 6d20948
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
33 changes: 33 additions & 0 deletions src/GDShrapt.Reader.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Security.Claims;
using System.Threading;
using System.Xml.Linq;
using static GDShrapt.Reader.GD;

namespace GDShrapt.Reader.Tests
Expand Down Expand Up @@ -2650,5 +2651,37 @@ static func my_int_function() -> int:
AssertHelper.CompareCodeStrings(code, @class.ToString());
AssertHelper.NoInvalidTokens(@class);
}

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

var code = @"
static var a = { a = 1, b = 2 }";

var @class = reader.ParseFileContent(code);

Assert.AreEqual(1, @class.Members.Count);

var variable = (GDVariableDeclaration)@class.Members[0];

Assert.IsInstanceOfType(variable.Initializer, typeof(GDDictionaryInitializerExpression));

var dictionary = (GDDictionaryInitializerExpression)variable.Initializer;

Assert.AreEqual(2, dictionary.KeyValues.Count);

foreach (var node in dictionary.KeyValues)
{
Assert.IsNotNull(node.Key);
Assert.IsNotNull(node.Assign);
Assert.IsNotNull(node.Value);
Assert.IsInstanceOfType(node.Value, typeof(GDNumberExpression));
}

AssertHelper.CompareCodeStrings(code, @class.ToString());
AssertHelper.NoInvalidTokens(@class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal override void HandleChar(char c, GDReadingState state)
switch (_form.State)
{
case State.Key:
this.ResolveExpression(c, state, _intendation);
this.ResolveExpression(c, state, _intendation, allowAssignment: false);
break;
case State.ColonOrAssign:
if (!_checkedColon)
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.1.3-alpha</Version>
<Version>4.1.4-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.1.3</AssemblyVersion>
<FileVersion>4.1.3</FileVersion>
<AssemblyVersion>4.1.4</AssemblyVersion>
<FileVersion>4.1.4</FileVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down
18 changes: 10 additions & 8 deletions src/GDShrapt.Reader/Resolvers/GDExpressionResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,21 @@ internal class GDExpressionResolver : GDResolver,
bool _ifExpressionChecked;
bool _isCompleted;
readonly int _intendation;
readonly bool _allowAssignment;

new ITokenReceiver<GDExpression> Owner { get; }
ITokenSkipReceiver<GDExpression> OwnerWithSkip { get; }
INewLineReceiver NewLineReceiver { get; }
public bool IsCompleted => _isCompleted;

public GDExpressionResolver(ITokenOrSkipReceiver<GDExpression> owner, int intendation, INewLineReceiver newLineReceiver = null)
public GDExpressionResolver(ITokenOrSkipReceiver<GDExpression> owner, int intendation, INewLineReceiver newLineReceiver = null, bool allowAssignment = true)
: base(owner)
{
Owner = owner;
OwnerWithSkip = owner;
NewLineReceiver = newLineReceiver;
_intendation = intendation;
}

public GDExpressionResolver(ITokenReceiver<GDExpression> owner, int intendation)
: base(owner)
{
Owner = owner;
_intendation = intendation;
_allowAssignment = allowAssignment;
}

internal override void HandleChar(char c, GDReadingState state)
Expand Down Expand Up @@ -213,6 +208,13 @@ internal override void HandleChar(char c, GDReadingState state)
return;
}

if (c == '=' && !_allowAssignment)
{
CompleteExpression(state);
state.PassChar(c);
return;
}

PushAndSwap(state, new GDDualOperatorExpression(_intendation, NewLineReceiver != null));
state.PassChar(c);
}
Expand Down
4 changes: 2 additions & 2 deletions src/GDShrapt.Reader/Resolvers/GDResolvingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ public static bool ResolvePoint(this ITokenOrSkipReceiver<GDPoint> receiver, cha
return result;
}

public static void ResolveExpression(this ITokenOrSkipReceiver<GDExpression> receiver, char c, GDReadingState state, int intendation, INewLineReceiver newLineReceiver = null)
public static void ResolveExpression(this ITokenOrSkipReceiver<GDExpression> receiver, char c, GDReadingState state, int intendation, INewLineReceiver newLineReceiver = null, bool allowAssignment = true)
{
if (!IsExpressionStopChar(c))
state.Push(new GDExpressionResolver(receiver, intendation, newLineReceiver));
state.Push(new GDExpressionResolver(receiver, intendation, newLineReceiver, allowAssignment));
else
receiver.HandleReceivedTokenSkip();
state.PassChar(c);
Expand Down

0 comments on commit 6d20948

Please sign in to comment.