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());
}
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());
}
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);
}
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);
}
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);
}
Aggregations