diff --git a/src/GDShrapt.Reader.Tests/ParsingTests.cs b/src/GDShrapt.Reader.Tests/ParsingTests.cs index 3f77906..217d45c 100644 --- a/src/GDShrapt.Reader.Tests/ParsingTests.cs +++ b/src/GDShrapt.Reader.Tests/ParsingTests.cs @@ -1,6 +1,7 @@ using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Linq; +using System.Security.Claims; using System.Threading; using static GDShrapt.Reader.GD; @@ -2228,6 +2229,7 @@ func _getter(): AssertHelper.NoInvalidTokens(@class); } + [TestMethod] public void ParseNewProperiesSyntaxTest2() { var reader = new GDScriptReader(); @@ -2249,26 +2251,7 @@ func _update_score_display(): AssertHelper.NoInvalidTokens(@class); } - public void ParseNewProperiesSyntaxTest3() - { - var reader = new GDScriptReader(); - - var code = @"var score: int: - get: - return score - set(value: int): - score = value - update_score_display() - -func update_score_display(): - pass # Do something to update the displayed score"; - - var @class = reader.ParseFileContent(code); - - AssertHelper.CompareCodeStrings(code, @class.ToString()); - AssertHelper.NoInvalidTokens(@class); - } - + [TestMethod] public void ParseNewExportsTest() { var reader = new GDScriptReader(); diff --git a/src/GDShrapt.Reader/Declarations/Property/GDGetAccessorBodyDeclaration.cs b/src/GDShrapt.Reader/Declarations/Property/GDGetAccessorBodyDeclaration.cs index 55910f2..04c1bf4 100644 --- a/src/GDShrapt.Reader/Declarations/Property/GDGetAccessorBodyDeclaration.cs +++ b/src/GDShrapt.Reader/Declarations/Property/GDGetAccessorBodyDeclaration.cs @@ -3,6 +3,7 @@ public class GDGetAccessorBodyDeclaration : GDAccessorDeclaration, ITokenOrSkipReceiver, ITokenOrSkipReceiver, + ITokenOrSkipReceiver, ITokenOrSkipReceiver { public GDGetKeyword GetKeyword @@ -16,34 +17,40 @@ public GDColon Colon get => _form.Token1; set => _form.Token1 = value; } + public GDExpression Expression + { + get => _form.Token2; + set => _form.Token2 = value; + } public GDStatementsList Statements { - get => _form.Token2 ?? (_form.Token2 = new GDStatementsList(Intendation + 1)); - set => _form.Token2 = value; + get => _form.Token3 ?? (_form.Token3 = new GDStatementsList(Intendation + 1)); + set => _form.Token3 = value; } public enum State { Get, Colon, + Expression, Statements, Completed } - readonly GDTokensForm _form; + readonly GDTokensForm _form; public override GDTokensForm Form => _form; - public GDTokensForm TypedForm => _form; + public GDTokensForm TypedForm => _form; public GDGetAccessorBodyDeclaration() { - _form = new GDTokensForm(this); + _form = new GDTokensForm(this); } public GDGetAccessorBodyDeclaration(int intendation) : base(intendation) { - _form = new GDTokensForm(this); + _form = new GDTokensForm(this); } public override GDNode CreateEmptyInstance() @@ -73,6 +80,10 @@ internal override void HandleChar(char c, GDReadingState state) if (!this.ResolveSpaceToken(c, state)) this.ResolveColon(c, state); break; + case State.Expression: + if (!this.ResolveSpaceToken(c, state)) + this.ResolveExpression(c, state, Intendation); + break; case State.Statements: this.HandleAsInvalidToken(c, state, x => x.IsSpace() || x.IsNewLine()); break; @@ -88,6 +99,7 @@ internal override void HandleNewLineChar(GDReadingState state) { case State.Get: case State.Colon: + case State.Expression: case State.Statements: _form.State = State.Completed; state.PushAndPassNewLine(Statements); @@ -126,7 +138,7 @@ void ITokenReceiver.HandleReceivedToken(GDColon token) if (_form.State == State.Colon) { Colon = token; - _form.State = State.Statements; + _form.State = State.Expression; return; } @@ -136,6 +148,28 @@ void ITokenReceiver.HandleReceivedToken(GDColon token) void ITokenSkipReceiver.HandleReceivedTokenSkip() { if (_form.State == State.Colon) + { + _form.State = State.Expression; + return; + } + + throw new GDInvalidStateException(); + } + void ITokenReceiver.HandleReceivedToken(GDExpression token) + { + if (_form.State == State.Expression) + { + Expression = token; + _form.State = State.Statements; + return; + } + + throw new GDInvalidStateException(); + } + + void ITokenSkipReceiver.HandleReceivedTokenSkip() + { + if (_form.State == State.Expression) { _form.State = State.Statements; return; diff --git a/src/GDShrapt.Reader/Declarations/Property/GDSetAccessorBodyDeclaration.cs b/src/GDShrapt.Reader/Declarations/Property/GDSetAccessorBodyDeclaration.cs index ed16a33..87ae834 100644 --- a/src/GDShrapt.Reader/Declarations/Property/GDSetAccessorBodyDeclaration.cs +++ b/src/GDShrapt.Reader/Declarations/Property/GDSetAccessorBodyDeclaration.cs @@ -6,6 +6,7 @@ public class GDSetAccessorBodyDeclaration : GDAccessorDeclaration, ITokenOrSkipReceiver, ITokenOrSkipReceiver, ITokenOrSkipReceiver, + ITokenOrSkipReceiver, ITokenOrSkipReceiver { public GDSetKeyword SetKeyword @@ -38,12 +39,18 @@ public GDColon Colon set => _form.Token4 = value; } - public GDStatementsList Statements + public GDExpression Expression { - get => _form.Token5 ?? (_form.Token5 = new GDStatementsList(Intendation + 1)); + get => _form.Token5; set => _form.Token5 = value; } + public GDStatementsList Statements + { + get => _form.Token6 ?? (_form.Token6 = new GDStatementsList(Intendation + 1)); + set => _form.Token6 = value; + } + public enum State { Set, @@ -51,23 +58,24 @@ public enum State Parameter, CloseBracket, Colon, + Expression, Statements, Completed } - readonly GDTokensForm _form; + readonly GDTokensForm _form; public override GDTokensForm Form => _form; - public GDTokensForm TypedForm => _form; + public GDTokensForm TypedForm => _form; public GDSetAccessorBodyDeclaration() { - _form = new GDTokensForm(this); + _form = new GDTokensForm(this); } public GDSetAccessorBodyDeclaration(int intendation) : base(intendation) { - _form = new GDTokensForm(this); + _form = new GDTokensForm(this); } public override GDNode CreateEmptyInstance() @@ -93,10 +101,26 @@ internal override void HandleChar(char c, GDReadingState state) if (!this.ResolveSpaceToken(c, state)) this.ResolveKeyword(c, state); break; + case State.OpenBracket: + if (!this.ResolveSpaceToken(c, state)) + this.ResolveOpenBracket(c, state); + break; + case State.Parameter: + if (!this.ResolveSpaceToken(c, state)) + this.ResolveParameter(c, state); + break; + case State.CloseBracket: + if (!this.ResolveSpaceToken(c, state)) + this.ResolveCloseBracket(c, state); + break; case State.Colon: if (!this.ResolveSpaceToken(c, state)) this.ResolveColon(c, state); break; + case State.Expression: + if (!this.ResolveSpaceToken(c, state)) + this.ResolveExpression(c, state, Intendation); + break; case State.Statements: this.HandleAsInvalidToken(c, state, x => x.IsSpace() || x.IsNewLine()); break; @@ -110,11 +134,15 @@ internal override void HandleNewLineChar(GDReadingState state) { switch (_form.State) { - case State.Set: - case State.OpenBracket: case State.Parameter: case State.CloseBracket: + _form.AddBeforeActiveToken(new GDNewLine()); + break; + + case State.Set: + case State.OpenBracket: case State.Colon: + case State.Expression: case State.Statements: _form.State = State.Completed; state.PushAndPassNewLine(Statements); @@ -130,7 +158,7 @@ void ITokenReceiver.HandleReceivedToken(GDSetKeyword token) if (_form.State == State.Set) { SetKeyword = token; - _form.State = State.Colon; + _form.State = State.OpenBracket; return; } @@ -222,7 +250,7 @@ void ITokenReceiver.HandleReceivedToken(GDColon token) if (_form.State == State.Colon) { Colon = token; - _form.State = State.Statements; + _form.State = State.Expression; return; } @@ -232,6 +260,29 @@ void ITokenReceiver.HandleReceivedToken(GDColon token) void ITokenSkipReceiver.HandleReceivedTokenSkip() { if (_form.State == State.Colon) + { + _form.State = State.Expression; + return; + } + + throw new GDInvalidStateException(); + } + + void ITokenReceiver.HandleReceivedToken(GDExpression token) + { + if (_form.State == State.Expression) + { + Expression = token; + _form.State = State.Statements; + return; + } + + throw new GDInvalidStateException(); + } + + void ITokenSkipReceiver.HandleReceivedTokenSkip() + { + if (_form.State == State.Expression) { _form.State = State.Statements; return; @@ -262,7 +313,5 @@ void ITokenSkipReceiver.HandleReceivedTokenSkip() throw new GDInvalidStateException(); } - - } } \ No newline at end of file diff --git a/src/GDShrapt.Reader/Resolvers/GDResolvingHelper.cs b/src/GDShrapt.Reader/Resolvers/GDResolvingHelper.cs index 50a207b..ac46d93 100644 --- a/src/GDShrapt.Reader/Resolvers/GDResolvingHelper.cs +++ b/src/GDShrapt.Reader/Resolvers/GDResolvingHelper.cs @@ -134,6 +134,19 @@ public static bool ResolveAssign(this ITokenOrSkipReceiver receiver, c return result; } + public static bool ResolveParameter(this ITokenOrSkipReceiver receiver, char c, GDReadingState state) + { + if (c.IsIdentifierStartChar()) + { + receiver.HandleReceivedToken(state.PushAndPass(new GDParameterDeclaration(), c)); + return true; + } + + receiver.HandleReceivedTokenSkip(); + state.PassChar(c); + return false; + } + public static bool ResolveCloseBracket(this ITokenOrSkipReceiver receiver, char c, GDReadingState state) { var result = c == ')'; diff --git a/src/GDShrapt.Reader/Resolvers/GDSetGetAccessorsResolver.cs b/src/GDShrapt.Reader/Resolvers/GDSetGetAccessorsResolver.cs index c9c8e0e..a52e4f0 100644 --- a/src/GDShrapt.Reader/Resolvers/GDSetGetAccessorsResolver.cs +++ b/src/GDShrapt.Reader/Resolvers/GDSetGetAccessorsResolver.cs @@ -23,12 +23,15 @@ public override string[] GeneratePatterns() "set=", "get:", - "set:" + "set(" }; } protected override void PatternMatched(string pattern, GDReadingState state) { + if (pattern != null) + SendIntendationTokensToOwner(); + switch (pattern) { case "set": @@ -46,8 +49,9 @@ protected override void PatternMatched(string pattern, GDReadingState state) state.Push(accessor); break; } - case "set:": + case "set(": { + // TODO: check spaces var accessor = new GDSetAccessorBodyDeclaration(LineIntendationThreshold); Owner.HandleReceivedToken(accessor); state.Push(accessor); diff --git a/src/GDShrapt.Reader/Walking/GDVisitor.cs b/src/GDShrapt.Reader/Walking/GDVisitor.cs index 2e0f7bc..ce9d467 100644 --- a/src/GDShrapt.Reader/Walking/GDVisitor.cs +++ b/src/GDShrapt.Reader/Walking/GDVisitor.cs @@ -1,6 +1,6 @@ namespace GDShrapt.Reader { - public abstract class GDVisitor : GDBaseVisitor + public abstract class GDVisitor : GDBaseVisitor, IGDVisitor { public virtual void DidLeft(GDNode expr) { @@ -691,5 +691,105 @@ public virtual void DidLeftExpression(GDExpression e) { // Nothing } + + public virtual void Visit(GDAwaitExpression e) + { + // Nothing + } + + public virtual void Visit(GDStringTypeNode type) + { + // Nothing + } + + public virtual void Visit(GDClassCustomAttribute a) + { + // Nothing + } + + public virtual void Visit(GDStringPartsList list) + { + // Nothing + } + + public virtual void Left(GDAwaitExpression e) + { + // Nothing + } + + public virtual void Left(GDStringTypeNode type) + { + // Nothing + } + + public virtual void Left(GDClassCustomAttribute a) + { + // Nothing + } + + public virtual void Left(GDStringPartsList list) + { + // Nothing + } + + public virtual void Left(GDTripleDoubleQuotasStringNode sn) + { + // Nothing + } + + public virtual void Left(GDTripleSingleQuotasStringNode sn) + { + // Nothing + } + + public virtual void Left(GDDoubleQuotasStringNode sn) + { + // Nothing + } + + public virtual void Left(GDSingleQuotasStringNode sn) + { + // Nothing + } + + public virtual void Visit(GDTripleSingleQuotasStringNode sn) + { + // Nothing + } + + public virtual void Visit(GDTripleDoubleQuotasStringNode sn) + { + // Nothing + } + + public virtual void Visit(GDDoubleQuotasStringNode sn) + { + // Nothing + } + + public virtual void Visit(GDSingleQuotasStringNode sn) + { + // Nothing + } + + public virtual void Visit(GDGetUniqueNodeExpression e) + { + // Nothing + } + + public virtual void Left(GDGetUniqueNodeExpression e) + { + // Nothing + } + + public virtual void Visit(GDSubTypeNode t) + { + // Nothing + } + + public virtual void Left(GDSubTypeNode t) + { + // Nothing + } } }