Search in sources :

Example 71 with ICompositeNode

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);
}
Also used : Assignment(org.eclipse.xtext.Assignment) INode(org.eclipse.xtext.nodemodel.INode) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 72 with ICompositeNode

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;
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 73 with ICompositeNode

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);
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 74 with ICompositeNode

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;
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 75 with ICompositeNode

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;
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ArrayList(java.util.ArrayList) List(java.util.List) AbstractRule(org.eclipse.xtext.AbstractRule)

Aggregations

ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)263 Test (org.junit.Test)87 INode (org.eclipse.xtext.nodemodel.INode)79 EObject (org.eclipse.emf.ecore.EObject)70 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)57 XtextResource (org.eclipse.xtext.resource.XtextResource)41 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)24 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)20 Resource (org.eclipse.emf.ecore.resource.Resource)19 Model (org.eclipse.xtext.valueconverter.bug250313.Model)15 IParseResult (org.eclipse.xtext.parser.IParseResult)14 ParserRule (org.eclipse.xtext.ParserRule)12 ITextRegion (org.eclipse.xtext.util.ITextRegion)12 RuleCall (org.eclipse.xtext.RuleCall)11 CrossReference (org.eclipse.xtext.CrossReference)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)8 AbstractRule (org.eclipse.xtext.AbstractRule)8 Keyword (org.eclipse.xtext.Keyword)8