Skip to content

Commit

Permalink
Fixed Unique node expression. Fixed subtype resolving. Fixed intended…
Browse files Browse the repository at this point in the history
… comments parsing
  • Loading branch information
elamaunt committed Jan 9, 2024
1 parent 79ef2a4 commit 02f2eda
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/GDShrapt.Reader.Tests/BigScriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,18 @@ public void BigScriptParsingTest4()
AssertHelper.CompareCodeStrings(fileText, declaration.ToString());
AssertHelper.NoInvalidTokens(declaration);
}

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

var code = "# _at_position is not used because it doesn't matter where on the panel\r\n# the item is dropped\r\nfunc _can_drop_data(_at_position: Vector2, data: Variant) -> bool:\t\r\n\tif data is InventoryItem:\r\n\t\t#This is the text that displays uupon pulling an item out.\r\n\t\t%summary.text =( str(\"atk:\" + str(data.physicalattack) +'\\n' + data.lore))\r\n\t\tif type == InventoryItem.Type.MAIN:\r\n\t\t\tif get_child_count() == 0:\r\n\t\t\t\treturn true\r\n\t\t\telse:\r\n\t\t\t\t# Swap two items\r\n\t\t\t\treturn get_child(0).type == data.type\r\n\t\telse:\r\n\t\t\treturn data.type == type\r\n\t\t\t\r\n\t\r\n\treturn false";

var declaration = reader.ParseFileContent(code);

AssertHelper.CompareCodeStrings(code, declaration.ToString());
AssertHelper.NoInvalidTokens(declaration);
}
}
}
30 changes: 30 additions & 0 deletions src/GDShrapt.Reader.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2303,5 +2303,35 @@ public void StatementAtributesTest2()
AssertHelper.CompareCodeStrings(code, @class.ToString());
AssertHelper.NoInvalidTokens(@class);
}

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

var code = @"if Input.is_anything_pressed() == false:
%summary.text = ""physical attack: "" + str(Physical_attack_sum) + '\n'";

var @class = reader.ParseStatement(code);

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

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

var code = @"# Custom init function so that it doesn't error
func init(t: InventoryItem.Type, cms: Vector2) -> void:
type = t
custom_minimum_size = cms";

var @class = reader.ParseFileContent(code);

AssertHelper.CompareCodeStrings(code, @class.ToString());
AssertHelper.NoInvalidTokens(@class);
}
}
}
124 changes: 124 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDGetUniqueNodeExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
namespace GDShrapt.Reader
{
public sealed class GDGetUniqueNodeExpression : GDExpression,
ITokenOrSkipReceiver<GDPercent>,
ITokenOrSkipReceiver<GDPathList>
{
public override int Priority => GDHelper.GetOperationPriority(GDOperationType.GetNode);

public GDPercent Percent
{
get => _form.Token0;
set => _form.Token0 = value;
}

public GDPathList Path
{
get => _form.Token1 ?? (_form.Token1 = new GDPathList());
set => _form.Token1 = value;
}

public enum State
{
Percent,
Path,
Completed
}

readonly GDTokensForm<State, GDPercent, GDPathList> _form;
public override GDTokensForm Form => _form;
public GDTokensForm<State, GDPercent, GDPathList> TypedForm => _form;
public GDGetUniqueNodeExpression()
{
_form = new GDTokensForm<State, GDPercent, GDPathList>(this);
}

internal override void HandleChar(char c, GDReadingState state)
{
switch (_form.State)
{
case State.Percent:
if (this.ResolveSpaceToken(c, state))
return;
this.ResolvePercent(c, state);
break;
case State.Path:
_form.State = State.Completed;

if (this.ResolveSpaceToken(c, state))
return;
state.PushAndPass(Path, c);
break;
default:
state.PopAndPass(c);
break;
}
}

internal override void HandleNewLineChar(GDReadingState state)
{
state.PopAndPassNewLine();
}

public override GDNode CreateEmptyInstance()
{
return new GDGetNodeExpression();
}

internal override void Visit(IGDVisitor visitor)
{
visitor.Visit(this);
}

internal override void Left(IGDVisitor visitor)
{
visitor.Left(this);
}

void ITokenReceiver<GDPercent>.HandleReceivedToken(GDPercent token)
{
if (_form.IsOrLowerState(State.Percent))
{
_form.State = State.Path;
Percent = token;
return;
}

throw new GDInvalidStateException();
}

void ITokenSkipReceiver<GDPercent>.HandleReceivedTokenSkip()
{
if (_form.IsOrLowerState(State.Percent))
{
_form.State = State.Path;
return;
}

throw new GDInvalidStateException();
}

void ITokenReceiver<GDPathList>.HandleReceivedToken(GDPathList token)
{
if (_form.IsOrLowerState(State.Path))
{
_form.State = State.Path;
Path = token;
return;
}

throw new GDInvalidStateException();
}

void ITokenSkipReceiver<GDPathList>.HandleReceivedTokenSkip()
{
if (_form.IsOrLowerState(State.Path))
{
_form.State = State.Path;
return;
}

throw new GDInvalidStateException();
}
}
}
7 changes: 7 additions & 0 deletions src/GDShrapt.Reader/Resolvers/GDExpressionResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ internal override void HandleChar(char c, GDReadingState state)
state.PassChar(c);
return;
}

if (c == '%')
{
PushAndSave(state, new GDGetUniqueNodeExpression());
state.PassChar(c);
return;
}
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Resolvers/GDIntendedResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,13 @@ protected void SendIntendationTokensToOwner()

protected void PassIntendationSequence(GDReadingState state)
{
if (_intendationTokensSent)
return;

for (int i = 0; i < _sequenceBuilder.Length; i++)
state.PassChar(_sequenceBuilder[i]);

ResetIntendation();
}

protected void ResetIntendation()
Expand Down
15 changes: 14 additions & 1 deletion src/GDShrapt.Reader/Resolvers/GDResolvingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public static void ResolveKeyword<T>(this ITokenOrSkipReceiver<T> receiver, char
state.PushAndPass(new GDKeywordResolver<T>(receiver), c);
}

public static bool ResolvePercent(this ITokenOrSkipReceiver<GDPercent> receiver, char c, GDReadingState state)
{
var result = c == '%';
if (result)
receiver.HandleReceivedToken(new GDPercent());
else
{
receiver.HandleReceivedTokenSkip();
state.PassChar(c);
}
return result;
}

public static bool ResolveDollar(this ITokenOrSkipReceiver<GDDollar> receiver, char c, GDReadingState state)
{
var result = c == '$';
Expand Down Expand Up @@ -376,7 +389,7 @@ public static bool ResolveCommentToken(this ITokenReceiver<GDComment> receiver,
{
if (IsCommentStartChar(c))
{
receiver.HandleReceivedToken(new GDComment());
receiver.HandleReceivedToken(state.Push(new GDComment()));
state.PassChar(c);
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/GDShrapt.Reader/Resolvers/GDStatementsResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ internal override void HandleCharAfterIntendation(char c, GDReadingState state)

if (_resolvedAsExpression)
{
if (c.IsExpressionStopChar())
{
Owner.HandleAsInvalidToken(c, state, x => x.IsSpace() || x.IsNewLine());
return;
}

// Resolving multiple expressions on the same string
var statement = new GDExpressionStatement();
Owner.HandleReceivedToken(statement);
Expand Down
32 changes: 32 additions & 0 deletions src/GDShrapt.Reader/Resolvers/GDTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ internal override void HandleChar(char c, GDReadingState state)
Owner.HandleReceivedToken(arrayTypeNode);

state.Push(arrayTypeNode);

if (_space != null)
{
for (int i = 0; i < _space.Sequence.Length; i++)
state.PassChar(_space.Sequence[i]);

_space = null;
}

state.PassChar(c);
return;
}
Expand All @@ -104,6 +113,29 @@ internal override void HandleChar(char c, GDReadingState state)
return;
}

if (c == '.' && _type != null)
{
var subTypeNode = new GDSubTypeNode();
subTypeNode.Add(new GDSingleTypeNode() { Type = _type });

Owner.HandleReceivedToken(subTypeNode);
_type = null;
state.Pop();

state.Push(subTypeNode);

if (_space != null)
{
for (int i = 0; i < _space.Sequence.Length; i++)
state.PassChar(_space.Sequence[i]);

_space = null;
}

state.PassChar(c);
return;
}

state.Pop();
Complete(state);
state.PassChar(c);
Expand Down
12 changes: 12 additions & 0 deletions src/GDShrapt.Reader/SimpleTokens/GDPercent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace GDShrapt.Reader
{
public sealed class GDPercent : GDSingleCharToken
{
public override char Char => '%';

public override GDSyntaxToken Clone()
{
return new GDPercent();
}
}
}
Loading

0 comments on commit 02f2eda

Please sign in to comment.