Search in sources :

Example 1 with ReprEmitter.emitVariables

use of com.squarespace.template.ReprEmitter.emitVariables in project template-compiler by Squarespace.

the class TreeEmitter method emitHeader.

private static void emitHeader(InstructionType type, Instruction inst, int depth, StringBuilder buf) {
    if (type.equals(InstructionType.ROOT)) {
        return;
    }
    indent(depth, buf);
    buf.append(type.toString());
    buf.append(" {").append(inst.getLineNumber()).append(',').append(inst.getCharOffset()).append("}");
    switch(type) {
        case BINDVAR:
            BindVarInst bindvar = (BindVarInst) inst;
            buf.append(' ').append(bindvar.getName()).append(" = ");
            emitVariables(bindvar.getVariables(), buf);
            break;
        case COMMENT:
            CommentInst comment = (CommentInst) inst;
            buf.append(' ');
            emitEscapedString(comment.getView(), buf);
            break;
        case EVAL:
            EvalInst eval = (EvalInst) inst;
            buf.append(' ');
            emitEscapedString(eval.body(), buf);
            break;
        case INCLUDE:
            {
                IncludeInst include = (IncludeInst) inst;
                buf.append(' ');
                emitArgs(include.getArguments(), buf);
                break;
            }
        case IF:
            {
                if (inst instanceof IfInst) {
                    buf.append(' ');
                    ReprEmitter.emitIfExpression((IfInst) inst, buf);
                } else {
                    IfPredicateInst predicateInst = (IfPredicateInst) inst;
                    Predicate predicate = predicateInst.getPredicate();
                    if (predicate != null) {
                        buf.append(' ').append(predicate);
                        Arguments args = predicateInst.getArguments();
                        if (!args.isEmpty()) {
                            buf.append(' ');
                            emitArgs(args, buf);
                        }
                    }
                }
                break;
            }
        case INJECT:
            {
                InjectInst inject = (InjectInst) inst;
                buf.append(' ');
                emitEscapedString(inject.variable(), buf);
                buf.append(' ');
                emitEscapedString(inject.filename(), buf);
                buf.append(' ');
                emitArgs(inject.arguments(), buf);
                break;
            }
        case OR_PREDICATE:
        case PREDICATE:
            PredicateInst predicateInst = (PredicateInst) inst;
            Predicate predicate = predicateInst.getPredicate();
            if (predicate != null) {
                buf.append(' ').append(predicate);
                Arguments args = predicateInst.getArguments();
                if (!args.isEmpty()) {
                    buf.append(' ');
                    emitArgs(args, buf);
                }
            }
            break;
        case REPEATED:
            RepeatedInst repeated = (RepeatedInst) inst;
            buf.append(' ');
            emitNames(repeated.getVariable(), buf);
            break;
        case SECTION:
            SectionInst section = (SectionInst) inst;
            buf.append(' ');
            emitNames(section.getVariable(), buf);
            break;
        case TEXT:
            TextInst text = (TextInst) inst;
            buf.append(' ');
            emitEscapedString(text.getView(), buf);
            break;
        case VARIABLE:
            {
                VariableInst varInst = (VariableInst) inst;
                Variables variables = varInst.getVariables();
                buf.append(' ');
                ReprEmitter.emitVariables(variables, buf);
                for (FormatterCall formatterCall : varInst.getFormatters()) {
                    buf.append('\n');
                    indent(depth + INCR, buf);
                    buf.append("| ");
                    buf.append(formatterCall.getFormatter().identifier());
                    Arguments args = formatterCall.getArguments();
                    if (!args.isEmpty()) {
                        buf.append(' ');
                        emitArgs(args, buf);
                    }
                }
                break;
            }
        default:
            break;
    }
    buf.append('\n');
}
Also used : PredicateInst(com.squarespace.template.Instructions.PredicateInst) IfPredicateInst(com.squarespace.template.Instructions.IfPredicateInst) CommentInst(com.squarespace.template.Instructions.CommentInst) SectionInst(com.squarespace.template.Instructions.SectionInst) IncludeInst(com.squarespace.template.Instructions.IncludeInst) RepeatedInst(com.squarespace.template.Instructions.RepeatedInst) EvalInst(com.squarespace.template.Instructions.EvalInst) BindVarInst(com.squarespace.template.Instructions.BindVarInst) IfPredicateInst(com.squarespace.template.Instructions.IfPredicateInst) VariableInst(com.squarespace.template.Instructions.VariableInst) TextInst(com.squarespace.template.Instructions.TextInst) ReprEmitter.emitVariables(com.squarespace.template.ReprEmitter.emitVariables) IfInst(com.squarespace.template.Instructions.IfInst) InjectInst(com.squarespace.template.Instructions.InjectInst)

Aggregations

BindVarInst (com.squarespace.template.Instructions.BindVarInst)1 CommentInst (com.squarespace.template.Instructions.CommentInst)1 EvalInst (com.squarespace.template.Instructions.EvalInst)1 IfInst (com.squarespace.template.Instructions.IfInst)1 IfPredicateInst (com.squarespace.template.Instructions.IfPredicateInst)1 IncludeInst (com.squarespace.template.Instructions.IncludeInst)1 InjectInst (com.squarespace.template.Instructions.InjectInst)1 PredicateInst (com.squarespace.template.Instructions.PredicateInst)1 RepeatedInst (com.squarespace.template.Instructions.RepeatedInst)1 SectionInst (com.squarespace.template.Instructions.SectionInst)1 TextInst (com.squarespace.template.Instructions.TextInst)1 VariableInst (com.squarespace.template.Instructions.VariableInst)1 ReprEmitter.emitVariables (com.squarespace.template.ReprEmitter.emitVariables)1