Skip to content

Commit

Permalink
Improved properties parsing and intendation parsing with multiline sp…
Browse files Browse the repository at this point in the history
…lit token
  • Loading branch information
elamaunt committed Jan 31, 2024
1 parent 562889a commit 7f9340f
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 59 deletions.
69 changes: 67 additions & 2 deletions src/GDShrapt.Reader.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ public void ParseNewProperiesSyntaxTest2()
{
var reader = new GDScriptReader();

var code = @"var _score: int
var code = @"
var score: int:
get:
return _score
Expand All @@ -2251,6 +2251,71 @@ public void ParseNewProperiesSyntaxTest3()
var reader = new GDScriptReader();

var code = @"var prop:
set ( value ) :
print(value)
get :
return 0";

var @class = reader.ParseFileContent(code);

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

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

var code = @"
var prop2:
set = _setter,
get = _getter";

var @class = reader.ParseFileContent(code);

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

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

var code = @"
var prop4: set\
=_setter , get\
=_getter";

var @class = reader.ParseFileContent(code);

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

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

var code = @"
var prop3: set=_setter, \
get=_getter";

var @class = reader.ParseFileContent(code);

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

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

var code = @"
var prop:
set ( value ) :
print(value)
get :
Expand All @@ -2264,7 +2329,7 @@ public void ParseNewProperiesSyntaxTest3()
get=_getter
var prop4: set\
=_setter, get\
=_setter , get\
=_getter";

var @class = reader.ParseFileContent(code);
Expand Down
49 changes: 16 additions & 33 deletions src/GDShrapt.Reader/Declarations/GDVariableDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public sealed class GDVariableDeclaration : GDIdentifiableClassMember,
IIntendedTokenOrSkipReceiver<GDAccessorDeclaration>,
ITokenOrSkipReceiver<GDComma>
{
private bool _skipComma;

public GDConstKeyword ConstKeyword
{
get => _form.Token0;
Expand Down Expand Up @@ -158,25 +156,14 @@ internal override void HandleChar(char c, GDReadingState state)
state.PushAndPass(new GDExpressionResolver(this, Intendation), c);
break;
case State.FirstAccessorDeclarationNode:
state.PushAndPass(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, true, Intendation + 1), c);
state.PushAndPass(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, this, true, false, Intendation + 1), c);
break;
case State.Comma:
if (_skipComma)
{
state.PushAndPass(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, false, Intendation + 1), c);
return;
}
this.ResolveComma(c, state);
break;
case State.SecondAccessorDeclarationNode:
if (c == ',' && Comma == null)
{
Comma = new GDComma();
return;
}

state.PushAndPass(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, !_skipComma, Intendation + 1), c);
break;
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;
default:
this.HandleAsInvalidToken(c, state, x => x.IsNewLine());
break;
Expand All @@ -187,27 +174,23 @@ internal override void HandleNewLineChar(GDReadingState state)
{
if (_form.IsOrLowerState(State.FirstAccessorDeclarationNode))
{
_skipComma = true;
state.PushAndPassNewLine(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, false, Intendation + 1));
state.PushAndPassNewLine(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, this, false, false, Intendation + 1));
return;
}

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

state.PopAndPassNewLine();
}
internal override void HandleLeftSlashChar(GDReadingState state)
{
if (_form.IsOrLowerState(State.FirstAccessorDeclarationNode))
/*if (_form.State == State.SecondAccessorDeclarationNode)
{
_skipComma = true;
}
state.PushAndPassNewLine(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, this, Comma != null, false, Intendation + 1));
return;
}*/

base.HandleLeftSlashChar(state);
state.PopAndPassNewLine();
}

public override GDNode CreateEmptyInstance()
Expand Down Expand Up @@ -461,7 +444,7 @@ void ITokenReceiver<GDAccessorDeclaration>.HandleReceivedToken(GDAccessorDeclara
if (_form.IsOrLowerState(State.FirstAccessorDeclarationNode))
{
FirstAccessorDeclarationNode = token;
_form.State = _skipComma ? State.SecondAccessorDeclarationNode : State.Comma;
_form.State = State.Comma;
return;
}

Expand All @@ -479,7 +462,7 @@ void ITokenSkipReceiver<GDAccessorDeclaration>.HandleReceivedTokenSkip()
{
if (_form.IsOrLowerState(State.FirstAccessorDeclarationNode))
{
_form.State = _skipComma ? State.Completed : State.Comma;
_form.State = State.Completed;
return;
}

Expand Down
118 changes: 96 additions & 22 deletions src/GDShrapt.Reader/Resolvers/GDIntendedResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Net.Sockets;
using System.Text;

namespace GDShrapt.Reader
{
Expand All @@ -17,6 +18,7 @@ internal abstract class GDIntendedResolver : GDResolver
int _spaceCounter;
bool _inComment;
bool _intendationTokensSent;
bool _lineSplitted;

new IIntendedTokenReceiver Owner { get; }

Expand Down Expand Up @@ -44,7 +46,10 @@ internal sealed override void HandleChar(char c, GDReadingState state)

bool HandleIntendation(char c, GDReadingState state)
{
if (AllowZeroIntendationOnFirstLine && _firstLine && !c.IsSpace())
if (_lineIntendationEnded)
return false;

if (AllowZeroIntendationOnFirstLine && _firstLine && !c.IsSpace() && !c.IsNewLine() && c != '#' && c != '\\')
{
_lineIntendationEnded = true;
return false;
Expand All @@ -55,10 +60,16 @@ bool HandleIntendation(char c, GDReadingState state)
{
if (c == '\n')
{
_firstLine = false;
_inComment = false;
_spaceCounter = 0;
_lineIntendation = 0;
if (_lineSplitted)
_lineSplitted = false;
else
{
_firstLine = false;
_inComment = false;
_spaceCounter = 0;
_lineIntendation = 0;
}

_sequenceBuilder.Append(c);
return true;
}
Expand All @@ -69,6 +80,13 @@ bool HandleIntendation(char c, GDReadingState state)
return true;
}

if (c == '\\')
{
_lineSplitted = true;
_sequenceBuilder.Append(c);
return true;
}

if (c == '\t')
{
if (_spaceCounter > 0)
Expand Down Expand Up @@ -154,7 +172,6 @@ internal override void HandleLeftSlashChar(GDReadingState state)
HandleLeftSlashCharAfterIntendation(state);
else
{
_inComment = true;
HandleIntendation('\\', state);
}
}
Expand All @@ -168,6 +185,7 @@ protected void SendIntendationTokensToOwner()

GDComment comment = null;
GDSpace space = null;
GDMultiLineSplitToken split = null;

for (int i = 0; i < _sequenceBuilder.Length; i++)
{
Expand All @@ -176,7 +194,9 @@ protected void SendIntendationTokensToOwner()
{
case '\t':
case ' ':
if (comment != null)
if (split != null)
split.Append(c);
else if (comment != null)
comment.Append(c);
else
{
Expand All @@ -186,18 +206,50 @@ protected void SendIntendationTokensToOwner()
}
break;
case '#':
if (space != null)
if (split != null)
split.Append(c);
else
{
space.Complete();
Owner.HandleReceivedToken(space);
space = null;
if (space != null)
{
space.Complete();
Owner.HandleReceivedToken(space);
space = null;
}

if (comment == null)
comment = new GDComment();
comment.Append(c);
}

if (comment == null)
comment = new GDComment();
comment.Append(c);
break;
case '\n':
if (split != null)
{
split.Append(c);
split.Complete();
Owner.HandleReceivedToken(split);
split = null;
}
else
{
if (space != null)
{
space.Complete();
Owner.HandleReceivedToken(space);
space = null;
}

if (comment != null)
{
comment.Complete();
Owner.HandleReceivedToken(comment);
comment = null;
}

Owner.HandleReceivedToken(new GDNewLine());
}
break;
case '\\':
if (space != null)
{
space.Complete();
Expand All @@ -207,19 +259,38 @@ protected void SendIntendationTokensToOwner()

if (comment != null)
{
comment.Complete();
Owner.HandleReceivedToken(comment);
comment = null;
comment.Append(c);
}
else
{
if (split == null)
split = new GDMultiLineSplitToken();
split.Append(c);
}

Owner.HandleReceivedToken(new GDNewLine());
break;
default:
comment.Append(c);
if (split != null)
split.Append(c);
else
comment.Append(c);
break;
}
}

if (split != null)
{
split.Complete();
Owner.HandleReceivedToken(split);
split = null;
}

if (comment != null)
{
comment.Complete();
Owner.HandleReceivedToken(comment);
comment = null;
}

if (space != null)
{
space.Complete();
Expand All @@ -231,6 +302,9 @@ protected void SendIntendationTokensToOwner()
}
else
{
if (_firstLine && AllowZeroIntendationOnFirstLine)
return;

Owner.HandleReceivedToken(new GDIntendation()
{
Sequence = string.Empty,
Expand Down
Loading

0 comments on commit 7f9340f

Please sign in to comment.