Search in sources :

Example 1 with IStrategoTuple

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

the class LanguageComponentsPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException, IOException {
    IStrategoList list = factory.makeList();
    for (ILanguageComponent component : context.language().components()) {
        final LanguageIdentifier id = component.id();
        final IStrategoString groupIdTerm = factory.makeString(id.groupId);
        final IStrategoString idTerm = factory.makeString(id.id);
        final IStrategoString versionTerm = factory.makeString(id.version.toString());
        final IStrategoString locationTerm = factory.makeString(component.location().getName().getURI());
        final IStrategoTuple tuple = factory.makeTuple(groupIdTerm, idTerm, versionTerm, locationTerm);
        list = factory.makeListCons(tuple, list);
    }
    return list;
}
Also used : LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 2 with IStrategoTuple

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

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

the class JSGLRCompletionService method recursiveCompletions.

private Collection<? extends ICompletion> recursiveCompletions(Iterable<IStrategoTerm> leftRecursive, Iterable<IStrategoTerm> rightRecursive, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
    Collection<ICompletion> completions = Lists.newLinkedList();
    // call Stratego part of the framework to compute change
    final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
    final ITermFactory termFactory = termFactoryService.get(component, null, false);
    for (IStrategoTerm term : leftRecursive) {
        IStrategoTerm sort = termFactory.makeString(ImploderAttachment.getSort(term));
        final IStrategoTerm strategoInput = termFactory.makeTuple(sort, term);
        IStrategoTerm proposals = null;
        try {
            proposals = strategoCommon.invoke(runtime, strategoInput, "get-proposals-left-recursive-" + languageName);
        } catch (Exception e) {
            logger.error("Getting proposals for {} failed", term);
            continue;
        }
        if (proposals == null) {
            logger.error("Getting proposals for {} failed", term);
            continue;
        }
        for (IStrategoTerm proposalTerm : proposals) {
            final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
            if (tuple.getSubtermCount() != 5 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl) || !(tuple.getSubterm(4) instanceof IStrategoString)) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            final String name = Tools.asJavaString(tuple.getSubterm(0));
            final String text = Tools.asJavaString(tuple.getSubterm(1));
            final String additionalInfo = Tools.asJavaString(tuple.getSubterm(2));
            final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
            final String prefix = Tools.asJavaString(tuple.getSubterm(4));
            if (change.getConstructor().getName().contains("REPLACE_TERM")) {
                final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, false, prefix, "");
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            }
        }
    }
    for (IStrategoTerm term : rightRecursive) {
        IStrategoTerm sort = termFactory.makeString(ImploderAttachment.getSort(term));
        final IStrategoTerm strategoInput = termFactory.makeTuple(sort, term);
        IStrategoTerm proposals = null;
        try {
            proposals = strategoCommon.invoke(runtime, strategoInput, "get-proposals-right-recursive-" + languageName);
        } catch (Exception e) {
            logger.error("Getting proposals for {} failed", term);
            continue;
        }
        if (proposals == null) {
            logger.error("Getting proposals for {} failed", term);
            continue;
        }
        for (IStrategoTerm proposalTerm : proposals) {
            final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
            if (tuple.getSubtermCount() != 5 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl) || !(tuple.getSubterm(4) instanceof IStrategoString)) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            final String name = Tools.asJavaString(tuple.getSubterm(0));
            final String text = Tools.asJavaString(tuple.getSubterm(1));
            final String additionalInfo = Tools.asJavaString(tuple.getSubterm(2));
            final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
            final String suffix = Tools.asJavaString(tuple.getSubterm(4));
            if (change.getConstructor().getName().contains("REPLACE_TERM")) {
                final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, false, "", suffix);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            }
        }
    }
    return completions;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl) MetaborgException(org.metaborg.core.MetaborgException)

Example 4 with IStrategoTuple

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

the class JSGLRCompletionService method listsCompletions.

public Collection<ICompletion> listsCompletions(int position, boolean blankLineCompletion, Iterable<IStrategoList> lists, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
    Collection<ICompletion> completions = Lists.newLinkedList();
    final ITermFactory termFactory = termFactoryService.get(component, null, false);
    for (IStrategoList list : lists) {
        ListImploderAttachment attachment = list.getAttachment(null);
        String sort = attachment.getSort().substring(0, attachment.getSort().length() - 1);
        String placeholderName = sort + "-Plhdr";
        IStrategoAppl listPlaceholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderName, 0));
        final IStrategoTerm strategoInput = termFactory.makeTuple(termFactory.makeString(sort), list, listPlaceholder, termFactory.makeInt(position));
        final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
        final IStrategoTerm proposalsLists = strategoCommon.invoke(runtime, strategoInput, "get-proposals-list-" + languageName);
        if (proposalsLists == null) {
            logger.error("Getting proposals for {} failed", strategoInput);
            continue;
        }
        for (IStrategoTerm proposalTerm : proposalsLists) {
            if (!(proposalTerm instanceof IStrategoTuple)) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
            if (tuple.getSubtermCount() != 4 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl)) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            final String name = Tools.asJavaString(tuple.getSubterm(0));
            final String text = Tools.asJavaString(tuple.getSubterm(1));
            final String additionalInfo = Tools.asJavaString(tuple.getSubterm(2));
            final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
            // if the change is inserting at the end of a list
            if (change.getConstructor().getName().contains("INSERT_AT_END")) {
                final ICompletion completion = createCompletionInsertAtEnd(name, text, additionalInfo, change, blankLineCompletion);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            } else if (change.getConstructor().getName().contains("INSERT_BEFORE")) {
                final ICompletion completion = createCompletionInsertBefore(name, text, additionalInfo, change);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            }
        }
    }
    return completions;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl)

Example 5 with IStrategoTuple

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

the class JSGLRCompletionService method placeholderCompletions.

public Collection<ICompletion> placeholderCompletions(IStrategoAppl placeholder, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
    Collection<ICompletion> completions = Lists.newLinkedList();
    // call Stratego part of the framework to compute change
    final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
    final ITermFactory termFactory = termFactoryService.get(component, null, false);
    IStrategoTerm placeholderParent = ParentAttachment.getParent(placeholder);
    if (placeholderParent == null) {
        placeholderParent = placeholder;
    }
    IStrategoInt placeholderIdx = termFactory.makeInt(-1);
    for (int i = 0; i < placeholderParent.getSubtermCount(); i++) {
        if (placeholderParent.getSubterm(i) == placeholder) {
            placeholderIdx = termFactory.makeInt(i);
        }
    }
    final String sort = ImploderAttachment.getSort(placeholder);
    final IStrategoTerm strategoInput = termFactory.makeTuple(termFactory.makeString(sort), placeholder, placeholderParent, placeholderIdx);
    final IStrategoTerm proposalsPlaceholder = strategoCommon.invoke(runtime, strategoInput, "get-proposals-placeholder-" + languageName);
    if (proposalsPlaceholder == null) {
        logger.error("Getting proposals for {} failed", placeholder);
        return completions;
    }
    for (IStrategoTerm proposalTerm : proposalsPlaceholder) {
        if (!(proposalTerm instanceof IStrategoTuple)) {
            logger.error("Unexpected proposal term {}, skipping", proposalTerm);
            continue;
        }
        final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
        if (tuple.getSubtermCount() != 4 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl)) {
            logger.error("Unexpected proposal term {}, skipping", proposalTerm);
            continue;
        }
        final String name = Tools.asJavaString(tuple.getSubterm(0));
        final String text = Tools.asJavaString(tuple.getSubterm(1));
        final String additionalInfo = Tools.asJavaString(tuple.getSubterm(2));
        final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
        if (change.getConstructor().getName().contains("REPLACE_TERM")) {
            final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, false, "", "");
            if (completion == null) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            completions.add(completion);
        }
    }
    return completions;
}
Also used : IStrategoInt(org.spoofax.interpreter.terms.IStrategoInt) ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl)

Aggregations

IStrategoString (org.spoofax.interpreter.terms.IStrategoString)10 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)9 HybridInterpreter (org.strategoxt.HybridInterpreter)7 ICompletion (org.metaborg.core.completion.ICompletion)6 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)6 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)6 StrategoAppl (org.spoofax.terms.StrategoAppl)5 FileObject (org.apache.commons.vfs2.FileObject)4 MetaborgException (org.metaborg.core.MetaborgException)4 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)4 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)4 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)2 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)2 Timer (org.metaborg.util.time.Timer)2 IStrategoInt (org.spoofax.interpreter.terms.IStrategoInt)2 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)2 Nullable (javax.annotation.Nullable)1 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)1 ITransformAction (org.metaborg.core.action.ITransformAction)1