public ParseResult <IN, OUT> ParseWithContext(IList <Token <IN> > tokens, object parsingContext = null, string startingNonTerminal = null) { var result = new ParseResult <IN, OUT>(); var cleaner = new SyntaxTreeCleaner <IN>(); var syntaxResult = SyntaxParser.Parse(tokens, startingNonTerminal); syntaxResult = cleaner.CleanSyntaxTree(syntaxResult); if (!syntaxResult.IsError && syntaxResult.Root != null) { var r = Visitor.VisitSyntaxTree(syntaxResult.Root, parsingContext); result.Result = r; result.SyntaxTree = syntaxResult.Root; result.IsError = false; } else { result.Errors = new List <ParseError>(); var unexpectedTokens = syntaxResult.Errors.Cast <UnexpectedTokenSyntaxError <IN> >().ToList(); var byEnding = unexpectedTokens.GroupBy(x => x.UnexpectedToken.Position).OrderBy(x => x.Key); var errors = new List <ParseError>(); foreach (var expecting in byEnding) { var expectingTokens = expecting.SelectMany(x => x.ExpectedTokens).Distinct(); var expected = new UnexpectedTokenSyntaxError <IN>(expecting.First().UnexpectedToken, expectingTokens.ToArray()); errors.Add(expected); } result.Errors.AddRange(errors); result.IsError = true; } return(result); }
public ParseResult <IN, OUT> ParseWithContext(IList <Token <IN> > tokens, object parsingContext = null, string startingNonTerminal = null) { var result = new ParseResult <IN, OUT>(); var cleaner = new SyntaxTreeCleaner <IN>(); var syntaxResult = SyntaxParser.Parse(tokens, startingNonTerminal); syntaxResult = cleaner.CleanSyntaxTree(syntaxResult); if (!syntaxResult.IsError && syntaxResult.Root != null) { var r = Visitor.VisitSyntaxTree(syntaxResult.Root, parsingContext); result.Result = r; result.IsError = false; } else { result.Errors = new List <ParseError>(); result.Errors.AddRange(syntaxResult.Errors); result.IsError = true; } return(result); }
public ParseResult <IN, OUT> Parse(IList <Token <IN> > tokens, string startingNonTerminal = null) { var result = new ParseResult <IN, OUT>(); var cleaner = new SyntaxTreeCleaner <IN>(); SyntaxParseResult <IN> syntaxResult = SyntaxParser.Parse(tokens, startingNonTerminal); Debug.WriteLine($" syntax result ok ?:>{!syntaxResult.IsError}< ended:>{syntaxResult.IsEnded}<"); syntaxResult = cleaner.CleanSyntaxTree(syntaxResult); if (!syntaxResult.IsError && syntaxResult.Root != null) { OUT r = Visitor.VisitSyntaxTree(syntaxResult.Root); result.Result = r; result.IsError = false; } else { result.Errors = new List <ParseError>(); result.Errors.AddRange(syntaxResult.Errors); result.IsError = true; } return(result); }