Search in sources :

Example 16 with IStrategoTerm

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

the class Typesmart method processModule.

private List<String> processModule(IStrategoTerm module) {
    if (module == null) {
        return Collections.emptyList();
    }
    assert ((IStrategoAppl) module).getName().equals("Module");
    List<String> imports = new ArrayList<>();
    IStrategoList decls = (IStrategoList) module.getSubterm(1);
    for (IStrategoTerm decl : decls) {
        String declName = ((IStrategoAppl) decl).getName();
        if (declName.equals("Imports")) {
            extractImports(decl.getSubterm(0), imports);
        } else if (declName.equals("Signature")) {
            processSignature(decl.getSubterm(0));
        }
    }
    return imports;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ArrayList(java.util.ArrayList) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Example 17 with IStrategoTerm

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

the class Typesmart method processMainStrategoFile.

private void processMainStrategoFile(File strFile, List<File> strjIncludeDirs) throws IOException {
    Set<String> seenImports = new HashSet<>();
    Set<File> seenFiles = new HashSet<>();
    LinkedList<String> todo = new LinkedList<>();
    String basePath = context.baseDir.getAbsolutePath();
    IStrategoTerm term = parseStratego(strFile);
    todo.addAll(processModule(term));
    boolean isRuntimeLibrary = strFile.getAbsolutePath().endsWith("org.metaborg.meta.lib.analysis/trans/runtime_libraries.str");
    while (!todo.isEmpty()) {
        String next = todo.pop();
        if (!isRuntimeLibrary && next.startsWith("runtime/") || !seenImports.add(next)) {
            continue;
        }
        Collection<File> files = findStrFiles(next, strjIncludeDirs);
        if (files.isEmpty() && !next.startsWith("lib")) {
            logger.warn("Could not extract typesmart info for unresolvable import " + next);
        }
        for (File file : files) {
            if (file.getAbsolutePath().startsWith(basePath) && seenFiles.add(file)) {
                // logger.debug("Entering module " + next);
                term = parseStratego(file);
                todo.addAll(processModule(term));
            }
        }
    }
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ParseFile(org.metaborg.spoofax.meta.core.pluto.build.misc.ParseFile) File(java.io.File) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 18 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm 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)

Example 19 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm 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)

Example 20 with IStrategoTerm

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

the class Sdf2RtgStamper method stampOf.

@Override
public Stamp stampOf(File file) {
    if (!FileCommands.exists(file)) {
        return new ValueStamp<>(this, null);
    }
    final IStrategoTerm term;
    try {
        term = context.parse(file);
    } catch (ParseException | IOException e) {
        return LastModifiedStamper.instance.stampOf(file);
    }
    if (term == null) {
        return LastModifiedStamper.instance.stampOf(file);
    }
    final Deliteralize deliteralize = new Deliteralize(context.termFactory(), false);
    final IStrategoTerm delit = deliteralize.transform(term);
    return new ValueStamp<>(this, delit);
}
Also used : ValueStamp(build.pluto.stamp.ValueStamp) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ParseException(org.metaborg.core.syntax.ParseException) IOException(java.io.IOException)

Aggregations

IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)82 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)31 FileObject (org.apache.commons.vfs2.FileObject)27 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)25 MetaborgException (org.metaborg.core.MetaborgException)19 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)17 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)16 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)15 Nullable (javax.annotation.Nullable)14 ISourceRegion (org.metaborg.core.source.ISourceRegion)12 ICompletion (org.metaborg.core.completion.ICompletion)11 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 IOException (java.io.IOException)9 IProject (org.metaborg.core.project.IProject)9 IMessage (org.metaborg.core.messages.IMessage)8 IToken (org.spoofax.jsglr.client.imploder.IToken)8 IContext (org.metaborg.core.context.IContext)7 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)7