public SyntaxParseResult <IN> ParseChoice(IList <Token <IN> > tokens, ChoiceClause <IN> choice, int position) { var currentPosition = position; SyntaxParseResult <IN> result = new SyntaxParseResult <IN>() { IsError = true, IsEnded = false, EndingPosition = currentPosition }; foreach (var alternate in choice.Choices) { if (alternate is TerminalClause <IN> terminalAlternate) { result = ParseTerminal(tokens, terminalAlternate, currentPosition); } else if (alternate is NonTerminalClause <IN> nonTerminalAlternate) { result = ParseNonTerminal(tokens, nonTerminalAlternate, currentPosition); } else { throw new InvalidOperationException("unable to apply repeater inside " + choice.GetType().Name); } if (result.IsOk) { if (choice.IsTerminalChoice && choice.IsDiscarded && result.Root is SyntaxLeaf <IN> leaf) { var discardedToken = new SyntaxLeaf <IN>(leaf.Token, true); result.Root = discardedToken; } return(result); } } if (result.IsError && choice.IsTerminalChoice) { var terminalAlternates = choice.Choices.Cast <TerminalClause <IN> >(); var expected = terminalAlternates.Select(x => x.ExpectedToken).ToList(); result.Errors.Add(new UnexpectedTokenSyntaxError <IN>(tokens[currentPosition], expected.ToArray())); } return(result); }
private SyntaxVisitorResult <IN, OUT> Visit(SyntaxLeaf <IN> leaf) { return(SyntaxVisitorResult <IN, OUT> .NewToken(leaf.Token)); }
private DotNode Leaf(SyntaxLeaf <IN> leaf) { return(Leaf(leaf.Token.TokenID, leaf.Token.Value)); }
private static SyntaxNode FactoryCreateColumnOrExpression(ParsingContext xoContext) { SyntaxKind eNextTokenKind = xoContext.List.Peek().ExpectedType; SyntaxNode oReturnNode; // Is any identifier or Expression (function/literal) if (SyntaxKindFacts.IsFunction(eNextTokenKind)) { // COUNT (*) oReturnNode = FactoryCreateColumnFunction(xoContext); } else if (eNextTokenKind == SyntaxKind.OpenParenthesisToken) { oReturnNode = FactoryInterpretOpenParenthesisToken(xoContext); } else if ( SyntaxKindFacts.IsIdentifier(eNextTokenKind) || SyntaxKindFacts.IsLiteral(eNextTokenKind)) { // Only Column List Nodes can create an Alias Boolean bIsAliasNeeded = xoContext.CurrentNode.ExpectedType == SyntaxKind.ColumnListNode && eNextTokenKind != SyntaxKind.StarToken; // Stars do not get alias' oReturnNode = FactoryCreateColumn(xoContext, bIsAliasNeeded); } else if (eNextTokenKind == SyntaxKind.CaseKeyword) { if (xoContext.CurrentNode.ExpectedType == SyntaxKind.ColumnListNode) { return(new Symbol( xoContext.List.Peek(), NodeStrategyFactory.FactoryCreateStrategy(xoContext.List.Pop().ExpectedType), -1)); } else { return(new SyntaxNode( xoContext.List.Peek(), NodeStrategyFactory.FactoryCreateStrategy(xoContext.List.Pop().ExpectedType))); } } else { oReturnNode = new SyntaxLeaf(xoContext.List.Pop()); } // If we have a trailing || and we arent already using a bar bar if (xoContext.CurrentNode.ExpectedType != SyntaxKind.BarBarToken && xoContext.List.Peek().ExpectedType == SyntaxKind.BarBarToken) { // Create a new bar bar node (to hold the children) SyntaxNode oBarNode = new SyntaxNode(xoContext.List.Pop()); // Add the child oBarNode.Add(oReturnNode); // return this collection return(oBarNode); } // Valid case, no fixing necessary else { return(oReturnNode); } }