Skip to content

Commit

Permalink
Merge branch 'Minor-Updates'
Browse files Browse the repository at this point in the history
  • Loading branch information
elamaunt committed Jan 12, 2024
2 parents 36ed92f + 556d74b commit d0acf4c
Show file tree
Hide file tree
Showing 48 changed files with 926 additions and 101 deletions.
27 changes: 27 additions & 0 deletions src/GDShrapt.Reader.Tests/BigScriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,32 @@ public void BigScriptParsingTest4()
AssertHelper.CompareCodeStrings(fileText, declaration.ToString());
AssertHelper.NoInvalidTokens(declaration);
}

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

var path = Path.Combine("Scripts", "Sample5.gd");
var declaration = reader.ParseFile(path);

var fileText = File.ReadAllText(path);

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);
}
}
}
3 changes: 3 additions & 0 deletions src/GDShrapt.Reader.Tests/GDShrapt.Reader.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
</ItemGroup>

<ItemGroup>
<None Update="Scripts\Sample5.gd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\Sample4.gd">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
93 changes: 93 additions & 0 deletions src/GDShrapt.Reader.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2303,5 +2303,98 @@ 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);
}

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

var code = @"
class_name Helper
func print_hello():
var messageGenerator = func(): return ""Hello, World!""
print(messageGenerator())
";

var @class = reader.ParseFileContent(code);

Assert.AreEqual(2, @class.Methods.First().Statements.Count);
AssertHelper.CompareCodeStrings(code, @class.ToString());
AssertHelper.NoInvalidTokens(@class);
}

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

var code = @"
class_name Helper
func switch(x):
match x:
1:
print(""It's one!"")
2:
print(""It's one times two!"")
var new_var:
print(""It's not 1 or 2, it's "", new_var)
";

var @class = reader.ParseFileContent(code);
Assert.AreEqual(1, @class.Methods.Count());

var method = @class.Methods.First();
Assert.AreEqual(1, method.Statements.Count);

var statement = method.Statements.First();

Assert.IsInstanceOfType(statement, typeof(GDMatchStatement));

var match = (GDMatchStatement)statement;

Assert.AreEqual(3, match.Cases.Count);

for (int i = 0; i < match.Cases.Count; i++)
{
var @case = match.Cases[i];
Assert.AreEqual(1, @case.Statements.Count);
}

AssertHelper.CompareCodeStrings(code, @class.ToString());
AssertHelper.NoInvalidTokens(@class);
}
}
}
42 changes: 42 additions & 0 deletions src/GDShrapt.Reader.Tests/Scripts/Sample5.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
extends PanelContainer
class_name InventorySlot


@export var type: InventoryItem.Type


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


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


return false


# _at_position is not used because it doesn't matter where on the panel
# the item is dropped
func _drop_data(_at_position: Vector2, data: Variant) -> void:
if get_child_count() > 0:
var item := get_child(0)
if item == data: return
remove_child(item)
data.get_parent().add_child(item)
data.get_parent().remove_child(data)
add_child(data)
1 change: 1 addition & 0 deletions src/GDShrapt.Reader/Basics/GDOperationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum GDOperationType
Pass,
MatchCaseVariable,
DictionaryInitializer,
GetUniqueNode,
GetNode,
NodePath,
If,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public enum State
}

readonly GDTokensForm<State, GDExpression, GDPairToken, GDExpression> _form;
readonly int _intendation;

internal GDDictionaryKeyValueDeclaration(int intendation)
{
_form = new GDTokensForm<State, GDExpression, GDPairToken, GDExpression>(this);
_intendation = intendation;
}

public GDDictionaryKeyValueDeclaration()
{
Expand All @@ -53,7 +60,7 @@ internal override void HandleChar(char c, GDReadingState state)
switch (_form.State)
{
case State.Key:
this.ResolveExpression(c, state);
this.ResolveExpression(c, state, _intendation);
break;
case State.ColonOrAssign:
if (!_checkedColon)
Expand All @@ -62,7 +69,7 @@ internal override void HandleChar(char c, GDReadingState state)
this.ResolveAssign(c, state);
break;
case State.Value:
this.ResolveExpression(c, state);
this.ResolveExpression(c, state, _intendation);
break;
default:
state.PopAndPass(c);
Expand Down
11 changes: 10 additions & 1 deletion src/GDShrapt.Reader/Declarations/GDEnumValueDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ public enum State
}

readonly GDTokensForm<State, GDIdentifier, GDPairToken, GDExpression> _form;
readonly int _intendation;

public override GDTokensForm Form => _form;
public GDTokensForm<State, GDIdentifier, GDPairToken, GDExpression> TypedForm => _form;

internal GDEnumValueDeclaration(int intendation)
{
_form = new GDTokensForm<State, GDIdentifier, GDPairToken, GDExpression>(this);
_intendation = intendation;
}

public GDEnumValueDeclaration()
{
_form = new GDTokensForm<State, GDIdentifier, GDPairToken, GDExpression>(this);
Expand All @@ -66,7 +75,7 @@ internal override void HandleChar(char c, GDReadingState state)
this.ResolveAssign(c, state);
break;
case State.Value:
this.ResolveExpression(c, state);
this.ResolveExpression(c, state, _intendation);
break;
default:
state.PopAndPass(c);
Expand Down
2 changes: 1 addition & 1 deletion src/GDShrapt.Reader/Declarations/GDMethodDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ internal override void HandleChar(char c, GDReadingState state)
break;

case State.Expression:
this.ResolveExpression(c, state);
this.ResolveExpression(c, state, Intendation);
break;
case State.Statements:
this.HandleAsInvalidToken(c, state, x => x.IsSpace() || x.IsNewLine());
Expand Down
10 changes: 9 additions & 1 deletion src/GDShrapt.Reader/Declarations/GDParameterDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ public enum State
}

readonly GDTokensForm<State, GDIdentifier, GDColon, GDTypeNode, GDAssign, GDExpression> _form;
readonly int _intendation;

public override GDTokensForm Form => _form;
public GDTokensForm<State, GDIdentifier, GDColon, GDTypeNode, GDAssign, GDExpression> TypedForm => _form;
internal GDParameterDeclaration(int intendation)
{
_form = new GDTokensForm<State, GDIdentifier, GDColon, GDTypeNode, GDAssign, GDExpression>(this);
_intendation = intendation;
}

public GDParameterDeclaration()
{
_form = new GDTokensForm<State, GDIdentifier, GDColon, GDTypeNode, GDAssign, GDExpression>(this);
Expand All @@ -71,7 +79,7 @@ internal override void HandleChar(char c, GDReadingState state)
this.ResolveAssign(c, state);
break;
case State.DefaultValue:
this.ResolveExpression(c, state);
this.ResolveExpression(c, state, _intendation);
break;
default:
state.PopAndPass(c);
Expand Down
2 changes: 1 addition & 1 deletion src/GDShrapt.Reader/Declarations/GDVariableDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ internal override void HandleChar(char c, GDReadingState state)
state.PushAndPass(new GDSingleCharTokenResolver<GDAssign>(this), c);
break;
case State.Initializer:
state.PushAndPass(new GDExpressionResolver(this), c);
state.PushAndPass(new GDExpressionResolver(this, Intendation), c);
break;
case State.FirstAccessorDeclarationNode:
state.PushAndPass(new GDSetGetAccessorsResolver<GDVariableDeclaration>(this, true, Intendation + 1), c);
Expand Down
10 changes: 9 additions & 1 deletion src/GDShrapt.Reader/Expressions/GDArrayInitializerExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public GDSquareOpenBracket SquareOpenBracket
}
public GDExpressionsList Values
{
get => _form.Token1 ?? (_form.Token1 = new GDExpressionsList());
get => _form.Token1 ?? (_form.Token1 = new GDExpressionsList(_intendation));
set => _form.Token1 = value;
}
public GDSquareCloseBracket SquareCloseBracket
Expand All @@ -31,9 +31,17 @@ public enum State
Completed
}

readonly int _intendation;
readonly GDTokensForm<State, GDSquareOpenBracket, GDExpressionsList, GDSquareCloseBracket> _form;
public override GDTokensForm Form => _form;
public GDTokensForm<State, GDSquareOpenBracket, GDExpressionsList, GDSquareCloseBracket> TypedForm => _form;

internal GDArrayInitializerExpression(int intendation)
{
_intendation = intendation;
_form = new GDTokensForm<State, GDSquareOpenBracket, GDExpressionsList, GDSquareCloseBracket>(this);
}

public GDArrayInitializerExpression()
{
_form = new GDTokensForm<State, GDSquareOpenBracket, GDExpressionsList, GDSquareCloseBracket>(this);
Expand Down
12 changes: 10 additions & 2 deletions src/GDShrapt.Reader/Expressions/GDAwaitExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public GDOpenBracket OpenBracket
}
public GDExpressionsList Parameters
{
get => _form.Token2 ?? (_form.Token2 = new GDExpressionsList());
get => _form.Token2 ?? (_form.Token2 = new GDExpressionsList(_intendation));
set => _form.Token2 = value;
}
public GDCloseBracket CloseBracket
Expand All @@ -40,9 +40,17 @@ public enum State
Completed
}

readonly int _intendation;
readonly GDTokensForm<State, GDAwaitKeyword, GDOpenBracket, GDExpressionsList, GDCloseBracket> _form;
public override GDTokensForm Form => _form;
public GDTokensForm<State, GDAwaitKeyword, GDOpenBracket, GDExpressionsList, GDCloseBracket> TypedForm => _form;

internal GDAwaitExpression(int intendation)
{
_intendation = intendation;
_form = new GDTokensForm<State, GDAwaitKeyword, GDOpenBracket, GDExpressionsList, GDCloseBracket>(this);
}

public GDAwaitExpression()
{
_form = new GDTokensForm<State, GDAwaitKeyword, GDOpenBracket, GDExpressionsList, GDCloseBracket>(this);
Expand Down Expand Up @@ -88,7 +96,7 @@ internal override void HandleNewLineChar(GDReadingState state)

public override GDNode CreateEmptyInstance()
{
return new GDYieldExpression();
return new GDAwaitExpression();
}

internal override void Visit(IGDVisitor visitor)
Expand Down
10 changes: 9 additions & 1 deletion src/GDShrapt.Reader/Expressions/GDBracketExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,16 @@ public enum State
}

readonly GDTokensForm<State, GDOpenBracket, GDExpression, GDCloseBracket> _form;
readonly int _intendation;

public override GDTokensForm Form => _form;
public GDTokensForm<State, GDOpenBracket, GDExpression, GDCloseBracket> TypedForm => _form;
internal GDBracketExpression(int intendation)
{
_form = new GDTokensForm<State, GDOpenBracket, GDExpression, GDCloseBracket>(this);
_intendation = intendation;
}

public GDBracketExpression()
{
_form = new GDTokensForm<State, GDOpenBracket, GDExpression, GDCloseBracket>(this);
Expand All @@ -51,7 +59,7 @@ internal override void HandleChar(char c, GDReadingState state)
break;
case State.Expression:
if (!this.ResolveSpaceToken(c, state))
this.ResolveExpression(c, state, this);
this.ResolveExpression(c, state, _intendation, this);
break;
case State.CloseBracket:
if (!this.ResolveSpaceToken(c, state))
Expand Down
Loading

0 comments on commit d0acf4c

Please sign in to comment.