Search in sources :

Example 66 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.

the class ResourceLoadTest method testNoExceptionDiagnostics_04.

@Test
public void testNoExceptionDiagnostics_04() throws Exception {
    XtextResource r = getResourceFromString(this.model);
    assertTrue(r.getErrors().isEmpty());
    ICompositeNode node = r.getParseResult().getRootNode();
    for (Iterator<INode> i = node.getAsTreeIterable().iterator(); i.hasNext(); ) {
        INode childNode = i.next();
        String subModel = model.substring(0, childNode.getOffset()) + model.substring(childNode.getOffset() + childNode.getLength());
        Resource res = getResourceFromStringAndExpect(model, UNKNOWN_EXPECTATION);
        assertNoExceptionDiagnostic(res, subModel);
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 67 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.

the class NodeModelBuilder method compressAndReturnParent.

public ICompositeNode compressAndReturnParent(ICompositeNode compositeNode) {
    CompositeNode casted = (CompositeNode) compositeNode;
    if (casted.basicGetParent() != null && casted.hasChildren() && casted.basicGetFirstChild() instanceof CompositeNode) {
        CompositeNode firstChild = (CompositeNode) casted.basicGetFirstChild();
        // it is our only child
        if (!firstChild.basicHasSiblings() && !firstChild.hasDirectSemanticElement() && firstChild.getLookAhead() == casted.getLookAhead() && firstChild.getSyntaxErrorMessage() == null) {
            // it is our only child and has no direct semantic element
            // so we can fold its grammar element into our own grammar elements
            // if it refers not to a syntax error or a semantic object
            EObject myGrammarElement = casted.getGrammarElement();
            Object childGrammarElement = firstChild.basicGetGrammarElement();
            EObject[] list = null;
            if (childGrammarElement instanceof EObject) {
                list = new EObject[] { myGrammarElement, (EObject) childGrammarElement };
            } else {
                list = newEObjectArray(myGrammarElement, (EObject[]) childGrammarElement);
            }
            casted.basicSetGrammarElement(cachedFoldedGrammarElements.intern(list));
            replaceChildren(firstChild, casted);
        }
    }
    CompositeNode result = null;
    if (casted.basicGetSemanticElement() == null) {
        if (casted instanceof CompositeNodeWithSemanticElement) {
            if (casted.getSyntaxErrorMessage() == null) {
                CompositeNode compressed = new CompositeNode();
                compressed.basicSetGrammarElement(casted.basicGetGrammarElement());
                compressed.basicSetLookAhead(compositeNode.getLookAhead());
                replace(casted, compressed);
                result = compressed.basicGetParent();
            } else {
                CompositeNodeWithSyntaxError compressed = new CompositeNodeWithSyntaxError();
                compressed.basicSetGrammarElement(casted.basicGetGrammarElement());
                compressed.basicSetLookAhead(compositeNode.getLookAhead());
                compressed.basicSetSyntaxErrorMessage(casted.getSyntaxErrorMessage());
                replace(casted, compressed);
                result = compressed.basicGetParent();
            }
        }
    }
    if (result == null) {
        result = casted.basicGetParent();
    }
    if (compressRoot && result instanceof RootNode) {
        if (casted.hasSiblings())
            throw new IllegalStateException("Root's child should never have siblings");
        replaceByRootNode(casted, (RootNode) result);
    }
    return result;
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject)

Example 68 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.

the class NodeModelBuilder method newCompositeNodeAsParentOf.

public ICompositeNode newCompositeNodeAsParentOf(EObject grammarElement, int lookahead, ICompositeNode existing) {
    CompositeNodeWithSemanticElement newComposite = new CompositeNodeWithSemanticElement();
    AbstractNode castedExisting = (AbstractNode) existing;
    newComposite.basicSetGrammarElement(grammarElement);
    newComposite.basicSetLookAhead(lookahead);
    CompositeNode existingParent = castedExisting.basicGetParent();
    newComposite.basicSetParent(existingParent);
    if (existingParent.basicGetFirstChild() == castedExisting) {
        existingParent.basicSetFirstChild(newComposite);
    }
    AbstractNode existingNextSibling = castedExisting.basicGetNextSibling();
    if (existingNextSibling == castedExisting) {
        newComposite.basicSetNextSibling(newComposite);
        newComposite.basicSetPreviousSibling(newComposite);
    } else {
        newComposite.basicSetNextSibling(existingNextSibling);
        existingNextSibling.basicSetPreviousSibling(newComposite);
        AbstractNode existingPreviousSibling = castedExisting.basicGetPreviousSibling();
        newComposite.basicSetPreviousSibling(existingPreviousSibling);
        existingPreviousSibling.basicSetNextSibling(newComposite);
    }
    newComposite.basicSetFirstChild(castedExisting);
    initializeFirstChildInvariant(newComposite, castedExisting);
    return compressAndReturnParent(existing);
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 69 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.

the class NodeModelBuilder method addChild.

public void addChild(ICompositeNode node, AbstractNode child) {
    checkValidNewChild(child);
    CompositeNode composite = (CompositeNode) node;
    AbstractNode firstChild = composite.basicGetFirstChild();
    if (firstChild == null) {
        composite.basicSetFirstChild(child);
        initializeFirstChildInvariant(composite, child);
    } else {
        child.basicSetParent(composite);
        AbstractNode nodePreviousSibling = firstChild.basicGetPreviousSibling();
        child.basicSetPreviousSibling(nodePreviousSibling);
        child.basicSetNextSibling(firstChild);
        if (nodePreviousSibling != null) {
            nodePreviousSibling.basicSetNextSibling(child);
        }
        firstChild.basicSetPreviousSibling(child);
    }
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 70 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-core by eclipse.

the class NodeModelBuilder method replaceAndTransferLookAhead.

public void replaceAndTransferLookAhead(INode oldNode, INode newRootNode) {
    AbstractNode newNode = ((CompositeNode) newRootNode).basicGetFirstChild();
    replaceWithoutChildren((AbstractNode) oldNode, newNode);
    if (oldNode instanceof ICompositeNode && newNode instanceof CompositeNode) {
        CompositeNode newCompositeNode = (CompositeNode) newNode;
        newCompositeNode.basicSetLookAhead(((ICompositeNode) oldNode).getLookAhead());
    }
    ICompositeNode root = newNode.getRootNode();
    BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) root).basicIterator();
    int offset = 0;
    while (iterator.hasNext()) {
        AbstractNode node = iterator.next();
        if (node instanceof LeafNode) {
            ((LeafNode) node).basicSetTotalOffset(offset);
            offset += node.getTotalLength();
        }
    }
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

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