Search in sources :

Example 1 with ParseException

use of org.metaborg.core.syntax.ParseException in project spoofax by metaborg.

the class ParseFileStrategy method invoke.

@Override
public IStrategoTerm invoke(Context context, IStrategoTerm current) {
    if (!Tools.isTermString(current))
        return null;
    try {
        final String path = Tools.asJavaString(current);
        final FileObject resource = resourceService.resolve(path);
        if (resource.getType() != FileType.FILE) {
            return null;
        }
        final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource);
        if (identifiedResource == null) {
            return null;
        }
        final String text = sourceTextService.text(resource);
        final ISpoofaxInputUnit input = unitService.inputUnit(resource, text, identifiedResource.language, identifiedResource.dialect);
        final ISpoofaxParseUnit result = syntaxService.parse(input);
        return result.ast();
    } catch (ParseException | IOException e) {
        throw new StrategoException("Parsing failed unexpectedly", e);
    }
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) ISpoofaxInputUnit(org.metaborg.spoofax.core.unit.ISpoofaxInputUnit) FileObject(org.apache.commons.vfs2.FileObject) ParseException(org.metaborg.core.syntax.ParseException) IOException(java.io.IOException) StrategoException(org.strategoxt.lang.StrategoException) IdentifiedResource(org.metaborg.core.language.IdentifiedResource)

Example 2 with ParseException

use of org.metaborg.core.syntax.ParseException 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 3 with ParseException

use of org.metaborg.core.syntax.ParseException in project spoofax by metaborg.

the class JSGLRParseService method getParserConfig.

public IParserConfig getParserConfig(ILanguageImpl lang, ISpoofaxInputUnit input) throws ParseException {
    IParserConfig config = parserConfigs.get(lang);
    if (config == null) {
        final ITermFactory termFactory = termFactoryService.getGeneric().getFactoryWithStorageType(IStrategoTerm.MUTABLE);
        final SyntaxFacet facet = lang.facet(SyntaxFacet.class);
        FileObject parseTable = null;
        boolean incrementalPTGen = false;
        for (ILanguageComponent component : lang.components()) {
            if (component.config().sdfEnabled() && component.config().sdf2tableVersion() == Sdf2tableVersion.incremental) {
                incrementalPTGen = true;
            }
        }
        if (facet.parseTable == null) {
            try {
                boolean multipleTables = false;
                for (ILanguageComponent component : lang.components()) {
                    if (component.config().sdfEnabled()) {
                        if (component.config().parseTable() != null) {
                            if (multipleTables) {
                                logger.error("Different components are specifying multiple parse tables.");
                                throw new ParseException(input);
                            }
                            parseTable = component.location().resolveFile(component.config().parseTable());
                            multipleTables = true;
                        }
                    }
                }
            } catch (FileSystemException e) {
                logger.error("Parse table not found or sdf is not enabled for this language.");
                throw new ParseException(input, e);
            }
        } else {
            parseTable = facet.parseTable;
        }
        try {
            if (parseTable == null || !parseTable.exists()) {
                logger.error("Parse table not found or sdf is not enabled for this language.");
                throw new ParseException(input);
            }
        } catch (FileSystemException e) {
            logger.error("Parse table not found or sdf is not enabled for this language.");
            throw new ParseException(input, e);
        }
        final IParseTableProvider provider;
        JSGLRVersion version = jsglrVersion(input);
        if (version == JSGLRVersion.v2) {
            provider = new JSGLR2FileParseTableProvider(parseTable, termFactory);
        } else {
            final ParseTable referenceParseTable = referenceParseTables.get(lang);
            if (referenceParseTable != null && incrementalPTGen) {
                provider = new JSGLR1IncrementalParseTableProvider(parseTable, termFactory, referenceParseTable);
            } else {
                provider = new JSGLR1FileParseTableProvider(parseTable, termFactory);
            }
        }
        config = new ParserConfig(Iterables.get(facet.startSymbols, 0), provider);
        parserConfigs.put(lang, config);
    }
    return config;
}
Also used : ParseTable(org.metaborg.sdf2table.parsetable.ParseTable) JSGLRVersion(org.metaborg.core.config.JSGLRVersion) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject) ParseException(org.metaborg.core.syntax.ParseException) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 4 with ParseException

use of org.metaborg.core.syntax.ParseException in project spoofax by metaborg.

the class JSGLRParseService method getCompletionParserConfig.

public IParserConfig getCompletionParserConfig(ILanguageImpl lang, ISpoofaxInputUnit input) throws ParseException {
    IParserConfig config = completionParserConfigs.get(lang);
    if (config == null) {
        final ITermFactory termFactory = termFactoryService.getGeneric().getFactoryWithStorageType(IStrategoTerm.MUTABLE);
        final SyntaxFacet facet = lang.facet(SyntaxFacet.class);
        FileObject completionParseTable = null;
        boolean incrementalPTGen = false;
        for (ILanguageComponent component : lang.components()) {
            if (component.config().sdfEnabled() && component.config().sdf2tableVersion() == Sdf2tableVersion.incremental) {
                incrementalPTGen = true;
            }
        }
        if (facet.completionParseTable == null) {
            try {
                boolean multipleTables = false;
                for (ILanguageComponent component : lang.components()) {
                    if (component.config().sdfEnabled()) {
                        if (component.config().completionsParseTable() != null) {
                            if (multipleTables) {
                                logger.error("Different components are specifying multiple completion parse tables.");
                                throw new ParseException(input);
                            }
                            completionParseTable = component.location().resolveFile(component.config().completionsParseTable());
                            multipleTables = true;
                        }
                    }
                }
            } catch (FileSystemException e) {
                logger.error("Completion parse table not found or sdf is not enabled for this language.");
                throw new ParseException(input, e);
            }
        } else {
            completionParseTable = facet.completionParseTable;
        }
        try {
            if (completionParseTable == null || !completionParseTable.exists()) {
                logger.error("Completion parse table not found or sdf is not enabled for this language.");
                throw new ParseException(input);
            }
        } catch (FileSystemException e) {
            logger.error("Completion parse table not found or sdf is not enabled for this language.");
            throw new ParseException(input, e);
        }
        final IParseTableProvider provider;
        final ParseTable referenceParseTable = referenceCompletionParseTables.get(lang);
        if (referenceParseTable != null && incrementalPTGen) {
            provider = new JSGLR1IncrementalParseTableProvider(completionParseTable, termFactory, referenceParseTable);
        } else {
            provider = new JSGLR1FileParseTableProvider(completionParseTable, termFactory);
        }
        config = new ParserConfig(Iterables.get(facet.startSymbols, 0), provider);
        completionParserConfigs.put(lang, config);
    }
    return config;
}
Also used : ParseTable(org.metaborg.sdf2table.parsetable.ParseTable) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject) ParseException(org.metaborg.core.syntax.ParseException) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 5 with ParseException

use of org.metaborg.core.syntax.ParseException in project spoofax by metaborg.

the class Sdf2RtgStamper method stampOf.

@Override
public Stamp stampOf(File file) {
    if (!FileCommands.exists(file)) {
        return new ValueStamp<>(this, null);
    }
    final IStrategoTerm term;
    try {
        term = context.parse(file);
    } catch (ParseException | IOException e) {
        return LastModifiedStamper.instance.stampOf(file);
    }
    if (term == null) {
        return LastModifiedStamper.instance.stampOf(file);
    }
    final Deliteralize deliteralize = new Deliteralize(context.termFactory(), false);
    final IStrategoTerm delit = deliteralize.transform(term);
    return new ValueStamp<>(this, delit);
}
Also used : ValueStamp(build.pluto.stamp.ValueStamp) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ParseException(org.metaborg.core.syntax.ParseException) IOException(java.io.IOException)

Aggregations

ParseException (org.metaborg.core.syntax.ParseException)10 FileObject (org.apache.commons.vfs2.FileObject)8 IOException (java.io.IOException)6 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)3 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)3 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)3 ValueStamp (build.pluto.stamp.ValueStamp)2 FileSystemException (org.apache.commons.vfs2.FileSystemException)2 JSGLRVersion (org.metaborg.core.config.JSGLRVersion)2 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)2 IdentifiedResource (org.metaborg.core.language.IdentifiedResource)2 ParseTable (org.metaborg.sdf2table.parsetable.ParseTable)2 ISpoofaxInputUnit (org.metaborg.spoofax.core.unit.ISpoofaxInputUnit)2 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)2 FileName (org.apache.commons.vfs2.FileName)1 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)1 UpdateKind (org.metaborg.core.build.UpdateKind)1 IdentifiedResourceChange (org.metaborg.core.resource.IdentifiedResourceChange)1 ResourceChange (org.metaborg.core.resource.ResourceChange)1 ResourceChangeKind (org.metaborg.core.resource.ResourceChangeKind)1