Search in sources :

Example 6 with ISpoofaxInputUnit

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

FileObject (org.apache.commons.vfs2.FileObject)6 ISpoofaxInputUnit (org.metaborg.spoofax.core.unit.ISpoofaxInputUnit)6 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)6 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)4 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)3 IOException (java.io.IOException)2 Nullable (javax.annotation.Nullable)2 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)2 IdentifiedResource (org.metaborg.core.language.IdentifiedResource)2 ParseException (org.metaborg.core.syntax.ParseException)2 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)2 MetaborgException (org.metaborg.core.MetaborgException)1 ICompletion (org.metaborg.core.completion.ICompletion)1 IContext (org.metaborg.core.context.IContext)1 ILanguage (org.metaborg.core.language.ILanguage)1 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)1 LanguageVersion (org.metaborg.core.language.LanguageVersion)1 IdentifiedDialect (org.metaborg.core.language.dialect.IdentifiedDialect)1 JSGLRParserConfiguration (org.metaborg.spoofax.core.syntax.JSGLRParserConfiguration)1 SyntaxFacet (org.metaborg.spoofax.core.syntax.SyntaxFacet)1