Search in sources :

Example 46 with Assignment

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

the class ConcreteSyntaxConstraintProvider method createSummarizedAssignments.

protected List<ISyntaxConstraint> createSummarizedAssignments(CompoundElement group, List<AbstractElement> candidates, EClass semanticType, boolean optional) {
    Multimap<String, Assignment> feature2ass = HashMultimap.create();
    Multimap<String, AbstractElement> feature2child = HashMultimap.create();
    for (AbstractElement c : candidates) {
        TreeIterator<EObject> i = EcoreUtil2.eAll(c);
        while (i.hasNext()) {
            EObject obj = i.next();
            if (obj instanceof RuleCall || obj instanceof Action || obj instanceof Alternatives)
                return Lists.newArrayList();
            else if (obj instanceof Group) {
                Set<String> names = Sets.newHashSet();
                for (Assignment ass : EcoreUtil2.getAllContentsOfType(obj, Assignment.class)) names.add(ass.getFeature());
                if (names.size() > 1)
                    i.prune();
            } else if (obj instanceof Assignment) {
                Assignment a = (Assignment) obj;
                feature2ass.put(a.getFeature(), a);
                feature2child.put(a.getFeature(), c);
                i.prune();
            }
        }
    }
    List<ISyntaxConstraint> result = Lists.newArrayList();
    for (Map.Entry<String, Collection<Assignment>> ent : feature2ass.asMap().entrySet()) {
        if (ent.getValue().size() < 2 || feature2child.get(ent.getKey()).size() < 2)
            continue;
        int required = 0, multiplies = 0;
        for (Assignment assignment : ent.getValue()) {
            AbstractElement e = assignment;
            while (e != group) if (isMultipleCardinality(e)) {
                multiplies++;
                break;
            } else
                e = (AbstractElement) e.eContainer();
            e = assignment;
            while (e != group) if (isOptionalCardinality(e))
                break;
            else
                e = (AbstractElement) e.eContainer();
            if (e == group)
                required++;
        }
        if (required > 1 || multiplies < 1)
            continue;
        candidates.removeAll(feature2child.get(ent.getKey()));
        optional = optional || required < 1;
        result.add(createElement(ConstraintType.ASSIGNMENT, ent.getValue().iterator().next(), semanticType, true, optional));
    }
    return result;
}
Also used : UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Group(org.eclipse.xtext.Group) Action(org.eclipse.xtext.Action) HashSet(java.util.HashSet) Set(java.util.Set) AbstractElement(org.eclipse.xtext.AbstractElement) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EObject(org.eclipse.emf.ecore.EObject) Collection(java.util.Collection) Map(java.util.Map)

Example 47 with Assignment

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

the class XtextValidator method checkCurrentMustBeUnassigned.

private void checkCurrentMustBeUnassigned(final AbstractElement element) {
    final ParserRule rule = GrammarUtil.containingParserRule(element);
    if (GrammarUtil.isDatatypeRule(rule))
        return;
    XtextSwitch<Boolean> visitor = new XtextSwitch<Boolean>() {

        private boolean isNull = !rule.isFragment();

        @Override
        public Boolean caseAbstractElement(AbstractElement object) {
            return isNull;
        }

        @Override
        public Boolean caseAlternatives(Alternatives object) {
            final boolean wasIsNull = isNull;
            boolean localIsNull = wasIsNull;
            for (AbstractElement element : object.getElements()) {
                isNull = wasIsNull;
                localIsNull &= doSwitch(element);
            }
            isNull = localIsNull;
            return isNull;
        }

        @Override
        public Boolean caseUnorderedGroup(UnorderedGroup object) {
            final boolean wasIsNull = isNull;
            boolean localIsNull = wasIsNull;
            for (AbstractElement element : object.getElements()) {
                isNull = wasIsNull;
                localIsNull |= doSwitch(element);
            }
            isNull = localIsNull;
            return isNull;
        }

        @Override
        public Boolean caseAssignment(Assignment object) {
            isNull = false;
            return isNull;
        }

        @Override
        public Boolean caseGroup(Group object) {
            for (AbstractElement element : object.getElements()) doSwitch(element);
            return isNull;
        }

        @Override
        public Boolean caseAction(Action object) {
            if (object == element) {
                if (!(isNull && !isMany(object))) {
                    error("An unassigned action is not allowed, when the 'current' was already created.", object, null);
                    checkDone();
                }
            }
            isNull = false;
            return isNull;
        }

        @Override
        public Boolean caseRuleCall(RuleCall object) {
            if (object == element) {
                AbstractRule calledRule = object.getRule();
                if (calledRule instanceof ParserRule && ((ParserRule) calledRule).isFragment()) {
                    isNull = false;
                    return isNull;
                }
                if (!(isNull && !isMany(object))) {
                    error("An unassigned rule call is not allowed, when the 'current' was already created.", object, null);
                    checkDone();
                }
            }
            return doSwitch(object.getRule());
        }

        @Override
        public Boolean caseParserRule(ParserRule object) {
            isNull = GrammarUtil.isDatatypeRule(object);
            return isNull;
        }

        @Override
        public Boolean caseTerminalRule(TerminalRule object) {
            isNull = true;
            return isNull;
        }

        public boolean isMany(AbstractElement element) {
            return GrammarUtil.isMultipleCardinality(element) || ((element.eContainer() instanceof AbstractElement) && isMany((AbstractElement) element.eContainer()));
        }
    };
    visitor.doSwitch(rule.getAlternatives());
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Group(org.eclipse.xtext.Group) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) XtextSwitch(org.eclipse.xtext.util.XtextSwitch) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 48 with Assignment

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

the class XtextValidator method checkAssignedActionAfterAssignment.

@Check
public void checkAssignedActionAfterAssignment(final Action action) {
    if (action.getFeature() != null) {
        ParserRule rule = GrammarUtil.containingParserRule(action);
        if (rule.isFragment() && !rule.isWildcard()) {
            error("An action is not allowed in fragments.", action, null);
            return;
        }
        XtextSwitch<Boolean> visitor = new XtextSwitch<Boolean>() {

            private boolean assignedActionAllowed = false;

            @Override
            public Boolean caseAbstractElement(AbstractElement object) {
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseAlternatives(Alternatives object) {
                boolean wasActionAllowed = assignedActionAllowed;
                boolean localActionAllowed = true;
                for (AbstractElement element : object.getElements()) {
                    assignedActionAllowed = wasActionAllowed;
                    localActionAllowed &= doSwitch(element);
                }
                assignedActionAllowed = wasActionAllowed || (localActionAllowed && !GrammarUtil.isOptionalCardinality(object));
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseUnorderedGroup(UnorderedGroup object) {
                boolean wasActionAllowed = assignedActionAllowed;
                boolean localActionAllowed = false;
                for (AbstractElement element : object.getElements()) {
                    assignedActionAllowed = wasActionAllowed;
                    localActionAllowed |= doSwitch(element);
                }
                assignedActionAllowed = wasActionAllowed || (localActionAllowed && !GrammarUtil.isOptionalCardinality(object));
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseAssignment(Assignment object) {
                assignedActionAllowed = assignedActionAllowed || !GrammarUtil.isOptionalCardinality(object);
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseGroup(Group object) {
                boolean wasAssignedActionAllowed = assignedActionAllowed;
                for (AbstractElement element : object.getElements()) doSwitch(element);
                assignedActionAllowed = wasAssignedActionAllowed || (assignedActionAllowed && !GrammarUtil.isOptionalCardinality(object));
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseAction(Action object) {
                if (object == action) {
                    if (!assignedActionAllowed) {
                        error("An action is not allowed in fragments and when the current may still be unassigned.", null);
                        checkDone();
                    }
                }
                assignedActionAllowed = true;
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseRuleCall(RuleCall object) {
                if (object.getRule() == null)
                    return assignedActionAllowed;
                assignedActionAllowed = assignedActionAllowed || doSwitch(object.getRule()) && !GrammarUtil.isOptionalCardinality(object);
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseParserRule(ParserRule object) {
                assignedActionAllowed = !GrammarUtil.isDatatypeRule(object) && !object.isFragment();
                return assignedActionAllowed;
            }

            @Override
            public Boolean caseTerminalRule(TerminalRule object) {
                return assignedActionAllowed;
            }
        };
        visitor.doSwitch(rule.getAlternatives());
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Group(org.eclipse.xtext.Group) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) UnorderedGroup(org.eclipse.xtext.UnorderedGroup) XtextSwitch(org.eclipse.xtext.util.XtextSwitch) TerminalRule(org.eclipse.xtext.TerminalRule) Check(org.eclipse.xtext.validation.Check)

Example 49 with Assignment

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

the class SerializerFragment2 method isUnassignedRuleCall.

private boolean isUnassignedRuleCall(final RuleCall c) {
    boolean _isEObjectRuleCall = GrammarUtil.isEObjectRuleCall(c);
    if (_isEObjectRuleCall) {
        return false;
    }
    final Assignment ass = GrammarUtil.containingAssignment(c);
    return ((ass == null) || GrammarUtil.isBooleanAssignment(ass));
}
Also used : Assignment(org.eclipse.xtext.Assignment)

Example 50 with Assignment

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

the class XtextLinkerTest method testImplicitNamedParameterLinking_02.

@Test
public void testImplicitNamedParameterLinking_02() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate test \'http://test\'");
    _builder.newLine();
    _builder.append("Root<MyParam>: rule=Rule<true>;");
    _builder.newLine();
    _builder.append("Rule<MyParam>: name=ID child=Root<false>?;");
    _builder.newLine();
    final String grammarAsString = _builder.toString();
    EObject _model = this.getModel(grammarAsString);
    final Grammar grammar = ((Grammar) _model);
    AbstractRule _head = IterableExtensions.<AbstractRule>head(grammar.getRules());
    final ParserRule rootRule = ((ParserRule) _head);
    AbstractRule _last = IterableExtensions.<AbstractRule>last(grammar.getRules());
    final ParserRule lastRule = ((ParserRule) _last);
    AbstractElement _alternatives = lastRule.getAlternatives();
    AbstractElement _last_1 = IterableExtensions.<AbstractElement>last(((Group) _alternatives).getElements());
    final Assignment lastAssignment = ((Assignment) _last_1);
    AbstractElement _terminal = lastAssignment.getTerminal();
    final RuleCall ruleCall = ((RuleCall) _terminal);
    final NamedArgument argument = IterableExtensions.<NamedArgument>head(ruleCall.getArguments());
    Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), argument.getParameter());
    Condition _value = argument.getValue();
    Assert.assertFalse(((LiteralCondition) _value).isTrue());
}
Also used : Assignment(org.eclipse.xtext.Assignment) LiteralCondition(org.eclipse.xtext.LiteralCondition) Condition(org.eclipse.xtext.Condition) ParserRule(org.eclipse.xtext.ParserRule) AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Grammar(org.eclipse.xtext.Grammar) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall) Test(org.junit.Test)

Aggregations

Assignment (org.eclipse.xtext.Assignment)60 EObject (org.eclipse.emf.ecore.EObject)27 Test (org.junit.Test)25 RuleCall (org.eclipse.xtext.RuleCall)22 Grammar (org.eclipse.xtext.Grammar)21 AbstractElement (org.eclipse.xtext.AbstractElement)15 ParserRule (org.eclipse.xtext.ParserRule)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)14 Action (org.eclipse.xtext.Action)13 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)12 AbstractRule (org.eclipse.xtext.AbstractRule)12 Group (org.eclipse.xtext.Group)11 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)9 Alternatives (org.eclipse.xtext.Alternatives)8 CrossReference (org.eclipse.xtext.CrossReference)7 ArrayList (java.util.ArrayList)6 EClass (org.eclipse.emf.ecore.EClass)6 InternalEObject (org.eclipse.emf.ecore.InternalEObject)6 NamedArgument (org.eclipse.xtext.NamedArgument)6 LiteralCondition (org.eclipse.xtext.LiteralCondition)5