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

The following examples show how to use org.eclipse.xtext.nodemodel.impl.CompositeNode. 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: 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 #2
Source File: NodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public static ParserRule getEntryParserRule(INode node) {
	ICompositeNode root = node.getRootNode();
	EObject ge1 = root.getGrammarElement();
	if (ge1 instanceof ParserRule) {
		return (ParserRule) ge1;
	} else if (ge1 instanceof Action) {
		INode firstChild = root.getFirstChild();
		while (firstChild.getGrammarElement() instanceof Action && firstChild instanceof CompositeNode) {
			firstChild = ((CompositeNode)firstChild).getFirstChild();
		}
		EObject ge2 = firstChild.getGrammarElement();
		if (ge2 instanceof ParserRule) {
			return (ParserRule) ge2;
		}
	}
	throw new IllegalStateException("No Root Parser Rule found; The Node Model is broken.");
}
 
Example #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
Source File: SyntheticLinkingSupport.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public SyntheticLinkingLeafNode(EObject semanticElement, String text, int offset, int length, EObject grammarElement, CompositeNode parent) {
	this.text = text;
	this.semanticElement = semanticElement;

	basicSetTotalOffset(offset);
	basicSetTotalLength(length);
	basicSetGrammarElement(grammarElement);
	basicSetParent(parent);
}
 
Example #10
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 #11
Source File: SyntheticLinkingSupport.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.10
 */
protected INode createCrossReferenceNode(EObject obj, EReference eRef, String crossRefString, int offset, int length) {
	CompositeNode parent = getParent(obj, eRef, crossRefString, offset, length);
	EObject grammarElement = getGrammarElement(obj, eRef, crossRefString, offset, length);
	return new SyntheticLinkingLeafNode(obj, crossRefString, offset, length, grammarElement, parent);
}
 
Example #12
Source File: LeafNode.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected CompositeNode basicGetParent() {
	return super.basicGetParent();
}
 
Example #13
Source File: LeafNode.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void basicSetParent(CompositeNode parent) {
	super.basicSetParent(parent);
}
 
Example #14
Source File: ProxyCompositeNode.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
@Override
@SuppressFBWarnings("EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS")
public boolean equals(final Object obj) {
  // this override is required for NodeTreeIterator (returned by iterator()) to work correctly in the context of NodeModelUtils
  return this == obj || obj instanceof CompositeNode && delegate() == obj;
}