Search in sources :

Example 1 with Alternatives

use of org.eclipse.xtext.Alternatives 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 Alternatives

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

the class XtextLinkerTest method testGuardLinking.

@Test
public void testGuardLinking() 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<MyArg>: <MyArg> name=ID | <!MyArg> name=STRING;");
    _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);
    AbstractElement _alternatives = rootRule.getAlternatives();
    final Alternatives alternatives = ((Alternatives) _alternatives);
    AbstractElement _head_1 = IterableExtensions.<AbstractElement>head(alternatives.getElements());
    Condition _guardCondition = ((Group) _head_1).getGuardCondition();
    final ParameterReference firstGuard = ((ParameterReference) _guardCondition);
    Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), firstGuard.getParameter());
    AbstractElement _last = IterableExtensions.<AbstractElement>last(alternatives.getElements());
    Condition _guardCondition_1 = ((Group) _last).getGuardCondition();
    final Negation secondGuard = ((Negation) _guardCondition_1);
    Condition _value = secondGuard.getValue();
    Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), ((ParameterReference) _value).getParameter());
}
Also used : LiteralCondition(org.eclipse.xtext.LiteralCondition) Condition(org.eclipse.xtext.Condition) ParserRule(org.eclipse.xtext.ParserRule) Group(org.eclipse.xtext.Group) AbstractElement(org.eclipse.xtext.AbstractElement) Negation(org.eclipse.xtext.Negation) ParameterReference(org.eclipse.xtext.ParameterReference) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) Alternatives(org.eclipse.xtext.Alternatives) Test(org.junit.Test)

Example 3 with Alternatives

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

the class NodeModelUtilsTest method testFindActualSemanticObjectFor_07.

@Test
public void testFindActualSemanticObjectFor_07() throws Exception {
    String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model:(name=ID|name=STRING?)*;";
    Grammar grammar = (Grammar) getModel(grammarString);
    ILeafNode star = NodeModelUtils.findLeafNodeAtOffset(NodeModelUtils.getNode(grammar), grammarString.indexOf('*'));
    EObject object = NodeModelUtils.findActualSemanticObjectFor(star);
    assertTrue(object instanceof Alternatives);
    ILeafNode questionMark = NodeModelUtils.findLeafNodeAtOffset(NodeModelUtils.getNode(grammar), grammarString.indexOf('?'));
    object = NodeModelUtils.findActualSemanticObjectFor(questionMark);
    assertTrue(object instanceof Assignment);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) Grammar(org.eclipse.xtext.Grammar) Alternatives(org.eclipse.xtext.Alternatives) Test(org.junit.Test)

Example 4 with Alternatives

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

the class NodeModelUtilsTest method testFindNodesForFeature_Elements.

@Test
public void testFindNodesForFeature_Elements() throws Exception {
    Grammar grammar = (Grammar) getModel("grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model:name='foo'|name='bar';");
    Alternatives body = (Alternatives) grammar.getRules().get(0).getAlternatives();
    List<INode> nodes = NodeModelUtils.findNodesForFeature(body, XtextPackage.eINSTANCE.getCompoundElement_Elements());
    assertEquals(2, nodes.size());
    assertEquals("name='foo'", nodes.get(0).getText());
    assertEquals("name='bar'", nodes.get(1).getText());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Grammar(org.eclipse.xtext.Grammar) Alternatives(org.eclipse.xtext.Alternatives) Test(org.junit.Test)

Example 5 with Alternatives

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

the class GrammarParserTest method testPrecedencies_01.

@Test
public void testPrecedencies_01() throws Exception {
    Alternatives alternatives = (Alternatives) getModel("'keyword' | 'keyword' & 'keyword' | 'keyword'");
    assertNotNull(alternatives);
    assertEquals(3, alternatives.getElements().size());
}
Also used : Alternatives(org.eclipse.xtext.Alternatives) Test(org.junit.Test)

Aggregations

Alternatives (org.eclipse.xtext.Alternatives)18 AbstractElement (org.eclipse.xtext.AbstractElement)9 Test (org.junit.Test)9 Assignment (org.eclipse.xtext.Assignment)8 Group (org.eclipse.xtext.Group)8 RuleCall (org.eclipse.xtext.RuleCall)7 Action (org.eclipse.xtext.Action)6 Grammar (org.eclipse.xtext.Grammar)6 ParserRule (org.eclipse.xtext.ParserRule)6 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)6 EObject (org.eclipse.emf.ecore.EObject)5 AbstractRule (org.eclipse.xtext.AbstractRule)5 TerminalRule (org.eclipse.xtext.TerminalRule)4 XtextSwitch (org.eclipse.xtext.util.XtextSwitch)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 ArrayList (java.util.ArrayList)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2 CompoundElement (org.eclipse.xtext.CompoundElement)2 Keyword (org.eclipse.xtext.Keyword)2 LiteralCondition (org.eclipse.xtext.LiteralCondition)2