Search in sources :

Example 51 with IParseResult

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

the class PartialParserTest method testInsertSlashInSecondNode.

@Test
public void testInsertSlashInSecondNode() throws Exception {
    IParseResult parseResult = resource.getParser().reparse(this.parseResult, new ReplaceRegion(model.indexOf('6'), 0, "/"));
    assertFalse(parseResult.hasSyntaxErrors());
    assertEquals(0, parseResult.getRootNode().getTotalOffset());
    assertSame(resource.getParseResult().getRootNode(), parseResult.getRootNode());
    String newModel = "a.b.c.d: 123;\n" + "e.f.g.h: 45/6;";
    EmfAssert.assertEObjectsEqual(getModel(newModel), resource.getParseResult().getRootASTElement());
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) IParseResult(org.eclipse.xtext.parser.IParseResult) Test(org.junit.Test)

Example 52 with IParseResult

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

the class AbstractPackratParser method parse.

protected final IParseResult parse(INonTerminalConsumer consumer) {
    if (activeMarker != null)
        throw new IllegalStateException("cannot parse now. Active marker is already assigned.");
    IMarker rootMarker = mark();
    IRootConsumerListener listener = new RootConsumerListener();
    try {
        consumer.consumeAsRoot(listener);
        IParseResult result = getParseResultFactory().createParseResult(activeMarker, input);
        rootMarker.commit();
        if (activeMarker != null)
            throw new IllegalStateException("cannot finish parse: active marker is still present.");
        return result;
    } catch (Exception e) {
        throw new WrappedException(e);
    }
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) IRootConsumerListener(org.eclipse.xtext.parser.packrat.consumers.IRootConsumerListener) IRootConsumerListener(org.eclipse.xtext.parser.packrat.consumers.IRootConsumerListener) IParseResult(org.eclipse.xtext.parser.IParseResult) IOException(java.io.IOException) WrappedException(org.eclipse.emf.common.util.WrappedException)

Example 53 with IParseResult

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

the class MultiLineJavaDocTypeReferenceProvider method computeEObjectReferencedInComment.

/**
 * {@inheritDoc}
 */
@Override
public EObjectInComment computeEObjectReferencedInComment(XtextResource resource, int offset) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult != null) {
        INode rootNode = parseResult.getRootNode();
        INode node = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
        EObject semanticObject = NodeModelUtils.findActualSemanticObjectFor(node);
        if (semanticObject != null) {
            EReference reference = getEReference(semanticObject, node, offset);
            if (reference != null) {
                IScope scope = getScope(semanticObject, reference, node, offset);
                List<ReplaceRegion> eObjectReferences = computeTypeRefRegions(node);
                for (ReplaceRegion eObjectReference : eObjectReferences) {
                    if (eObjectReference.getOffset() <= offset && offset <= eObjectReference.getEndOffset()) {
                        String eObjectReferenceText = eObjectReference.getText();
                        if (!Strings.isNullOrEmpty(eObjectReferenceText)) {
                            ITextRegion region = new TextRegion(eObjectReference.getOffset(), eObjectReference.getLength());
                            IEObjectDescription candidate = getElementFromScope(scope, node, region, eObjectReferenceText);
                            if (candidate != null) {
                                EObject eObject = candidate.getEObjectOrProxy();
                                if (eObject != null) {
                                    return new EObjectInComment(eObject, region);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObjectInComment(org.eclipse.xtext.documentation.EObjectInComment) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) IParseResult(org.eclipse.xtext.parser.IParseResult) EReference(org.eclipse.emf.ecore.EReference) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 54 with IParseResult

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

the class Bug419429Test method replaceAndReparse.

protected void replaceAndReparse(String model, int offset, int length, String change, String expectedReparseRegion) throws Exception {
    IParseResult parseResult = getParseResultAndExpect(model, UNKNOWN_EXPECTATION);
    PartialParsingPointers parsingPointers = getPartialParser().calculatePartialParsingPointers(parseResult, offset, length);
    String reparseRegion = getPartialParser().insertChangeIntoReplaceRegion(parsingPointers.getDefaultReplaceRootNode(), new ReplaceRegion(offset, length, change));
    assertEquals(expectedReparseRegion, reparseRegion);
    final Wrapper<Boolean> unloaded = Wrapper.wrap(Boolean.FALSE);
    getPartialParser().setUnloader(new IReferableElementsUnloader() {

        @Override
        public void unloadRoot(EObject root) {
            unloaded.set(Boolean.TRUE);
        }
    });
    IParseResult partiallyReparse = reparse(parseResult, offset, length, change);
    assertTrue("unloaded", unloaded.get());
    String expectedReparseModel = model.substring(0, offset) + change + model.substring(offset + length);
    assertEquals(expectedReparseModel, partiallyReparse.getRootNode().getText());
    compareWithFullParse(model, offset, length, change);
}
Also used : IReferableElementsUnloader(org.eclipse.xtext.parser.antlr.IReferableElementsUnloader) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult) PartialParsingPointers(org.eclipse.xtext.parser.impl.PartialParsingPointers)

Example 55 with IParseResult

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

the class DefaultTaskFinder method findTasks.

@Override
public List<Task> findTasks(Resource resource) {
    TaskTags taskTags = taskTagProvider.getTaskTags(resource);
    if (isEmpty(taskTags) || !(resource instanceof XtextResource)) {
        return emptyList();
    }
    IParseResult parseResult = ((XtextResource) resource).getParseResult();
    if (parseResult == null || parseResult.getRootNode() == null) {
        return emptyList();
    }
    List<Task> tasks = findTasks(parseResult.getRootNode(), taskTags);
    return unmodifiableList(tasks);
}
Also used : XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult)

Aggregations

IParseResult (org.eclipse.xtext.parser.IParseResult)70 EObject (org.eclipse.emf.ecore.EObject)27 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)19 StringReader (java.io.StringReader)16 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)13 XtextResource (org.eclipse.xtext.resource.XtextResource)13 INode (org.eclipse.xtext.nodemodel.INode)11 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)10 ParserRule (org.eclipse.xtext.ParserRule)8 ITextRegion (org.eclipse.xtext.util.ITextRegion)7 Test (org.junit.Test)7 TextRegion (org.eclipse.xtext.util.TextRegion)5 Resource (org.eclipse.emf.ecore.resource.Resource)4 ArrayList (java.util.ArrayList)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 List (java.util.List)2 URI (org.eclipse.emf.common.util.URI)2