Search in sources :

Example 1 with NullTreeBuilder

use of org.spoofax.jsglr.client.NullTreeBuilder in project spoofax by metaborg.

the class JSGLR1I method parse.

public ParseContrib parse(@Nullable JSGLRParserConfiguration parserConfig) throws IOException {
    if (parserConfig == null) {
        parserConfig = new JSGLRParserConfiguration();
    }
    final String fileName = resource != null ? resource.getName().getURI() : null;
    final JSGLRParseErrorHandler errorHandler = new JSGLRParseErrorHandler(this, resource, getParseTable(config.getParseTableProvider()).hasRecovers());
    final Timer timer = new Timer(true);
    SGLRParseResult result;
    try {
        // should throw a fatal, or return a non-null result
        result = actuallyParse(input, fileName, parserConfig);
        assert result != null;
    } catch (SGLRException | InterruptedException e) {
        result = null;
        errorHandler.setRecoveryFailed(parserConfig.recovery);
        errorHandler.processFatalException(new NullTokenizer(input, fileName), e);
    }
    final long duration = timer.stop();
    final IStrategoTerm ast;
    if (result != null) {
        // No fatals occurred, so either parsing succeeded or recovery succeeded
        ast = (IStrategoTerm) result.output;
        if (ast == null) {
            // so we have nothing to do
            assert parser.getTreeBuilder() instanceof NullTreeBuilder;
        } else {
            // in case recovery was required, collect the recoverable errors
            errorHandler.setRecoveryFailed(false);
            errorHandler.gatherNonFatalErrors(ast);
            if (resource != null) {
                SourceAttachment.putSource(ast, resource);
            }
        }
    } else {
        ast = null;
    }
    final boolean hasAst = ast != null;
    final Iterable<IMessage> messages = errorHandler.messages();
    final boolean hasErrors = MessageUtils.containsSeverity(messages, MessageSeverity.ERROR);
    return new ParseContrib(hasAst, hasAst && !hasErrors, ast, messages, duration);
}
Also used : ParseContrib(org.metaborg.spoofax.core.unit.ParseContrib) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IMessage(org.metaborg.core.messages.IMessage) NullTokenizer(org.spoofax.jsglr.client.imploder.NullTokenizer) SGLRParseResult(org.spoofax.jsglr.client.SGLRParseResult) Timer(org.metaborg.util.time.Timer) NullTreeBuilder(org.spoofax.jsglr.client.NullTreeBuilder) SGLRException(org.spoofax.jsglr.shared.SGLRException)

Aggregations

IMessage (org.metaborg.core.messages.IMessage)1 ParseContrib (org.metaborg.spoofax.core.unit.ParseContrib)1 Timer (org.metaborg.util.time.Timer)1 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)1 NullTreeBuilder (org.spoofax.jsglr.client.NullTreeBuilder)1 SGLRParseResult (org.spoofax.jsglr.client.SGLRParseResult)1 NullTokenizer (org.spoofax.jsglr.client.imploder.NullTokenizer)1 SGLRException (org.spoofax.jsglr.shared.SGLRException)1