Search in sources :

Example 1 with INFAState

use of org.eclipse.xtext.grammaranalysis.INFAState in project xtext-core by eclipse.

the class AbstractCachingNFABuilder method getState.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final S getState(AbstractElement ele) {
    if (ele == null)
        return null;
    for (Adapter a : ele.eAdapters()) if (a instanceof INFAState) {
        INFAState s = (INFAState) a;
        if (s.getBuilder() == this)
            return (S) s;
    }
    S t = createState(ele);
    ele.eAdapters().add((Adapter) t);
    return t;
}
Also used : INFAState(org.eclipse.xtext.grammaranalysis.INFAState) Adapter(org.eclipse.emf.common.notify.Adapter)

Example 2 with INFAState

use of org.eclipse.xtext.grammaranalysis.INFAState in project xtext-core by eclipse.

the class AbstractPDAProvider method createState.

/**
 * @since 2.0
 */
protected PDAState createState(PDAContext<CTX> ctx, RuleCallStackElement stack, INFAState<?, ?> fromNfa, EClass constructedType, boolean returning, boolean canReturn, Set<Pair<Boolean, INFAState<?, ?>>> visited) {
    Set<Pair<Boolean, INFAState<?, ?>>> visited2 = Sets.newHashSet();
    if (stack == null || !canReachContextEnd(ctx, stack.cloneWithoutVisited(), fromNfa, constructedType, returning, canReturn, visited2))
        return null;
    if (constructedType == null)
        constructedType = getConstructedType(fromNfa.getGrammarElement());
    AbstractElement ge = fromNfa.getGrammarElement();
    PDAState result = null;
    if (canEnterRuleCall(fromNfa)) {
        if (returning) {
            if ((result = ctx.ruleCallExit.get(fromNfa)) == null)
                ctx.ruleCallExit.put(fromNfa, result = createState(PDAStateType.RULECALL_EXIT, ge));
        } else {
            if ((result = ctx.ruleCallEnter.get(fromNfa)) == null)
                ctx.ruleCallEnter.put(fromNfa, result = createState(PDAStateType.RULECALL_ENTER, ge));
        }
    } else {
        if ((result = ctx.elements.get(fromNfa)) == null)
            ctx.elements.put(fromNfa, result = createState(PDAStateType.ELEMENT, ge));
    }
    if (!visited.add(Tuples.<Boolean, INFAState<?, ?>>create(returning, fromNfa)))
        return result;
    if (!returning && canEnterRuleCall(fromNfa)) {
        if (stack.hasEnteredTwice(fromNfa))
            return result;
        stack = stackPush(stack, fromNfa);
        visited = Sets.newHashSet();
    }
    if (result.followers == null || result.followers == Collections.EMPTY_SET) {
        result.followers = Sets.newHashSet();
    }
    if (isFinalState(ctx.context, fromNfa, returning, canReturn))
        result.followers.add(ctx.stop);
    for (INFAState<?, ?> follower : getFollowers(ctx.context, fromNfa, returning, canReturn)) {
        boolean folCanReturn = !canEnterRuleCall(follower);
        boolean folReturning = follower.hasOutgoingRuleCall() && folCanReturn;
        PDAState r = createState(ctx, stack, follower, constructedType, folReturning, folCanReturn, visited);
        if (r != null)
            result.followers.add(r);
    }
    if (canReturn && fromNfa.isEndState() && stack != null && stack.getRuleCall() != null) {
        visited = Sets.newHashSet();
        PDAState r = createState(ctx, stack.getParent(), stack.getRuleCall(), constructedType, true, true, visited);
        if (r != null)
            result.followers.add(r);
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) INFAState(org.eclipse.xtext.grammaranalysis.INFAState) IPDAState(org.eclipse.xtext.grammaranalysis.IPDAState) Pair(org.eclipse.xtext.util.Pair)

Aggregations

INFAState (org.eclipse.xtext.grammaranalysis.INFAState)2 Adapter (org.eclipse.emf.common.notify.Adapter)1 AbstractElement (org.eclipse.xtext.AbstractElement)1 IPDAState (org.eclipse.xtext.grammaranalysis.IPDAState)1 Pair (org.eclipse.xtext.util.Pair)1