use of com.squarespace.template.Instructions.AlternatesWithInst in project template-compiler by Squarespace.
the class InstructionReprTest method testAlternatesWithRepr.
@Test
public void testAlternatesWithRepr() {
CodeMaker mk = maker();
AlternatesWithInst a1 = mk.alternates();
assertEquals(a1.repr(), "{.alternates with}");
assertNotEquals(a1.repr(), "{.alternatesWith}");
assertNotEquals(a1.repr(), "{.alternateswith}");
a1.getConsequent().add(mk.text("---"));
assertEquals(a1.repr(), "{.alternates with}---");
assertEquals(ReprEmitter.get(a1, false), "{.alternates with}");
a1.setPreprocessScope();
assertEquals(ReprEmitter.get(a1, false), "{^.alternates with}");
}
use of com.squarespace.template.Instructions.AlternatesWithInst in project template-compiler by Squarespace.
the class InstructionReprTest method testRepeatedRepr.
@Test
public void testRepeatedRepr() {
CodeMaker mk = maker();
RepeatedInst r1 = mk.repeated("a.b");
assertEquals(r1.repr(), "{.repeated section a.b}");
r1.getConsequent().add(mk.text("A"));
assertEquals(r1.repr(), "{.repeated section a.b}A");
AlternatesWithInst a1 = mk.alternates();
r1.setAlternatesWith(a1);
assertEquals(r1.repr(), "{.repeated section a.b}A{.alternates with}");
a1.getConsequent().add(mk.text("-"));
assertEquals(r1.repr(), "{.repeated section a.b}A{.alternates with}-");
r1.setAlternative(mk.end());
assertEquals(r1.repr(), "{.repeated section a.b}A{.alternates with}-{.end}");
assertEquals(ReprEmitter.get(r1, false), "{.repeated section a.b}");
}
use of com.squarespace.template.Instructions.AlternatesWithInst in project template-compiler by Squarespace.
the class InstructionEqualityTest method testRepeatEquals.
@Test
public void testRepeatEquals() throws CodeSyntaxException {
CodeMaker mk = maker();
RepeatedInst r1 = mk.repeated("foo.bar");
RepeatedInst r2 = mk.repeated("foo.bar");
assertEquals(r1, r2);
assertFalse(r1.equals(null));
assertNotEquals(r1, mk.repeated("bar.foo"));
assertNotEquals(r1, mk.repeated("@"));
AlternatesWithInst a1 = mk.alternates();
AlternatesWithInst a2 = mk.alternates();
r1.setAlternatesWith(a1);
assertNotEquals(r1, r2);
r2.setAlternatesWith(a2);
assertEquals(r1, r2);
testBlockEquals(r1, r2);
}
use of com.squarespace.template.Instructions.AlternatesWithInst 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.AlternatesWithInst in project template-compiler by Squarespace.
the class ReprEmitter method emit.
public static void emit(RepeatedInst inst, StringBuilder buf, boolean recurse) {
buf.append('{');
emitPreprocess(inst, buf);
buf.append(".repeated section ");
emitNames(inst.getVariable(), buf);
buf.append('}');
if (recurse) {
// Special case of a block, since we want the "alternates with" block to
// be emitted between the consequent and alternative.
emitBlock(inst.getConsequent(), buf, recurse);
AlternatesWithInst a2 = inst.getAlternatesWith();
if (a2 != null) {
a2.repr(buf, recurse);
}
Instruction alt = inst.getAlternative();
if (alt != null) {
alt.repr(buf, recurse);
}
}
}
Aggregations