Search in sources :

Example 76 with ICompositeNode

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

the class AbstractParseTreeConstructor method serializeSubtree.

@Override
public TreeConstructionReport serializeSubtree(EObject object, ITokenStream out) throws IOException {
    TreeConstructionReportImpl report = createReport(object);
    AbstractToken root = serialize(object, report);
    Set<ICompositeNode> roots = Sets.newHashSet();
    Map<EObject, AbstractToken> obj2token = Maps.newHashMap();
    collectRootsAndEObjects(root, obj2token, roots);
    // dump("", root);
    Map<ILeafNode, EObject> comments = commentAssociater.associateCommentsWithSemanticEObjects(object, roots);
    for (ICompositeNode r : roots) assignNodesByMatching(obj2token, r, comments);
    WsMergerStream wsout = new WsMergerStream(out);
    // dump("", root);
    // System.out.println(EmfFormatter.objToStr(roots.iterator().next(),
    // ParsetreePackage.Literals.ABSTRACT_NODE__TOTAL_LENGTH,
    // ParsetreePackage.Literals.ABSTRACT_NODE__TOTAL_OFFSET,
    // ParsetreePackage.Literals.ABSTRACT_NODE__TOTAL_LINE, ParsetreePackage.Literals.ABSTRACT_NODE__PARENT));
    ITextRegion previousLocation = ITextRegion.EMPTY_REGION;
    initStream(root, wsout);
    previousLocation = write(root, wsout, previousLocation);
    wsout.flush();
    report.setPreviousLocation(previousLocation);
    return report;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 77 with ICompositeNode

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

the class NodeIterator method findNextSibling.

protected INode findNextSibling(INode node) {
    ICompositeNode parent = node.getParent();
    if (parent == null) {
        return null;
    }
    INode successor = node.getNextSibling();
    if (successor != null) {
        return successor;
    }
    return findNextSibling(parent);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 78 with ICompositeNode

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

the class DefaultLocationInFileProvider method getLocationNodes.

protected List<INode> getLocationNodes(EObject obj) {
    final EStructuralFeature nameFeature = getIdentifierFeature(obj);
    if (nameFeature != null) {
        List<INode> result = NodeModelUtils.findNodesForFeature(obj, nameFeature);
        if (!result.isEmpty())
            return result;
    }
    List<INode> resultNodes = Lists.newArrayList();
    final ICompositeNode startNode = findNodeFor(obj);
    INode keywordNode = null;
    // use LeafNodes instead of children?
    for (INode child : startNode.getChildren()) {
        EObject grammarElement = child.getGrammarElement();
        if (grammarElement instanceof RuleCall) {
            RuleCall ruleCall = (RuleCall) grammarElement;
            String ruleName = ruleCall.getRule().getName();
            if (ruleName.equals("ID")) {
                resultNodes.add(child);
            }
        } else if (grammarElement instanceof Keyword) {
            // TODO use only keywords, that aren't symbols like '=' ?
            if (keywordNode == null && useKeyword((Keyword) grammarElement, obj)) {
                keywordNode = child;
            }
        }
    }
    if (resultNodes.isEmpty() && keywordNode != null)
        resultNodes.add(keywordNode);
    return resultNodes;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Keyword(org.eclipse.xtext.Keyword) EObject(org.eclipse.emf.ecore.EObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) RuleCall(org.eclipse.xtext.RuleCall)

Example 79 with ICompositeNode

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

the class Linker method ensureLinked.

public void ensureLinked(EObject obj, IDiagnosticProducer producer) {
    ICompositeNode node = NodeModelUtils.getNode(obj);
    if (node == null)
        return;
    Set<EReference> handledReferences = new HashSet<EReference>();
    ensureLinked(obj, producer, node, handledReferences, false);
    producer.setNode(node);
    setDefaultValues(obj, handledReferences, producer);
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) EReference(org.eclipse.emf.ecore.EReference) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 80 with ICompositeNode

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

the class Linker method ensureLinked.

private void ensureLinked(EObject obj, IDiagnosticProducer producer, ICompositeNode node, Set<EReference> handledReferences, boolean dontCheckParent) {
    for (INode abstractNode = node.getFirstChild(); abstractNode != null; abstractNode = abstractNode.getNextSibling()) {
        EObject grammarElement = abstractNode.getGrammarElement();
        if (grammarElement instanceof CrossReference) {
            CrossReference ref = (CrossReference) grammarElement;
            producer.setNode(abstractNode);
            ensureIsLinked(obj, abstractNode, ref, handledReferences, producer);
        } else if (grammarElement instanceof RuleCall && abstractNode instanceof ICompositeNode) {
            RuleCall ruleCall = (RuleCall) grammarElement;
            AbstractRule calledRule = ruleCall.getRule();
            if (calledRule instanceof ParserRule && ((ParserRule) calledRule).isFragment()) {
                ensureLinked(obj, producer, (ICompositeNode) abstractNode, handledReferences, true);
            }
        }
    }
    if (!dontCheckParent && shouldCheckParentNode(node)) {
        ensureLinked(obj, producer, node.getParent(), handledReferences, false);
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) INode(org.eclipse.xtext.nodemodel.INode) EObject(org.eclipse.emf.ecore.EObject) CrossReference(org.eclipse.xtext.CrossReference) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

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