Search in sources :

Example 71 with Grammar

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

the class NodeModelUtilsTest method testCompactDump_1.

@Test
public void testCompactDump_1() throws Exception {
    String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model:name=ID;";
    Grammar grammar = (Grammar) getModel(grammarString);
    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" + "  ePackage=[EPackage] => ''bar''\n" + "}";
    assertEquals(expected.toString(), actual);
}
Also used : Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Example 72 with Grammar

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

the class NodeModelUtilsTest method testFindActualSemanticObjectFor_11.

@Test
public void testFindActualSemanticObjectFor_11() throws Exception {
    String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model<Param>:name=ID;";
    Grammar grammar = (Grammar) getModel(grammarString);
    ILeafNode lessThan = NodeModelUtils.findLeafNodeAtOffset(NodeModelUtils.getNode(grammar), grammarString.indexOf("<"));
    EObject object = NodeModelUtils.findActualSemanticObjectFor(lessThan);
    assertTrue(object instanceof ParserRule);
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Example 73 with Grammar

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

the class NodeModelUtilsTest method testFindNodesForFeature.

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

Example 74 with Grammar

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

the class NodeModelUtilsTest method testFindActualNode_02.

@Test
public void testFindActualNode_02() throws Exception {
    String grammarString = "grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model<Param>:name=ID;";
    Grammar grammar = (Grammar) getModel(grammarString);
    ParserRule rule = (ParserRule) grammar.getRules().get(0);
    ICompositeNode node = NodeModelUtils.findActualNodeFor(rule);
    assertEquals(" Model<Param>:name=ID;", node.getText());
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Example 75 with Grammar

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

the class NodeModelUtilsTest method testFindNodesForFeature_Cardinality_2.

@Test
public void testFindNodesForFeature_Cardinality_2() throws Exception {
    Grammar grammar = (Grammar) getModel("grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model: (name+='foo'*)?;");
    Assignment assignment = (Assignment) grammar.getRules().get(0).getAlternatives();
    List<INode> nodes = NodeModelUtils.findNodesForFeature(assignment, XtextPackage.eINSTANCE.getAbstractElement_Cardinality());
    assertEquals(2, nodes.size());
    assertEquals("*", nodes.get(0).getText());
    assertEquals("?", nodes.get(1).getText());
}
Also used : Assignment(org.eclipse.xtext.Assignment) INode(org.eclipse.xtext.nodemodel.INode) 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