Java Code Examples for org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator#hasNext()
The following examples show how to use
org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator#hasNext() .
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: HiddenLeafAccess.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
protected List<ILeafNode> findNextHiddenLeafs(final INode node) { ArrayList<ILeafNode> _xblockexpression = null; { final ArrayList<ILeafNode> result = CollectionLiterals.<ILeafNode>newArrayList(); final NodeIterator ni = new NodeIterator(node); while (ni.hasNext()) { { final INode next = ni.next(); if ((next instanceof ILeafNode)) { boolean _isHidden = ((ILeafNode)next).isHidden(); if (_isHidden) { result.add(((ILeafNode)next)); } else { return result; } } } } _xblockexpression = result; } return _xblockexpression; }
Example 2
Source File: HiddenTokenSequencer.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected Set<INode> getTrailingCommentsIncludingWhitespace(ILeafNode node) { Set<INode> associatedNodes = Sets.newHashSet(); Set<INode> pendingWhitespace = Sets.newHashSet(); INode lastLink = node; NodeIterator ni = new NodeIterator(lastLink); while(ni.hasNext()) { INode next = ni.next(); if (tokenUtil.isCommentNode(next)) { if (isTrailingCommentFor(next, lastLink)) { lastLink = next; associatedNodes.addAll(pendingWhitespace); associatedNodes.add(next); } else { break; } } else if (tokenUtil.isWhitespaceNode(next)) { pendingWhitespace.add(next); } else if (next instanceof ILeafNode){ break; } } return associatedNodes; }
Example 3
Source File: HiddenTokenSequencer.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
protected List<INode> getRemainingHiddenNodesInContainer(INode from, INode root) { if (from == null || root == null) return Collections.emptyList(); List<INode> out = Lists.newArrayList(); NodeIterator ni = new NodeIterator(from); while (ni.hasNext()) { INode next = ni.next(); if (next.getTotalOffset() > root.getTotalEndOffset()) return out; else if (tokenUtil.isWhitespaceOrCommentNode(next)) { out.add(next); } else if (tokenUtil.isToken(next)) return Collections.emptyList(); } return out; }
Example 4
Source File: ReorderingHiddenTokenSequencer.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** * Helper method that collects all the hidden tokens that follow the given node. * * @param from * starting point in the node model, must not be {@code null} * @param deletedSemanticElements * set of semantic elements that have been deleted from the model, must not be {@code null} * @return list of hidden tokens, never {@code null}, can be empty */ private List<INode> getFollowingHiddenTokens(final INode from, final Set<EObject> deletedSemanticElements) { List<INode> result = Lists.newArrayList(); NodeIterator nodes = new NodeIterator(from); while (nodes.hasNext()) { INode next = nodes.next(); if (next.getTotalOffset() > rootEndOffset || next.equals(lastEmittedNode)) { break; } else if (tokenUtil.isWhitespaceOrCommentNode(next)) { if (!emittedComments.contains(next)) { result.add(next); } } else if (belongsToDeletedElement(next)) { handleDeletedElement(result, deletedSemanticElements, next); nodes.prune(); } else { break; } } return result; }
Example 5
Source File: ReorderingHiddenTokenSequencer.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** * Returns trailing comments of the given leaf node. * * @param node * leaf element of the node model to start looking from, must not be {@code null} * @return list of hidden tokens, can be empty, never {@code null} */ private List<INode> getTrailingCommentsIncludingWhitespace(final ILeafNode node) { List<INode> associatedNodes = Lists.newLinkedList(); Set<INode> pendingWhitespace = Sets.newHashSet(); INode lastLink = node; NodeIterator nodes = new NodeIterator(lastLink); while (nodes.hasNext()) { INode next = nodes.next(); if (tokenUtil.isCommentNode(next)) { if (isTrailingCommentFor(next, lastLink)) { lastLink = next; associatedNodes.addAll(pendingWhitespace); pendingWhitespace.clear(); associatedNodes.add(next); } else { break; } } else if (tokenUtil.isWhitespaceNode(next)) { pendingWhitespace.add(next); } else if (next instanceof ILeafNode) { break; } } return associatedNodes; }
Example 6
Source File: ReorderingHiddenTokenSequencer.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** * Returns the list of nodes that belong to 'root' node and follow the 'from' node. * * @param from * node model element to start from, must not be {@code null} * @param root * top-level node model element, must not be {@code null} * @return list of hiddens nodes, possibly empty, but never {@code null} */ private List<INode> getRemainingHiddenNodesInContainer(final INode from, final INode root) { if (from == null || root == null) { return Collections.emptyList(); } List<INode> out = Lists.newArrayList(); NodeIterator nodes = new NodeIterator(from); while (nodes.hasNext()) { INode next = nodes.next(); if (next.getStartLine() > root.getEndLine()) { return out; } else if (tokenUtil.isWhitespaceOrCommentNode(next)) { out.add(next); } else if (tokenUtil.isToken(next)) { return out; } } return out; }
Example 7
Source File: NodeModelAccess.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
public ILeafNode findNextLeaf(INode node, Function1<? super ILeafNode, ? extends Boolean> matches) { if (node != null) { if (node instanceof ILeafNode && matches.apply((ILeafNode) node)) { return (ILeafNode) node; } NodeIterator ni = new NodeIterator(node); while (ni.hasNext()) { INode next = ni.next(); if (next instanceof ILeafNode && matches.apply((ILeafNode) next)) { return (ILeafNode) next; } } } return null; }
Example 8
Source File: NodeIteratorTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected void checkAscending(int index) { int i = index; NodeIterator nodeIterator = new NodeIterator(nodes[i]); while (nodeIterator.hasNext()) { assertEquals("Ascending " + i, nodes[++i], nodeIterator.next()); } }
Example 9
Source File: ReorderingHiddenTokenSequencer.java From dsl-devkit with Eclipse Public License 1.0 | 5 votes |
/** * Collects & emits the set of comments/whitespaces that logically belong to a container of some objects and <b>not</b> to the first child of the container. * * @param container * node model element that represents a container, must not be {@code null} */ private void emitContainerComments(final INode container) { List<INode> containerComments = Lists.newArrayList(); Set<INode> firstElementComments = getHiddenNodesBelongingTo(NodeModelUtils.findActualSemanticObjectFor(container)); NodeIterator nodes = new NodeIterator(container); while (nodes.hasNext()) { INode next = nodes.next(); if (firstElementComments.contains(next) || tokenUtil.isToken(next)) { break; } else if (tokenUtil.isWhitespaceOrCommentNode(next)) { containerComments.add(next); } } emitHiddenTokens(containerComments); }
Example 10
Source File: ReorderingHiddenTokenSequencer.java From dsl-devkit with Eclipse Public License 1.0 | 4 votes |
/** * Collects all the hidden tokens between two given nodes of the node model. * * @param from * node that serves as a start point, must not be {@code null} * @param to * search end point, must not be {@code null} * @param deletedSemanticElements * set of the deleted semantic elements, must not be {@code null} * @return list of hidden tokens, never {@code null}, but can be empty */ private List<INode> getHiddenTokensBetween(final INode from, final INode to, final Set<EObject> deletedSemanticElements) { EObject fromElement = NodeModelUtils.findActualSemanticObjectFor(from); if (from.equals(NodeModelUtils.getNode(fromElement))) { // If the starting node represents some container, emit the comments that belong to it // This is needed to correctly handle some edge cases like ImportList in AvqScript // Logic for distinguishing between container's comments and the ones of first element is expected to be implemented in 'isLeadingCommentFor' emitContainerComments(from); } List<INode> result = Lists.newArrayList(); boolean handleReordering = from.getTotalOffset() > to.getTotalOffset(); if (!handleReordering) { // Elements are not reordered // Just going through the interval and collecting comments, unless they have already been emitted NodeIterator nodes = new NodeIterator(from); while (nodes.hasNext()) { INode next = nodes.next(); if (tokenUtil.isWhitespaceOrCommentNode(next)) { if (!emittedComments.contains(next)) { result.add(next); } } else if (next.equals(to)) { // We have hit the 'to' node // If it is a composite one, we have to iterate through its children // and collect whitespaces/comments until we encounter first token (keyword, identifier...) if (next instanceof ICompositeNode && (GrammarUtil.isDatatypeRuleCall(next.getGrammarElement()) || GrammarUtil.isEnumRuleCall(next.getGrammarElement()) || next.getGrammarElement() instanceof CrossReference)) { while (nodes.hasNext()) { INode lastNodeChild = nodes.next(); if (tokenUtil.isWhitespaceOrCommentNode(lastNodeChild)) { if (!emittedComments.contains(lastNodeChild)) { result.add(lastNodeChild); } } else if (lastNodeChild instanceof ILeafNode) { break; } } break; } else { // 'to' node is not a composite one, nothing to do here, just exit the loop break; } } else if (belongsToDeletedElement(next)) { handleDeletedElement(result, deletedSemanticElements, next); nodes.prune(); } else if (tokenUtil.isToken(next)) { // We have encountered some token, but not the one we expected // Will be handled by invoking 'getLeadingCommentsIncludingWhitespace' method later handleReordering = true; break; } } } if (handleReordering) { return getLeadingCommentsIncludingWhitespace(to); } return result; }