Search in sources :

Example 6 with CompoundElement

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

the class GrammarElementTitleSwitch method addQualified.

protected String addQualified(String result, AbstractElement ele) {
    if (!showQualified && !showRule)
        return result;
    AbstractRule rule = GrammarUtil.containingRule(ele);
    if (rule == null)
        return "<AbstractElement not contained in AbstractRule!>:" + result;
    if (!showQualified)
        return result + ":" + rule.getName();
    GrammarElementTitleSwitch others = copy();
    others.showQualified = false;
    others.showRule = false;
    List<AbstractElement> elementsWithSameName = Lists.newArrayList();
    for (AbstractElement candidate : EcoreUtil2.getAllContentsOfType(rule, ele.getClass())) if (candidate == ele || result.equals(others.doSwitch(candidate)))
        elementsWithSameName.add(candidate);
    if (elementsWithSameName.size() < 2) {
        if (showRule)
            return rule.getName() + ":" + result;
        return result;
    }
    Map<CompoundElement, Node> nodes = Maps.newHashMap();
    for (AbstractElement collision : elementsWithSameName) {
        EObject current = EcoreUtil2.getContainerOfType(collision, CompoundElement.class);
        Node node = new Node(null, collision == ele ? result : "");
        while (current instanceof CompoundElement) {
            CompoundElement container = (CompoundElement) current;
            Node cntNode = nodes.get(container);
            if (cntNode == null)
                nodes.put(container, cntNode = new Node(container, null));
            if (!cntNode.children.contains(node))
                cntNode.children.add(node);
            node = cntNode;
            current = current.eContainer();
        }
    }
    if (showRule)
        return rule.getName() + ":" + nodes.get(rule.getAlternatives());
    return nodes.get(rule.getAlternatives()).toString();
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) AbstractRule(org.eclipse.xtext.AbstractRule) CompoundElement(org.eclipse.xtext.CompoundElement)

Example 7 with CompoundElement

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

the class CardinalityAwareSyntacticSequencer method emit_ParenthesizedElement_LeftParenthesisKeyword_0_a.

@Override
protected void emit_ParenthesizedElement_LeftParenthesisKeyword_0_a(EObject semanticObject, ISynNavigable transition, List<INode> nodes) {
    if (semanticObject instanceof CompoundElement) {
        CompoundElement ele = (CompoundElement) semanticObject;
        if (!Strings.isNullOrEmpty(ele.getCardinality()) && ele.getElements().size() > 1) {
            Keyword kw = grammarAccess.getParenthesizedElementAccess().getLeftParenthesisKeyword_0();
            acceptUnassignedKeyword(kw, kw.getValue(), nodes == null || nodes.isEmpty() ? null : (ILeafNode) nodes.get(0));
        }
    }
    super.emit_ParenthesizedCondition_LeftParenthesisKeyword_0_a(semanticObject, transition, nodes);
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Keyword(org.eclipse.xtext.Keyword) CompoundElement(org.eclipse.xtext.CompoundElement)

Example 8 with CompoundElement

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

the class XtextValidationTest method testDuplicateFeatures_02.

@Test
public void testDuplicateFeatures_02() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "A : (b+=B)*;\n" + "B : C | D;\n" + "C : 'c' name=ID ('e' e+=E)+;\n" + "E : name=ID;\n" + "F : C | E;\n" + "D : 'd' name=ID 'ref' ref=[F];";
    Grammar grammar = (Grammar) getModel(grammarAsText);
    AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
    XtextValidator validator = get(XtextValidator.class);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
    messageAcceptor.expectedContext(grammar.getRules().get(1).getType(), grammar.getRules().get(2).getType(), grammar.getRules().get(4).getType(), ((CompoundElement) grammar.getRules().get(2).getAlternatives()).getElements().get(1), grammar.getRules().get(3).getAlternatives(), ((CompoundElement) grammar.getRules().get(5).getAlternatives()).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 9 with CompoundElement

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

the class XtextValidationTest method testNameClash_02.

@Test
public void testNameClash_02() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Class returns Class: {Class_} name=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(), ((Action) element.getElements().get(0)).getType());
    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 10 with CompoundElement

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

the class XtextValidationTest method testPredicatedUnorderedGroup_01.

@Test
public void testPredicatedUnorderedGroup_01() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model: =>(name=ID & value=STRING);\n";
    Grammar grammar = (Grammar) getModel(grammarAsText);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
    messageAcceptor.expectedContext(grammar.getRules().get(0).getAlternatives(), ((CompoundElement) grammar.getRules().get(0).getAlternatives()).getElements().get(0));
    PredicateUsesUnorderedGroupInspector inspector = new PredicateUsesUnorderedGroupInspector(messageAcceptor);
    inspector.inspect(grammar);
    messageAcceptor.validate();
}
Also used : Grammar(org.eclipse.xtext.Grammar) CompoundElement(org.eclipse.xtext.CompoundElement) Test(org.junit.Test)

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