org.antlr.v4.runtime.atn.RuleStartState Java Examples

The following examples show how to use org.antlr.v4.runtime.atn.RuleStartState. 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: ErrorHandler.java    From presto with Apache License 2.0 6 votes vote down vote up
public Result process(ATNState currentState, int tokenIndex, RuleContext context)
{
    RuleStartState startState = atn.ruleToStartState[currentState.ruleIndex];

    if (isReachable(currentState, startState)) {
        // We've been dropped inside a rule in a state that's reachable via epsilon transitions. This is,
        // effectively, equivalent to starting at the beginning (or immediately outside) the rule.
        // In that case, backtrack to the beginning to be able to take advantage of logic that remaps
        // some rules to well-known names for reporting purposes
        currentState = startState;
    }

    Set<Integer> endTokens = process(new ParsingState(currentState, tokenIndex, false, parser), 0);
    Set<Integer> nextTokens = new HashSet<>();
    while (!endTokens.isEmpty() && context.invokingState != -1) {
        for (int endToken : endTokens) {
            ATNState nextState = ((RuleTransition) atn.states.get(context.invokingState).transition(0)).followState;
            nextTokens.addAll(process(new ParsingState(nextState, endToken, false, parser), 0));
        }
        context = context.parent;
        endTokens = nextTokens;
    }

    return new Result(furthestTokenIndex, candidates);
}
 
Example #2
Source File: ErrorHandler.java    From presto with Apache License 2.0 6 votes vote down vote up
private boolean isReachable(ATNState target, RuleStartState from)
{
    Deque<ATNState> activeStates = new ArrayDeque<>();
    activeStates.add(from);

    while (!activeStates.isEmpty()) {
        ATNState current = activeStates.pop();

        if (current.stateNumber == target.stateNumber) {
            return true;
        }

        for (int i = 0; i < current.getNumberOfTransitions(); i++) {
            Transition transition = current.transition(i);

            if (transition.isEpsilon()) {
                activeStates.push(transition.target);
            }
        }
    }

    return false;
}
 
Example #3
Source File: GrammarParserInterpreter.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** 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 BitSet findOuterMostDecisionStates() {
	BitSet track = new BitSet(atn.states.size());
	int numberOfDecisions = atn.getNumberOfDecisions();
	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 instanceof StarLoopEntryState) {
			StarLoopEntryState loopEntry = (StarLoopEntryState)decisionState;
			if ( loopEntry.isPrecedenceDecision ) {
				// 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;
}
 
Example #4
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Handle _ruleRef(GrammarAST node) {
	Rule r = g.getRule(node.getText());
	if ( r==null ) {
		g.tool.errMgr.grammarError(ErrorType.INTERNAL_ERROR, g.fileName, node.getToken(), "Rule "+node.getText()+" 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 = Integer.parseInt(((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);
}
 
Example #5
Source File: AtnFormatter.java    From antlr4-autosuggest with Apache License 2.0 5 votes vote down vote up
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 #6
Source File: ATNPrinter.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 #7
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@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 #8
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** 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 #9
Source File: LeftRecursionDetector.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void check() {
	for (RuleStartState start : atn.ruleToStartState) {
		//System.out.print("check "+start.rule.name);
		rulesVisitedPerRuleCheck.clear();
		rulesVisitedPerRuleCheck.add(start);
		//FASerializer ser = new FASerializer(atn.g, start);
		//System.out.print(":\n"+ser+"\n");

		check(g.getRule(start.ruleIndex), start, new HashSet<ATNState>());
	}
	//System.out.println("cycles="+listOfRecursiveCycles);
	if ( !listOfRecursiveCycles.isEmpty() ) {
		g.tool.errMgr.leftRecursionCycles(g.fileName, listOfRecursiveCycles);
	}
}
 
Example #10
Source File: LeftRecursionDetector.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/** 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;
}