Search in sources :

Example 1 with CompoundElement

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

the class ConcreteSyntaxConstraintProvider method createElement.

protected ISyntaxConstraint createElement(EObject obj) {
    if (!(obj instanceof AbstractElement))
        return null;
    AbstractElement ele = (AbstractElement) obj;
    boolean multiple = false;
    boolean optional = false;
    EClass semanticType = null;
    while (true) {
        multiple = multiple || isMultipleCardinality(ele);
        optional = optional || isOptionalCardinality(ele);
        if (ele.eContainer() instanceof ParserRule && ((ParserRule) ele.eContainer()).getType().getClassifier() instanceof EClass)
            semanticType = (EClass) ((ParserRule) ele.eContainer()).getType().getClassifier();
        if (ele instanceof Assignment) {
            return createElement(ConstraintType.ASSIGNMENT, ele, semanticType, multiple, optional);
        } else if (ele instanceof Group || ele instanceof UnorderedGroup) {
            CompoundElement comp = (CompoundElement) ele;
            AbstractElement lastChild = null;
            for (AbstractElement o : comp.getElements()) if (containsRelevantElement(o)) {
                if (lastChild == null)
                    lastChild = o;
                else {
                    List<AbstractElement> c = new ArrayList<AbstractElement>(comp.getElements());
                    List<ISyntaxConstraint> e = createSummarizedAssignments(comp, c, semanticType, optional);
                    if (e.size() == 1 && c.size() == 0)
                        return e.get(0);
                    return createElement(ConstraintType.GROUP, ele, c, e, semanticType, multiple, optional);
                }
            }
            if (lastChild == null)
                return null;
            ele = lastChild;
            continue;
        } else if (ele instanceof Alternatives) {
            int relevantChildren = 0;
            AbstractElement lastChild = null;
            for (AbstractElement o : ((CompoundElement) ele).getElements()) if (containsRelevantElement(o)) {
                relevantChildren++;
                lastChild = o;
            }
            if (relevantChildren < ((CompoundElement) ele).getElements().size())
                optional = true;
            if (relevantChildren > 1)
                return createElement(ConstraintType.ALTERNATIVE, ele, semanticType, multiple, optional);
            if (lastChild == null)
                return null;
            ele = lastChild;
            continue;
        } else if (ele instanceof Action) {
            semanticType = (EClass) ((Action) ele).getType().getClassifier();
            return createElement(ConstraintType.ACTION, ele, semanticType, multiple, optional);
        } else if (ele instanceof RuleCall) {
            AbstractRule rule = ((RuleCall) ele).getRule();
            if (rule.getType().getClassifier() instanceof EClass) {
                ele = rule.getAlternatives();
                continue;
            }
        }
        return null;
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Group(org.eclipse.xtext.Group) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) ArrayList(java.util.ArrayList) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EClass(org.eclipse.emf.ecore.EClass) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) CompoundElement(org.eclipse.xtext.CompoundElement) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 2 with CompoundElement

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

the class XtextValidationTest method testNameClash_03.

@Test
public void testNameClash_03() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Foo: myVars=ID my_vars=ID;\n";
    Grammar grammar = (Grammar) getModel(grammarAsText);
    AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
    XtextValidator validator = get(XtextValidator.class);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
    CompoundElement element = (CompoundElement) grammar.getRules().get(0).getAlternatives();
    messageAcceptor.expectedContext(grammar.getRules().get(0).getType(), element.getElements().get(0), element.getElements().get(1));
    validator.setMessageAcceptor(messageAcceptor);
    validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP);
    messageAcceptor.validate();
}
Also used : Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) CompoundElement(org.eclipse.xtext.CompoundElement) Test(org.junit.Test)

Example 3 with CompoundElement

use of org.eclipse.xtext.CompoundElement 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 4 with CompoundElement

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

the class FlattenedGrammarAccess method copyRuleBodies.

private Multimap<TerminalRule, AbstractRule> copyRuleBodies(final List<AbstractRule> copies, final Map<RuleWithParameterValues, AbstractRule> origToCopy) {
    abstract class __FlattenedGrammarAccess_1 extends EcoreUtil.Copier {

        final __FlattenedGrammarAccess_1 _this__FlattenedGrammarAccess_1 = this;

        abstract Set<Parameter> getParameterConfig(final RuleCall origRuleCall, final RuleCall copyRuleCall);

        abstract void mergePredicates(final AbstractElement into, final AbstractElement from);

        abstract void mergeCardinalities(final AbstractElement into, final AbstractElement from);

        abstract boolean evaluate(final Condition condition);
    }
    final HashMultimap<TerminalRule, AbstractRule> calledFrom = HashMultimap.<TerminalRule, AbstractRule>create();
    for (final AbstractRule copy : copies) {
        {
            AbstractRule orig = RuleWithParameterValues.getOriginalRule(copy);
            final Set<Parameter> paramValues = RuleWithParameterValues.getParamValues(copy);
            EcoreUtil.Copier copier = new __FlattenedGrammarAccess_1() {

                @Override
                protected void copyReference(final EReference eReference, final EObject eObject, final EObject copyEObject) {
                    if ((eReference == XtextPackage.Literals.RULE_CALL__RULE)) {
                        RuleCall origRuleCall = ((RuleCall) eObject);
                        RuleCall copyRuleCall = ((RuleCall) copyEObject);
                        AbstractRule _rule = origRuleCall.getRule();
                        Set<Parameter> _parameterConfig = this.getParameterConfig(origRuleCall, copyRuleCall);
                        RuleWithParameterValues _ruleWithParameterValues = new RuleWithParameterValues(_rule, _parameterConfig);
                        AbstractRule calledCopy = origToCopy.get(_ruleWithParameterValues);
                        copyRuleCall.setRule(calledCopy);
                        if ((calledCopy instanceof TerminalRule)) {
                            calledFrom.put(((TerminalRule) calledCopy), copy);
                        }
                    } else {
                        super.copyReference(eReference, eObject, copyEObject);
                    }
                }

                Set<Parameter> getParameterConfig(final RuleCall origRuleCall, final RuleCall copyRuleCall) {
                    boolean _isEmpty = origRuleCall.getArguments().isEmpty();
                    if (_isEmpty) {
                        return Collections.<Parameter>emptySet();
                    }
                    final Function1<NamedArgument, Boolean> _function = (NamedArgument it) -> {
                        return Boolean.valueOf(this.evaluate(it.getValue()));
                    };
                    final Function1<NamedArgument, Parameter> _function_1 = (NamedArgument it) -> {
                        return it.getParameter();
                    };
                    Set<Parameter> result = IterableExtensions.<Parameter>toSet(IterableExtensions.<NamedArgument, Parameter>map(IterableExtensions.<NamedArgument>filter(origRuleCall.getArguments(), _function), _function_1));
                    return result;
                }

                @Override
                protected void copyContainment(final EReference eReference, final EObject eObject, final EObject copyEObject) {
                    boolean _matched = false;
                    if (Objects.equal(eReference, XtextPackage.Literals.RULE_CALL__ARGUMENTS)) {
                        _matched = true;
                    }
                    if (!_matched) {
                        if (Objects.equal(eReference, XtextPackage.Literals.GROUP__GUARD_CONDITION)) {
                            _matched = true;
                        }
                    }
                    if (_matched) {
                        return;
                    }
                    super.copyContainment(eReference, eObject, copyEObject);
                }

                @Override
                public EObject copy(final EObject eObject) {
                    if ((eObject instanceof Group)) {
                        Group group = ((Group) eObject);
                        Condition _guardCondition = group.getGuardCondition();
                        boolean _tripleNotEquals = (_guardCondition != null);
                        if (_tripleNotEquals) {
                            boolean _evaluate = this.evaluate(group.getGuardCondition());
                            boolean _not = (!_evaluate);
                            if (_not) {
                                return null;
                            }
                        }
                    }
                    EObject result = super.copy(eObject);
                    if ((result instanceof CompoundElement)) {
                        List<AbstractElement> elements = ((CompoundElement) result).getElements();
                        int _size = elements.size();
                        boolean _tripleEquals = (_size == 1);
                        if (_tripleEquals) {
                            if (((!((CompoundElement) result).isFirstSetPredicated()) && (!((CompoundElement) result).isPredicated()))) {
                                AbstractElement element = elements.get(0);
                                this.mergeCardinalities(element, ((AbstractElement) result));
                                this.mergePredicates(element, ((AbstractElement) result));
                                return element;
                            } else {
                                AbstractElement element_1 = elements.get(0);
                                this.mergePredicates(((AbstractElement) result), element_1);
                                element_1.setFirstSetPredicated(false);
                                element_1.setPredicated(false);
                            }
                        }
                    }
                    if ((eObject instanceof AbstractElement)) {
                        OriginalElement original = new OriginalElement(((AbstractElement) eObject));
                        EClass _eClass = ((AbstractElement) eObject).eClass();
                        EClass _eClass_1 = result.eClass();
                        boolean _notEquals = (!Objects.equal(_eClass, _eClass_1));
                        if (_notEquals) {
                            String _name = result.eClass().getName();
                            String _plus = ("copy is: \'" + _name);
                            String _plus_1 = (_plus + "\' but original was: \'");
                            String _name_1 = ((AbstractElement) eObject).eClass().getName();
                            String _plus_2 = (_plus_1 + _name_1);
                            String _plus_3 = (_plus_2 + "\'");
                            throw new IllegalStateException(_plus_3);
                        }
                        original.attachToEmfObject(result);
                    }
                    return result;
                }

                void mergePredicates(final AbstractElement into, final AbstractElement from) {
                    boolean _isPredicated = from.isPredicated();
                    if (_isPredicated) {
                        into.setPredicated(true);
                        into.setFirstSetPredicated(false);
                    } else {
                        if (((!into.isPredicated()) && from.isFirstSetPredicated())) {
                            into.setFirstSetPredicated(true);
                        }
                    }
                }

                void mergeCardinalities(final AbstractElement into, final AbstractElement from) {
                    String c1 = into.getCardinality();
                    String c2 = from.getCardinality();
                    String _switchResult = null;
                    boolean _matched = false;
                    if ((Objects.equal(c1, "*") || Objects.equal(c2, "*"))) {
                        _matched = true;
                    }
                    if (!_matched) {
                        if ((Objects.equal(c1, "+") && Objects.equal(c2, "?"))) {
                            _matched = true;
                        }
                    }
                    if (!_matched) {
                        if ((Objects.equal(c1, "?") && Objects.equal(c2, "+"))) {
                            _matched = true;
                        }
                    }
                    if (_matched) {
                        _switchResult = "*";
                    }
                    if (!_matched) {
                        if (Objects.equal(c1, null)) {
                            _matched = true;
                            _switchResult = c2;
                        }
                    }
                    if (!_matched) {
                        _switchResult = c1;
                    }
                    into.setCardinality(_switchResult);
                }

                boolean evaluate(final Condition condition) {
                    boolean result = new ConditionEvaluator(paramValues).evaluate(condition);
                    return result;
                }
            };
            EObject _copy = copier.copy(orig.getAlternatives());
            AbstractElement copiedBody = ((AbstractElement) _copy);
            copier.copyReferences();
            copy.setAlternatives(copiedBody);
            if ((orig instanceof ParserRule)) {
                ParserRule castedCopy = ((ParserRule) copy);
                boolean _isDefinesHiddenTokens = ((ParserRule) orig).isDefinesHiddenTokens();
                if (_isDefinesHiddenTokens) {
                    castedCopy.setDefinesHiddenTokens(true);
                    EList<AbstractRule> _hiddenTokens = ((ParserRule) orig).getHiddenTokens();
                    for (final AbstractRule rule : _hiddenTokens) {
                        {
                            RuleWithParameterValues _ruleWithParameterValues = new RuleWithParameterValues(rule);
                            final AbstractRule copiedTerminalRule = origToCopy.get(_ruleWithParameterValues);
                            EList<AbstractRule> _hiddenTokens_1 = castedCopy.getHiddenTokens();
                            _hiddenTokens_1.add(copiedTerminalRule);
                            calledFrom.put(((TerminalRule) copiedTerminalRule), castedCopy);
                        }
                    }
                }
            }
        }
    }
    return calledFrom;
}
Also used : Group(org.eclipse.xtext.Group) ParserRule(org.eclipse.xtext.ParserRule) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) RuleCall(org.eclipse.xtext.RuleCall) EClass(org.eclipse.emf.ecore.EClass) EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) NamedArgument(org.eclipse.xtext.NamedArgument) EReference(org.eclipse.emf.ecore.EReference) OriginalElement(org.eclipse.xtext.xtext.OriginalElement) Condition(org.eclipse.xtext.Condition) AbstractElement(org.eclipse.xtext.AbstractElement) RuleWithParameterValues(org.eclipse.xtext.xtext.RuleWithParameterValues) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) ConditionEvaluator(org.eclipse.xtext.xtext.ConditionEvaluator) EList(org.eclipse.emf.common.util.EList) Parameter(org.eclipse.xtext.Parameter) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) CompoundElement(org.eclipse.xtext.CompoundElement)

Example 5 with CompoundElement

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

the class AbstractNFAState method collectOutgoingByContainer.

protected void collectOutgoingByContainer(AbstractElement element, Set<AbstractElement> visited, boolean isRuleCall, AbstractElement loopCenter) {
    EObject container = element.eContainer();
    if (container instanceof Group || container instanceof UnorderedGroup) {
        CompoundElement compoundContainer = (CompoundElement) container;
        List<AbstractElement> siblings = compoundContainer.getElements();
        int i = siblings.indexOf(element);
        switch(builder.getDirection()) {
            case FORWARD:
                if ((i + 1) >= siblings.size()) {
                    if (GrammarUtil.isMultipleCardinality(compoundContainer))
                        addOutgoing(compoundContainer, visited, isRuleCall, compoundContainer);
                    collectOutgoingByContainer(compoundContainer, visited, isRuleCall, loopCenter);
                } else {
                    AbstractElement next = siblings.get(i + 1);
                    addOutgoing(next, visited, isRuleCall, loopCenter);
                    if (GrammarUtil.isOptionalCardinality(next))
                        collectOutgoingByContainer(next, visited, isRuleCall, loopCenter);
                }
                break;
            case BACKWARD:
                if (i <= 0) {
                    if (GrammarUtil.isMultipleCardinality(compoundContainer))
                        addOutgoing(compoundContainer, visited, isRuleCall, compoundContainer);
                    collectOutgoingByContainer(compoundContainer, visited, isRuleCall, loopCenter);
                } else {
                    AbstractElement next = siblings.get(i - 1);
                    addOutgoing(next, visited, isRuleCall, loopCenter);
                    if (GrammarUtil.isOptionalCardinality(next))
                        collectOutgoingByContainer(next, visited, isRuleCall, loopCenter);
                }
                break;
        }
    } else if (container instanceof AbstractRule)
        endState = true;
    else if (container instanceof AbstractElement) {
        AbstractElement elementContainer = (AbstractElement) container;
        if (GrammarUtil.isMultipleCardinality(elementContainer))
            addOutgoing(elementContainer, visited, isRuleCall, elementContainer);
        collectOutgoingByContainer(elementContainer, visited, isRuleCall, loopCenter);
    } else
        throw new IllegalStateException("Unknown container: " + container);
}
Also used : UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Group(org.eclipse.xtext.Group) AbstractElement(org.eclipse.xtext.AbstractElement) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) EObject(org.eclipse.emf.ecore.EObject) CompoundElement(org.eclipse.xtext.CompoundElement) AbstractRule(org.eclipse.xtext.AbstractRule)

Aggregations

CompoundElement (org.eclipse.xtext.CompoundElement)10 AbstractElement (org.eclipse.xtext.AbstractElement)5 AbstractRule (org.eclipse.xtext.AbstractRule)5 EObject (org.eclipse.emf.ecore.EObject)4 Grammar (org.eclipse.xtext.Grammar)4 Group (org.eclipse.xtext.Group)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)3 ParserRule (org.eclipse.xtext.ParserRule)3 RuleCall (org.eclipse.xtext.RuleCall)3 List (java.util.List)2 EClass (org.eclipse.emf.ecore.EClass)2 Action (org.eclipse.xtext.Action)2 Alternatives (org.eclipse.xtext.Alternatives)2 Assignment (org.eclipse.xtext.Assignment)2 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Set (java.util.Set)1 EList (org.eclipse.emf.common.util.EList)1