Java Code Examples for org.eclipse.xtext.nodemodel.ICompositeNode#getParent()

The following examples show how to use org.eclipse.xtext.nodemodel.ICompositeNode#getParent() . 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: TokenSequencePreservingPartialParsingHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected PartialParsingPointers calculatePartialParsingPointers(ICompositeNode oldRoot, ILeafNode left, ILeafNode right) {
	ICompositeNode result = right.getParent();
	while(result.getTotalOffset() > left.getTotalOffset()) {
		result = result.getParent();
	}
	List<ICompositeNode> nodesEnclosingRegion = getAllParents(result);
	Range range = new Range(left.getTotalOffset(), right.getTotalEndOffset());
	List<ICompositeNode> validReplaceRootNodes = internalFindValidReplaceRootNodeForChangeRegion(nodesEnclosingRegion);

	filterInvalidRootNodes(validReplaceRootNodes);

	if (validReplaceRootNodes.isEmpty()) {
		validReplaceRootNodes = Collections.singletonList(oldRoot);
	}
	return new PartialParsingPointers(oldRoot, range.getOffset(), range.getLength(), validReplaceRootNodes, nodesEnclosingRegion);
}
 
Example 2
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isInvalidLastChildNode(ICompositeNode candidate, INode lastChild) {
	if (lastChild != null && lastChild.getSyntaxErrorMessage() != null) {
		EObject lastChildGrammarElement = lastChild.getGrammarElement();
		if (lastChildGrammarElement == null)
			return true;
		AbstractElement candidateElement = getCandidateElement(candidate.getGrammarElement());
		if (candidateElement != null) {
			if (isCalledBy(lastChildGrammarElement, candidateElement)) {
				while(candidate != null) {
					if (candidateElement != null && hasMandatoryFollowElements(candidateElement))
						return true;
					candidate = candidate.getParent();
					if (candidate != null)
						candidateElement = getCandidateElement(candidate.getGrammarElement());
				}
			}
			return true;
		}
	}
	return false;
}
 
Example 3
Source File: DefaultLocationInFileProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the smallest node that covers all assigned values of the given object. It handles the semantics of {@link Action
 * actions} and {@link RuleCall unassigned rule calls}.
 * 
 * @return the minimal node that covers all assigned values of the given object.
 * @since 2.3
 */
protected ICompositeNode findNodeFor(EObject semanticObject) {
	ICompositeNode result = NodeModelUtils.getNode(semanticObject);
	if (result != null) {
		ICompositeNode node = result;
		while (GrammarUtil.containingAssignment(node.getGrammarElement()) == null && node.getParent() != null && !node.getParent().hasDirectSemanticElement()) {
			ICompositeNode parent = node.getParent();
			if (node.hasSiblings()) {
				for(INode sibling: parent.getChildren()) {
					EObject grammarElement = sibling.getGrammarElement();
					if (grammarElement != null && GrammarUtil.containingAssignment(grammarElement) != null) {
						result = parent;
					}
				}
			}
			node = parent;
		}
	}
	return result;
}
 
Example 4
Source File: FixedPartialParsingHelper.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
protected boolean isInvalidLastChildNode(ICompositeNode candidate, final INode lastChild) {
  if (lastChild != null && lastChild.getSyntaxErrorMessage() != null) {
    EObject lastChildGrammarElement = lastChild.getGrammarElement();
    if (lastChildGrammarElement == null) {
      return true;
    }
    AbstractElement candidateElement = getCandidateElement(candidate.getGrammarElement());
    if (candidateElement != null) {
      if (isCalledBy(lastChildGrammarElement, candidateElement)) {
        while (candidate != null) {
          if (candidateElement != null && hasMandatoryFollowElements(candidateElement)) {
            return true;
          }
          candidate = candidate.getParent();
          if (candidate != null) {
            candidateElement = getCandidateElement(candidate.getGrammarElement());
          }
        }
      }
      return true;
    }
  }
  return false;
}
 
Example 5
Source File: N4JSQuickfixProvider.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private ICompositeNode findParentNodeWithSemanticElementOfType(ICompositeNode node, Class<?> semanticElementType) {
	ICompositeNode parentNode = node.getParent();
	while (!parentNode.hasDirectSemanticElement()
			|| !semanticElementType.isInstance(parentNode.getSemanticElement())) {
		parentNode = parentNode.getParent();
	}
	return parentNode;
}
 
Example 6
Source File: TokenSequencePreservingPartialParsingHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected List<ICompositeNode> getAllParents(ICompositeNode node) {
	List<ICompositeNode> list = Lists.newArrayList(node);
	node = node.getParent();
	while(node != null) {
		list.add(node);
		node = node.getParent();
	}
	return Lists.reverse(list);
}
 
Example 7
Source File: ImportsCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public EObject findActualSemanticObjectFor(final ICompositeNode rootNode, final ITextRegion textRegion) {
  ILeafNode leafNodeAtOffset = NodeModelUtils.findLeafNodeAtOffset(rootNode, textRegion.getOffset());
  EObject semanticElementOffset = leafNodeAtOffset.getSemanticElement();
  ICompositeNode actualOffsetNode = NodeModelUtils.findActualNodeFor(semanticElementOffset);
  int _offset = textRegion.getOffset();
  int _length = textRegion.getLength();
  final int endOffset = (_offset + _length);
  while (((actualOffsetNode.getParent() != null) && (actualOffsetNode.getTotalEndOffset() < endOffset))) {
    actualOffsetNode = actualOffsetNode.getParent();
  }
  final EObject actualSemanticObj = actualOffsetNode.getSemanticElement();
  return actualSemanticObj;
}
 
Example 8
Source File: ParserBasedContentAssistContextFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected Multimap<EObject, AbstractElement> computeCurrentModel(EObject currentModel, INode lastCompleteNode,
		Collection<AbstractElement> followElements) {
	Multimap<EObject, AbstractElement> result = LinkedHashMultimap.create();
	ICompositeNode currentParserNode = NodeModelUtils.getNode(currentModel);
	if (currentParserNode == null) {
		result.putAll(currentModel, followElements);
		return result;
	}
	EObject currentGrammarElement = currentParserNode.getGrammarElement();
	AbstractRule currentRule = getRule(currentGrammarElement);
	for (AbstractElement grammarElement : followElements) {
		AbstractRule rule = currentRule;
		ICompositeNode loopParserNode = currentParserNode;
		EObject loopLastGrammarElement = lastCompleteNode.getGrammarElement();
		while (!canBeCalledAfter(rule, loopLastGrammarElement, lastCompleteNode.getText(), grammarElement) && loopParserNode.getParent() != null) {
			loopLastGrammarElement = loopParserNode.getGrammarElement();
			loopParserNode = loopParserNode.getParent();
			while (loopParserNode.getGrammarElement() == null && loopParserNode.getParent() != null)
				loopParserNode = loopParserNode.getParent();
			EObject loopGrammarElement = loopParserNode.getGrammarElement();
			rule = getRule(loopGrammarElement);
		}
		EObject context = loopParserNode.getSemanticElement();
		result.put(context, grammarElement);
	}
	return result;
}
 
Example 9
Source File: AbstractParseTreeConstructor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void assignNodesByMatching(Map<EObject, AbstractToken> eObject2Token, ICompositeNode rootNode,
		Map<ILeafNode, EObject> comments) throws IOException {
	NodeIterator contents = new NodeIterator(rootNode);
	while (contents.hasNext()) {
		INode containedNode = contents.next();
		AbstractRule rule = containedNode.getGrammarElement() instanceof AbstractRule ? (AbstractRule) containedNode
				.getGrammarElement() : null;
		if (hiddenTokenHelper.isWhitespace(rule))
			continue;
		else if (containedNode instanceof ILeafNode && hiddenTokenHelper.isComment(rule))
			assignComment((ILeafNode) containedNode, eObject2Token, comments);
		else if (tokenUtil.isToken(containedNode)) {
			Pair<List<ILeafNode>, List<ILeafNode>> leadingAndTrailingHiddenTokens = tokenUtil
					.getLeadingAndTrailingHiddenTokens(containedNode);
			for (ILeafNode leadingHiddenNode : leadingAndTrailingHiddenTokens.getFirst()) {
				if (tokenUtil.isCommentNode(leadingHiddenNode)) {
					assignComment(leadingHiddenNode, eObject2Token, comments);
				}
			}
			assignTokenByMatcher(containedNode, eObject2Token);
			for (ILeafNode trailingHiddenNode : leadingAndTrailingHiddenTokens.getSecond()) {
				if (tokenUtil.isCommentNode(trailingHiddenNode)) {
					assignComment(trailingHiddenNode, eObject2Token, comments);
				}
			}
			contents.prune();
			ICompositeNode parentNode = containedNode.getParent();
			while (parentNode != null && assignTokenDirect(parentNode, eObject2Token))
				parentNode = parentNode.getParent();
			if (containedNode.getOffset() > rootNode.getEndOffset()) {
				break;
			}
		}
	}
}
 
Example 10
Source File: AbstractParseTreeConstructor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void collectRootsAndEObjects(AbstractToken token, Map<EObject, AbstractToken> obj2token,
		Set<ICompositeNode> roots) {
	ICompositeNode node = NodeModelUtils.getNode(token.getEObjectConsumer().getEObject());
	if (node != null && !containsNodeOrAnyParent(roots, node)) {
		while (node.getParent() != null && !node.getParent().hasDirectSemanticElement())
			node = node.getParent();
		roots.add(node);
	}
	if (!token.getTokensForSemanticChildren().isEmpty()) {
		obj2token.put(token.getTokensForSemanticChildren().get(0).getEObjectConsumer().getEObject(), token);
		for (AbstractToken t : token.getTokensForSemanticChildren())
			if (!t.getTokensForSemanticChildren().isEmpty())
				collectRootsAndEObjects(t, obj2token, roots);
	}
}
 
Example 11
Source File: ContentAssistContextFactory.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Multimap<EObject, AbstractElement> computeCurrentModel(EObject currentModel, INode lastCompleteNode,
		Collection<AbstractElement> followElements) {
	Multimap<EObject, AbstractElement> result = LinkedHashMultimap.create();
	ICompositeNode currentParserNode = NodeModelUtils.getNode(currentModel);
	if (currentParserNode == null) {
		result.putAll(currentModel, followElements);
		return result;
	}
	EObject currentGrammarElement = currentParserNode.getGrammarElement();
	AbstractRule currentRule = getRule(currentGrammarElement);
	for (AbstractElement grammarElement : followElements) {
		AbstractRule rule = currentRule;
		ICompositeNode loopParserNode = currentParserNode;
		EObject loopLastGrammarElement = lastCompleteNode.getGrammarElement();
		while (!canBeCalledAfter(rule, loopLastGrammarElement, lastCompleteNode.getText(), grammarElement) && loopParserNode.getParent() != null) {
			loopLastGrammarElement = loopParserNode.getGrammarElement();
			loopParserNode = loopParserNode.getParent();
			while (loopParserNode.getGrammarElement() == null && loopParserNode.getParent() != null)
				loopParserNode = loopParserNode.getParent();
			EObject loopGrammarElement = loopParserNode.getGrammarElement();
			rule = getRule(loopGrammarElement);
		}
		EObject context = loopParserNode.getSemanticElement();
		result.put(context, grammarElement);
	}
	return result;
}
 
Example 12
Source File: EntryPointFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected ICompositeNode findEntryPoint(ICompositeNode node, int offset) {
	ICompositeNode result = node;
	result = getApplicableNode(result);
	while (result != null) {
		int remainingLookAhead = result.getLookAhead();
		if (remainingLookAhead != 0) {
			Iterator<ILeafNode> leafNodes = result.getLeafNodes().iterator();
			while (leafNodes.hasNext() && remainingLookAhead > 0) {
				ILeafNode leaf = leafNodes.next();
				if (leaf.getTotalOffset() >= offset) {
					break;
				}
				if (!leaf.isHidden()) {
					if (remainingLookAhead > 0) {
						remainingLookAhead--;
					}
					if (remainingLookAhead == 0) {
						if (shouldUseParent(result, offset, leaf)) {
							ICompositeNode parent = result.getParent();
							return parent;
						}
						return result;
					}
				}
			}
		}
		result = getApplicableNode(result.getParent());
	}
	return result;
}
 
Example 13
Source File: EntryPointFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected ICompositeNode normalizeToParent(ICompositeNode result) {
	if (result == null) {
		return result;
	}
	ICompositeNode parent = result.getParent();
	while (parent != null && parent.getFirstChild().equals(result)
			&& parent.getLookAhead() == result.getLookAhead()) {
		result = parent;
		parent = result.getParent();
	}
	return result;
}
 
Example 14
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Filter the nodes from the iterator that do not have any impact on the parse result.
 *
 * For now we filter mostly regions that do have lookahead 1 and are closed before the requested region starts.
 */
private Iterator<INode> filterIterator(TreeIterator<AbstractNode> iterator) {
	return new AbstractIterator<>() {
		@Override
		protected INode computeNext() {
			if (iterator.hasNext()) {
				INode result = iterator.next();
				if (result instanceof ICompositeNode) {
					ICompositeNode casted = (ICompositeNode) result;
					if (casted.getTotalEndOffset() < endOffset - 1) {
						if (casted.hasChildren() && casted.getLookAhead() == 1) {
							AbstractElement grammarElement = (AbstractElement) casted.getGrammarElement();
							// Filter script elements and member declarations to the left of the cursor position.
							if (grammarElement == scriptElementCall || grammarElement == memberDeclarationCall) {
								INode sibling = casted.getNextSibling();
								while (sibling instanceof ILeafNode) {
									ILeafNode siblingLeaf = (ILeafNode) sibling;
									if (siblingLeaf.isHidden()) {
										if (siblingLeaf.getTotalEndOffset() >= endOffset) {
											return result;
										}
									} else {
										break;
									}
									sibling = siblingLeaf.getNextSibling();
								}
								iterator.prune();

								// filter statements that are completed before the cursor position and are not
								// part of the lookahead
							} else if (grammarElement == statementsCall) {
								// check if this is in the parents lookAhead to disambiguate block from object
								// literal
								ICompositeNode parent = casted.getParent();
								if (parent.getLookAhead() > 1) {
									ILeafNode firstLeaf = Iterables.get(casted.getLeafNodes(), 0);
									int remainingLA = parent.getLookAhead();
									Iterator<ILeafNode> parentLeafs = parent.getLeafNodes().iterator();
									while (parentLeafs.hasNext() && remainingLA > 0) {
										ILeafNode leafNode = parentLeafs.next();
										if (leafNode == firstLeaf) {
											break;
										}
										if (!leafNode.isHidden()) {
											remainingLA--;
											if (remainingLA == 0) {
												iterator.prune();
											}
										}
									}
								}

								// Reduce the size of object literals.
							} else if (grammarElement == propertyAssignmentCall1
									|| grammarElement == propertyAssignmentCall2) {
								iterator.prune();
								Iterator<ILeafNode> localLeafs = casted.getLeafNodes().iterator();
								while (localLeafs.hasNext()) {
									ILeafNode leaf = localLeafs.next();
									if (!leaf.isHidden()) {
										return leaf;
									}
								}
							}
						}
					}
				}
				return result;
			}
			return endOfData();
		}
	};

}
 
Example 15
Source File: NodeModelUtils.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * <p>Returns the node that covers all assigned values of the given object. It handles the semantics of {@link Action
 * actions} and {@link RuleCall unassigned rule calls}. The returned node will include unassigned surrounding leafs,
 * e.g. if you use something like {@code Parenthesized expressions} redundant parentheses will be part of the returned node.</p>
 * <p>Consider the following simple expression (a number literal): 
 * <pre>
 *   ((1))
 * </pre>
 * Assuming it was parsed from a grammar like this:
 * <pre>
 * Expression: Number | Parentheses;
 * Parentheses: '(' Expression ')';
 * Number: value=INT
 * </pre>
 * The actual node for the only semantic object that was produced from the input {@code ((1))} is the root node 
 * even though the minimal node would be the one with the text {@code 1}.
 * 
 * @param semanticObject the semantic object whose node should be provided.
 * @return the node that covers all assigned values of the given object.
 */
/* @Nullable */
public static ICompositeNode findActualNodeFor(/* @Nullable */ EObject semanticObject) {
	ICompositeNode node = getNode(semanticObject);
	if (node != null) {
		while(GrammarUtil.containingAssignment(node.getGrammarElement()) == null) {
			ICompositeNode parent = node.getParent();
			if (parent != null && !parent.hasDirectSemanticElement() && !GrammarUtil.isEObjectFragmentRuleCall(parent.getGrammarElement())) {
				node = parent;
			} else {
				break;
			}
		}
	}
	return node;
}
 
Example 16
Source File: EntryPointFinder.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected ICompositeNode getApplicableNode(ICompositeNode result) {
	while (result != null && (result.getGrammarElement() == null)) {
		result = result.getParent();
	}
	return normalizeToParent(result);
}