Search in sources :

Example 1 with StrategoException

use of org.strategoxt.lang.StrategoException 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)

Aggregations

IOException (java.io.IOException)1 FileObject (org.apache.commons.vfs2.FileObject)1 IdentifiedResource (org.metaborg.core.language.IdentifiedResource)1 ParseException (org.metaborg.core.syntax.ParseException)1 ISpoofaxInputUnit (org.metaborg.spoofax.core.unit.ISpoofaxInputUnit)1 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)1 StrategoException (org.strategoxt.lang.StrategoException)1