Search in sources :

Example 6 with IStrategoString

use of org.spoofax.interpreter.terms.IStrategoString 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 7 with IStrategoString

use of org.spoofax.interpreter.terms.IStrategoString in project spoofax by metaborg.

the class OutlineFacetFromESV method create.

@Nullable
public static OutlineFacet create(IStrategoAppl esv) {
    final IStrategoAppl outlineTerm = ESVReader.findTerm(esv, "OutlineView");
    if (outlineTerm == null) {
        return null;
    }
    final String strategyName = ESVReader.termContents(outlineTerm.getSubterm(0));
    final int expandTo;
    final IStrategoAppl expandToTerm = ESVReader.findTerm(esv, "ExpandToLevel");
    if (expandToTerm == null) {
        expandTo = 0;
    } else {
        final IStrategoTerm expandToNumberTerm = expandToTerm.getSubterm(0);
        if (expandToNumberTerm instanceof IStrategoInt) {
            final IStrategoInt expandToNumberIntTerm = (IStrategoInt) expandToNumberTerm;
            expandTo = expandToNumberIntTerm.intValue();
        } else if (expandToNumberTerm instanceof IStrategoString) {
            final IStrategoString expandToNumberStringTerm = (IStrategoString) expandToNumberTerm;
            expandTo = Integer.parseInt(expandToNumberStringTerm.stringValue());
        } else {
            expandTo = 0;
        }
    }
    return new OutlineFacet(strategyName, expandTo);
}
Also used : IStrategoInt(org.spoofax.interpreter.terms.IStrategoInt) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) Nullable(javax.annotation.Nullable)

Example 8 with IStrategoString

use of org.spoofax.interpreter.terms.IStrategoString in project spoofax by metaborg.

the class OutlineService method icon.

@Nullable
private FileObject icon(IStrategoTerm term, FileObject location) {
    final IStrategoList annos = term.getAnnotations();
    if (annos == null) {
        return null;
    }
    if (annos.getSubtermCount() != 1) {
        return null;
    }
    final IStrategoTerm iconTerm = annos.getSubterm(0);
    if (!(iconTerm instanceof IStrategoString)) {
        return null;
    }
    final IStrategoString iconTermString = (IStrategoString) iconTerm;
    final String iconLocation = iconTermString.stringValue();
    try {
        return location.resolveFile(iconLocation);
    } catch (FileSystemException e) {
        logger.error("Cannot resolve icon {} in {}", e, iconLocation, location);
        return null;
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) Nullable(javax.annotation.Nullable)

Example 9 with IStrategoString

use of org.spoofax.interpreter.terms.IStrategoString in project spoofax by metaborg.

the class Typesmart method extractImports.

private void extractImports(IStrategoTerm importDecls, List<String> imports) {
    for (IStrategoTerm importDecl : importDecls) {
        String importName = ((IStrategoString) importDecl.getSubterm(0)).stringValue();
        String importDeclName = ((IStrategoAppl) importDecl).getName();
        if (importDeclName.equals("Import")) {
            imports.add(importName);
        } else {
            // wildcard import
            imports.add(importName + "/-");
        }
    }
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Example 10 with IStrategoString

use of org.spoofax.interpreter.terms.IStrategoString in project spoofax by metaborg.

the class Typesmart method processSignature.

private void processSignature(IStrategoTerm sigDecls) {
    for (IStrategoTerm decl : sigDecls) {
        String declName = ((IStrategoAppl) decl).getName();
        if (!declName.equals("Constructors")) {
            continue;
        }
        next_constr: for (IStrategoTerm constr : decl.getSubterm(0)) {
            String kind = ((IStrategoAppl) constr).getName();
            String cname;
            IStrategoAppl typeTerm;
            if (kind.equals("OpDeclInj") || kind.equals("ExtOpDeclInj")) {
                cname = "";
                typeTerm = (IStrategoAppl) constr.getSubterm(0);
            } else {
                cname = ((IStrategoString) constr.getSubterm(0)).stringValue();
                typeTerm = (IStrategoAppl) constr.getSubterm(1);
            }
            List<SortType> sortTypes;
            if (typeTerm.getName().equals("ConstType")) {
                // no constructor arguments
                sortTypes = new ArrayList<>(1);
                SortType t = extractSortType(typeTerm.getSubterm(0));
                if (t == null) {
                    continue next_constr;
                }
                sortTypes.add(t);
            } else if (typeTerm.getName().equals("FunType")) {
                IStrategoTerm[] argTypes = typeTerm.getSubterm(0).getAllSubterms();
                sortTypes = new ArrayList<>(argTypes.length + 1);
                for (IStrategoTerm argType : argTypes) {
                    SortType t = extractSortType(argType.getSubterm(0));
                    if (t == null) {
                        continue next_constr;
                    }
                    sortTypes.add(t);
                }
                SortType t = extractSortType(typeTerm.getSubterm(1).getSubterm(0));
                if (t == null) {
                    continue next_constr;
                }
                sortTypes.add(t);
            } else {
                throw new IllegalArgumentException("Found constructor declaration in unexpected format " + constr);
            }
            addConstructorSignature(cname, sortTypes);
        }
    }
}
Also used : SortType(org.spoofax.terms.typesmart.types.SortType) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ArrayList(java.util.ArrayList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ArrayList(java.util.ArrayList) TList(org.spoofax.terms.typesmart.types.TList) LinkedList(java.util.LinkedList) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) List(java.util.List) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Aggregations

IStrategoString (org.spoofax.interpreter.terms.IStrategoString)29 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)16 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)11 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)11 FileObject (org.apache.commons.vfs2.FileObject)10 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 MetaborgException (org.metaborg.core.MetaborgException)7 HybridInterpreter (org.strategoxt.HybridInterpreter)7 ICompletion (org.metaborg.core.completion.ICompletion)6 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)6 StrategoAppl (org.spoofax.terms.StrategoAppl)5 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)4 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)4 Nullable (javax.annotation.Nullable)3 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)3 IStrategoInt (org.spoofax.interpreter.terms.IStrategoInt)3 File (java.io.File)2 IOException (java.io.IOException)2 AnalysisException (org.metaborg.core.analysis.AnalysisException)2 ConfigException (org.metaborg.core.config.ConfigException)2