Search in sources :

Example 6 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-eclipse by eclipse.

the class JavaElementDelegate method findJavaElement.

protected IJavaElement findJavaElement(XtextResource resource, int offset) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult == null)
        return null;
    INode root = parseResult.getRootNode();
    INode node = NodeModelUtils.findLeafNodeAtOffset(root, offset);
    if (node == null)
        return null;
    INode previousSementic = null, nextSemantic = null;
    NodeIterator backwards = new NodeIterator(node);
    while (backwards.hasPrevious()) {
        INode n = backwards.previous();
        if (n instanceof ILeafNode && !((ILeafNode) n).isHidden()) {
            previousSementic = n;
            break;
        }
    }
    NodeIterator forward = new NodeIterator(node);
    while (forward.hasNext()) {
        INode n = forward.next();
        if (n instanceof ILeafNode && !((ILeafNode) n).isHidden()) {
            nextSemantic = n;
            break;
        }
    }
    if (previousSementic == null || nextSemantic == null)
        return null;
    EObject prevObj = NodeModelUtils.findActualSemanticObjectFor(previousSementic);
    EObject nextObj = NodeModelUtils.findActualSemanticObjectFor(nextSemantic);
    if (prevObj == null || nextObj == null)
        return null;
    EObject element = findCommonContainer(prevObj, nextObj);
    JvmIdentifiableElement jvmElement = findAssociatedJvmElement(element);
    if (jvmElement == null)
        return null;
    IJavaElement javaElement = elementFinder.findElementFor(jvmElement);
    return javaElement;
}
Also used : NodeIterator(org.eclipse.xtext.parsetree.reconstr.impl.NodeIterator) IJavaElement(org.eclipse.jdt.core.IJavaElement) INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 7 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-xtend by eclipse.

the class CustomXtendParser method parse.

@Override
public IParseResult parse(RuleCall ruleCall, Reader reader, int initialLookAhead) {
    NodeModelBuilder builder = createNodeModelBuilder();
    builder.setForcedFirstGrammarElement(ruleCall);
    IParseResult parseResult = doParse(ruleCall.getRule().getName(), new ReaderCharStream(reader), builder, initialLookAhead);
    return parseResult;
}
Also used : NodeModelBuilder(org.eclipse.xtext.nodemodel.impl.NodeModelBuilder) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 8 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-xtend by eclipse.

the class XtendBatchCompiler method getXtendFile.

/* @Nullable */
protected XtendFile getXtendFile(Resource resource) {
    XtextResource xtextResource = (XtextResource) resource;
    IParseResult parseResult = xtextResource.getParseResult();
    if (parseResult != null) {
        EObject model = parseResult.getRootASTElement();
        if (model instanceof XtendFile) {
            XtendFile xtendFile = (XtendFile) model;
            return xtendFile;
        }
    }
    return null;
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 9 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class GrammarResource method doLinking.

/**
 * Overridden to do only the clean-part of the linking but not
 * the actual linking. This is deferred until someone wants to access
 * the content of the resource.
 */
@Override
protected void doLinking() {
    IParseResult parseResult = getParseResult();
    if (parseResult == null || parseResult.getRootASTElement() == null)
        return;
    XtextLinker castedLinker = (XtextLinker) getLinker();
    castedLinker.discardGeneratedPackages(parseResult.getRootASTElement());
}
Also used : IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 10 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class XtextLinkingService method getUsedGrammar.

private List<EObject> getUsedGrammar(Grammar grammar, INode node) {
    try {
        String grammarName = (String) valueConverterService.toValue("", "GrammarID", node);
        if (grammarName != null) {
            final ResourceSet resourceSet = grammar.eResource().getResourceSet();
            List<Resource> resources = resourceSet.getResources();
            for (int i = 0; i < resources.size(); i++) {
                Resource resource = resources.get(i);
                EObject rootElement = null;
                if (resource instanceof XtextResource) {
                    IParseResult parseResult = ((XtextResource) resource).getParseResult();
                    if (parseResult != null)
                        rootElement = parseResult.getRootASTElement();
                } else if (!resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof Grammar) {
                    Grammar otherGrammar = (Grammar) rootElement;
                    if (grammarName.equals(otherGrammar.getName())) {
                        if (resource instanceof DerivedStateAwareResource)
                            resource.getContents();
                        return Collections.<EObject>singletonList(otherGrammar);
                    }
                }
            }
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + "." + fileExtension);
            URI normalizedURI = null;
            if (resourceSet instanceof XtextResourceSet) {
                XtextResourceSet set = (XtextResourceSet) resourceSet;
                normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
            } else {
                normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
            }
            final Resource resource = resourceSet.getResource(normalizedURI, true);
            if (!resource.getContents().isEmpty()) {
                final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
                if (grammarName.equals(usedGrammar.getName()))
                    return Collections.<EObject>singletonList(usedGrammar);
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        log.debug("Cannot load used grammar.", e);
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        log.debug("Cannot load used grammar.", e);
        return Collections.emptyList();
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Grammar(org.eclipse.xtext.Grammar) URI(org.eclipse.emf.common.util.URI) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Aggregations

IParseResult (org.eclipse.xtext.parser.IParseResult)60 EObject (org.eclipse.emf.ecore.EObject)22 StringReader (java.io.StringReader)16 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)14 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)10 INode (org.eclipse.xtext.nodemodel.INode)10 XtextResource (org.eclipse.xtext.resource.XtextResource)10 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)9 ParserRule (org.eclipse.xtext.ParserRule)8 Test (org.junit.Test)7 ITextRegion (org.eclipse.xtext.util.ITextRegion)6 Resource (org.eclipse.emf.ecore.resource.Resource)4 TextRegion (org.eclipse.xtext.util.TextRegion)4 URI (org.eclipse.emf.common.util.URI)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 ParseResult (org.eclipse.xtext.parser.ParseResult)3 Scope (org.yakindu.sct.model.sgraph.Scope)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2