Skip to content

Commit

Permalink
light refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b00 committed Mar 24, 2021
1 parent 7256fbe commit 84b0385
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
12 changes: 6 additions & 6 deletions sly/parser/parser/llparser/EBNFRecursiveDescentSyntaxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,16 @@ public override SyntaxParseResult<IN> Parse(IList<Token<IN>> tokens, Rule<IN> ru
}
else if (clause is ChoiceClause<IN> choice)
{
var optionResult = ParseChoice(tokens, choice, currentPosition);
currentPosition = optionResult.EndingPosition;
if (optionResult.IsError && optionResult.Errors != null && optionResult.Errors.Count > 0)
var choiceResult = ParseChoice(tokens, choice, currentPosition);
currentPosition = choiceResult.EndingPosition;
if (choiceResult.IsError && choiceResult.Errors != null && choiceResult.Errors.Any())
{
errors.AddRange(optionResult.Errors);
errors.AddRange(choiceResult.Errors);
}

isError = optionResult.IsError;
isError = choiceResult.IsError;

children.Add(optionResult.Root);
children.Add(choiceResult.Root);
}

if (isError) break;
Expand Down
40 changes: 28 additions & 12 deletions sly/parser/parser/llparser/RecursiveDescentSyntaxParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,9 @@ public SyntaxParseResult<IN> ParseNonTerminal(IList<Token<IN>> tokens, NonTermin
.ToList();

if (rules.Count == 0 )
{
if (startPosition < tokens.Count)
{
errors.Add(new UnexpectedTokenSyntaxError<IN>(tokens[startPosition],
allAcceptableTokens.ToArray<IN>()));
}
else
{
errors.Add(new UnexpectedTokenSyntaxError<IN>(new Token<IN>() { IsEOS = true},allAcceptableTokens.ToArray<IN>()) );
}

}
{
return NoMatchingRuleError(tokens, currentPosition, allAcceptableTokens);
}

var innerRuleErrors = new List<UnexpectedTokenSyntaxError<IN>>();
var greaterIndex = 0;
Expand Down Expand Up @@ -461,6 +452,31 @@ public SyntaxParseResult<IN> ParseNonTerminal(IList<Token<IN>> tokens, NonTermin
return result;
}

private static SyntaxParseResult<IN> NoMatchingRuleError(IList<Token<IN>> tokens, int currentPosition, List<IN> allAcceptableTokens)
{
var noRuleErrors = new List<UnexpectedTokenSyntaxError<IN>>();

if (currentPosition < tokens.Count)
{
noRuleErrors.Add(new UnexpectedTokenSyntaxError<IN>(tokens[currentPosition],
allAcceptableTokens.ToArray<IN>()));
}
else
{
noRuleErrors.Add(new UnexpectedTokenSyntaxError<IN>(new Token<IN>() {IsEOS = true},
allAcceptableTokens.ToArray<IN>()));
}

var error = new SyntaxParseResult<IN>();
error.IsError = true;
error.Root = null;
error.IsEnded = false;
error.Errors = noRuleErrors;
error.EndingPosition = currentPosition;

return error;
}

public virtual void Init(ParserConfiguration<IN, OUT> configuration, string root)
{
if (root != null) StartingNonTerminal = root;
Expand Down

0 comments on commit 84b0385

Please sign in to comment.