@Override public void replaceAndTransferLookAhead(INode oldNode, INode newRootNode) { Iterator<AbstractNode> oldNodes = ((AbstractNode) oldNode).basicIterator(); Iterator<AbstractNode> newNodes = ((AbstractNode) newRootNode).basicIterator(); newNodes.next(); // basicGetFirstChild to skip that one while(oldNodes.hasNext()) { AbstractNode nextOld = oldNodes.next(); AbstractNode nextNew = newNodes.next(); if (nextOld instanceof CompositeNode) { setLookAhead((CompositeNode) nextNew, ((CompositeNode) nextOld).getLookAhead()); } } if (newNodes.hasNext()) { throw new RuntimeException(); } super.replaceAndTransferLookAhead(oldNode, newRootNode); }
/** * Returns true if the previous document state was completely broken, e.g. the parser did not recover at all. * This may happen e.g. in Xtend for documents like * <pre>import static class C {}</pre> * where the class keyword is consumed as an invalid token in the import declaration and everything thereafter * is unrecoverable. */ protected boolean isBrokenPreviousState(IParseResult previousParseResult, int offset) { if (previousParseResult.hasSyntaxErrors()) { BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) previousParseResult.getRootNode()).basicIterator(); while(iterator.hasPrevious()) { AbstractNode previous = iterator.previous(); if (previous.getGrammarElement() == null) { return true; } if (previous instanceof ILeafNode && previous.getOffset() <= offset) { break; } } } return false; }
private boolean isRangePartOfExceedingLookAhead(CompositeNode node, ReplaceRegion replaceRegion) { TreeIterator<AbstractNode> iterator = node.basicIterator(); int lookAhead = node.getLookAhead(); if (lookAhead == 0) { return false; } while(iterator.hasNext()) { AbstractNode child = iterator.next(); if (child instanceof CompositeNode) { if (child.getTotalOffset() < replaceRegion.getEndOffset()) lookAhead = Math.max(((CompositeNode) child).getLookAhead(), lookAhead); } else if (!((ILeafNode) child).isHidden()) { lookAhead--; if (lookAhead == 0) { if (child.getTotalOffset() >= replaceRegion.getEndOffset()) return false; } } } return lookAhead > 0; }
@Override public Iterable<INode> getSyntaxErrors() { if (rootNode == null || !hasSyntaxErrors()) return Collections.emptyList(); return new Iterable<INode>() { @Override @SuppressWarnings("unchecked") public Iterator<INode> iterator() { Iterator<? extends INode> result = Iterators.filter(((CompositeNode) rootNode).basicIterator(), new Predicate<AbstractNode>() { @Override public boolean apply(AbstractNode input) { return input.getSyntaxErrorMessage() != null; } }); return (Iterator<INode>) result; } }; }
protected Iterable<ILeafNode> getReverseLeafNodes(final AbstractNode node) { return Iterables.filter(new Iterable<INode>() { @Override public Iterator<INode> iterator() { return new AbstractIterator<INode>() { private BidiTreeIterator<AbstractNode> delegate = node.basicIterator(); @Override protected INode computeNext() { if (delegate.hasPrevious()) return delegate.previous(); return endOfData(); } }; } }, ILeafNode.class); }
@Before public void setUp() throws Exception { nodes = new INode[NUM_NODES]; NodeModelBuilder builder = new NodeModelBuilder(); nodes[0] = new CompositeNode(); nodes[1] = new CompositeNode(); nodes[2] = new HiddenLeafNode(); nodes[3] = new LeafNode(); nodes[4] = new HiddenLeafNode(); nodes[5] = new CompositeNode(); nodes[6] = new LeafNode(); nodes[7] = new CompositeNode(); nodes[8] = new HiddenLeafNode(); nodes[9] = new LeafNode(); builder.addChild((ICompositeNode)nodes[0], (AbstractNode)nodes[1]); builder.addChild((ICompositeNode)nodes[0], (AbstractNode)nodes[5]); builder.addChild((ICompositeNode)nodes[0], (AbstractNode)nodes[7]); builder.addChild((ICompositeNode)nodes[0], (AbstractNode)nodes[9]); builder.addChild((ICompositeNode)nodes[1], (AbstractNode)nodes[2]); builder.addChild((ICompositeNode)nodes[1], (AbstractNode)nodes[3]); builder.addChild((ICompositeNode)nodes[1], (AbstractNode)nodes[4]); builder.addChild((ICompositeNode)nodes[5], (AbstractNode)nodes[6]); builder.addChild((ICompositeNode)nodes[7], (AbstractNode)nodes[8]); }
@Test public void testNodePath() throws Exception { NodeModelBuilder builder = new NodeModelBuilder(); ICompositeNode n = new CompositeNode(); ICompositeNode n1 = new CompositeNode(); builder.addChild(n, (AbstractNode) n1); ICompositeNode n2 = new CompositeNode(); builder.addChild(n, (AbstractNode) n2); ILeafNode l1 = new LeafNode(); builder.addChild(n2, (AbstractNode) l1); ILeafNode l2 = new LeafNode(); builder.addChild(n2, (AbstractNode) l2); assertEquals(n, find(n,n)); assertEquals(n1, find(n,n1)); assertEquals(n2, find(n,n2)); assertEquals(l1, find(n,l1)); assertEquals(l2, find(n,l2)); }
private boolean isRangePartOfExceedingLookAhead(final CompositeNode node, final ReplaceRegion replaceRegion) { TreeIterator<AbstractNode> iterator = node.basicIterator(); int lookAhead = node.getLookAhead(); if (lookAhead == 0) { return false; } while (iterator.hasNext()) { AbstractNode child = iterator.next(); if (child instanceof CompositeNode) { if (child.getTotalOffset() < replaceRegion.getEndOffset()) { lookAhead = Math.max(((CompositeNode) child).getLookAhead(), lookAhead); } } else if (!((ILeafNode) child).isHidden()) { lookAhead--; if (lookAhead == 0) { if (child.getTotalOffset() >= replaceRegion.getEndOffset()) { return false; } } } } return lookAhead > 0; }
@Test public void testGetRootNode_Parent() { AbstractNode node = createNode(); ICompositeNode rootNode = builder.newRootNode("My input"); ICompositeNode parent = builder.newCompositeNode(null, 0, rootNode); builder.addChild(parent, node); assertSame(rootNode, node.getRootNode()); }
@Test public void testIterator_Next() { AbstractNode node = createNode(); BidiIterator<INode> iterator = node.iterator(); assertTrue(iterator.hasNext()); assertSame(node, iterator.next()); assertFalse(iterator.hasNext()); try { iterator.next(); fail("Expected NoSuchElementException"); } catch(NoSuchElementException e) { // ok } }
@Test public void testIterator_Previous() { AbstractNode node = createNode(); BidiIterator<INode> iterator = node.iterator(); assertTrue(iterator.hasPrevious()); assertSame(node, iterator.previous()); assertFalse(iterator.hasPrevious()); try { iterator.previous(); fail("Expected NoSuchElementException"); } catch(NoSuchElementException e) { // ok } }
@Test public void testIterator_Bidi() { AbstractNode node = createNode(); BidiIterator<INode> iterator = node.iterator(); assertSame(node, iterator.next()); assertTrue(iterator.hasPrevious()); assertSame(node, iterator.previous()); assertTrue(iterator.hasNext()); }
@Test public void testTreeIterator_Next() { ICompositeNode rootNode = builder.newRootNode("input"); AbstractNode node = createNode(); builder.addChild(rootNode, node); BidiIterator<INode> iterator = node.iterator(); assertTrue(iterator.hasNext()); assertSame(node, iterator.next()); assertFalse(iterator.hasNext()); try { iterator.next(); fail("Expected NoSuchElementException"); } catch(NoSuchElementException e) { // ok } }
@Test public void testTreeIterator_Next_NoParent() { AbstractNode node = createNode(); BidiIterator<INode> iterator = node.iterator(); assertTrue(iterator.hasNext()); assertSame(node, iterator.next()); assertFalse(iterator.hasNext()); try { iterator.next(); fail("Expected NoSuchElementException"); } catch(NoSuchElementException e) { // ok } }
@Test public void testTreeIterator_Previous() { ICompositeNode rootNode = builder.newRootNode("input"); AbstractNode node = createNode(); builder.addChild(rootNode, node); BidiIterator<INode> iterator = node.iterator(); assertTrue(iterator.hasPrevious()); assertSame(node, iterator.previous()); assertFalse(iterator.hasPrevious()); try { iterator.previous(); fail("Expected NoSuchElementException"); } catch(NoSuchElementException e) { // ok } }
@Test public void testTreeIterator_Previous_NoParent() { AbstractNode node = createNode(); BidiIterator<INode> iterator = node.iterator(); assertTrue(iterator.hasPrevious()); assertSame(node, iterator.previous()); assertFalse(iterator.hasPrevious()); try { iterator.previous(); fail("Expected NoSuchElementException"); } catch(NoSuchElementException e) { // ok } }
@Test public void testTreeIterator_Bidi() { ICompositeNode rootNode = builder.newRootNode("input"); AbstractNode node = createNode(); builder.addChild(rootNode, node); BidiIterator<INode> iterator = node.iterator(); assertSame(node, iterator.next()); assertTrue(iterator.hasPrevious()); assertSame(node, iterator.previous()); assertTrue(iterator.hasNext()); }
@Test public void testTreeIterator_Bidi_NoParent() { AbstractNode node = createNode(); BidiIterator<INode> iterator = node.iterator(); assertSame(node, iterator.next()); assertTrue(iterator.hasPrevious()); assertSame(node, iterator.previous()); assertTrue(iterator.hasNext()); }
@Test public void testGetNextSibling_SingleChild() { ICompositeNode rootNode = builder.newRootNode("input"); AbstractNode node = createNode(); builder.addChild(rootNode, node); assertFalse(node.hasNextSibling()); assertNull(node.getNextSibling()); }
@Test public void testGetNextSibling_FirstChild() { ICompositeNode rootNode = builder.newRootNode("input"); AbstractNode first = createNode(); AbstractNode second = createNode(); builder.addChild(rootNode, first); builder.addChild(rootNode, second); assertTrue(first.hasNextSibling()); assertSame(second, first.getNextSibling()); }
@Test public void testGetNextSibling_LastChild() { ICompositeNode rootNode = builder.newRootNode("input"); AbstractNode first = createNode(); AbstractNode second = createNode(); builder.addChild(rootNode, first); builder.addChild(rootNode, second); assertFalse(second.hasNextSibling()); assertNull(second.getNextSibling()); }
@Test public void testGetPreviousSibling_SingleChild() { ICompositeNode rootNode = builder.newRootNode("input"); AbstractNode node = createNode(); builder.addChild(rootNode, node); assertFalse(node.hasPreviousSibling()); assertNull(node.getPreviousSibling()); assertFalse(node.hasSiblings()); }
@Test public void testGetPreviousSibling_FirstChild() { ICompositeNode rootNode = builder.newRootNode("input"); AbstractNode first = createNode(); AbstractNode second = createNode(); builder.addChild(rootNode, first); builder.addChild(rootNode, second); assertFalse(first.hasPreviousSibling()); assertNull(first.getPreviousSibling()); assertTrue(first.hasSiblings()); }
@Test public void testGetPreviousSibling_LastChild() { ICompositeNode rootNode = builder.newRootNode("input"); AbstractNode first = createNode(); AbstractNode second = createNode(); builder.addChild(rootNode, first); builder.addChild(rootNode, second); assertTrue(second.hasPreviousSibling()); assertSame(first, second.getPreviousSibling()); assertTrue(second.hasSiblings()); }
@Test public void testIteratorForEmptyNode() { RootNode rootNode = createCompositeNode(); BidiTreeIterator<AbstractNode> forwardIterator = rootNode.basicIterator(); assertTrue(forwardIterator.hasNext()); assertSame(rootNode, forwardIterator.next()); assertFalse(forwardIterator.hasNext()); BidiTreeIterator<AbstractNode> backwardsIterator = rootNode.basicIterator(); assertTrue(backwardsIterator.hasPrevious()); assertSame(rootNode, backwardsIterator.next()); assertFalse(backwardsIterator.hasPrevious()); }
@Before public void setUp() throws Exception { NodeModelBuilder builder = new NodeModelBuilder(); nodes = new INode[NUM_NODES]; nodes[0] = new CompositeNode(); nodes[1] = new LeafNode(); nodes[2] = new CompositeNode(); nodes[3] = new CompositeNode(); nodes[4] = new LeafNode(); nodes[5] = new LeafNode(); nodes[6] = new LeafNode(); nodes[7] = new CompositeNode(); nodes[8] = new LeafNode(); nodes[9] = new LeafNode(); nodes[10]= new CompositeNode(); builder.addChild((ICompositeNode)nodes[0], (AbstractNode)nodes[1]); builder.addChild((ICompositeNode)nodes[0], (AbstractNode)nodes[2]); builder.addChild((ICompositeNode)nodes[2], (AbstractNode)nodes[3]); builder.addChild((ICompositeNode)nodes[3], (AbstractNode)nodes[4]); builder.addChild((ICompositeNode)nodes[3], (AbstractNode)nodes[5]); builder.addChild((ICompositeNode)nodes[2], (AbstractNode)nodes[6]); builder.addChild((ICompositeNode)nodes[2], (AbstractNode)nodes[7]); builder.addChild((ICompositeNode)nodes[2], (AbstractNode)nodes[8]); builder.addChild((ICompositeNode)nodes[0], (AbstractNode)nodes[9]); builder.addChild((ICompositeNode)nodes[0], (AbstractNode)nodes[10]); }
private ICompositeNode newSimpleNodeAdapter(final INode... nodes) { NodeModelBuilder builder = new NodeModelBuilder(); ICompositeNode result = new CompositeNodeWithSemanticElement(); for(INode node: nodes) { builder.addChild(result, (AbstractNode) node); } return result; }
/** * Find the leaf node at the given offset. May return <code>null</code> if the given offset is not valid for the * node (sub-)tree. * * A node matches the <code>leafNodeOffset</code> if it fulfills the following condition: * <pre> * node.totalOffset <= leafNodeOffset && * node.totalEndOffset > leafNodeOffset * </pre> * * @param node the container node. May not be <code>null</code>. * @param leafNodeOffset the offset that is covered by the searched node. * @return the leaf node at the given offset or <code>null</code>. */ /* @Nullable */ public static ILeafNode findLeafNodeAtOffset(/* @NonNull */ INode node, int leafNodeOffset) { INode localNode = node; while(!(localNode instanceof AbstractNode)) { localNode = localNode.getParent(); } int offset = localNode.getTotalOffset(); int length = localNode.getTotalLength(); BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) localNode).basicIterator(); if (leafNodeOffset > (offset + length) / 2) { while (iterator.hasPrevious()) { AbstractNode previous = iterator.previous(); int previousOffset = previous.getTotalOffset(); int previousLength = previous.getTotalLength(); if (!intersects(previousOffset, previousLength, leafNodeOffset)) { if (previousOffset + previousLength <= leafNodeOffset) { return null; } iterator.prune(); } else { if (previous instanceof ILeafNode) return (ILeafNode) previous; } } } else { while (iterator.hasNext()) { AbstractNode next = iterator.next(); int nextOffset = next.getTotalOffset(); int nextLength = next.getTotalLength(); if (!intersects(nextOffset, nextLength, leafNodeOffset)) { if (nextOffset > leafNodeOffset) { return null; } iterator.prune(); } else { if (next instanceof ILeafNode) return (ILeafNode) next; } } } return null; }
@Override protected AbstractNode basicGetFirstChild() { return super.basicGetFirstChild(); }
@Test public void testGetParent() { AbstractNode node = createNode(); ICompositeNode parent = builder.newRootNode("input"); builder.addChild(parent, node); assertSame(parent, node.getParent()); }
@Test public void testGetParent_NoParent() { AbstractNode node = createNode(); assertNull(node.getParent()); }
@Test public void testGetRootNode() { AbstractNode node = createNode(); ICompositeNode rootNode = builder.newRootNode("My input"); builder.addChild(rootNode, node); assertSame(rootNode, node.getRootNode()); }
@Test public void testGetRootNode_NoParent() { AbstractNode node = createNode(); assertNull(node.getRootNode()); }
@Test public void testGetRootNode_NoRoot() { AbstractNode node = createNode(); ICompositeNode parent = new CompositeNode(); builder.addChild(parent, node); assertNull(node.getRootNode()); }
@Test public void testGetText_NoParent() { AbstractNode node = createNode(); assertNull(node.getText()); }
@Override protected void basicSetFirstChild(AbstractNode firstChild) { super.basicSetFirstChild(firstChild); }
@Override protected AbstractNode basicGetLastChild() { return super.basicGetLastChild(); }
@Override protected AbstractNode basicGetPreviousSibling() { return super.basicGetPreviousSibling(); }