ZeroOrMoreExpression implements ParsingExpression { @Override public Instruction[] compile(CompilationHandler compiler) { Instruction[] sub = compiler.compile(subExpression); Instruction[] result = new Instruction[sub.length + 2]; result[0] = Instruction.choice(sub.length + 2); System.arraycopy(sub, 0, result, 1, sub.length); result[sub.length + 1] = Instruction.commitVerify(-1 - sub.length); return result; } ZeroOrMoreExpression(ParsingExpression subExpression); @Override Instruction[] compile(CompilationHandler compiler); @Override String toString();  }
@Test public void should_compile() { ZeroOrMoreExpression expression = new ZeroOrMoreExpression(new SubExpression(1, 2)); Instruction[] instructions = expression.compile(new CompilationHandler()); assertThat(instructions).isEqualTo(new Instruction[] { Instruction.choice(4), SubExpression.mockInstruction(1), SubExpression.mockInstruction(2), Instruction.commitVerify(-3) }); }