Search in sources :

Example 26 with MetaborgRuntimeException

use of org.metaborg.core.MetaborgRuntimeException in project spoofax by metaborg.

the class LegacyParseFilePrimitive method call.

@Override
public boolean call(IContext env, Strategy[] strategies, IStrategoTerm[] terms) throws InterpreterException {
    if (!Tools.isTermString(terms[0]))
        return false;
    if (!Tools.isTermString(terms[3]))
        return false;
    try {
        final String pathOrInput = Tools.asJavaString(terms[0]);
        final String pathOrInput2 = Tools.asJavaString(terms[3]);
        FileObject resource;
        String text;
        try {
            resource = resourceService.resolve(pathOrInput);
            if (!resource.exists() || resource.getType() != FileType.FILE) {
                resource = resourceService.resolve(pathOrInput2);
                text = pathOrInput;
            } else {
                text = sourceTextService.text(resource);
            }
        } catch (MetaborgRuntimeException | IOException e) {
            resource = resourceService.resolve(pathOrInput2);
            text = pathOrInput;
        }
        if (resource.getType() != FileType.FILE) {
            return false;
        }
        final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource);
        if (identifiedResource == null) {
            return false;
        }
        final ISpoofaxInputUnit input = unitService.inputUnit(resource, text, identifiedResource.language, identifiedResource.dialect);
        final ISpoofaxParseUnit result = syntaxService.parse(input);
        if (result.valid() && result.success()) {
            env.setCurrent(result.ast());
        } else {
            return false;
        }
    } catch (ParseException | IOException e) {
        throw new InterpreterException("Parsing failed unexpectedly", e);
    }
    return true;
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) ISpoofaxInputUnit(org.metaborg.spoofax.core.unit.ISpoofaxInputUnit) InterpreterException(org.spoofax.interpreter.core.InterpreterException) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) ParseException(org.metaborg.core.syntax.ParseException) IdentifiedResource(org.metaborg.core.language.IdentifiedResource)

Aggregations

MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)26 FileObject (org.apache.commons.vfs2.FileObject)16 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)9 MetaborgException (org.metaborg.core.MetaborgException)7 Nullable (javax.annotation.Nullable)6 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)6 FileSystemException (org.apache.commons.vfs2.FileSystemException)4 IContext (org.metaborg.core.context.IContext)4 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)4 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)4 File (java.io.File)3 IOException (java.io.IOException)3 ContextException (org.metaborg.core.context.ContextException)3 ILanguage (org.metaborg.core.language.ILanguage)3 IdentifiedDialect (org.metaborg.core.language.dialect.IdentifiedDialect)3 IMessage (org.metaborg.core.messages.IMessage)3 IProject (org.metaborg.core.project.IProject)3 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)3 FileName (org.apache.commons.vfs2.FileName)2 LocalFile (org.apache.commons.vfs2.provider.local.LocalFile)2