public ParserOperation.ParserOp Lookup(int CurrentState, ParserStackItem NextInput) { int lookaheadIndex; if (NextInput == null) { lookaheadIndex = ProductionPatterns.Length + TokenPatterns.Length; } else if (NextInput.ProductionName != null) { lookaheadIndex = IndexOfFirstMatch(ProductionPatterns, NextInput); } else { lookaheadIndex = IndexOfFirstMatch(TokenPatterns, NextInput) + ProductionPatterns.Length; } if (lookaheadIndex == -1) { throw new Exceptions.UnknownTokenException(NextInput); } else { var transition = operations[CurrentState, lookaheadIndex]; if (transition == null) { throw new Exceptions.SyntaxError(NextInput.Token, ValidNextPatterns(CurrentState)); } return(transition); } }
public ParserStackItem Run() { ParserStackItem result = null; while ((result = Step()) == null) { } return(result); }
private int IndexOfFirstMatch(StackItemPattern[] pattern, ParserStackItem item) { for (var i = 0; i < pattern.Length; i++) { if (pattern[i].Matches(item)) { return(i); } } return(-1); }
internal bool Matches(ParserStackItem stackItem) { if (stackItem == null) { return(PatternType == EPatternType.EndOfInput); } switch (PatternType) { case EPatternType.Literal: return(stackItem.Token != null && stackItem.Token.Value == Value); case EPatternType.TokenType: return(stackItem.Token != null && stackItem.Token.TokenType == Value); case EPatternType.Production: return(stackItem.ProductionName != null && stackItem.ProductionName == Value); case EPatternType.EndOfInput: return(false); default: throw new InvalidOperationException("Unknown Pattern Type"); } }