Skip to content

Commit

Permalink
Merge pull request #253 from b3b00/tech/optim-bypass-nodes
Browse files Browse the repository at this point in the history
Tech/optim bypass nodes
  • Loading branch information
b3b00 authored Nov 5, 2021
2 parents 6c8bd2f + 0c4f32d commit bb49568
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 4 deletions.
Binary file added afterRemoveByPassOptim.dtp
Binary file not shown.
123 changes: 123 additions & 0 deletions afterRemoveByPassOptim.dtp.DotSettings

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion sly/parser/parser/SyntaxParseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public void AddExpectings(IEnumerable<IN> expected)
}
Expecting.AddRange(expected);
}


public bool HasByPassNodes { get; set; } = false;

}
}
7 changes: 6 additions & 1 deletion sly/parser/parser/SyntaxTreeCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public SyntaxParseResult<IN> CleanSyntaxTree(SyntaxParseResult<IN> result)
var tree = result.Root;
if (tree != null)
{
tree = RemoveByPassNodes(tree);
if (result.HasByPassNodes)
{
tree = RemoveByPassNodes(tree);
}


if (NeedAssociativityProcessing(tree)) tree = SetAssociativity(tree);
result.Root = tree;
}
Expand Down
16 changes: 15 additions & 1 deletion sly/parser/parser/llparser/EBNFRecursiveDescentSyntaxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,20 @@ public SyntaxParseResult<IN> ParseZeroOrMore(IList<Token<IN>> tokens, ZeroOrMore

var innerErrors = new List<UnexpectedTokenSyntaxError<IN>>();

bool hasByPasNodes = false;
while (stillOk)
{
SyntaxParseResult<IN> innerResult = null;
if (innerClause is TerminalClause<IN> term)
{
manyNode.IsManyTokens = true;
innerResult = ParseTerminal(tokens, term, currentPosition);
hasByPasNodes = hasByPasNodes || innerResult.HasByPassNodes;
}
else if (innerClause is NonTerminalClause<IN> nonTerm)
{
innerResult = ParseNonTerminal(tokens, nonTerm, currentPosition);
hasByPasNodes = hasByPasNodes || innerResult.HasByPassNodes;
if (nonTerm.IsGroup)
manyNode.IsManyGroups = true;
else
Expand All @@ -384,12 +387,14 @@ public SyntaxParseResult<IN> ParseZeroOrMore(IList<Token<IN>> tokens, ZeroOrMore
{
manyNode.IsManyGroups = true;
innerResult = ParseNonTerminal(tokens, innerClause as NonTerminalClause<IN>, currentPosition);
hasByPasNodes = hasByPasNodes || innerResult.HasByPassNodes;
}
else if (innerClause is ChoiceClause<IN> choice)
{
manyNode.IsManyTokens = choice.IsTerminalChoice;
manyNode.IsManyValues = choice.IsNonTerminalChoice;
innerResult = ParseChoice(tokens, choice, currentPosition);
hasByPasNodes = hasByPasNodes || innerResult.HasByPassNodes;
}
else
{
Expand All @@ -401,6 +406,7 @@ public SyntaxParseResult<IN> ParseZeroOrMore(IList<Token<IN>> tokens, ZeroOrMore
manyNode.Add(innerResult.Root);
currentPosition = innerResult.EndingPosition;
lastInnerResult = innerResult;
hasByPasNodes = hasByPasNodes || innerResult.HasByPassNodes;
}
else
{
Expand All @@ -419,6 +425,7 @@ public SyntaxParseResult<IN> ParseZeroOrMore(IList<Token<IN>> tokens, ZeroOrMore
result.Errors = innerErrors;
result.Root = manyNode;
result.IsEnded = lastInnerResult != null && lastInnerResult.IsEnded;
result.HasByPassNodes = hasByPasNodes;
return result;
}

Expand All @@ -432,22 +439,26 @@ public SyntaxParseResult<IN> ParseOneOrMore(IList<Token<IN>> tokens, OneOrMoreCl

SyntaxParseResult<IN> lastInnerResult = null;

bool hasByPasNodes = false;
SyntaxParseResult<IN> firstInnerResult = null;
if (innerClause is TerminalClause<IN>)
{
manyNode.IsManyTokens = true;
firstInnerResult = ParseTerminal(tokens, innerClause as TerminalClause<IN>, currentPosition);
hasByPasNodes = hasByPasNodes || firstInnerResult.HasByPassNodes;
}
else if (innerClause is NonTerminalClause<IN>)
{
manyNode.IsManyValues = true;
firstInnerResult = ParseNonTerminal(tokens, innerClause as NonTerminalClause<IN>, currentPosition);
hasByPasNodes = hasByPasNodes || firstInnerResult.HasByPassNodes;
}
else if (innerClause is ChoiceClause<IN> choice)
{
manyNode.IsManyTokens = choice.IsTerminalChoice;
manyNode.IsManyValues = choice.IsNonTerminalChoice;
firstInnerResult = ParseChoice(tokens, choice, currentPosition);
hasByPasNodes = hasByPasNodes || firstInnerResult.HasByPassNodes;
}
else
{
Expand Down Expand Up @@ -480,6 +491,7 @@ public SyntaxParseResult<IN> ParseOneOrMore(IList<Token<IN>> tokens, OneOrMoreCl
result.IsError = isError;
result.Root = manyNode;
result.IsEnded = lastInnerResult != null && lastInnerResult.IsEnded;
result.HasByPassNodes = hasByPasNodes;
return result;
}

Expand All @@ -492,6 +504,7 @@ public SyntaxParseResult<IN> ParseOption(IList<Token<IN>> tokens, OptionClause<I

SyntaxParseResult<IN> innerResult = null;

bool hasByPassNode = false;

if (innerClause is TerminalClause<IN> term)
innerResult = ParseTerminal(tokens, term, currentPosition);
Expand Down Expand Up @@ -540,11 +553,12 @@ public SyntaxParseResult<IN> ParseOption(IList<Token<IN>> tokens, OptionClause<I
else
{
var node = innerResult.Root;

var children = new List<ISyntaxNode<IN>> {innerResult.Root};
result.Root =
new OptionSyntaxNode<IN>( rule.NonTerminalName,children, rule.GetVisitor());
result.EndingPosition = innerResult.EndingPosition;
result.HasByPassNodes = innerResult.HasByPassNodes;
}

return result;
Expand Down
3 changes: 3 additions & 0 deletions sly/parser/parser/llparser/RecursiveDescentSyntaxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ protected SyntaxNode<IN> ManageExpressionRules(Rule<IN> rule, SyntaxNode<IN> nod
if (rule.IsExpressionRule && rule.IsByPassRule)
{
node.IsByPassNode = true;
node.HasByPassNodes = true;
}
else if (rule.IsExpressionRule && !rule.IsByPassRule)
{
Expand Down Expand Up @@ -363,6 +364,7 @@ public SyntaxParseResult<IN> ParseTerminal(IList<Token<IN>> tokens, TerminalClau
var token = tokens[position];
token.Discarded = terminal.Discarded;
result.Root = new SyntaxLeaf<IN>(token,terminal.Discarded);
result.HasByPassNodes = false;
return result;
}

Expand Down Expand Up @@ -440,6 +442,7 @@ public SyntaxParseResult<IN> ParseNonTerminal(IList<Token<IN>> tokens, NonTermin
result.EndingPosition = max.EndingPosition;
result.IsError = max.IsError;
result.IsEnded = max.IsEnded;
result.HasByPassNodes = max.HasByPassNodes;

if (rulesResults.Any())
{
Expand Down
2 changes: 2 additions & 0 deletions sly/parser/syntax/tree/ISyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ public interface ISyntaxNode<IN> where IN : struct

bool Discarded { get; }
string Name { get; }

bool HasByPassNodes { get; set; }
}
}
3 changes: 2 additions & 1 deletion sly/parser/syntax/tree/SyntaxLeaf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public SyntaxLeaf(Token<IN> token, bool discarded)
public Token<IN> Token { get; }
public bool Discarded { get; }
public string Name => Token.TokenID.ToString();


public bool HasByPassNodes { get; set; } = false;
}
}
2 changes: 2 additions & 0 deletions sly/parser/syntax/tree/SyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public SyntaxNode(string name, List<ISyntaxNode<IN>> children = null, MethodInfo
public bool Discarded => false;
public string Name { get; set; }

public bool HasByPassNodes { get; set; } = false;

#region expression syntax nodes

public OperationMetaData<IN> Operation { get; set; } = null;
Expand Down

0 comments on commit bb49568

Please sign in to comment.