org.eclipse.xtext.nodemodel.impl.AbstractNode Java Examples

The following examples show how to use org.eclipse.xtext.nodemodel.impl.AbstractNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FixedPartialParsingHelper.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
NodeModelTokenSource(INode node, int startOffset, int endOffset, ContentAssistTokenTypeMapper tokenTypeMapper,
		N4JSGrammarAccess grammarAccess, boolean filter) {
	this.startOffset = startOffset;
	this.endOffset = endOffset;
	this.tokenTypeMapper = tokenTypeMapper;

	this.leafNodes = getLeafIterator(((AbstractNode) node).basicIterator(), filter);
	this.rightCurlyInBlock = grammarAccess.getBlockAccess().getRightCurlyBracketKeyword_2();
	this.rightCurlyInArrowExpression = grammarAccess.getArrowExpressionAccess().getRightCurlyBracketKeyword_1_0_2();
	this.semicolon = grammarAccess.getSemiAccess().getSemicolonKeyword();
	this.scriptElementCall = grammarAccess.getScriptAccess().getScriptElementsScriptElementParserRuleCall_1_1_0();
	this.memberDeclarationCall = grammarAccess.getMembersAccess()
			.getOwnedMembersRawN4MemberDeclarationParserRuleCall_1_0();
	this.statementsCall = grammarAccess.getBlockAccess().getStatementsStatementParserRuleCall_1_0();
	this.propertyAssignmentCall1 = grammarAccess.getObjectLiteralAccess()
			.getPropertyAssignmentsPropertyAssignmentParserRuleCall_2_0_0();
	this.propertyAssignmentCall2 = grammarAccess.getObjectLiteralAccess()
			.getPropertyAssignmentsPropertyAssignmentParserRuleCall_2_1_1_0();
}
 
Example #3
Source File: BasicNodeIteratorTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testIterateThreeNodesInReverse() {
	AbstractNode alpha = nodeWithTwoSiblings();
	BasicNodeIterator it = new BasicNodeIterator(alpha);
	
	Assert.assertTrue(it.hasPrevious());
	it.previous();
	
	Assert.assertTrue(it.hasPrevious());
	it.previous();

	Assert.assertTrue(it.hasPrevious());
	it.previous();
	
	Assert.assertFalse(it.hasPrevious());
}
 
Example #4
Source File: CompositeNodeTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: BasicNodeIteratorTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testIterateThreeNodes() {
	AbstractNode alpha = nodeWithTwoSiblings();
	BasicNodeIterator it = new BasicNodeIterator(alpha);
	
	Assert.assertTrue(it.hasNext());
	it.next();
	
	Assert.assertTrue(it.hasNext());
	it.next();

	Assert.assertTrue(it.hasNext());
	it.next();
	
	Assert.assertFalse(it.hasNext());
}
 
Example #6
Source File: HiddenAndTokenNodeIteratorTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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]);
}
 
Example #7
Source File: LazyURIEncoderTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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));
}
 
Example #8
Source File: ParseResult.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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;
		}
	};
}
 
Example #9
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example #10
Source File: TokenSequencePreservingPartialParsingHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #11
Source File: LookAheadPreservingNodeModelBuilder.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #12
Source File: AbstractContextualAntlrParser.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected IParseResult doParse(final String ruleName, final CharStream in, final NodeModelBuilder nodeModelBuilder, final int initialLookAhead) {
  final IParseResult parseResult = super.doParse(ruleName, in, nodeModelBuilder, initialLookAhead);
  if (delegate == null || parseResult.hasSyntaxErrors()) {
    return parseResult;
  }
  // If delegation was potentially used, we need to check for syntax errors in replaced nodes
  boolean hasError = false;
  Iterator<AbstractNode> nodeIterator = ((CompositeNode) parseResult.getRootNode()).basicIterator();
  while (nodeIterator.hasNext()) {
    AbstractNode node = nodeIterator.next();
    if (node.getSyntaxErrorMessage() != null) {
      hasError = true;
      break;
    }
  }
  if (hasError) {
    return new ParseResult(parseResult.getRootASTElement(), parseResult.getRootNode(), true);
  }
  return parseResult;
}
 
Example #13
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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
	}
}
 
Example #14
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #15
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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
	}
}
 
Example #16
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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
	}
}
 
Example #17
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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
	}
}
 
Example #18
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #19
Source File: BasicNodeIteratorTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test(expected=NoSuchElementException.class)
public void testNextTooFarThrowsException() {
	AbstractNode single = getSingleNode();
	BasicNodeIterator it = new BasicNodeIterator(single);
	
	it.next();

	it.next();
}
 
Example #20
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testGetNextSibling_SingleChild() {
	ICompositeNode rootNode = builder.newRootNode("input");
	AbstractNode node = createNode();
	builder.addChild(rootNode, node);
	assertFalse(node.hasNextSibling());
	assertNull(node.getNextSibling());
}
 
Example #21
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #22
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #23
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #24
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #25
Source File: AbstractNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #26
Source File: RootNodeTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}
 
Example #27
Source File: BasicNodeIterableTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void forEachTest() {
	AbstractNode alpha = BasicNodeIteratorTest.nodeWithTwoSiblings();
	BasicNodeIterable iterable = new BasicNodeIterable(alpha);
	
	List<String> tokens = new ArrayList<String>();
	for (INode node : iterable) {
		tokens.add(node.getText());
	}
	
	Assert.assertEquals("alpha", tokens.get(0));
	Assert.assertEquals("beta", tokens.get(1));
	Assert.assertEquals("gamma", tokens.get(2));
}
 
Example #28
Source File: BasicNodeIterableTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void forEachReverseTest() {
	AbstractNode alpha = BasicNodeIteratorTest.nodeWithTwoSiblings();
	BasicNodeIterable iterable = new BasicNodeIterable(alpha);
	
	List<String> tokens = new ArrayList<String>();
	for (INode node : iterable.reverse()) {
		tokens.add(node.getText());
	}
	
	Assert.assertEquals("gamma", tokens.get(0));
	Assert.assertEquals("beta", tokens.get(1));
	Assert.assertEquals("alpha", tokens.get(2));
}
 
Example #29
Source File: NodeIteratorTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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]);
}
 
Example #30
Source File: LazyLinkerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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;
}