Skip to content

Commit

Permalink
error reporting for repeated nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
olduh29 committed May 21, 2018
1 parent 2c0bb2e commit 74a5c07
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions sly/lexer/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Token<T>


public TokenPosition Position { get; set; }

public int PositionInTokenFlow { get; set; }
public T TokenID { get; set; }
public bool IsComment { get; set; }

Expand Down
5 changes: 5 additions & 0 deletions sly/parser/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public ParseResult<IN,OUT> Parse(string source, string startingNonTerminal = nul
try
{
IList<Token<IN>> tokens = Lexer.Tokenize(source).ToList<Token<IN>>();
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
token.PositionInTokenFlow = i;
}
result = Parse(tokens, startingNonTerminal);
}
catch(LexerException e)
Expand Down
2 changes: 1 addition & 1 deletion sly/parser/parser/SyntaxParseResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SyntaxParseResult<IN> where IN : struct

public bool IsError{get; set;}

public List<UnexpectedTokenSyntaxError<IN>> Errors {get; set;}
public List<UnexpectedTokenSyntaxError<IN>> Errors { get; set; } = new List<UnexpectedTokenSyntaxError<IN>>();

public int EndingPosition { get; set; }

Expand Down
22 changes: 13 additions & 9 deletions sly/parser/parser/llparser/EBNFRecursiveDescentSyntaxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void InitStartingTokensWithNonTerminal(Rule<IN> rule, NonTerminalClause<

private void InitStartingTokensWithZeroOrMore(Rule<IN> rule, ZeroOrMoreClause<IN> manyClause,
Dictionary<string, NonTerminal<IN>> nonTerminals)
{
{
if (manyClause.Clause is TerminalClause<IN>)
{
TerminalClause<IN> term = manyClause.Clause as TerminalClause<IN>;
Expand Down Expand Up @@ -129,8 +129,6 @@ private void InitStartingTokensWithOption(Rule<IN> rule, OptionClause<IN> option

public override SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, Rule<IN> rule, int position, string nonTerminalName)
{


int currentPosition = position;
List<UnexpectedTokenSyntaxError<IN>> errors = new List<UnexpectedTokenSyntaxError<IN>>();
bool isError = false;
Expand All @@ -142,10 +140,10 @@ public override SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, Rule<IN> ru
children = new List<ISyntaxNode<IN>>();
foreach (IClause<IN> clause in rule.Clauses)
{
if (clause is TerminalClause<IN>)
if (clause is TerminalClause<IN> termClause)
{
SyntaxParseResult<IN> termRes =
ParseTerminal(tokens, clause as TerminalClause<IN>, currentPosition);
ParseTerminal(tokens, termClause, currentPosition);
if (!termRes.IsError)
{
children.Add(termRes.Root);
Expand All @@ -165,6 +163,7 @@ public override SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, Rule<IN> ru
ParseNonTerminal(tokens, clause as NonTerminalClause<IN>, currentPosition);
if (!nonTerminalResult.IsError)
{
errors.AddRange(nonTerminalResult.Errors);
children.Add(nonTerminalResult.Root);
currentPosition = nonTerminalResult.EndingPosition;
}
Expand All @@ -188,6 +187,7 @@ public override SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, Rule<IN> ru
}
if (!manyResult.IsError)
{
errors.AddRange(manyResult.Errors);
children.Add(manyResult.Root);
currentPosition = manyResult.EndingPosition;
}
Expand Down Expand Up @@ -233,7 +233,7 @@ public override SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, Rule<IN> ru


public SyntaxParseResult<IN> ParseZeroOrMore(IList<Token<IN>> tokens, ZeroOrMoreClause<IN> clause, int position)
{
{
SyntaxParseResult<IN> result = new SyntaxParseResult<IN>();
ManySyntaxNode<IN> manyNode = new ManySyntaxNode<IN>("");
int currentPosition = position;
Expand All @@ -242,6 +242,8 @@ public SyntaxParseResult<IN> ParseZeroOrMore(IList<Token<IN>> tokens, ZeroOrMore

SyntaxParseResult<IN> lastInnerResult = null;

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

while (stillOk)
{
SyntaxParseResult<IN> innerResult = null;
Expand All @@ -265,12 +267,17 @@ public SyntaxParseResult<IN> ParseZeroOrMore(IList<Token<IN>> tokens, ZeroOrMore
currentPosition = innerResult.EndingPosition;
lastInnerResult = innerResult;
}
else
{
innerErrors.AddRange(innerResult.Errors);
}
stillOk = stillOk && innerResult != null && !(innerResult.IsError) && currentPosition < tokens.Count;
}


result.EndingPosition = currentPosition;
result.IsError = false;
result.Errors= innerErrors;
result.Root = manyNode;
result.IsEnded = lastInnerResult != null && lastInnerResult.IsEnded;
return result;
Expand Down Expand Up @@ -332,20 +339,17 @@ public SyntaxParseResult<IN> ParseOneOrMore(IList<Token<IN>> tokens, OneOrMoreCl
public SyntaxParseResult<IN> ParseOption(IList<Token<IN>> tokens, OptionClause<IN> clause, int position)
{
SyntaxParseResult<IN> result = new SyntaxParseResult<IN>();
ManySyntaxNode<IN> manyNode = new ManySyntaxNode<IN>("");
int currentPosition = position;
IClause<IN> innerClause = clause.Clause;

SyntaxParseResult<IN> innerResult = null;

if (innerClause is TerminalClause<IN>)
{
manyNode.IsManyTokens = true;
innerResult = ParseTerminal(tokens, innerClause as TerminalClause<IN>, currentPosition);
}
else if (innerClause is NonTerminalClause<IN>)
{
manyNode.IsManyValues = true;
innerResult = ParseNonTerminal(tokens, innerClause as NonTerminalClause<IN>, currentPosition);
}
else
Expand Down
9 changes: 7 additions & 2 deletions sly/parser/parser/llparser/RecursiveDescentSyntaxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, string startingNonTe
List<int> endingPositions = rs.Select(r => r.EndingPosition).ToList<int>();
int lastposition = endingPositions.Max();
List<SyntaxParseResult<IN>> furtherResults = rs.Where<SyntaxParseResult<IN>>(r => r.EndingPosition == lastposition).ToList<SyntaxParseResult<IN>>();

errors.Add(new UnexpectedTokenSyntaxError<IN>(tokens[lastposition], null));
furtherResults.ForEach(r =>
{
Expand All @@ -139,10 +140,12 @@ public SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, string startingNonTe
{
result = new SyntaxParseResult<IN>();
errors.Sort();

if (errors.Count > 0)
{
List<UnexpectedTokenSyntaxError<IN>> singleError = new List<UnexpectedTokenSyntaxError<IN>>() { errors[errors.Count() - 1] };
result.Errors = singleError;
int lastErrorPosition = errors.Select(e => e.UnexpectedToken.PositionInTokenFlow).ToList().Max();
var lastErrors = errors.Where(e => e.UnexpectedToken.PositionInTokenFlow == lastErrorPosition).ToList();
result.Errors = lastErrors;
}
else
{
Expand Down Expand Up @@ -336,6 +339,7 @@ public SyntaxParseResult<IN> ParseNonTerminal(IList<Token<IN>> tokens, NonTermin
if (!innerRuleRes.IsError && okResult == null)
{
okResult = innerRuleRes;
okResult.Errors = innerRuleRes.Errors;
found = true;
currentPosition = innerRuleRes.EndingPosition;
}
Expand Down Expand Up @@ -375,6 +379,7 @@ public SyntaxParseResult<IN> ParseNonTerminal(IList<Token<IN>> tokens, NonTermin
{
result.IsError = true;
result.Errors = errors;
greaterIndex = errors.Select(e => e.UnexpectedToken.PositionInTokenFlow).Max();
result.EndingPosition = greaterIndex;
}
return result;
Expand Down

0 comments on commit 74a5c07

Please sign in to comment.