@Test public void testTreeIteratorForSyntheticNodes_Forward() throws Exception { EObject object = getModel("(d - e) / e * d // fasdf s"); ICompositeNode root = NodeModelUtils.getNode(object).getRootNode(); INode firstChild = root.getFirstChild(); INode firstGrandChild = ((ICompositeNode) firstChild).getFirstChild(); INode firstGrandGrandChild = ((ICompositeNode) firstGrandChild).getFirstChild(); INode firstGrandGrandGrandChild = ((ICompositeNode) firstGrandGrandChild).getFirstChild(); INode synthetic = ((ICompositeNode) firstGrandGrandGrandChild).getFirstChild(); assertTrue(synthetic instanceof SyntheticCompositeNode); INode expectedLastChild = ((ICompositeNode)synthetic).getLastChild(); while(expectedLastChild instanceof ICompositeNode) expectedLastChild = ((ICompositeNode)expectedLastChild).getLastChild(); INode lastChild = null; for(INode child: synthetic.getAsTreeIterable()) lastChild = child; assertEquals(expectedLastChild, lastChild); }
@Test public void testTreeIteratorForSyntheticNodes_Backwards() throws Exception { EObject object = getModel("d - e / e * d"); ICompositeNode root = NodeModelUtils.getNode(object).getRootNode(); INode firstChild = root.getFirstChild(); INode firstGrandChild = ((ICompositeNode) firstChild).getFirstChild(); INode sibling = firstGrandChild.getNextSibling().getNextSibling().getNextSibling(); INode siblingChild = ((ICompositeNode) sibling).getFirstChild(); INode siblingGrandChild = ((ICompositeNode) siblingChild).getFirstChild(); INode synthetic = ((ICompositeNode) siblingGrandChild).getFirstChild(); assertTrue(synthetic instanceof SyntheticCompositeNode); INode expectedFirstChild = ((ICompositeNode)synthetic).getFirstChild(); while(expectedFirstChild instanceof ICompositeNode) expectedFirstChild = ((ICompositeNode)expectedFirstChild).getFirstChild(); INode actualFirstChild = null; for(INode child: synthetic.getAsTreeIterable().reverse()) actualFirstChild = child; assertEquals(expectedFirstChild, actualFirstChild); }
@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); }
protected ICompositeNode getReplacedNode(PartialParsingPointers parsingPointers) { List<ICompositeNode> validReplaceRootNodes = parsingPointers.getValidReplaceRootNodes(); ICompositeNode replaceMe = null; for (int i = validReplaceRootNodes.size() - 1; i >= 0; --i) { replaceMe = validReplaceRootNodes.get(i); if (!(replaceMe instanceof SyntheticCompositeNode)) { break; } } return replaceMe; }
protected boolean isInvalidRootNode(ICompositeNode candidate) { if (candidate instanceof SyntheticCompositeNode) return true; if (candidate.getGrammarElement() instanceof RuleCall) { AbstractRule rule = ((RuleCall) candidate.getGrammarElement()).getRule(); if (!(rule instanceof ParserRule) || GrammarUtil.isDatatypeRule((ParserRule) rule)) return true; } if (candidate.getGrammarElement() instanceof Action) { return true; } return false; }
protected boolean isInvalidRootNode(ICompositeNode rootNode, ICompositeNode candidate) { int endOffset = candidate.getTotalEndOffset(); if (candidate instanceof SyntheticCompositeNode) return true; if (candidate.getGrammarElement() instanceof RuleCall) { AbstractRule rule = ((RuleCall) candidate.getGrammarElement()).getRule(); if (!(rule instanceof ParserRule)) return true; ParserRule casted = (ParserRule) rule; if (GrammarUtil.isDatatypeRule(casted) || casted.isFragment() || !casted.getParameters().isEmpty()) { return true; } if (isInvalidDueToPredicates((AbstractElement) candidate.getGrammarElement())) return true; } if (candidate.getGrammarElement() instanceof Action) { return true; } if (endOffset == rootNode.getTotalEndOffset()) { INode lastChild = getLastChild(candidate); if (lastChild instanceof ICompositeNode) { INode lastLeaf = getLastLeaf(candidate); if (isInvalidLastChildNode(candidate, lastLeaf)) { return true; } } if (isInvalidLastChildNode(candidate, lastChild)) { return true; } } return false; }
protected boolean isInvalidRootNode(final ICompositeNode rootNode, final ICompositeNode candidate) { int endOffset = candidate.getTotalEndOffset(); if (candidate instanceof SyntheticCompositeNode) { return true; } if (candidate.getGrammarElement() instanceof RuleCall) { AbstractRule rule = ((RuleCall) candidate.getGrammarElement()).getRule(); if (!(rule instanceof ParserRule) || GrammarUtil.isDatatypeRule((ParserRule) rule)) { return true; } else if (isInvalidDueToPredicates((AbstractElement) candidate.getGrammarElement())) { return true; } } if (candidate.getGrammarElement() instanceof Action) { return true; } if (endOffset == rootNode.getTotalEndOffset()) { INode lastChild = getLastChild(candidate); if (lastChild instanceof ICompositeNode) { INode lastLeaf = getLastLeaf(candidate); if (isInvalidLastChildNode(candidate, lastLeaf)) { return true; } } if (isInvalidLastChildNode(candidate, lastChild)) { return true; } } return false; }