Search in sources :

Example 6 with ISynAbsorberState

use of org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState in project xtext-core by eclipse.

the class SyntacticSequencerPDAProviderNavigatorTest method testAmbiguousRecursion.

@Test
public void testAmbiguousRecursion() throws Exception {
    StringBuilder grammar = new StringBuilder();
    grammar.append("Addition returns Expr: Prim ({Add.left=current} '+' right=Prim)*;\n");
    grammar.append("Prim returns Expr: {Val} name=ID | '(' Addition ')';\n");
    ISynAbsorberState start = getParserRule(grammar.toString(), "Prim", "Val");
    ISynTransition trans1 = findTransition(start, "start", "name=ID");
    assertFalse(trans1.involvesUnassignedTokenRuleCalls());
    assertTrue(trans1.isSyntacticallyAmbiguous());
    // assertEquals(1, trans1.getDistanceWithStackToAbsorber(newStack()));
    assertEquals("[{Val}]", trans1.getShortestPathToAbsorber(newStack()).toString());
    RuleCallStack stack2 = newStack();
    ISynTransition trans2 = findTransition(start, "name=ID", "stop");
    assertFalse(trans2.involvesUnassignedTokenRuleCalls());
    assertFalse(trans2.isSyntacticallyAmbiguous());
    // assertEquals(0, trans2.getDistanceWithStackToAbsorber(stack2));
    assertEquals("[]", trans2.getShortestPathToAbsorber(stack2).toString());
// RuleCallStack stack3 = newStack(trans1, ">>Addition", ">>Prim");
// ISynTransition trans3 = findTransition(start, "name=ID", "stop");
// assertFalse(trans3.involvesUnassignedTokenRuleCalls());
// assertFalse(trans3.isSyntacticallyAmbiguous());
// //		assertEquals(3, trans3.getDistanceWithStackToAbsorber(stack3));
// assertEquals("[<<Prim, <<Addition, ')']", trans3.getShortestStackpruningPathToAbsorber(stack3).toString());
}
Also used : ISynAbsorberState(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState) RuleCallStack(org.eclipse.xtext.serializer.sequencer.RuleCallStack) ISynTransition(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynTransition) Test(org.junit.Test)

Example 7 with ISynAbsorberState

use of org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState in project xtext-core by eclipse.

the class SyntacticSequencerPDAProviderNavigatorTest method getParserRule.

protected ISynAbsorberState getParserRule(String body, String name, String typeName) throws Exception {
    Grammar grammar = (Grammar) getModel(HEADER + body);
    // SyntacticSequencerPDA2SimpleDot.drawGrammar("pdf/" + getName(),
    // grammar);
    // SyntacticSequencerPDA2ExtendedDot.drawGrammar(createSequenceParserPDAProvider(),
    // "pdf/" + getName(), grammar);
    ISyntacticSequencerPDAProvider pdaProvider = get(ISyntacticSequencerPDAProvider.class);
    SerializationContextMap<ISynAbsorberState> pdas = pdaProvider.getSyntacticSequencerPDAs(grammar);
    for (SerializationContextMap.Entry<ISynAbsorberState> e : pdas.sortedCopy().values()) {
        for (ISerializationContext context : e.getContexts()) {
            if (context.getAssignedAction() != null)
                continue;
            ISynAbsorberState pda = e.getValue();
            ParserRule rule = context.getParserRule();
            EClass type = context.getType();
            boolean nameMatches = rule == null || name == null || rule.getName().equals(name);
            boolean typeMatches = type == null || typeName == null || typeName.equals(type.getName());
            if (nameMatches && typeMatches)
                return pda;
        }
    }
    throw new IllegalStateException();
}
Also used : ISynAbsorberState(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState) ParserRule(org.eclipse.xtext.ParserRule) EClass(org.eclipse.emf.ecore.EClass) ISyntacticSequencerPDAProvider(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider) SerializationContextMap(org.eclipse.xtext.serializer.analysis.SerializationContextMap) Grammar(org.eclipse.xtext.Grammar)

Example 8 with ISynAbsorberState

use of org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState in project xtext-core by eclipse.

the class SyntacticSequencerPDAProviderTest method pda2lines2.

private List<String> pda2lines2(ISynAbsorberState start) {
    Set<ISynAbsorberState> states = Sets.newHashSet(start);
    collectAbsorberStates(start, Sets.<ISynState>newHashSet(), states);
    List<String> pdalines = Lists.newArrayList();
    for (ISynAbsorberState state : states) for (ISynTransition child : state.getOutTransitions()) pdalines.add("  " + pathToStr2(child));
    Collections.sort(pdalines);
    return pdalines;
}
Also used : ISynAbsorberState(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState) ISynTransition(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynTransition)

Example 9 with ISynAbsorberState

use of org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState in project xtext-core by eclipse.

the class SemanticSequencerNfaProvider method getSemanticSequencerNFAs.

@Override
public SerializationContextMap<Nfa<ISemState>> getSemanticSequencerNFAs(Grammar grammar) {
    SerializationContextMap<Nfa<ISemState>> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<Nfa<ISemState>> builder = SerializationContextMap.builder();
    SerializationContextMap<ISynAbsorberState> PDAs = pdaProvider.getSyntacticSequencerPDAs(grammar);
    for (SerializationContextMap.Entry<ISynAbsorberState> e : PDAs.values()) {
        ISynAbsorberState synState = e.getValue();
        for (EClass type : e.getTypes()) {
            List<ISerializationContext> contexts = e.getContexts(type);
            try {
                SemNfa nfa = createNfa(grammar, synState, type);
                builder.put(contexts, nfa);
            } catch (Exception x) {
                LOG.error("Error during static analysis of context '" + contexts + "': " + x.getMessage(), x);
            }
        }
    }
    SerializationContextMap<Nfa<ISemState>> result = builder.create();
    cache.put(grammar, result);
    return result;
}
Also used : ISynAbsorberState(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Nfa(org.eclipse.xtext.util.formallang.Nfa)

Example 10 with ISynAbsorberState

use of org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState in project xtext-core by eclipse.

the class AbstractSyntacticSequencer method findTransition.

protected ISynTransition findTransition(ISerializationContext context, EObject semanticObject, ISynFollowerOwner fromState, INode fromNode, AbstractElement toEle, INode toNode, RuleCallStack stack) {
    if (fromState == null)
        return null;
    if (fromState instanceof ISynAbsorberState) {
        ISynAbsorberState fromAbsorber = (ISynAbsorberState) fromState;
        ISynTransition transition = fromAbsorber.getOutTransitionsByElement().get(toEle);
        if (transition == null) {
            if (errorAcceptor != null)
                errorAcceptor.accept(diagnosticProvider.createInvalidFollowingAbsorberDiagnostic(context, semanticObject, fromAbsorber, toEle));
            return null;
        }
        return transition;
    }
    throw new IllegalStateException();
}
Also used : ISynAbsorberState(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState) ISynTransition(org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynTransition)

Aggregations

ISynAbsorberState (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynAbsorberState)19 ISynTransition (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynTransition)10 Test (org.junit.Test)8 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)3 EClass (org.eclipse.emf.ecore.EClass)2 Grammar (org.eclipse.xtext.Grammar)2 ISyntacticSequencerPDAProvider (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider)2 ISynNavigable (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynNavigable)2 ISynState (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynState)2 ParserRule (org.eclipse.xtext.ParserRule)1 INode (org.eclipse.xtext.nodemodel.INode)1 SynAbsorberNfaAdapter (org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.SynAbsorberNfaAdapter)1 SerializationContextMap (org.eclipse.xtext.serializer.analysis.SerializationContextMap)1 RuleCallStack (org.eclipse.xtext.serializer.sequencer.RuleCallStack)1 Nfa (org.eclipse.xtext.util.formallang.Nfa)1