use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class NodeModelUtils method findActualSemanticObjectFor.
/**
* Returns the semantic object that is really associated with the actual container node of the given node. It
* handles the structural semantics that results from {@link Action Actions} and {@link RuleCall unassigned rule
* calls}.
*
* @return the semantic object that is really associated with the actual container node of the given node.
*/
/* @Nullable */
public static EObject findActualSemanticObjectFor(/* @Nullable */
INode node) {
if (node == null)
return null;
if (node.hasDirectSemanticElement())
return node.getSemanticElement();
EObject grammarElement = node.getGrammarElement();
ICompositeNode parent = node.getParent();
if (grammarElement == null)
return findActualSemanticObjectFor(parent);
Assignment assignment = GrammarUtil.containingAssignment(grammarElement);
if (assignment != null) {
if (GrammarUtil.isEObjectFragmentRule(GrammarUtil.containingRule(assignment))) {
EObject result = findActualSemanticObjectInChildren(node, grammarElement);
if (result != null)
return result;
}
if (parent.hasDirectSemanticElement())
return findActualSemanticObjectFor(parent);
INode sibling = parent.getFirstChild();
while (!sibling.equals(node)) {
EObject siblingGrammarElement = sibling.getGrammarElement();
if (siblingGrammarElement != null && GrammarUtil.containingAssignment(siblingGrammarElement) == null) {
if (GrammarUtil.isEObjectRuleCall(siblingGrammarElement)) {
return findActualSemanticObjectFor(sibling);
}
if (siblingGrammarElement.eClass() == XtextPackage.Literals.ACTION) {
return findActualSemanticObjectFor(sibling);
}
}
sibling = sibling.getNextSibling();
}
} else if (!GrammarUtil.isEObjectFragmentRuleCall(grammarElement)) {
EObject result = findActualSemanticObjectInChildren(node, grammarElement);
if (result != null)
return result;
}
return findActualSemanticObjectFor(parent);
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class AbstractInternalAntlrParser method afterParserOrEnumRuleCall.
// externalized usage patterns from generated sources
// currentNode = currentNode.getParent();
protected void afterParserOrEnumRuleCall() {
ICompositeNode newCurrent = nodeBuilder.compressAndReturnParent(currentNode);
if (currentNode == lastConsumedNode) {
lastConsumedNode = newCurrent;
}
currentNode = newCurrent;
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class PartialParsingHelper method calculatePartialParsingPointers.
public PartialParsingPointers calculatePartialParsingPointers(IParseResult previousParseResult, final int offset, int replacedTextLength) {
int myOffset = offset;
int myReplacedTextLength = replacedTextLength;
ICompositeNode oldRootNode = previousParseResult.getRootNode();
if (myOffset == oldRootNode.getTotalLength() && myOffset != 0) {
// newText is appended, so look for the last original character instead
--myOffset;
myReplacedTextLength = 1;
}
// include any existing parse errors
Range range = new Range(myOffset, myReplacedTextLength + myOffset);
if (previousParseResult.hasSyntaxErrors())
range.mergeAllSyntaxErrors(oldRootNode);
myOffset = range.getOffset();
List<ICompositeNode> nodesEnclosingRegion = collectNodesEnclosingChangeRegion(oldRootNode, range);
List<ICompositeNode> validReplaceRootNodes = internalFindValidReplaceRootNodeForChangeRegion(nodesEnclosingRegion, range);
filterInvalidRootNodes(oldRootNode, validReplaceRootNodes);
if (validReplaceRootNodes.isEmpty()) {
validReplaceRootNodes = Collections.singletonList(oldRootNode);
}
return new PartialParsingPointers(oldRootNode, myOffset, myReplacedTextLength, validReplaceRootNodes, nodesEnclosingRegion);
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class PartialParsingHelper method collectNodesEnclosingChangeRegion.
private void collectNodesEnclosingChangeRegion(ICompositeNode parent, Range range, List<ICompositeNode> nodesEnclosingRegion) {
nodesEnclosingRegion.add(parent);
BidiIterator<INode> iterator = parent.getChildren().iterator();
while (iterator.hasPrevious()) {
INode prev = iterator.previous();
if (prev instanceof ICompositeNode) {
if (nodeEnclosesRegion((ICompositeNode) prev, range)) {
collectNodesEnclosingChangeRegion((ICompositeNode) prev, range, nodesEnclosingRegion);
break;
}
}
}
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.
the class AbstractParseTreeConstructor method assignNodesByMatching.
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;
}
}
}
}
Aggregations