Search in sources :

Example 41 with IStrategoAppl

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

the class StylerFacetFromESV method create.

@Nullable
public static StylerFacet create(IStrategoAppl esv) {
    final StylerFacet facet = new StylerFacet();
    final Iterable<IStrategoAppl> styleDefs = ESVReader.collectTerms(esv, "ColorDef");
    final Map<String, IStyle> namedStyles = Maps.newHashMap();
    for (IStrategoAppl styleDef : styleDefs) {
        final IStrategoAppl styleTerm = (IStrategoAppl) styleDef.getSubterm(1);
        final IStrategoConstructor styleCons = styleTerm.getConstructor();
        final IStyle style;
        if (styleCons.getName().equals("Attribute")) {
            style = style(styleTerm);
        } else if (styleCons.getName().equals("AttributeRef")) {
            final String name = Tools.asJavaString(styleTerm.getSubterm(0));
            style = namedStyles.get(name);
            if (style == null) {
                logger.error("Cannot resolve style definition " + name + " in style definition " + styleDef);
                continue;
            }
        } else {
            logger.error("Unhandled style " + styleCons + " in style definition " + styleDef);
            continue;
        }
        namedStyles.put(Tools.asJavaString(styleDef.getSubterm(0)), style);
    }
    final Iterable<IStrategoAppl> styleRules = ESVReader.collectTerms(esv, "ColorRule");
    if (Iterables.isEmpty(styleRules)) {
        return null;
    }
    for (IStrategoAppl styleRule : styleRules) {
        final IStrategoAppl styleTerm = (IStrategoAppl) styleRule.getSubterm(1);
        final IStrategoConstructor styleCons = styleTerm.getConstructor();
        final IStyle style;
        if (styleCons.getName().equals("Attribute")) {
            style = style(styleTerm);
        } else if (styleCons.getName().equals("AttributeRef")) {
            final String name = Tools.asJavaString(styleTerm.getSubterm(0));
            style = namedStyles.get(name);
            if (style == null) {
                logger.error("Cannot resolve style definition " + name + " in style rule " + styleRule);
                continue;
            }
        } else {
            logger.error("Unhandled style " + styleCons + " in style rule " + styleRule);
            continue;
        }
        final IStrategoAppl node = (IStrategoAppl) styleRule.getSubterm(0);
        final IStrategoConstructor nodeCons = node.getConstructor();
        if (nodeCons.getName().equals("SortAndConstructor")) {
            final String sort = Tools.asJavaString(node.getSubterm(0).getSubterm(0));
            final String cons = Tools.asJavaString(node.getSubterm(1).getSubterm(0));
            facet.mapSortConsToStyle(sort, cons, style);
        } else if (nodeCons.getName().equals("ConstructorOnly")) {
            final String cons = Tools.asJavaString(node.getSubterm(0).getSubterm(0));
            facet.mapConsToStyle(cons, style);
        } else if (nodeCons.getName().equals("Sort")) {
            final String sort = Tools.asJavaString(node.getSubterm(0));
            facet.mapSortToStyle(sort, style);
        } else if (nodeCons.getName().equals("Token")) {
            final IStrategoAppl tokenAppl = (IStrategoAppl) node.getSubterm(0);
            final String token = tokenAppl.getConstructor().getName();
            facet.mapTokenToStyle(token, style);
        } else {
            logger.error("Unhandled node " + nodeCons + " in style rule " + styleRule);
            continue;
        }
    }
    return facet;
}
Also used : IStyle(org.metaborg.core.style.IStyle) IStrategoConstructor(org.spoofax.interpreter.terms.IStrategoConstructor) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) Nullable(javax.annotation.Nullable)

Example 42 with IStrategoAppl

use of org.spoofax.interpreter.terms.IStrategoAppl in project nabl by metaborg.

the class StrategoTermIndices method match.

public static Optional<TermIndex> match(IStrategoTerm term) {
    if (!(Tools.isTermAppl(term) && Tools.hasConstructor((IStrategoAppl) term, OP, ARITY))) {
        return Optional.empty();
    }
    IStrategoTerm resourceTerm = term.getSubterm(0);
    IStrategoTerm idTerm = term.getSubterm(1);
    if (!(Tools.isTermString(resourceTerm) && Tools.isTermInt(idTerm))) {
        return Optional.empty();
    }
    return Optional.of(ImmutableTermIndex.of(Tools.asJavaString(resourceTerm), Tools.asJavaInt(idTerm)));
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl)

Aggregations

IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)42 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)26 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)21 ICompletion (org.metaborg.core.completion.ICompletion)13 Nullable (javax.annotation.Nullable)12 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)9 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)8 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)8 Completion (org.metaborg.core.completion.Completion)7 ITokens (org.spoofax.jsglr.client.imploder.ITokens)7 ImploderAttachment (org.spoofax.jsglr.client.imploder.ImploderAttachment)7 StrategoTerm (org.spoofax.terms.StrategoTerm)7 HybridInterpreter (org.strategoxt.HybridInterpreter)7 FileObject (org.apache.commons.vfs2.FileObject)6 MetaborgException (org.metaborg.core.MetaborgException)6 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)6 CompletionKind (org.metaborg.core.completion.CompletionKind)5 StrategoAppl (org.spoofax.terms.StrategoAppl)5 IToken (org.spoofax.jsglr.client.imploder.IToken)4 ISourceRegion (org.metaborg.core.source.ISourceRegion)3