Search in sources :

Example 46 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.

the class AntlrParserTest method testParseWithoutActionCall.

/**
 * Model: '<code>myID</code>'
 * The following parse tree is expected:
 * <pre>
 * CompositeNode (GrammarElement: Rule[Model], Element: null)
 *  |
 * CompositeNode (GrammarElement: RuleCall[Child], Element: Child[myID])
 *  |
 * LeafNode (GrammarElement: RuleCall[ID], Element: null)
 * </pre>
 */
@Test
public void testParseWithoutActionCall() throws Exception {
    XtextResource resource = helper.getResourceFromString("myID");
    assertTrue(resource.getErrors().toString(), resource.getErrors().isEmpty());
    Model model = (Model) resource.getContents().get(0);
    assertNotNull("model", model);
    assertEquals(model.eClass().getName(), ActionLangPackage.Literals.CHILD, model.eClass());
    ICompositeNode node = NodeModelUtils.getNode(model);
    assertNotNull("node", node);
    assertEquals("node.grammarElement", grammarAccess.getModelAccess().getChildParserRuleCall_0(), node.getGrammarElement());
    assertEquals(node.getChildren().toString(), 1, Iterables.size(node.getChildren()));
    INode childNode = node.getFirstChild();
    assertTrue(childNode.toString(), childNode instanceof ILeafNode);
    assertEquals("childNode.grammarElement", grammarAccess.getChildAccess().getNameIDTerminalRuleCall_0(), childNode.getGrammarElement());
    assertFalse("childNode.element", childNode.hasDirectSemanticElement());
    ICompositeNode rootNode = resource.getParseResult().getRootNode();
    assertNotNull("rootNode", rootNode);
    assertEquals("rootNode.grammarElement", grammarAccess.getModelRule(), rootNode.getGrammarElement());
    assertEquals(rootNode.getChildren().toString(), 1, Iterables.size(rootNode.getChildren()));
    assertEquals("node is child of rootNode", rootNode, node.getParent());
    assertFalse("rootNode.element", rootNode.hasDirectSemanticElement());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Model(org.eclipse.xtext.testlanguages.actionLang.Model) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 47 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.

the class HiddenTokenSequencerTest method getNodeSequence.

@SuppressWarnings("deprecation")
private List<String> getNodeSequence(EObject model) {
    List<String> result = Lists.newArrayList();
    GrammarElementTitleSwitch titleSwitch = new GrammarElementTitleSwitch().showAssignments();
    // System.out.println(NodeModelUtils.compactDump(NodeModelUtils.findActualNodeFor(model), true));
    org.eclipse.xtext.serializer.sequencer.EmitterNodeIterator ni = new org.eclipse.xtext.serializer.sequencer.EmitterNodeIterator(NodeModelUtils.findActualNodeFor(model), null, true, true);
    while (ni.hasNext()) {
        INode next = ni.next();
        if (next instanceof ILeafNode)
            result.add(titleSwitch.doSwitch(next.getGrammarElement()) + " -> " + next.getText());
        if (next instanceof ICompositeNode)
            result.add(titleSwitch.doSwitch(next.getGrammarElement()));
    }
    return result;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 48 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode 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 49 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode 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 50 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.

the class ParseErrorHandlingTest method testBug236425.

/**
 * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=236425
 * @throws Exception
 */
@Test
public void testBug236425() throws Exception {
    with(ReferenceGrammarTestLanguageStandaloneSetup.class);
    String model = "spielplatz 100 }";
    EObject object = getModelAndExpect(model, 1);
    ICompositeNode node = NodeModelUtils.getNode(object).getRootNode();
    assertEquals(1, Iterables.size(allSyntaxErrors(node)));
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Test(org.junit.Test)

Aggregations

ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)263 Test (org.junit.Test)87 INode (org.eclipse.xtext.nodemodel.INode)79 EObject (org.eclipse.emf.ecore.EObject)70 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)57 XtextResource (org.eclipse.xtext.resource.XtextResource)41 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)24 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)20 Resource (org.eclipse.emf.ecore.resource.Resource)19 Model (org.eclipse.xtext.valueconverter.bug250313.Model)15 IParseResult (org.eclipse.xtext.parser.IParseResult)14 ParserRule (org.eclipse.xtext.ParserRule)12 ITextRegion (org.eclipse.xtext.util.ITextRegion)12 RuleCall (org.eclipse.xtext.RuleCall)11 CrossReference (org.eclipse.xtext.CrossReference)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)8 AbstractRule (org.eclipse.xtext.AbstractRule)8 Keyword (org.eclipse.xtext.Keyword)8