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);
}
}
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;
}
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);
}
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);
}
}
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();
}
}
}
Aggregations