Search in sources :

Example 1 with IStrategoConstructor

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

Aggregations

Nullable (javax.annotation.Nullable)1 IStyle (org.metaborg.core.style.IStyle)1 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)1 IStrategoConstructor (org.spoofax.interpreter.terms.IStrategoConstructor)1