Search in sources :

Example 1 with ISpoofaxParseUnit

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

the class ParsePrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, org.spoofax.interpreter.core.IContext strategoContext) throws MetaborgException, IOException {
    // Determine what to parse.
    if (!(current instanceof IStrategoString)) {
        throw new MetaborgException("Cannot parse, input string or file " + current + " is not a string");
    }
    final String stringOrFile = ((IStrategoString) current).stringValue();
    final String text;
    @Nullable final FileObject file;
    final IStrategoTerm isFileTerm = tvars[0];
    if (!(isFileTerm instanceof IStrategoInt)) {
        throw new MetaborgException("Cannot parse, input kind " + isFileTerm + " is not an integer");
    }
    if (((IStrategoInt) isFileTerm).intValue() == 1) {
        file = resourceService.resolve(stringOrFile);
        if (!file.exists() || !file.isFile()) {
            throw new MetaborgException("Cannot parse, input file " + file + " does not exist or is not a file");
        }
        text = sourceTextService.text(file);
    } else {
        file = null;
        text = stringOrFile;
    }
    // Determine which language to parse it with.
    final ILanguageImpl langImpl;
    final IStrategoTerm nameOrGroupIdTerm = tvars[1];
    final IStrategoTerm idTerm = tvars[2];
    final IStrategoTerm versionTerm = tvars[3];
    if (nameOrGroupIdTerm instanceof IStrategoTuple) {
        // No name, groupId, id, and version was set, auto detect language to parse with.
        if (file == null) {
            throw new MetaborgException("Cannot parse a string, no language to parse it with was given");
        }
        final IContext context = metaborgContext(strategoContext);
        if (context != null) {
            langImpl = languageIdentifierService.identify(file, context.project());
        } else {
            langImpl = languageIdentifierService.identify(file);
        }
        if (langImpl == null) {
            throw new MetaborgException("Cannot parse, language for " + file + " could not be identified");
        }
    } else if (idTerm instanceof IStrategoTuple) {
        // No id was set, name is set.
        if (!(nameOrGroupIdTerm instanceof IStrategoString)) {
            throw new MetaborgException("Cannot parse, language name " + nameOrGroupIdTerm + " is not a string");
        }
        final String name = ((IStrategoString) nameOrGroupIdTerm).stringValue();
        final ILanguage lang = languageService.getLanguage(name);
        if (lang == null) {
            throw new MetaborgException("Cannot parse, language " + nameOrGroupIdTerm + " does not exist");
        }
        langImpl = lang.activeImpl();
        if (langImpl == null) {
            throw new MetaborgException("Cannot parse, language " + lang + " has no implementation loaded");
        }
    } else {
        // A groupId, id, and version is set.
        if (!(nameOrGroupIdTerm instanceof IStrategoString)) {
            throw new MetaborgException("Cannot parse, language groupId " + nameOrGroupIdTerm + " is not a string");
        }
        final String groupId = ((IStrategoString) nameOrGroupIdTerm).stringValue();
        if (!(idTerm instanceof IStrategoString)) {
            throw new MetaborgException("Cannot parse, language id " + idTerm + " is not a string");
        }
        final String id = ((IStrategoString) idTerm).stringValue();
        if (!(versionTerm instanceof IStrategoString)) {
            throw new MetaborgException("Cannot parse, language version " + versionTerm + " is not a string");
        }
        final String versionStr = ((IStrategoString) versionTerm).stringValue();
        final LanguageVersion version = LanguageVersion.parse(versionStr);
        final LanguageIdentifier langId = new LanguageIdentifier(groupId, id, version);
        langImpl = languageService.getImpl(langId);
        if (langImpl == null) {
            throw new MetaborgException("Cannot parse, language implementation " + langId + " does not exist");
        }
    }
    // Parse the text.
    final ISpoofaxInputUnit input;
    if (file != null) {
        @Nullable ILanguageImpl dialect;
        try {
            final IdentifiedDialect identifierDialect = dialectIdentifier.identify(file);
            if (identifierDialect != null) {
                dialect = identifierDialect.dialect;
            } else {
                dialect = null;
            }
        } catch (MetaborgException | MetaborgRuntimeException e) {
            // Ignore
            dialect = null;
        }
        input = unitService.inputUnit(file, text, langImpl, dialect);
    } else {
        input = unitService.inputUnit(text, langImpl, null);
    }
    final ISpoofaxParseUnit result = syntaxService.parse(input);
    if (result.valid() && result.success()) {
        return result.ast();
    } else {
        return null;
    }
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IContext(org.metaborg.core.context.IContext) MetaborgException(org.metaborg.core.MetaborgException) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoInt(org.spoofax.interpreter.terms.IStrategoInt) ILanguage(org.metaborg.core.language.ILanguage) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ISpoofaxInputUnit(org.metaborg.spoofax.core.unit.ISpoofaxInputUnit) LanguageVersion(org.metaborg.core.language.LanguageVersion) FileObject(org.apache.commons.vfs2.FileObject) IdentifiedDialect(org.metaborg.core.language.dialect.IdentifiedDialect) Nullable(javax.annotation.Nullable)

Example 2 with ISpoofaxParseUnit

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

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

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

the class StrategoTransformer method transformAllParsed.

@Override
public Collection<ISpoofaxTransformUnit<ISpoofaxParseUnit>> transformAllParsed(Iterable<ISpoofaxParseUnit> inputs, IContext context, TransformActionContrib action, ITransformConfig config) throws TransformException {
    final int size = Iterables.size(inputs);
    final Collection<ISpoofaxTransformUnit<ISpoofaxParseUnit>> transformUnits = Lists.newArrayListWithCapacity(size);
    for (ISpoofaxParseUnit input : inputs) {
        transformUnits.add(transform(input, context, action, input.source(), input.ast(), config));
    }
    return transformUnits;
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) ISpoofaxTransformUnit(org.metaborg.spoofax.core.unit.ISpoofaxTransformUnit)

Example 5 with ISpoofaxParseUnit

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

the class SpoofaxContext method parse.

@Nullable
public IStrategoTerm parse(File file) throws IOException, ParseException {
    final FileObject resource = resourceService.resolve(file);
    final ILanguageImpl language = languageIdentifierService.identify(resource);
    if (language == null) {
        return null;
    }
    final String text = sourceTextService.text(resource);
    final ISpoofaxInputUnit inputUnit = unitService.inputUnit(resource, text, language, null);
    final ISpoofaxParseUnit result = syntaxService.parse(inputUnit);
    if (!result.valid() || !result.success()) {
        return null;
    }
    final IStrategoTerm term = result.ast();
    return term;
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ISpoofaxInputUnit(org.metaborg.spoofax.core.unit.ISpoofaxInputUnit) FileObject(org.apache.commons.vfs2.FileObject) Nullable(javax.annotation.Nullable)

Aggregations

ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)15 FileObject (org.apache.commons.vfs2.FileObject)9 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)7 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)7 MetaborgException (org.metaborg.core.MetaborgException)6 ISpoofaxInputUnit (org.metaborg.spoofax.core.unit.ISpoofaxInputUnit)6 AnalysisException (org.metaborg.core.analysis.AnalysisException)5 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)5 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)5 ISpoofaxAnalyzeUnit (org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)5 IOException (java.io.IOException)4 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)4 HybridInterpreter (org.strategoxt.HybridInterpreter)4 Nullable (javax.annotation.Nullable)3 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)3 IMessage (org.metaborg.core.messages.IMessage)3 AnalyzeContrib (org.metaborg.spoofax.core.unit.AnalyzeContrib)3 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 Inject (com.google.inject.Inject)2