org.antlr.v4.runtime.atn.RuleStopState Java Examples
The following examples show how to use
org.antlr.v4.runtime.atn.RuleStopState.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AtnFormatter.java From antlr4-autosuggest with Apache License 2.0 | 5 votes |
private void appendTableOfRuleToStartState() { for (int i = 0; i < recognizer.getATN().ruleToStartState.length; ++i) { RuleStartState startState = recognizer.getATN().ruleToStartState[i]; RuleStopState endState = recognizer.getATN().ruleToStopState[i]; result.append(String.format("Rule %2d %-20s start: %d stop: %d", i, recognizer.getRuleNames()[i], startState.stateNumber, endState.stateNumber)); result.append("\n"); } }
Example #2
Source File: ATNPrinter.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
String getStateString(ATNState s) { int n = s.stateNumber; String stateStr = "s"+n; if ( s instanceof StarBlockStartState ) stateStr = "StarBlockStart_"+n; else if ( s instanceof PlusBlockStartState ) stateStr = "PlusBlockStart_"+n; else if ( s instanceof BlockStartState) stateStr = "BlockStart_"+n; else if ( s instanceof BlockEndState ) stateStr = "BlockEnd_"+n; else if ( s instanceof RuleStartState) stateStr = "RuleStart_"+g.getRule(s.ruleIndex).name+"_"+n; else if ( s instanceof RuleStopState ) stateStr = "RuleStop_"+g.getRule(s.ruleIndex).name+"_"+n; else if ( s instanceof PlusLoopbackState) stateStr = "PlusLoopBack_"+n; else if ( s instanceof StarLoopbackState) stateStr = "StarLoopBack_"+n; else if ( s instanceof StarLoopEntryState) stateStr = "StarLoopEntry_"+n; return stateStr; }
Example #3
Source File: ParserATNFactory.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override public Handle rule(GrammarAST ruleAST, String name, 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.out.println(ruleAST.toStringTree()+":\n"+ser.asString()); ruleAST.atnState = start; return h; }
Example #4
Source File: ParserATNFactory.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** Define all the rule begin/end ATNStates to solve forward reference * issues. */ void createRuleStartAndStopATNStates() { atn.ruleToStartState = new RuleStartState[g.rules.size()]; atn.ruleToStopState = new RuleStopState[g.rules.size()]; for (Rule r : g.rules.values()) { RuleStartState start = newState(RuleStartState.class, r.ast); RuleStopState stop = newState(RuleStopState.class, r.ast); start.stopState = stop; start.isLeftRecursiveRule = r instanceof LeftRecursiveRule; start.setRuleIndex(r.index); stop.setRuleIndex(r.index); atn.ruleToStartState[r.index] = start; atn.ruleToStopState[r.index] = stop; } }
Example #5
Source File: LeftRecursionDetector.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** From state s, look for any transition to a rule that is currently * being traced. When tracing r, visitedPerRuleCheck has r * initially. If you reach a rule stop state, return but notify the * invoking rule that the called rule is nullable. This implies that * invoking rule must look at follow transition for that invoking state. * * The visitedStates tracks visited states within a single rule so * we can avoid epsilon-loop-induced infinite recursion here. Keep * filling the cycles in listOfRecursiveCycles and also, as a * side-effect, set leftRecursiveRules. */ public boolean check(Rule enclosingRule, ATNState s, Set<ATNState> visitedStates) { if ( s instanceof RuleStopState) return true; if ( visitedStates.contains(s) ) return false; visitedStates.add(s); //System.out.println("visit "+s); int n = s.getNumberOfTransitions(); boolean stateReachesStopState = false; for (int i=0; i<n; i++) { Transition t = s.transition(i); if ( t instanceof RuleTransition ) { RuleTransition rt = (RuleTransition) t; Rule r = g.getRule(rt.ruleIndex); if ( rulesVisitedPerRuleCheck.contains((RuleStartState)t.target) ) { addRulesToCycle(enclosingRule, r); } else { // must visit if not already visited; mark target, pop when done rulesVisitedPerRuleCheck.add((RuleStartState)t.target); // send new visitedStates set per rule invocation boolean nullable = check(r, t.target, new HashSet<ATNState>()); // we're back from visiting that rule rulesVisitedPerRuleCheck.remove((RuleStartState)t.target); if ( nullable ) { stateReachesStopState |= check(enclosingRule, rt.followState, visitedStates); } } } else if ( t.isEpsilon() ) { stateReachesStopState |= check(enclosingRule, t.target, visitedStates); } // else ignore non-epsilon transitions } return stateReachesStopState; }
Example #6
Source File: ErrorHandler.java From presto with Apache License 2.0 | 4 votes |
private Set<Integer> process(ParsingState start, int precedence) { Set<Integer> result = memo.get(start); if (result != null) { return result; } ImmutableSet.Builder<Integer> endTokens = ImmutableSet.builder(); // Simulates the ATN by consuming input tokens and walking transitions. // The ATN can be in multiple states (similar to an NFA) Deque<ParsingState> activeStates = new ArrayDeque<>(); activeStates.add(start); while (!activeStates.isEmpty()) { ParsingState current = activeStates.pop(); ATNState state = current.state; int tokenIndex = current.tokenIndex; boolean suppressed = current.suppressed; while (stream.get(tokenIndex).getChannel() == Token.HIDDEN_CHANNEL) { // Ignore whitespace tokenIndex++; } int currentToken = stream.get(tokenIndex).getType(); if (state.getStateType() == RULE_START) { int rule = state.ruleIndex; if (specialRules.containsKey(rule)) { if (!suppressed) { record(tokenIndex, specialRules.get(rule)); } suppressed = true; } else if (ignoredRules.contains(rule)) { // TODO expand ignored rules like we expand special rules continue; } } if (state instanceof RuleStopState) { endTokens.add(tokenIndex); continue; } for (int i = 0; i < state.getNumberOfTransitions(); i++) { Transition transition = state.transition(i); if (transition instanceof RuleTransition) { RuleTransition ruleTransition = (RuleTransition) transition; for (int endToken : process(new ParsingState(ruleTransition.target, tokenIndex, suppressed, parser), ruleTransition.precedence)) { activeStates.push(new ParsingState(ruleTransition.followState, endToken, suppressed, parser)); } } else if (transition instanceof PrecedencePredicateTransition) { if (precedence < ((PrecedencePredicateTransition) transition).precedence) { activeStates.push(new ParsingState(transition.target, tokenIndex, suppressed, parser)); } } else if (transition.isEpsilon()) { activeStates.push(new ParsingState(transition.target, tokenIndex, suppressed, parser)); } else if (transition instanceof WildcardTransition) { throw new UnsupportedOperationException("not yet implemented: wildcard transition"); } else { IntervalSet labels = transition.label(); if (transition instanceof NotSetTransition) { labels = labels.complement(IntervalSet.of(Token.MIN_USER_TOKEN_TYPE, atn.maxTokenType)); } // Surprisingly, TokenStream (i.e. BufferedTokenStream) may not have loaded all the tokens from the // underlying stream. TokenStream.get() does not force tokens to be buffered -- it just returns what's // in the current buffer, or fail with an IndexOutOfBoundsError. Since Antlr decided the error occurred // within the current set of buffered tokens, stop when we reach the end of the buffer. if (labels.contains(currentToken) && tokenIndex < stream.size() - 1) { activeStates.push(new ParsingState(transition.target, tokenIndex + 1, false, parser)); } else { if (!suppressed) { record(tokenIndex, getTokenNames(labels)); } } } } } result = endTokens.build(); memo.put(start, result); return result; }
Example #7
Source File: ParserATNFactory.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
public void addFollowLink(int ruleIndex, ATNState right) { // add follow edge from end of invoked rule RuleStopState stop = atn.ruleToStopState[ruleIndex]; // System.out.println("add follow link from "+ruleIndex+" to "+right); epsilon(stop, right); }