public virtual Handle _RuleRef([NotNull] GrammarAST node) { Rule r = g.GetRule(node.Text); if (r == null) { g.tool.errMgr.GrammarError(ErrorType.INTERNAL_ERROR, g.fileName, node.Token, "Rule " + node.Text + " undefined"); return(null); } RuleStartState start = atn.ruleToStartState[r.index]; ATNState left = NewState(node); ATNState right = NewState(node); int precedence = 0; if (((GrammarASTWithOptions)node).GetOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME) != null) { precedence = int.Parse(((GrammarASTWithOptions)node).GetOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME)); } RuleTransition call = new RuleTransition(start, r.index, precedence, right); left.AddTransition(call); node.atnState = left; return(new Handle(left, right)); }
/** identify the ATN states where we need to set the outer alt number. * For regular rules, that's the block at the target to rule start state. * For left-recursive rules, we track the primary block, which looks just * like a regular rule's outer block, and the star loop block (always * there even if 1 alt). */ public virtual BitSet FindOuterMostDecisionStates() { BitSet track = new BitSet(atn.states.Count); int numberOfDecisions = atn.NumberOfDecisions; for (int i = 0; i < numberOfDecisions; i++) { DecisionState decisionState = atn.GetDecisionState(i); RuleStartState startState = atn.ruleToStartState[decisionState.ruleIndex]; // Look for StarLoopEntryState that is in any left recursive rule if (decisionState is StarLoopEntryState) { StarLoopEntryState loopEntry = (StarLoopEntryState)decisionState; if (loopEntry.precedenceRuleDecision) { // Recursive alts always result in a (...)* in the transformed // left recursive rule and that always has a BasicBlockStartState // even if just 1 recursive alt exists. ATNState blockStart = loopEntry.Transition(0).target; // track the StarBlockStartState associated with the recursive alternatives track.Set(blockStart.stateNumber); } } else if (startState.Transition(0).target == decisionState) { // always track outermost block for any rule if it exists track.Set(decisionState.stateNumber); } } return(track); }
/// <summary>Begin parsing at startRuleIndex</summary> public virtual ParserRuleContext Parse(int startRuleIndex) { RuleStartState startRuleStartState = _atn.ruleToStartState[startRuleIndex]; InterpreterRuleContext rootContext = new InterpreterRuleContext(null, ATNState.InvalidStateNumber, startRuleIndex); if (startRuleStartState.isPrecedenceRule) { EnterRecursionRule(rootContext, startRuleStartState.stateNumber, startRuleIndex, 0); } else { EnterRule(rootContext, startRuleStartState.stateNumber, startRuleIndex); } while (true) { ATNState p = AtnState; switch (p.StateType) { case StateType.RuleStop: { // pop; return from rule if (RuleContext.IsEmpty) { if (startRuleStartState.isPrecedenceRule) { ParserRuleContext result = RuleContext; Sharpen.Tuple <ParserRuleContext, int> parentContext = _parentContextStack.Pop(); UnrollRecursionContexts(parentContext.Item1); return(result); } else { ExitRule(); return(rootContext); } } VisitRuleStopState(p); break; } default: { try { VisitState(p); } catch (RecognitionException e) { State = _atn.ruleToStopState[p.ruleIndex].stateNumber; Context.exception = e; ErrorHandler.ReportError(this, e); ErrorHandler.Recover(this, e); } break; } } } }
/** Define all the rule begin/end ATNStates to solve forward reference * issues. */ internal virtual void CreateRuleStartAndStopATNStates() { atn.ruleToStartState = new RuleStartState[g.rules.Count]; atn.ruleToStopState = new RuleStopState[g.rules.Count]; foreach (Rule r in g.rules.Values) { RuleStartState start = NewState <RuleStartState>(r.ast); RuleStopState stop = NewState <RuleStopState>(r.ast); start.stopState = stop; start.isPrecedenceRule = r is LeftRecursiveRule; start.SetRuleIndex(r.index); stop.SetRuleIndex(r.index); atn.ruleToStartState[r.index] = start; atn.ruleToStopState[r.index] = stop; } }
public override ATN CreateATN() { // BUILD ALL START STATES (ONE PER MODE) ICollection <string> modes = ((LexerGrammar)g).modes.Keys; foreach (string modeName in modes) { // create s0, start state; implied Tokens rule node TokensStartState startState = NewState <TokensStartState>(null); atn.DefineMode(modeName, startState); } // INIT ACTION, RULE->TOKEN_TYPE MAP atn.ruleToTokenType = new int[g.rules.Count]; foreach (Rule r in g.rules.Values) { atn.ruleToTokenType[r.index] = g.GetTokenType(r.name); } // CREATE ATN FOR EACH RULE _CreateATN(g.rules.Values); atn.lexerActions = new ILexerAction[indexToActionMap.Count]; foreach (KeyValuePair <int, ILexerAction> entry in indexToActionMap) { atn.lexerActions[entry.Key] = entry.Value; } // LINK MODE START STATE TO EACH TOKEN RULE foreach (string modeName in modes) { IList <Rule> rules = ((LexerGrammar)g).modes[modeName]; TokensStartState startState = atn.modeNameToStartState[modeName]; foreach (Rule r in rules) { if (!r.IsFragment()) { RuleStartState s = atn.ruleToStartState[r.index]; Epsilon(startState, s); } } } ATNOptimizer.Optimize(g, atn); return(atn); }
public virtual Handle Rule([NotNull] GrammarAST ruleAST, [NotNull] string name, [NotNull] Handle blk) { Rule r = g.GetRule(name); RuleStartState start = atn.ruleToStartState[r.index]; Epsilon(start, blk.left); RuleStopState stop = atn.ruleToStopState[r.index]; Epsilon(blk.right, stop); Handle h = new Handle(start, stop); //ATNPrinter ser = new ATNPrinter(g, h.left); //System.Console.WriteLine(ruleAST.ToStringTree() + ":\n" + ser.AsString()); ruleAST.atnState = start; return(h); }
protected internal virtual void VisitRuleStopState(ATNState p) { RuleStartState ruleStartState = _atn.ruleToStartState[p.ruleIndex]; if (ruleStartState.isPrecedenceRule) { Sharpen.Tuple <ParserRuleContext, int> parentContext = _parentContextStack.Pop(); UnrollRecursionContexts(parentContext.Item1); State = parentContext.Item2; } else { ExitRule(); } RuleTransition ruleTransition = (RuleTransition)_atn.states[State].Transition(0); State = ruleTransition.followState.stateNumber; }
protected internal virtual void VisitState(ATNState p) { int edge; if (p.NumberOfTransitions > 1) { ErrorHandler.Sync(this); edge = Interpreter.AdaptivePredict(TokenStream, ((DecisionState)p).decision, RuleContext); } else { edge = 1; } Transition transition = p.Transition(edge - 1); switch (transition.TransitionType) { case TransitionType.Epsilon: { if (pushRecursionContextStates.Get(p.stateNumber) && !(transition.target is LoopEndState)) { InterpreterRuleContext ctx = new InterpreterRuleContext(_parentContextStack.Peek().Item1, _parentContextStack.Peek().Item2, RuleContext.RuleIndex); PushNewRecursionContext(ctx, _atn.ruleToStartState[p.ruleIndex].stateNumber, RuleContext.RuleIndex); } break; } case TransitionType.Atom: { Match(((AtomTransition)transition).token); break; } case TransitionType.Range: case TransitionType.Set: case TransitionType.NotSet: { if (!transition.Matches(TokenStream.La(1), TokenConstants.MinUserTokenType, 65535)) { ErrorHandler.RecoverInline(this); } MatchWildcard(); break; } case TransitionType.Wildcard: { MatchWildcard(); break; } case TransitionType.Rule: { RuleStartState ruleStartState = (RuleStartState)transition.target; int ruleIndex = ruleStartState.ruleIndex; InterpreterRuleContext ctx_1 = new InterpreterRuleContext(RuleContext, p.stateNumber, ruleIndex); if (ruleStartState.isPrecedenceRule) { EnterRecursionRule(ctx_1, ruleStartState.stateNumber, ruleIndex, ((RuleTransition)transition).precedence); } else { EnterRule(ctx_1, transition.target.stateNumber, ruleIndex); } break; } case TransitionType.Predicate: { PredicateTransition predicateTransition = (PredicateTransition)transition; if (!Sempred(RuleContext, predicateTransition.ruleIndex, predicateTransition.predIndex)) { throw new FailedPredicateException(this); } break; } case TransitionType.Action: { ActionTransition actionTransition = (ActionTransition)transition; Action(RuleContext, actionTransition.ruleIndex, actionTransition.actionIndex); break; } case TransitionType.Precedence: { if (!Precpred(RuleContext, ((PrecedencePredicateTransition)transition).precedence)) { throw new FailedPredicateException(this, string.Format("precpred(_ctx, {0})", ((PrecedencePredicateTransition)transition).precedence)); } break; } default: { throw new NotSupportedException("Unrecognized ATN transition type."); } } State = transition.target.stateNumber; }
private HashSet <ATNState> ComputeSingle(List <Edge> parse) { List <Edge> copy = parse.ToList(); HashSet <ATNState> result = new HashSet <ATNState>(); if (_log_closure) { System.Console.Error.WriteLine("Computing closure for the following parse:"); System.Console.Error.Write(PrintSingle(parse)); System.Console.Error.WriteLine(); } if (!copy.Any()) { return(result); } Edge last_transaction = copy.First(); if (last_transaction == null) { return(result); } ATNState current_state = last_transaction._to; if (current_state == null) { throw new Exception(); } for (; ;) { if (_log_closure) { System.Console.Error.WriteLine("Getting closure of " + current_state.stateNumber); } HashSet <ATNState> c = closure(current_state); if (_log_closure) { System.Console.Error.WriteLine("closure " + string.Join(" ", c.Select(s => s.stateNumber))); } bool do_continue = false; ATN atn = current_state.atn; int rule = current_state.ruleIndex; RuleStartState start_state = atn.ruleToStartState[rule]; RuleStopState stop_state = atn.ruleToStopState[rule]; bool changed = false; foreach (ATNState s in c) { if (result.Contains(s)) { continue; } changed = true; result.Add(s); if (s == stop_state) { do_continue = true; } } if (!changed) { break; } if (!do_continue) { break; } for (; ;) { if (!copy.Any()) { break; } copy.RemoveAt(0); if (!copy.Any()) { break; } last_transaction = copy.First(); if (start_state == last_transaction._from) { copy.RemoveAt(0); if (!copy.Any()) { break; } last_transaction = copy.First(); // Get follow state of rule-type transition. ATNState from_state = last_transaction._from; if (from_state == null) { break; } ATNState follow_state = last_transaction._follow; current_state = follow_state; if (current_state == null) { throw new Exception(); } break; } } } return(result); }
protected internal virtual void VisitState(ATNState p) { int predictedAlt = 1; if (p.NumberOfTransitions > 1) { predictedAlt = VisitDecisionState((DecisionState)p); } Transition transition = p.Transition(predictedAlt - 1); switch (transition.TransitionType) { case TransitionType.Epsilon: { if (pushRecursionContextStates.Get(p.stateNumber) && !(transition.target is LoopEndState)) { // We are at the start of a left recursive rule's (...)* loop // and we're not taking the exit branch of loop. InterpreterRuleContext localctx = CreateInterpreterRuleContext(_parentContextStack.Peek().Item1, _parentContextStack.Peek().Item2, _ctx.RuleIndex); PushNewRecursionContext(localctx, atn.ruleToStartState[p.ruleIndex].stateNumber, _ctx.RuleIndex); } break; } case TransitionType.Atom: { Match(((AtomTransition)transition).label); break; } case TransitionType.Range: case TransitionType.Set: case TransitionType.NotSet: { if (!transition.Matches(_input.La(1), TokenConstants.MinUserTokenType, 65535)) { RecoverInline(); } MatchWildcard(); break; } case TransitionType.Wildcard: { MatchWildcard(); break; } case TransitionType.Rule: { RuleStartState ruleStartState = (RuleStartState)transition.target; int ruleIndex = ruleStartState.ruleIndex; InterpreterRuleContext newctx = CreateInterpreterRuleContext(_ctx, p.stateNumber, ruleIndex); if (ruleStartState.isPrecedenceRule) { EnterRecursionRule(newctx, ruleStartState.stateNumber, ruleIndex, ((RuleTransition)transition).precedence); } else { EnterRule(newctx, transition.target.stateNumber, ruleIndex); } break; } case TransitionType.Predicate: { PredicateTransition predicateTransition = (PredicateTransition)transition; if (!Sempred(_ctx, predicateTransition.ruleIndex, predicateTransition.predIndex)) { throw new FailedPredicateException(this); } break; } case TransitionType.Action: { ActionTransition actionTransition = (ActionTransition)transition; Action(_ctx, actionTransition.ruleIndex, actionTransition.actionIndex); break; } case TransitionType.Precedence: { if (!Precpred(_ctx, ((PrecedencePredicateTransition)transition).precedence)) { throw new FailedPredicateException(this, string.Format("precpred(_ctx, {0})", ((PrecedencePredicateTransition)transition).precedence)); } break; } default: { throw new NotSupportedException("Unrecognized ATN transition type."); } } State = transition.target.stateNumber; }