Search in sources :

Example 1 with ParseContrib

use of org.metaborg.spoofax.core.unit.ParseContrib in project spoofax by metaborg.

the class JSGLRParseService method parse.

@Override
public ISpoofaxParseUnit parse(ISpoofaxInputUnit input, IProgress progress, ICancel cancel) throws ParseException {
    final FileObject source = input.source();
    final ILanguageImpl langImpl;
    final ILanguageImpl base;
    if (input.dialect() != null) {
        langImpl = input.dialect();
        base = input.langImpl();
    } else {
        langImpl = input.langImpl();
        base = null;
    }
    final String text = input.text();
    final ITermFactory termFactory = termFactoryService.get(langImpl, null, false);
    final IParserConfig config;
    JSGLRParserConfiguration parserConfig = input.config();
    if (parserConfig == null) {
        parserConfig = defaultParserConfig;
    }
    if (parserConfig.completion) {
        config = getCompletionParserConfig(langImpl, input);
    } else {
        config = getParserConfig(langImpl, input);
    }
    try {
        logger.trace("Parsing {}", source);
        JSGLRVersion version = jsglrVersion(input);
        final JSGLRI<?> parser;
        boolean dataDependentParsing = false;
        for (ILanguageComponent component : langImpl.components()) {
            if (component.config().dataDependent()) {
                dataDependentParsing = true;
            }
        }
        if (version == JSGLRVersion.v2) {
            if (dataDependentParsing) {
                parser = new JSGLR2I(config, termFactory, langImpl, null, source, text, true);
            } else {
                parser = new JSGLR2I(config, termFactory, langImpl, null, source, text, false);
            }
        } else {
            if (base != null) {
                parser = new JSGLR1I(config, termFactory, base, langImpl, source, text);
            } else {
                parser = new JSGLR1I(config, termFactory, langImpl, null, source, text);
            }
        }
        final ParseContrib contrib = parser.parse(parserConfig);
        if (version == JSGLRVersion.v2) {
            if (contrib.valid)
                logger.info("Valid JSGLR2 parse");
            else
                logger.info("Invalid JSGLR2 parse");
        }
        final ISpoofaxParseUnit unit = unitService.parseUnit(input, contrib);
        return unit;
    } catch (IOException | InvalidParseTableException | ParseTableReadException e) {
        throw new ParseException(input, e);
    }
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) ParseContrib(org.metaborg.spoofax.core.unit.ParseContrib) JSGLRVersion(org.metaborg.core.config.JSGLRVersion) IOException(java.io.IOException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ParseException(org.metaborg.core.syntax.ParseException) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) InvalidParseTableException(org.spoofax.jsglr.client.InvalidParseTableException) ParseTableReadException(org.spoofax.jsglr2.parsetable.ParseTableReadException)

Example 2 with ParseContrib

use of org.metaborg.spoofax.core.unit.ParseContrib 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)

Example 3 with ParseContrib

use of org.metaborg.spoofax.core.unit.ParseContrib in project spoofax by metaborg.

the class JSGLR2I 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;
    String startSymbol = getOrDefaultStartSymbol(parserConfig);
    final Timer timer = new Timer(true);
    final IStrategoTerm ast = parser.parse(input, fileName, startSymbol);
    final long duration = timer.stop();
    final boolean hasAst = ast != null;
    final boolean hasErrors = ast == null;
    final Iterable<IMessage> messages;
    if (hasErrors)
        messages = Collections.singletonList(MessageFactory.newParseErrorAtTop(resource, "Invalid syntax", null));
    else
        messages = Collections.emptyList();
    return new ParseContrib(hasAst, hasAst && !hasErrors, ast, messages, duration);
}
Also used : ParseContrib(org.metaborg.spoofax.core.unit.ParseContrib) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) Timer(org.metaborg.util.time.Timer) IMessage(org.metaborg.core.messages.IMessage)

Aggregations

ParseContrib (org.metaborg.spoofax.core.unit.ParseContrib)3 IMessage (org.metaborg.core.messages.IMessage)2 Timer (org.metaborg.util.time.Timer)2 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)2 IOException (java.io.IOException)1 FileObject (org.apache.commons.vfs2.FileObject)1 JSGLRVersion (org.metaborg.core.config.JSGLRVersion)1 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)1 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)1 ParseException (org.metaborg.core.syntax.ParseException)1 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)1 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)1 InvalidParseTableException (org.spoofax.jsglr.client.InvalidParseTableException)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 ParseTableReadException (org.spoofax.jsglr2.parsetable.ParseTableReadException)1