Search in sources :

Example 36 with RuleCall

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

the class GrammarAccessExtensions method getSingleElementDescription.

private List<String> getSingleElementDescription(final AbstractElement ele) {
    final ArrayList<String> r = new ArrayList<String>(2);
    boolean _matched = false;
    if (ele instanceof Keyword) {
        _matched = true;
        String _value = ((Keyword) ele).getValue();
        r.add(_value);
    }
    if (!_matched) {
        if (ele instanceof Assignment) {
            _matched = true;
            String _feature = ((Assignment) ele).getFeature();
            r.add(_feature);
        }
    }
    if (!_matched) {
        if (ele instanceof RuleCall) {
            _matched = true;
            String _name = ((RuleCall) ele).getRule().getName();
            r.add(_name);
        }
    }
    if (!_matched) {
        if (ele instanceof Action) {
            _matched = true;
            TypeRef _type = ((Action) ele).getType();
            EClassifier _classifier = null;
            if (_type != null) {
                _classifier = _type.getClassifier();
            }
            boolean _tripleNotEquals = (_classifier != null);
            if (_tripleNotEquals) {
                String _name = ((Action) ele).getType().getClassifier().getName();
                r.add(_name);
            }
            boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(((Action) ele).getFeature());
            boolean _not = (!_isNullOrEmpty);
            if (_not) {
                String _feature = ((Action) ele).getFeature();
                r.add(_feature);
            }
        }
    }
    if (!_matched) {
        if (ele instanceof CrossReference) {
            _matched = true;
            TypeRef _type = ((CrossReference) ele).getType();
            EClassifier _classifier = null;
            if (_type != null) {
                _classifier = _type.getClassifier();
            }
            boolean _tripleNotEquals = (_classifier != null);
            if (_tripleNotEquals) {
                String _name = ((CrossReference) ele).getType().getClassifier().getName();
                r.add(_name);
            }
        }
    }
    if (!_matched) {
        if (ele instanceof EnumLiteralDeclaration) {
            _matched = true;
            String _name = ((EnumLiteralDeclaration) ele).getEnumLiteral().getName();
            r.add(_name);
        }
    }
    return r;
}
Also used : Assignment(org.eclipse.xtext.Assignment) Action(org.eclipse.xtext.Action) Keyword(org.eclipse.xtext.Keyword) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) TypeRef(org.eclipse.xtext.TypeRef) ArrayList(java.util.ArrayList) EClassifier(org.eclipse.emf.ecore.EClassifier) CrossReference(org.eclipse.xtext.CrossReference) RuleCall(org.eclipse.xtext.RuleCall)

Example 37 with RuleCall

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

the class GrammarAccessExtensions method toStringLiteral.

public CharSequence toStringLiteral(final AbstractElement it) {
    CharSequence _switchResult = null;
    boolean _matched = false;
    if (it instanceof RuleCall) {
        AbstractRule _rule = ((RuleCall) it).getRule();
        boolean _tripleNotEquals = (_rule != null);
        if (_tripleNotEquals) {
            _matched = true;
            _switchResult = AntlrGrammarGenUtil.getQualifiedNameAsString(((RuleCall) it));
        }
    }
    if (!_matched) {
        if (it instanceof Keyword) {
            _matched = true;
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("\"");
            String _stringInAntlrAction = AntlrGrammarGenUtil.toStringInAntlrAction(((Keyword) it).getValue());
            _builder.append(_stringInAntlrAction);
            _builder.append("\"");
            _switchResult = _builder;
        }
    }
    if (!_matched) {
        _switchResult = "null";
    }
    return _switchResult;
}
Also used : Keyword(org.eclipse.xtext.Keyword) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 38 with RuleCall

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

the class Xtext2EcoreTransformer method deriveFeatures.

private Xtext2EcoreInterpretationContext deriveFeatures(final Xtext2EcoreInterpretationContext context, AbstractElement element) {
    XtextSwitch<Xtext2EcoreInterpretationContext> visitor = new XtextSwitch<Xtext2EcoreInterpretationContext>() {

        /*
			 * Used for Alternatives and UnorderedGroups
			 */
        @Override
        public Xtext2EcoreInterpretationContext caseCompoundElement(CompoundElement object) {
            List<Xtext2EcoreInterpretationContext> contexts = new ArrayList<Xtext2EcoreInterpretationContext>();
            for (AbstractElement group : object.getElements()) {
                contexts.add(deriveFeatures(context, group));
            }
            Xtext2EcoreInterpretationContext result = context;
            if (!contexts.isEmpty()) {
                if (GrammarUtil.isOptionalCardinality(object)) {
                    contexts.add(0, result);
                } else {
                    result = contexts.get(0);
                }
                result = result.mergeSpawnedContexts(contexts);
            }
            return result;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseAssignment(Assignment object) {
            try {
                context.addFeature(object);
            } catch (TransformationException ex) {
                reportError(ex);
            }
            return context;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseGroup(Group object) {
            return visitElements(object, object.getElements());
        }

        private Xtext2EcoreInterpretationContext visitElements(AbstractElement caller, List<AbstractElement> elementsToProcess) {
            Xtext2EcoreInterpretationContext result = deriveFeatures(context.spawnContextForGroup(), elementsToProcess);
            if (GrammarUtil.isMultipleCardinality(caller)) {
                result = deriveFeatures(result.spawnContextForGroup(), elementsToProcess);
            }
            if (GrammarUtil.isOptionalCardinality(caller)) {
                result = result.mergeSpawnedContexts(Arrays.asList(context, result));
            }
            return result;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseAlternatives(Alternatives object) {
            List<Xtext2EcoreInterpretationContext> contexts = newArrayList();
            if (GrammarUtil.isOptionalCardinality(object)) {
                contexts.add(context);
            }
            for (AbstractElement alternative : object.getElements()) {
                contexts.add(deriveFeatures(context.spawnContextForGroup(), alternative));
            }
            Xtext2EcoreInterpretationContext result = context.mergeSpawnedContexts(contexts);
            if (GrammarUtil.isMultipleCardinality(object)) {
                for (AbstractElement alternative : object.getElements()) {
                    deriveFeatures(result.spawnContextForGroup(), alternative);
                }
            }
            return result;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseRuleCall(RuleCall object) {
            AbstractRule calledRule = object.getRule();
            if (isWildcardFragment(calledRule)) {
                AbstractElement ruleBody = calledRule.getAlternatives();
                if (ruleBody != null) {
                    return visitElements(object, Collections.singletonList(ruleBody));
                }
                return context;
            }
            if (isParserRuleFragment(calledRule)) {
                return context;
            }
            if (!GrammarUtil.isOptionalCardinality(object)) {
                // announced during the first iteration
                if (calledRule != null && calledRule instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) calledRule)) {
                    try {
                        EClassifierInfo eClassifierInfo = findOrCreateEClassifierInfo(calledRule);
                        return context.spawnContextWithCalledRule(eClassifierInfo, object);
                    } catch (TransformationException e) {
                        reportError(e);
                    }
                }
            }
            return context;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseAction(Action object) {
            try {
                TypeRef actionTypeRef = object.getType();
                EClassifierInfo actionType = findOrCreateEClassifierInfo(actionTypeRef, null, true);
                EClassifierInfo currentCompatibleType = context.getCurrentCompatibleType();
                Xtext2EcoreInterpretationContext ctx = context.spawnContextWithReferencedType(actionType, object);
                if (object.getFeature() != null) {
                    ctx.addFeature(object.getFeature(), currentCompatibleType, GrammarUtil.isMultipleAssignment(object), true, object);
                }
                return ctx;
            } catch (TransformationException e) {
                reportError(e);
            }
            return context;
        }

        @Override
        public Xtext2EcoreInterpretationContext defaultCase(EObject object) {
            return context;
        }
    };
    return visitor.doSwitch(element);
}
Also used : Group(org.eclipse.xtext.Group) ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) TypeRef(org.eclipse.xtext.TypeRef) ArrayList(java.util.ArrayList) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) List(java.util.List) ArrayList(java.util.ArrayList) XtextSwitch(org.eclipse.xtext.util.XtextSwitch) CompoundElement(org.eclipse.xtext.CompoundElement) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 39 with RuleCall

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

the class ContextPDAProvider method getContextPDAs.

@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getContextPDAs(Grammar grammar) {
    Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
    SerializationContextMap<Pda<ISerState, RuleCall>> grammarPDAs = grammarPdaProvider.getGrammarPDAs(grammar);
    Multimap<Action, SerializerPDA> actionPdas = ArrayListMultimap.create();
    Multimap<Action, ISerializationContext> actionContexts = LinkedHashMultimap.create();
    Map<ParserRule, Integer> indexedRules = indexRules(grammar);
    for (SerializationContextMap.Entry<Pda<ISerState, RuleCall>> e : grammarPDAs.values()) {
        List<ISerializationContext> contexts = e.getContexts();
        Pda<ISerState, RuleCall> pda = e.getValue();
        List<ISerState> actions = Lists.newArrayList();
        for (ISerState state : nfaUtil.collect(pda)) {
            if (GrammarUtil.isAssignedAction(state.getGrammarElement())) {
                actions.add(state);
            }
        }
        if (actions.isEmpty()) {
            Pda<ISerState, RuleCall> filtered = filterUnneededUnassignedRuleCalls(pda, indexedRules);
            result.put(contexts, filtered);
        } else {
            try {
                SerializerPDA rulePda = extract(pda.getStop());
                Pda<ISerState, RuleCall> filtered = filterUnneededUnassignedRuleCalls(rulePda, indexedRules);
                result.put(contexts, filtered);
                for (ISerState state : actions) {
                    Action action = (Action) state.getGrammarElement();
                    SerializerPDA actionPda = extract(state);
                    actionPdas.put(action, actionPda);
                    actionContexts.putAll(action, contexts);
                }
            } catch (Exception x) {
                LOG.error("Error extracting PDA for action in context '" + contexts + "': " + x.getMessage(), x);
            }
        }
    }
    for (Map.Entry<Action, Collection<SerializerPDA>> action : actionPdas.asMap().entrySet()) {
        SerializerPDA merged = merge(new ActionContext(null, action.getKey()), action.getValue());
        Set<Set<Parameter>> parameterPermutations = Sets.newLinkedHashSet();
        for (ISerializationContext container : actionContexts.get(action.getKey())) {
            parameterPermutations.add(container.getEnabledBooleanParameters());
        }
        // for (IContext container : actionContexts.get(action.getKey())) {
        for (Set<Parameter> parameters : parameterPermutations) {
            ISerializationContext context = new ActionContext(/* container */
            null, action.getKey());
            if (!parameters.isEmpty())
                context = new SerializationContext.ParameterValueContext(context, parameters);
            Pda<ISerState, RuleCall> filtered = filterUnneededUnassignedRuleCalls(merged, indexedRules);
            result.put(context, filtered);
        }
    // }
    }
    return result.create();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Pda(org.eclipse.xtext.util.formallang.Pda) RuleCall(org.eclipse.xtext.RuleCall) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) ActionContext(org.eclipse.xtext.serializer.analysis.SerializationContext.ActionContext) Collection(java.util.Collection) Parameter(org.eclipse.xtext.Parameter) HashMap(java.util.HashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 40 with RuleCall

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

the class ContextPDAProvider method getFilterableRule.

protected ParserRule getFilterableRule(ISerState state) {
    if (state.getType() == SerStateType.PUSH) {
        RuleCall rc = (RuleCall) state.getGrammarElement();
        AbstractRule rule = rc.getRule();
        if (rule instanceof ParserRule) {
            ParserRule pr = (ParserRule) rule;
            if (pr.isFragment()) {
                return null;
            }
            if (pr.isDefinesHiddenTokens()) {
                return null;
            }
            return pr;
        }
    }
    return null;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule) 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