Java Code Examples for org.eclipse.xtext.parser.IParseResult#getRootASTElement()
The following examples show how to use
org.eclipse.xtext.parser.IParseResult#getRootASTElement() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractContextualAntlrParser.java From dsl-devkit with Eclipse Public License 1.0 | 6 votes |
/** {@inheritDoc} */ @Override protected IParseResult doParse(final String ruleName, final CharStream in, final NodeModelBuilder nodeModelBuilder, final int initialLookAhead) { final IParseResult parseResult = super.doParse(ruleName, in, nodeModelBuilder, initialLookAhead); if (delegate == null || parseResult.hasSyntaxErrors()) { return parseResult; } // If delegation was potentially used, we need to check for syntax errors in replaced nodes boolean hasError = false; Iterator<AbstractNode> nodeIterator = ((CompositeNode) parseResult.getRootNode()).basicIterator(); while (nodeIterator.hasNext()) { AbstractNode node = nodeIterator.next(); if (node.getSyntaxErrorMessage() != null) { hasError = true; break; } } if (hasError) { return new ParseResult(parseResult.getRootASTElement(), parseResult.getRootNode(), true); } return parseResult; }
Example 2
Source File: N4LanguageUtils.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Parses the given string with the parser of the Xtext language denoted by the given file extension. In case of * syntax errors, the returned parse result will have a non-empty list of {@link ParseResult#errors}. */ public static <T extends EObject> ParseResult<T> parseXtextLanguage(String fileExtOfLanguage, ParserRule parserRuleOrNull, Class<T> expectedTypeOfRoot, Reader sourceReader) { final IParser parser = getServiceForContext(fileExtOfLanguage, IParser.class) .orElseThrow(() -> new RuntimeException( "Cannot obtain Xtext parser for language with file extension: " + fileExtOfLanguage)); final IParseResult result; if (parserRuleOrNull != null) { result = parser.parse(parserRuleOrNull, sourceReader); } else { result = parser.parse(sourceReader); } final Iterable<SyntaxErrorMessage> errors = Iterables.transform(result.getSyntaxErrors(), node -> node.getSyntaxErrorMessage()); final EObject root = result.getRootASTElement(); if (root != null && expectedTypeOfRoot.isInstance(root)) { final T rootCasted = expectedTypeOfRoot.cast(root); return new ParseResult<>(rootCasted, errors); } return new ParseResult<>(null, errors); }
Example 3
Source File: N4JSResource.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Overridden to make sure that we add the root AST element sneakily to the resource list to make sure that no * accidental proxy resolution happens and that we do not increment the modification counter of the contents list. */ @Override protected void updateInternalState(IParseResult newParseResult) { setParseResult(newParseResult); EObject newRootAstElement = newParseResult.getRootASTElement(); if (newRootAstElement != null && !getContents().contains(newRootAstElement)) { // do not increment the modification counter here sneakyAddToContent(newRootAstElement); } reattachModificationTracker(newRootAstElement); clearErrorsAndWarnings(); addSyntaxErrors(); doLinking(); // make sure that the cache adapter is installed on this resource IResourceScopeCache cache = getCache(); if (cache instanceof OnChangeEvictingCache) { ((OnChangeEvictingCache) cache).getOrCreate(this); } }
Example 4
Source File: XbaseHighlightingCalculator.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@Override public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) { if (resource == null) return; IParseResult parseResult = resource.getParseResult(); if (parseResult == null || parseResult.getRootASTElement() == null) return; if (highlightedIdentifiers == null) { highlightedIdentifiers = initializeHighlightedIdentifiers(); idLengthsToHighlight = new BitSet(); for (String s : highlightedIdentifiers.keySet()) { idLengthsToHighlight.set(s.length()); } } //TODO remove this check when the typesystem works without a java project if (resource.isValidationDisabled()) { highlightSpecialIdentifiers(acceptor, parseResult.getRootNode()); return; } doProvideHighlightingFor(resource, acceptor, cancelIndicator); }
Example 5
Source File: StextResource.java From statecharts with Eclipse Public License 1.0 | 6 votes |
protected void parseStatechart(Statechart statechart) { IParseResult parseResult = parse(statechart, StatechartSpecification.class.getSimpleName()); StatechartSpecification rootASTElement = (StatechartSpecification) parseResult.getRootASTElement(); statechart.setNamespace(rootASTElement.getNamespace()); statechart.getScopes().clear(); EList<Scope> definitionScopes = rootASTElement.getScopes(); if (definitionScopes != null) { statechart.getScopes().addAll(definitionScopes); } statechart.getAnnotations().clear(); EList<Annotation> annotations = rootASTElement.getAnnotations(); if (annotations != null) { statechart.getAnnotations().addAll(annotations); } }
Example 6
Source File: XtendJavaElementDelegateJunitLaunch.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
/** * @param resource the Xtext resource to parse * @return if the resource contains exactly one class, returns the IJavaElement associated with that class, * otherwise returns null */ private IJavaElement getXtendClass(XtextResource resource) { IParseResult parseResult = resource.getParseResult(); if (parseResult == null) { return null; } EObject root = parseResult.getRootASTElement(); if (root instanceof XtendFile) { XtendFile xtendFile = (XtendFile) root; EList<XtendTypeDeclaration> xtendTypes = xtendFile.getXtendTypes(); if (xtendTypes.size() == 1) { XtendTypeDeclaration element = xtendTypes.get(0); JvmIdentifiableElement jvmElement = findAssociatedJvmElement(element); if (jvmElement == null) { return null; } IJavaElement javaElement = elementFinder.findElementFor(jvmElement); return javaElement; } } return null; }
Example 7
Source File: DotFoldingRegionProvider.java From gef with Eclipse Public License 2.0 | 6 votes |
protected void computeObjectFolding(XtextResource xtextResource, IFoldingRegionAcceptor<ITextRegion> foldingRegionAcceptor) { acceptedRegions.clear(); IParseResult parseResult = xtextResource.getParseResult(); if (parseResult != null) { EObject rootASTElement = parseResult.getRootASTElement(); if (rootASTElement != null) { TreeIterator<EObject> allContents = rootASTElement .eAllContents(); while (allContents.hasNext()) { EObject eObject = allContents.next(); if (isHandled(eObject)) { computeObjectFolding(eObject, foldingRegionAcceptor); } if (eObject instanceof Attribute) { computeDotAttributeValueFolding((Attribute) eObject, foldingRegionAcceptor); } if (!shouldProcessContent(eObject)) { allContents.prune(); } } } } }
Example 8
Source File: StextResource.java From statecharts with Eclipse Public License 1.0 | 5 votes |
protected void parseTransition(Transition transition) { IParseResult parseResult = parse(transition, TransitionSpecification.class.getSimpleName()); TransitionSpecification rootASTElement = (TransitionSpecification) parseResult.getRootASTElement(); transition.getProperties().clear(); if (rootASTElement.getReaction() != null) { TransitionReaction reaction = rootASTElement.getReaction(); transition.setEffect(reaction.getEffect()); transition.setTrigger(reaction.getTrigger()); transition.getProperties().addAll(reaction.getProperties()); } }
Example 9
Source File: XtextResource.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
/** * @param oldParseResult the previous parse result that should be detached if necessary. * @param newParseResult the current parse result that should be attached to the content of this resource * @since 2.1 */ protected void updateInternalState(IParseResult oldParseResult, IParseResult newParseResult) { if (oldParseResult != null && oldParseResult.getRootASTElement() != null && oldParseResult.getRootASTElement() != newParseResult.getRootASTElement()) { EObject oldRootAstElement = oldParseResult.getRootASTElement(); if (oldRootAstElement != newParseResult.getRootASTElement()) { unload(oldRootAstElement); getContents().remove(oldRootAstElement); } } updateInternalState(newParseResult); }
Example 10
Source File: DerivedStateAwareResource.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
/** * {@inheritDoc} * <p> * Overridden to make sure that we do not initialize a resource * just to compute the root URI fragment for the parse result. */ @Override protected String getURIFragmentRootSegment(EObject eObject) { if (unloadingContents == null) { IParseResult parseResult = getParseResult(); if (parseResult != null && eObject == parseResult.getRootASTElement()) { return "0"; } } return super.getURIFragmentRootSegment(eObject); }
Example 11
Source File: GrammarResource.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
/** * 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()); }
Example 12
Source File: XtendBatchCompiler.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
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; }
Example 13
Source File: DotHoverUtils.java From gef with Eclipse Public License 2.0 | 5 votes |
static private Color parse(String attributeValue) { Injector dotColorInjector = DotActivator.getInstance().getInjector( DotActivator.ORG_ECLIPSE_GEF_DOT_INTERNAL_LANGUAGE_DOTCOLOR); IParser parser = dotColorInjector.getInstance(IParser.class); IParseResult result = parser.parse(new StringReader(attributeValue)); return (Color) result.getRootASTElement(); }
Example 14
Source File: DotHTMLLabelJavaFxNode.java From gef with Eclipse Public License 2.0 | 5 votes |
private HtmlLabel parseLabel(final String label) { Injector labelInjector = DotActivator.getInstance().getInjector( DotActivator.ORG_ECLIPSE_GEF_DOT_INTERNAL_LANGUAGE_DOTHTMLLABEL); DotHtmlLabelParser parser = labelInjector .getInstance(DotHtmlLabelParser.class); IParseResult result = parser .parse(new StringReader(label != null ? label : new String())); return (HtmlLabel) result.getRootASTElement(); }
Example 15
Source File: DefaultSemanticHighlightingCalculator.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected void searchAndHighlightElements(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) { IParseResult parseResult = resource.getParseResult(); if (parseResult == null) throw new IllegalStateException("resource#parseResult may not be null"); EObject element = parseResult.getRootASTElement(); highlightElementRecursively(element, acceptor, cancelIndicator); }
Example 16
Source File: AbstractContextualAntlrParser.java From dsl-devkit with Eclipse Public License 1.0 | 5 votes |
/** {@inheritDoc} */ @Override public IParseResult doParse(final Reader reader) { IParseResult parseResult = super.doParse(reader); if (parseResult.getRootASTElement() == null) { // Most of ASMD languages are not good with empty models, so create an empty root element AbstractRule rule = GrammarUtil.findRuleForName(grammarAccess.getGrammar(), getDefaultRuleName()); EObject newRoot = getElementFactory().create(rule.getType().getClassifier()); return new ParseResult(newRoot, parseResult.getRootNode(), true); } return parseResult; }
Example 17
Source File: StextResource.java From statecharts with Eclipse Public License 1.0 | 5 votes |
protected void parseState(State state) { IParseResult parseResult = parse(state, StateSpecification.class.getSimpleName()); StateSpecification rootASTElement = (StateSpecification) parseResult.getRootASTElement(); state.getScopes().clear(); if (rootASTElement.getScope() != null) { state.getScopes().add(rootASTElement.getScope()); } }
Example 18
Source File: STextExpressionParser.java From statecharts with Eclipse Public License 1.0 | 5 votes |
public EObject parseExpression(String expression, String ruleName, String specification) { StextResource resource = getResource(); resource.setURI(URI.createPlatformResourceURI(getUri(), true)); ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule(); parserRule.setName(ruleName); IParseResult result = parser.parse(parserRule, new StringReader(expression)); EObject rootASTElement = result.getRootASTElement(); resource.getContents().add(rootASTElement); ListBasedDiagnosticConsumer diagnosticsConsumer = new ListBasedDiagnosticConsumer(); Statechart sc = SGraphFactory.eINSTANCE.createStatechart(); sc.setDomainID(domainId); sc.setName("sc"); if (specification != null) { sc.setSpecification(specification); } resource.getContents().add(sc); resource.getLinkingDiagnostics().clear(); linker.linkModel(sc, diagnosticsConsumer); linker.linkModel(rootASTElement, diagnosticsConsumer); resource.resolveLazyCrossReferences(CancelIndicator.NullImpl); resource.resolveLazyCrossReferences(CancelIndicator.NullImpl); Multimap<SpecificationElement, Diagnostic> diagnostics = resource.getLinkingDiagnostics(); if (diagnostics.size() > 0) { throw new LinkingException(diagnostics.toString()); } if (result.hasSyntaxErrors()) { StringBuilder errorMessages = new StringBuilder(); Iterable<INode> syntaxErrors = result.getSyntaxErrors(); for (INode iNode : syntaxErrors) { errorMessages.append(iNode.getSyntaxErrorMessage()); errorMessages.append("\n"); } throw new SyntaxException("Could not parse expression, syntax errors: " + errorMessages); } if (diagnosticsConsumer.hasConsumedDiagnostics(Severity.ERROR)) { throw new LinkingException("Error during linking: " + diagnosticsConsumer.getResult(Severity.ERROR)); } return rootASTElement; }
Example 19
Source File: AbstractSGenTest.java From statecharts with Eclipse Public License 1.0 | 5 votes |
protected EObject parseExpression(String expression, String ruleName) { XtextResource resource = resourceProvider.get(); resource.setURI(URI.createPlatformPluginURI("path", true)); ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule(); parserRule.setName(ruleName); IParseResult result = parser.parse(parserRule, new StringReader( expression)); EObject rootASTElement = result.getRootASTElement(); resource.getContents().add(rootASTElement); ListBasedDiagnosticConsumer diagnosticsConsumer = new ListBasedDiagnosticConsumer(); linker.linkModel(result.getRootASTElement(), diagnosticsConsumer); if (result.hasSyntaxErrors()) { StringBuilder errorMessages = new StringBuilder(); Iterable<INode> syntaxErrors = result.getSyntaxErrors(); for (INode iNode : syntaxErrors) { errorMessages.append(iNode.getSyntaxErrorMessage()); errorMessages.append("\n"); } throw new RuntimeException( "Could not parse expression, syntax errors: " + errorMessages); } if (diagnosticsConsumer.hasConsumedDiagnostics(Severity.ERROR)) { throw new RuntimeException("Error during linking: " + diagnosticsConsumer.getResult(Severity.ERROR)); } return rootASTElement; }
Example 20
Source File: STextExpressionParser.java From statecharts with Eclipse Public License 1.0 | 4 votes |
public Scope createInterfaceScope(String contextScope) { ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule(); parserRule.setName(InterfaceScope.class.getSimpleName()); IParseResult result = parser.parse(parserRule, new StringReader(contextScope)); return (Scope) result.getRootASTElement(); }