Skip to content

Commit

Permalink
WIP: cloning basics
Browse files Browse the repository at this point in the history
  • Loading branch information
elamaunt committed Jun 26, 2021
1 parent 238c90e commit e2951a9
Show file tree
Hide file tree
Showing 118 changed files with 672 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Atributes/GDClassNameAtribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IKeywordReceiver<GDClassNameKeyword>.HandleReceivedToken(GDClassNameKeyword token)
{
if (_form.State == State.ClassName)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Atributes/GDExtendsAtribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IKeywordReceiver<GDExtendsKeyword>.HandleReceivedToken(GDExtendsKeyword token)
{
if (_form.State == State.Extends)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Atributes/GDToolAtribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IKeywordReceiver<GDToolKeyword>.HandleReceivedToken(GDToolKeyword token)
{
if (_form.State == State.Tool)
Expand Down
13 changes: 13 additions & 0 deletions src/GDShrapt.Reader/Basics/GDNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ public override string ToString()
return builder.ToString();
}

/// <summary>
/// Creates empty instance of this node type
/// </summary>
/// <returns>New empty instance</returns>
public abstract GDNode CreateEmptyInstance();

public override GDSyntaxToken Clone()
{
var node = CreateEmptyInstance();
node.Form.CloneFrom(node.Form);
return node;
}

void ITokenReceiver.HandleReceivedToken(GDInvalidToken token)
{
Form.AddBeforeActiveToken(token);
Expand Down
15 changes: 13 additions & 2 deletions src/GDShrapt.Reader/Basics/GDSyntaxToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GDShrapt.Reader
/// Basic syntax node.
/// </summary>
[DebuggerDisplay("{DebuggerView}")]
public abstract class GDSyntaxToken : GDReader
public abstract class GDSyntaxToken : GDReader, ICloneable
{
GDNode _parent;

Expand All @@ -34,7 +34,7 @@ internal set
}

/// <summary>
/// Removes this node parent or do nothing if <see cref="Parent"/> <see langword="null"/>
/// Removes this node from parent or do nothing if <see cref="Parent"/> <see langword="null"/>
/// </summary>
public bool RemoveFromParent()
{
Expand All @@ -50,6 +50,17 @@ public virtual void AppendTo(StringBuilder builder)
builder.Append(ToString());
}

/// <summary>
/// Creates deep clone of the current token and it's children
/// </summary>
/// <returns>New token with all children (if node)</returns>
public abstract GDSyntaxToken Clone();

object ICloneable.Clone()
{
return Clone();
}

[DebuggerHidden]
internal string DebuggerView => $"{NodeName} '{ToString()}'";
}
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDClassDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public GDClassDeclaration()
public IEnumerable<GDEnumDeclaration> Enums => Members.OfType<GDEnumDeclaration>();
public IEnumerable<GDInnerClassDeclaration> InnerClasses => Members.OfType<GDInnerClassDeclaration>();

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

internal override void HandleChar(char c, GDReadingState state)
{
if (IsSpace(c))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ internal override void HandleNewLineChar(GDReadingState state)
_form.AddBeforeActiveToken(new GDNewLine());
}

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

void IExpressionsReceiver.HandleReceivedToken(GDExpression token)
{
if (_form.State == State.Key)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDEnumDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IKeywordReceiver<GDEnumKeyword>.HandleReceivedToken(GDEnumKeyword token)
{
if (_form.State == State.Enum)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDEnumValueDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ internal override void HandleNewLineChar(GDReadingState state)
_form.AddBeforeActiveToken(new GDNewLine());
}

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

void IIdentifierReceiver.HandleReceivedToken(GDIdentifier token)
{
if (_form.State == State.Identifier)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDExportDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IKeywordReceiver<GDExportKeyword>.HandleReceivedToken(GDExportKeyword token)
{
if (_form.State == State.Export)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDInnerClassDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ internal override void HandleNewLineChar(GDReadingState state)
}
}

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

void IKeywordReceiver<GDClassKeyword>.HandleReceivedToken(GDClassKeyword token)
{
if (_form.State == State.Class)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDMatchCaseDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ internal override void HandleNewLineChar(GDReadingState state)
}
}

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

void ITokenReceiver<GDColon>.HandleReceivedToken(GDColon token)
{
if (_form.State == State.Colon)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDMethodDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IKeywordReceiver<GDStaticKeyword>.HandleReceivedToken(GDStaticKeyword token)
{
if (_form.State == State.Static)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDParameterDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IIdentifierReceiver.HandleReceivedToken(GDIdentifier token)
{
if(_form.State == State.Identifier)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDSignalDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IKeywordReceiver<GDSignalKeyword>.HandleReceivedToken(GDSignalKeyword token)
{
if (_form.State == State.Signal)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Declarations/GDVariableDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IKeywordReceiver<GDConstKeyword>.HandleReceivedToken(GDConstKeyword token)
{
if (_form.State == State.Const)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,10 @@ void ITokenReceiver<GDSquareCloseBracket>.HandleReceivedTokenSkip()

throw new GDInvalidReadingStateException();
}

public override GDNode CreateEmptyInstance()
{
return new GDArrayInitializerExpression();
}
}
}
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDBoolExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IKeywordReceiver<GDTrueKeyword>.HandleReceivedToken(GDTrueKeyword token)
{
if (_form.State != State.Completed)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDBracketExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ internal override void HandleNewLineChar(GDReadingState state)
}
}

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

void ITokenReceiver<GDOpenBracket>.HandleReceivedToken(GDOpenBracket token)
{
if (_form.State == State.OpenBracket)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDBreakExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PassNewLine();
}

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

void IKeywordReceiver<GDBreakKeyword>.HandleReceivedToken(GDBreakKeyword token)
{
if (_form.State == State.Break)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDBreakPointExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PassNewLine();
}

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

void IKeywordReceiver<GDBreakPointKeyword>.HandleReceivedToken(GDBreakPointKeyword token)
{
if (_form.State == State.BreakPoint)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace GDShrapt.Reader
{
public sealed class GDCallExression : GDExpression,
public sealed class GDCallExpression : GDExpression,
IExpressionsReceiver,
ITokenReceiver<GDOpenBracket>,
ITokenReceiver<GDCloseBracket>
Expand Down Expand Up @@ -35,7 +35,7 @@ enum State

readonly GDTokensForm<State, GDExpression, GDOpenBracket, GDExpressionsList, GDCloseBracket> _form;
internal override GDTokensForm Form => _form;
public GDCallExression()
public GDCallExpression()
{
_form = new GDTokensForm<State, GDExpression, GDOpenBracket, GDExpressionsList, GDCloseBracket>(this);
}
Expand Down Expand Up @@ -78,6 +78,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void IExpressionsReceiver.HandleReceivedToken(GDExpression token)
{
if (_form.State == State.Caller)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDContinueExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PassNewLine();
}

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

void IKeywordReceiver<GDContinueKeyword>.HandleReceivedToken(GDContinueKeyword token)
{
if (_form.State == State.Continue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void ITokenReceiver<GDFigureOpenBracket>.HandleReceivedToken(GDFigureOpenBracket token)
{
if (_form.State == State.FigureOpenBracket)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDDualOperatorExression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public override GDExpression SwapRight(GDExpression expression)
return right;
}

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

void IExpressionsReceiver.HandleReceivedToken(GDExpression token)
{
if (_form.State == State.LeftExpression)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDGetNodeExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ internal override void HandleNewLineChar(GDReadingState state)
state.PopAndPassNewLine();
}

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

void ITokenReceiver<GDDollar>.HandleReceivedToken(GDDollar token)
{
if (_form.State == State.Dollar)
Expand Down
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDIdentifierExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@ internal override void HandleNewLineChar(GDReadingState state)
{
state.PopAndPassNewLine();
}

public override GDNode CreateEmptyInstance()
{
return new GDIdentifierExpression();
}
}
}
5 changes: 5 additions & 0 deletions src/GDShrapt.Reader/Expressions/GDIfExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public override void RebuildBranchesOfPriorityIfNeeded()
FalseExpression = FalseExpression.RebuildRootOfPriorityIfNeeded();
}

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

void IExpressionsReceiver.HandleReceivedToken(GDExpression token)
{
if (_form.State == State.True)
Expand Down
4 changes: 2 additions & 2 deletions src/GDShrapt.Reader/Expressions/GDIndexerExression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public override void RebuildBranchesOfPriorityIfNeeded()
CallerExpression = CallerExpression.RebuildRootOfPriorityIfNeeded();
}

public override string ToString()
public override GDNode CreateEmptyInstance()
{
return $"{CallerExpression}[{InnerExpression}]";
return new GDIndexerExression();
}

void IExpressionsReceiver.HandleReceivedToken(GDExpression token)
Expand Down
Loading

0 comments on commit e2951a9

Please sign in to comment.