Java 类org.antlr.v4.runtime.atn.RuleTransition 实例源码

项目:codebuff    文件:ParserATNFactory.java   
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);
}
项目:goworks    文件:ParserDebuggerReferenceAnchorsParserTask.java   
@Override
protected void visitState(ATNState p) {
    super.visitState(p);

    if (p.getNumberOfTransitions() > 1) {
        return;
    }

    Transition transition = p.transition(0);
    if (transition instanceof RuleTransition) {
        // rule transition created a new context
        associatedTransitions.put(_ctx, transition);
    }
    else if (!p.onlyHasEpsilonTransitions()) {
        // match transition created a new terminal or error node
        associatedTransitions.put(_ctx.getChild(_ctx.getChildCount() - 1), transition);
    }
}
项目:antlrworks2    文件:ParserDebuggerReferenceAnchorsParserTask.java   
@Override
protected void visitState(ATNState p) {
    super.visitState(p);

    if (p.getNumberOfTransitions() > 1) {
        return;
    }

    Transition transition = p.transition(0);
    if (transition instanceof RuleTransition) {
        // rule transition created a new context
        associatedTransitions.put(_ctx, transition);
    }
    else if (!p.onlyHasEpsilonTransitions()) {
        // match transition created a new terminal or error node
        associatedTransitions.put(_ctx.getChild(_ctx.getChildCount() - 1), transition);
    }
}
项目:codebuff    文件:TailEpsilonRemover.java   
@Override
public void visitState(ATNState p) {
    if (p.getStateType() == ATNState.BASIC && p.getNumberOfTransitions() == 1) {
        ATNState q = p.transition(0).target;
        if (p.transition(0) instanceof RuleTransition) {
            q = ((RuleTransition) p.transition(0)).followState;
        }
        if (q.getStateType() == ATNState.BASIC) {
            // we have p-x->q for x in {rule, action, pred, token, ...}
            // if edge out of q is single epsilon to block end
            // we can strip epsilon p-x->q-eps->r
            Transition trans = q.transition(0);
            if (q.getNumberOfTransitions() == 1 && trans instanceof EpsilonTransition) {
                ATNState r = trans.target;
                if (r instanceof BlockEndState || r instanceof PlusLoopbackState || r instanceof StarLoopbackState) {
                    // skip over q
                    if (p.transition(0) instanceof RuleTransition) {
                        ((RuleTransition) p.transition(0)).followState = r;
                    } else {
                        p.transition(0).target = r;
                    }
                    _atn.removeState(q);
                }
            }
        }
    }
}
项目:codebuff    文件:ParserATNFactory.java   
public Handle elemList(List<Handle> els) {
    int n = els.size();
    for (int i = 0; i < n - 1; i++) {   // hook up elements (visit all but last)
        Handle el = els.get(i);
        // if el is of form o-x->o for x in {rule, action, pred, token, ...}
        // and not last in alt
           Transition tr = null;
           if ( el.left.getNumberOfTransitions()==1 ) tr = el.left.transition(0);
           boolean isRuleTrans = tr instanceof RuleTransition;
           if ( el.left.getStateType() == ATNState.BASIC &&
            el.right.getStateType()== ATNState.BASIC &&
            tr!=null && (isRuleTrans && ((RuleTransition)tr).followState == el.right || tr.target == el.right) )
        {
            // we can avoid epsilon edge to next el
            if ( isRuleTrans ) ((RuleTransition)tr).followState = els.get(i+1).left;
               else tr.target = els.get(i+1).left;
            atn.removeState(el.right); // we skipped over this state
        }
        else { // need epsilon if previous block's right end node is complicated
            epsilon(el.right, els.get(i+1).left);
        }
    }
    Handle first = els.get(0);
    Handle last = els.get(n -1);
    if ( first==null || last==null ) {
        g.tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, "element list has first|last == null");
    }
    return new Handle(first.left, last.right);
}
项目:codebuff    文件:ParserATNFactory.java   
public void addRuleFollowLinks() {
    for (ATNState p : atn.states) {
        if ( p!=null &&
             p.getStateType() == ATNState.BASIC && p.getNumberOfTransitions()==1 &&
             p.transition(0) instanceof RuleTransition )
        {
            RuleTransition rt = (RuleTransition) p.transition(0);
            addFollowLink(rt.ruleIndex, rt.followState);
        }
    }
}
项目:codebuff    文件:LeftRecursionDetector.java   
/** 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;
}
项目:goworks    文件:TreeCorrectionParserATNSimulator.java   
@Override
public ATNState getReachableTarget(ATNConfig source, Transition trans, int ttype) {
    if (trans instanceof RuleTransition) {
        IntervalSet suppressed = getSuppressedSet(startIndex);
        if (suppressed.contains(((RuleTransition)trans).ruleIndex)) {
            return null;
        }
    }

    return super.getReachableTarget(source, trans, ttype);
}
项目:Scratch-ApuC    文件:ParserInterpreter.java   
protected void visitRuleStopState(ATNState p) {
    RuleStartState ruleStartState = atn.ruleToStartState[p.ruleIndex];
    if (ruleStartState.isPrecedenceRule) {
        Pair<ParserRuleContext, Integer> parentContext = _parentContextStack.pop();
        unrollRecursionContexts(parentContext.a);
        setState(parentContext.b);
    }
    else {
        exitRule();
    }

    RuleTransition ruleTransition = (RuleTransition)atn.states.get(getState()).transition(0);
    setState(ruleTransition.followState.stateNumber);
}