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