Search in sources :

Example 81 with Grammar

use of org.eclipse.xtext.Grammar 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 82 with Grammar

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

the class NodeModelUtilsTest method testFindNodesForFeature_MultipleFeature.

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

Example 83 with Grammar

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

the class NodeModelUtilsTest method testCompactDump_2.

@Test
public void testCompactDump_2() throws Exception {
    String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo x 'bar' Model:name=ID;";
    Grammar grammar = (Grammar) getModelAndExpect(getAsStream(grammarString), 1);
    String actual = NodeModelUtils.compactDump(NodeModelUtils.getNode(grammar.getMetamodelDeclarations().get(0)), true);
    String expected = "GeneratedMetamodel {\n" + "  hidden WS returns EString: => ' '\n" + "  'generate' => 'generate'\n" + "  name=ValidID {\n" + "    hidden WS returns EString: => ' '\n" + "    ID => 'foo'\n" + "  }\n" + "  hidden WS returns EString: => ' '\n" + "  ID returns EString: => 'x' SyntaxError: [org.eclipse.xtext.diagnostics.Diagnostic.Syntax] extraneous input 'x' expecting RULE_STRING\n" + "  hidden WS returns EString: => ' '\n" + "  ePackage=[EPackage] => ''bar''\n" + "}";
    assertEquals(expected, actual);
}
Also used : Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Example 84 with Grammar

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

the class NodeModelUtilsTest method testFindLeafNodeAtOffset_1.

@Test
public void testFindLeafNodeAtOffset_1() throws Exception {
    String grammarText = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model : (name=ID value=ID);";
    Grammar grammar = (Grammar) getModel(grammarText);
    int equalsSign = grammarText.indexOf('=');
    ICompositeNode grammarNode = NodeModelUtils.getNode(grammar);
    ILeafNode leafNodeAtOffset = NodeModelUtils.findLeafNodeAtOffset(grammarNode, equalsSign);
    assertEquals("=", leafNodeAtOffset.getText());
    boolean syntheticNodeSeen = false;
    INode parent = leafNodeAtOffset.getParent();
    while (parent != null) {
        // walk up the tree to make sure we call #findLeafNodeAtOffset with synthetic nodes, too
        ILeafNode otherLeafNode = NodeModelUtils.findLeafNodeAtOffset(parent, equalsSign);
        assertSame(leafNodeAtOffset, otherLeafNode);
        if (parent instanceof SyntheticCompositeNode)
            syntheticNodeSeen = true;
        parent = parent.getParent();
    }
    assertTrue(syntheticNodeSeen);
}
Also used : SyntheticCompositeNode(org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode) INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Example 85 with Grammar

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

the class NodeModelUtilsTest method testFindActualSemanticObjectFor_08.

@Test
public void testFindActualSemanticObjectFor_08() throws Exception {
    String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model:/**/name=ID;";
    Grammar grammar = (Grammar) getModel(grammarString);
    ILeafNode leadingComment = NodeModelUtils.findLeafNodeAtOffset(NodeModelUtils.getNode(grammar), grammarString.indexOf('*'));
    EObject object = NodeModelUtils.findActualSemanticObjectFor(leadingComment);
    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) Test(org.junit.Test)

Aggregations

Grammar (org.eclipse.xtext.Grammar)356 Test (org.junit.Test)273 ParserRule (org.eclipse.xtext.ParserRule)151 AbstractRule (org.eclipse.xtext.AbstractRule)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)101 EObject (org.eclipse.emf.ecore.EObject)70 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 XtextResource (org.eclipse.xtext.resource.XtextResource)33 RuleCall (org.eclipse.xtext.RuleCall)28 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)23 TypeReference (org.eclipse.xtext.xtext.generator.model.TypeReference)23 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)21 Assignment (org.eclipse.xtext.Assignment)21 EPackage (org.eclipse.emf.ecore.EPackage)20 AbstractElement (org.eclipse.xtext.AbstractElement)17 TerminalRule (org.eclipse.xtext.TerminalRule)17 Resource (org.eclipse.emf.ecore.resource.Resource)16 EClassifier (org.eclipse.emf.ecore.EClassifier)13 InternalEObject (org.eclipse.emf.ecore.InternalEObject)13 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)12