use of com.squarespace.template.Instructions.IfPredicateInst 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;
}
}
use of com.squarespace.template.Instructions.IfPredicateInst in project template-compiler by Squarespace.
the class CodeMaker method ifpred.
public IfPredicateInst ifpred(Predicate predicate, Arguments args) throws ArgumentsException {
IfPredicateInst res = new IfPredicateInst(predicate, args);
predicate.validateArgs(args);
return res;
}
use of com.squarespace.template.Instructions.IfPredicateInst 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');
}
use of com.squarespace.template.Instructions.IfPredicateInst in project template-compiler by Squarespace.
the class InstructionReprTest method testIfPredicate.
@Test
public void testIfPredicate() throws ArgumentsException {
CodeMaker mk = maker();
IfPredicateInst i1 = mk.ifpred(CorePredicates.PLURAL);
assertEquals(i1.repr(), "{.if plural?}");
i1 = mk.ifpred(UnitTestPredicates.REQUIRED_ARGS, mk.args(" 1 2 3"));
assertEquals(i1.repr(), "{.if required-args? 1 2 3}");
i1 = new IfPredicateInst(UnitTestPredicates.INVALID_ARGS, new Arguments(mk.view("/abc/def/ghi")));
assertEquals(i1.repr(), "{.if invalid-args?/abc/def/ghi}");
}
use of com.squarespace.template.Instructions.IfPredicateInst in project template-compiler by Squarespace.
the class InstructionEqualityTest method testIfPredicateEquals.
@Test
public void testIfPredicateEquals() throws CodeSyntaxException, ArgumentsException {
CodeMaker mk = maker();
IfPredicateInst i1 = mk.ifpred(PLURAL);
IfPredicateInst i2 = mk.ifpred(PLURAL);
assertEquals(i1, i2);
IfPredicateInst i3 = mk.ifpred(SINGULAR);
assertFalse(i1.equals(null));
assertNotEquals(i1, i3);
assertNotEquals(i1, mk.ifexpn(mk.strlist("@"), mk.oplist()));
}
Aggregations