Search in sources :

Example 41 with IParseResult

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

the class XtextResource method doLoad.

@Override
protected void doLoad(InputStream inputStream, Map<?, ?> options) throws IOException {
    setEncodingFromOptions(options);
    IParseResult result;
    if (entryPoint == null) {
        result = parser.parse(createReader(inputStream));
    } else {
        result = parser.parse(entryPoint, createReader(inputStream));
    }
    updateInternalState(this.parseResult, result);
}
Also used : IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 42 with IParseResult

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

the class XtextResource method update.

public void update(int offset, int replacedTextLength, String newText) {
    if (!isLoaded()) {
        throw new IllegalStateException("You can't update an unloaded resource.");
    }
    try {
        isUpdating = true;
        IParseResult oldParseResult = parseResult;
        ReplaceRegion replaceRegion = new ReplaceRegion(new TextRegion(offset, replacedTextLength), newText);
        IParseResult newParseResult;
        ParserRule oldEntryPoint = NodeModelUtils.getEntryParserRule(oldParseResult.getRootNode());
        if (entryPoint == null || entryPoint == oldEntryPoint) {
            newParseResult = parser.reparse(oldParseResult, replaceRegion);
        } else {
            StringBuilder builder = new StringBuilder(oldParseResult.getRootNode().getText());
            replaceRegion.applyTo(builder);
            newParseResult = parser.parse(entryPoint, new StringReader(builder.toString()));
        }
        updateInternalState(oldParseResult, newParseResult);
    } finally {
        isUpdating = false;
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) TextRegion(org.eclipse.xtext.util.TextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) StringReader(java.io.StringReader) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 43 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 44 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 45 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)

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