Search in sources :

Example 1 with VariableInst

use of com.squarespace.template.Instructions.VariableInst in project template-compiler by Squarespace.

the class ReferenceScanner method extract.

/**
 * Extracts reference metrics from a single instruction.
 */
public void extract(Instruction inst) {
    String name = null;
    if (inst == null) {
        return;
    }
    if (!(inst instanceof RootInst)) {
        refs.increment(inst);
    }
    switch(inst.getType()) {
        case TEXT:
            refs.textBytes += ((TextInst) inst).getView().length();
            break;
        case ALTERNATES_WITH:
            AlternatesWithInst alternatesWith = (AlternatesWithInst) inst;
            extractBlock(alternatesWith.getConsequent());
            extract(alternatesWith.getAlternative());
            break;
        case IF:
            {
                BlockInstruction blockInst = (BlockInstruction) inst;
                refs.addIfInstruction(blockInst);
                if (inst instanceof IfInst) {
                    IfInst ifInst = (IfInst) inst;
                    for (Object[] var : ifInst.getVariables()) {
                        name = ReprEmitter.get(var);
                        refs.addVariable(name);
                    }
                } else {
                    IfPredicateInst ifInst = (IfPredicateInst) inst;
                    refs.increment(ifInst.getPredicate());
                }
                extractBlock(blockInst.getConsequent());
                extract(blockInst.getAlternative());
                break;
            }
        case OR_PREDICATE:
        case PREDICATE:
            PredicateInst predicateInst = (PredicateInst) inst;
            Predicate predicate = predicateInst.getPredicate();
            if (predicate != null) {
                refs.increment(predicate);
                predicate.addReferences(predicateInst.getArguments(), refs);
            }
            extractBlock(predicateInst.getConsequent());
            extract(predicateInst.getAlternative());
            break;
        case REPEATED:
            RepeatedInst repeated = (RepeatedInst) inst;
            name = ReprEmitter.get(repeated.getVariable());
            refs.pushSection(name);
            extractBlock(repeated.getConsequent());
            extract(repeated.getAlternative());
            extract(repeated.getAlternatesWith());
            refs.popSection();
            break;
        case ROOT:
            extractBlock(((RootInst) inst).getConsequent());
            break;
        case SECTION:
            SectionInst section = (SectionInst) inst;
            name = ReprEmitter.get(section.getVariable());
            refs.pushSection(name);
            extractBlock(section.getConsequent());
            extract(section.getAlternative());
            refs.popSection();
            break;
        case VARIABLE:
            VariableInst varInst = (VariableInst) inst;
            name = ReprEmitter.get(varInst.getVariables());
            refs.addVariable(name);
            for (FormatterCall call : varInst.getFormatters()) {
                refs.increment(call.getFormatter());
            }
            break;
        default:
            break;
    }
}
Also used : PredicateInst(com.squarespace.template.Instructions.PredicateInst) IfPredicateInst(com.squarespace.template.Instructions.IfPredicateInst) SectionInst(com.squarespace.template.Instructions.SectionInst) RepeatedInst(com.squarespace.template.Instructions.RepeatedInst) RootInst(com.squarespace.template.Instructions.RootInst) IfPredicateInst(com.squarespace.template.Instructions.IfPredicateInst) VariableInst(com.squarespace.template.Instructions.VariableInst) TextInst(com.squarespace.template.Instructions.TextInst) IfInst(com.squarespace.template.Instructions.IfInst) AlternatesWithInst(com.squarespace.template.Instructions.AlternatesWithInst)

Example 2 with VariableInst

use of com.squarespace.template.Instructions.VariableInst in project template-compiler by Squarespace.

the class Tokenizer method parseVariable.

/**
 * Parses a variable reference that can consist of one or more variable names followed
 * by an optional list of formatters. Formatters can be chained so the output of one
 * formatter can be "piped" into the next.
 */
private boolean parseVariable() throws CodeSyntaxException {
    int start = matcher.matchStart();
    Variables vars = parseVariables();
    if (vars == null) {
        return false;
    }
    VariableInst instruction = maker.var(vars);
    List<FormatterCall> formatters = parseFormatters(instruction, start);
    if (formatters == null) {
        emitInstruction(instruction);
    } else if (!formatters.isEmpty()) {
        instruction.setFormatters(formatters);
        emitInstruction(instruction);
    }
    return true;
}
Also used : VariableInst(com.squarespace.template.Instructions.VariableInst)

Example 3 with VariableInst

use of com.squarespace.template.Instructions.VariableInst 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)

Example 4 with VariableInst

use of com.squarespace.template.Instructions.VariableInst in project template-compiler by Squarespace.

the class InstructionReprTest method testVariableRepr.

@Test
public void testVariableRepr() throws ArgumentsException {
    CodeMaker mk = maker();
    assertEquals(mk.var("@").repr(), "{@}");
    assertEquals(mk.var("@index").repr(), "{@index}");
    assertEquals(mk.var("a.b.c").repr(), "{a.b.c}");
    VariableInst v1 = mk.var("a.b", JSON);
    assertEquals(v1.repr(), "{a.b|json}");
    v1 = mk.var("@", mk.fmt(PLURALIZE, mk.args(" a1 a2")));
    assertEquals(v1.repr(), "{@|pluralize a1 a2}");
    v1 = mk.var("@", mk.formatters(mk.fmt(PLURALIZE, mk.args(" a1 a2")), mk.fmt(TRUNCATE, mk.args(" 34"))));
    assertEquals(v1.repr(), "{@|pluralize a1 a2|truncate 34}");
}
Also used : VariableInst(com.squarespace.template.Instructions.VariableInst) Test(org.testng.annotations.Test)

Example 5 with VariableInst

use of com.squarespace.template.Instructions.VariableInst in project template-compiler by Squarespace.

the class InstructionEqualityTest method testVariableEquals.

@Test
public void testVariableEquals() throws CodeSyntaxException, ArgumentsException {
    CodeMaker mk = maker();
    VariableInst v1 = mk.var("foo.bar");
    assertEquals(v1, mk.var("foo.bar"));
    assertFalse(v1.equals(null));
    assertNotEquals(v1, mk.var("foo"));
    assertNotEquals(v1, mk.var("@"));
    assertNotEquals(v1, mk.var("bar.foo"));
    assertNotEquals(v1, mk.comment("foo.bar"));
    assertNotEquals(v1, mk.end());
    v1 = mk.var("foo.bar", mk.formatters(JSON));
    assertEquals(v1, mk.var("foo.bar", mk.formatters(JSON)));
    assertFalse(v1.equals(null));
    assertNotEquals(v1, mk.var("@", JSON));
    VariableInst x = mk.var("foo.bar", PLURALIZE);
    assertNotEquals(v1, x);
    assertNotEquals(v1, mk.var("bar.foo", JSON));
    assertNotEquals(v1, mk.var("foo.bar"));
    assertNotEquals(v1, mk.end());
    // With arguments
    Arguments args1 = mk.args(" a1 a2");
    Arguments args2 = mk.args(" a2 a1");
    VariableInst v2 = mk.var("foo.bar", mk.formatters(mk.fmt(PLURALIZE, args1)));
    assertNotEquals(v1, v2);
    assertEquals(v2, mk.var("foo.bar", mk.fmt(PLURALIZE, args1)));
    assertNotEquals(v2, mk.var("foo.bar", mk.fmt(PLURALIZE, args2)));
    VariableInst v3 = mk.var("a.b.c", mk.fmt(SLUGIFY, args1));
    assertEquals(v3, mk.var("a.b.c", mk.fmt(SLUGIFY, args1)));
    VariableInst v4 = mk.var(mk.vars("a", "b", "c"));
    assertEquals(v4, mk.var(mk.vars("a", "b", "c")));
}
Also used : VariableInst(com.squarespace.template.Instructions.VariableInst) Test(org.testng.annotations.Test)

Aggregations

VariableInst (com.squarespace.template.Instructions.VariableInst)6 Test (org.testng.annotations.Test)3 IfInst (com.squarespace.template.Instructions.IfInst)2 IfPredicateInst (com.squarespace.template.Instructions.IfPredicateInst)2 PredicateInst (com.squarespace.template.Instructions.PredicateInst)2 RepeatedInst (com.squarespace.template.Instructions.RepeatedInst)2 SectionInst (com.squarespace.template.Instructions.SectionInst)2 TextInst (com.squarespace.template.Instructions.TextInst)2 AlternatesWithInst (com.squarespace.template.Instructions.AlternatesWithInst)1 BindVarInst (com.squarespace.template.Instructions.BindVarInst)1 CommentInst (com.squarespace.template.Instructions.CommentInst)1 EofInst (com.squarespace.template.Instructions.EofInst)1 EvalInst (com.squarespace.template.Instructions.EvalInst)1 IncludeInst (com.squarespace.template.Instructions.IncludeInst)1 InjectInst (com.squarespace.template.Instructions.InjectInst)1 RootInst (com.squarespace.template.Instructions.RootInst)1 ReprEmitter.emitVariables (com.squarespace.template.ReprEmitter.emitVariables)1