Search in sources :

Example 41 with RuleCall

use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.

the class ContextTypePDAProvider method getContextTypePDAs.

@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getContextTypePDAs(Grammar grammar) {
    SerializationContextMap<Pda<ISerState, RuleCall>> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<Pda<ISerState, RuleCall>> builder = SerializationContextMap.builder();
    SerializationContextMap<Pda<ISerState, RuleCall>> contextPDAs = pdaProvider.getContextPDAs(grammar);
    for (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> e : contextPDAs.values()) {
        List<ISerializationContext> parents = e.getContexts();
        Pda<ISerState, RuleCall> contextPDA = e.getValue();
        try {
            Map<ISerState, Integer> distances = nfaUtil.distanceToFinalStateMap(contextPDA);
            Set<EClass> types = collectTypes(contextPDA, distances);
            if (types.size() == 1) {
                for (ISerializationContext parent : parents) {
                    TypeContext ctx = new TypeContext(parent, types.iterator().next());
                    builder.put(ctx, contextPDA);
                }
            } else {
                for (EClass type : types) {
                    Pda<ISerState, RuleCall> filtered = filterByType(contextPDA, type, distances);
                    for (ISerializationContext parent : parents) {
                        TypeContext typeContext = new TypeContext(parent, type);
                        builder.put(typeContext, filtered);
                    }
                }
            }
        } catch (Exception x) {
            LOG.error("Error extracting PDAs for types for context '" + parents + "': " + x.getMessage(), x);
        }
    }
    SerializationContextMap<Pda<ISerState, RuleCall>> result = builder.create();
    cache.put(grammar, result);
    return result;
}
Also used : Pda(org.eclipse.xtext.util.formallang.Pda) TypeContext(org.eclipse.xtext.serializer.analysis.SerializationContext.TypeContext) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext)

Example 42 with RuleCall

use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.

the class GrammarPDAProvider method getGrammarPDAs.

@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getGrammarPDAs(Grammar grammar) {
    RuleNames names = RuleNames.getRuleNames(grammar, true);
    RuleFilter filter = new RuleFilter();
    filter.setDiscardTerminalRules(true);
    filter.setDiscardUnreachableRules(false);
    filter.setDiscardRuleTypeRef(false);
    Grammar flattened = new FlattenedGrammarAccess(names, filter).getFlattenedGrammar();
    Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
    for (ParserRule rule : GrammarUtil.allParserRules(flattened)) {
        RuleWithParameterValues withParams = RuleWithParameterValues.findInEmfObject(rule);
        AbstractRule original = withParams.getOriginal();
        if (original instanceof ParserRule && isValidRule((ParserRule) original)) {
            ISerializationContext context = createContext((ParserRule) original, withParams.getParamValues());
            try {
                Pda<ISerState, RuleCall> pda = createPDA(grammar, rule);
                result.put(context, pda);
            } catch (Exception e) {
                LOG.error("Error creating PDA for context '" + context + "': " + e.getMessage(), e);
            }
        }
    }
    return result.create();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleWithParameterValues(org.eclipse.xtext.xtext.RuleWithParameterValues) Grammar(org.eclipse.xtext.Grammar) Pda(org.eclipse.xtext.util.formallang.Pda) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) RuleCall(org.eclipse.xtext.RuleCall) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 43 with RuleCall

use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.

the class SyntacticSequencerPDAProvider method getType.

protected SynStateType getType(ISerState state) {
    switch(state.getType()) {
        case ELEMENT:
            AbstractElement ele = state.getGrammarElement();
            Assignment ass;
            if (ele instanceof Action) {
                if (((Action) ele).getFeature() == null)
                    return SynStateType.UNASSIGEND_ACTION_CALL;
                else
                    return SynStateType.ASSIGNED_ACTION_CALL;
            } else if (GrammarUtil.containingCrossReference(ele) != null) {
                if (ele instanceof RuleCall) {
                    RuleCall rc = (RuleCall) ele;
                    if (rc.getRule() instanceof ParserRule)
                        return SynStateType.ASSIGNED_CROSSREF_DATATYPE_RULE_CALL;
                    if (rc.getRule() instanceof TerminalRule)
                        return SynStateType.ASSIGNED_CROSSREF_TERMINAL_RULE_CALL;
                    if (rc.getRule() instanceof EnumRule)
                        return SynStateType.ASSIGNED_CROSSREF_ENUM_RULE_CALL;
                } else if (ele instanceof Keyword)
                    return SynStateType.ASSIGNED_CROSSREF_KEYWORD;
            } else if ((ass = GrammarUtil.containingAssignment(ele)) != null) {
                if (ele instanceof RuleCall) {
                    RuleCall rc = (RuleCall) ele;
                    if (rc.getRule() instanceof ParserRule) {
                        if (rc.getRule().getType().getClassifier() instanceof EClass)
                            return SynStateType.ASSIGNED_PARSER_RULE_CALL;
                        return SynStateType.ASSIGNED_DATATYPE_RULE_CALL;
                    }
                    if (rc.getRule() instanceof TerminalRule)
                        return SynStateType.ASSIGNED_TERMINAL_RULE_CALL;
                    if (rc.getRule() instanceof EnumRule)
                        return SynStateType.ASSIGNED_ENUM_RULE_CALL;
                } else if (ele instanceof Keyword) {
                    if (GrammarUtil.isBooleanAssignment(ass))
                        return SynStateType.ASSIGNED_BOOLEAN_KEYWORD;
                    else
                        return SynStateType.ASSIGNED_KEYWORD;
                }
            } else {
                if (ele instanceof RuleCall) {
                    RuleCall rc = (RuleCall) ele;
                    if (rc.getRule() instanceof ParserRule)
                        return SynStateType.UNASSIGNED_DATATYPE_RULE_CALL;
                    if (rc.getRule() instanceof TerminalRule)
                        return SynStateType.UNASSIGNED_TERMINAL_RULE_CALL;
                } else if (ele instanceof Keyword)
                    return SynStateType.UNASSIGEND_KEYWORD;
            }
            break;
        case PUSH:
            return SynStateType.UNASSIGNED_PARSER_RULE_ENTER;
        case POP:
            return SynStateType.UNASSIGNED_PARSER_RULE_EXIT;
        case START:
            return SynStateType.START;
        case STOP:
            return SynStateType.STOP;
    }
    throw new RuntimeException("no type found for " + state);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) Action(org.eclipse.xtext.Action) EClass(org.eclipse.emf.ecore.EClass) AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) TerminalRule(org.eclipse.xtext.TerminalRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 44 with RuleCall

use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.

the class SyntacticSequencerPDAProvider method getSyntacticSequencerPDAs.

@Override
public SerializationContextMap<ISynAbsorberState> getSyntacticSequencerPDAs(Grammar grammar) {
    SerializationContextMap<ISynAbsorberState> cached = cache.get(grammar);
    if (cached != null)
        return cached;
    SerializationContextMap.Builder<ISynAbsorberState> builder = SerializationContextMap.builder();
    SerializationContextMap<Pda<ISerState, RuleCall>> typePDAs = pdaProvider.getContextTypePDAs(grammar);
    for (Entry<Pda<ISerState, RuleCall>> e : typePDAs.values()) {
        Pda<ISerState, RuleCall> pda = e.getValue();
        List<ISerializationContext> contexts = e.getContexts();
        try {
            EClass type = contexts.get(0).getType();
            Map<ISerState, SynAbsorberState> absorbers = Maps.newLinkedHashMap();
            Map<SynAbsorberState, Map<ISerState, SynState>> emitters = Maps.newLinkedHashMap();
            SynAbsorberState state = createAbsorberState(pda.getStart(), absorbers, emitters, type);
            builder.put(contexts, state);
        } catch (Exception x) {
            String ctxs = Joiner.on(", ").join(contexts);
            LOG.error("Error creating PDA for syntactic sequencer for contexts: " + ctxs + ": " + x.getMessage(), x);
        }
    }
    SerializationContextMap<ISynAbsorberState> result = builder.create();
    cache.put(grammar, result);
    return result;
}
Also used : Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Map(java.util.Map)

Example 45 with RuleCall

use of org.eclipse.xtext.RuleCall in project xtext-core by eclipse.

the class AbstractSyntacticSequencer method acceptNode.

protected void acceptNode(INode node) {
    Object ge = node.getGrammarElement();
    if (ge instanceof Keyword)
        acceptUnassignedKeyword((Keyword) ge, node.getText(), (ILeafNode) node);
    else if (ge instanceof RuleCall) {
        RuleCall rc = (RuleCall) ge;
        if (rc.getRule() instanceof TerminalRule)
            acceptUnassignedTerminal(rc, node.getText(), (ILeafNode) node);
        else if (rc.getRule() instanceof ParserRule) {
            StringBuilder text = new StringBuilder();
            for (ILeafNode leaf : node.getLeafNodes()) if (text.length() > 0 || !leaf.isHidden())
                text.append(leaf.getText());
            acceptUnassignedDatatype(rc, text.toString(), (ICompositeNode) node);
        } else if (rc.getRule() instanceof EnumRule)
            acceptUnassignedEnum(rc, node.getText(), (ICompositeNode) node);
    } else if (ge instanceof Action)
        acceptUnassignedAction((Action) ge);
    else
        throw new RuntimeException("Unexpected grammar element: " + node.getGrammarElement());
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) Action(org.eclipse.xtext.Action) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Keyword(org.eclipse.xtext.Keyword) EObject(org.eclipse.emf.ecore.EObject) TerminalRule(org.eclipse.xtext.TerminalRule) RuleCall(org.eclipse.xtext.RuleCall)

Aggregations

RuleCall (org.eclipse.xtext.RuleCall)95 ParserRule (org.eclipse.xtext.ParserRule)41 AbstractRule (org.eclipse.xtext.AbstractRule)36 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)33 Grammar (org.eclipse.xtext.Grammar)28 AbstractElement (org.eclipse.xtext.AbstractElement)26 Assignment (org.eclipse.xtext.Assignment)22 TerminalRule (org.eclipse.xtext.TerminalRule)19 Action (org.eclipse.xtext.Action)17 CrossReference (org.eclipse.xtext.CrossReference)13 Group (org.eclipse.xtext.Group)13 TypeRef (org.eclipse.xtext.TypeRef)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 ArrayList (java.util.ArrayList)11 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)11 InternalEObject (org.eclipse.emf.ecore.InternalEObject)10 Keyword (org.eclipse.xtext.Keyword)10 EClass (org.eclipse.emf.ecore.EClass)9 EnumRule (org.eclipse.xtext.EnumRule)9