@Override public Object get(Context<Object> context) { return context.getValueStack().pop(); }
@Override public final T get(Context<Object> context) { @SuppressWarnings("unchecked") B builder = (B) context.getValueStack().pop(); return build(builder); }
Node parseListBlock(StringBuilderVar block) { Context<Object> context = getContext(); Node innerRoot = parseInternal(block); setContext(context); // we need to save and restore the context since we might be recursing block.clearContents(); //debugMsg("parsed list block " + innerRoot.toString() + " adjusting indices by " + getContext().getValueStack().peek(), block.getString()); return withIndicesShifted(innerRoot, (Integer) context.getValueStack().pop()); }
@Override public Object get(Context<Object> context) { return context.getValueStack().pop(); }
/** * Removes the value at the top of the value stack and returns it. * * @return the current top value * @throws IllegalArgumentException if the stack is empty */ public V pop() { check(); return context.getValueStack().pop(); }
/** * Removes the value the given number of elements below the top of the value stack. * * @param down the number of elements to skip before removing the value (0 being equivalent to drop()) * @return true * @throws IllegalArgumentException if the stack does not contain enough elements to perform this operation */ public boolean drop(int down) { check(); context.getValueStack().pop(down); return true; }
/** * Removes the value the given number of elements below the top of the value stack. * * @param down the number of elements to skip before removing the value (0 being equivalent to pop()) * @return the value * @throws IllegalArgumentException if the stack does not contain enough elements to perform this operation */ public V pop(int down) { check(); return context.getValueStack().pop(down); }
@Override public final T get(Context<Object> context) { @SuppressWarnings("unchecked") B builder = (B) context.getValueStack().pop(); return build(builder); }
/** * Removes the value at the top of the value stack. * * @return true * @throws IllegalArgumentException if the stack is empty */ public boolean drop() { check(); context.getValueStack().pop(); return true; }
public static ExpressionList parse (String input) { if (parser == null) parser = Parboiled.createParser(PluralPropertyParser.class); ParsingResult<Object> result = new RecoveringParseRunner<Object>(parser.Translate()).run(input); Object e = result.valueStack.pop(); return (ExpressionList) e; }
@Test public void blockWithEnd() throws Exception { ParsingResult<BlockNode> result = parse(underTest.NodeRule(), "{% block test %}{% endblock test %}"); assertThat(result.matched, is(true)); BlockNode node = result.valueStack.pop(); assertThat(node.getBlockIdentifier().getIdentifier(), is("test")); }
@Test public void NodeRuleSimple() throws Exception { ParsingResult<IncludeNode> result = parse(underTest.NodeRule(), "{% include 'test' %}"); assertThat(result.matched, is(true)); IncludeNode node = result.valueStack.pop(); assertThat(node.isIgnoreMissing(), is(false)); assertThat(node.isInheritModel(), is(true)); assertThat(node.getMapExpression(), instanceOf(MapExpression.class)); }
@Test public void trueExpression() throws Exception { ParsingResult<Expression> result = parse(underTest.ExpressionRule(), "true"); assertThat(result.matched, is(true)); Expression expression = result.valueStack.pop(); assertThat(expression, is(instanceOf(ConstantExpression.class))); ConstantExpression variableExpression = (ConstantExpression) expression; assertEquals(variableExpression.getConstantValue(), true); assertThat(variableExpression.getPosition().getLine(), is(1)); assertThat(variableExpression.getPosition().getColumn(), is(1)); }
@Test public void output() throws Exception { ParsingResult<OutputNode> result = parse(underTest.NodeRule(), "{{ one }}"); assertThat(result.matched, is(true)); OutputNode outputNode = result.valueStack.pop(); assertThat(outputNode.getExpression(), instanceOf(VariableExpression.class)); } }
@Test public void multiBinaryOperationExpression() throws Exception { ParsingResult<Expression> result = parse(underTest.ExpressionRule(), "2 * 2 - 2"); assertThat(result.matched, is(true)); Expression expression = result.valueStack.pop(); assertThat(expression, instanceOf(BinaryOperationExpression.class)); BinaryOperationExpression exp = (BinaryOperationExpression) expression; assertThat(exp.getBinaryOperator(), instanceOf(SubtractOperator.class)); }
@Test public void ifNodeWithElseIf() throws Exception { ParsingResult<IfNode> result = parse(underTest.NodeRule(), "{% if (true) %}{% elseif (false) %}{% endif %}"); assertThat(result.matched, is(true)); IfNode ifNode = result.valueStack.pop(); assertThat(ifNode.getConditionNodes().size(), is(2)); } @Test
@Test public void ifNodeWithMultipleElseIf() throws Exception { ParsingResult<IfNode> result = parse(underTest.NodeRule(), "{% if (true) %}{% elseif (false) %}{% elseif (false) %}{% endif %}"); assertThat(result.matched, is(true)); IfNode ifNode = result.valueStack.pop(); assertThat(ifNode.getConditionNodes().size(), is(3)); } }
@Test public void macroNodeWithNames() throws Exception { ParsingResult<MacroNode> result = parse(underTest.NodeRule(), "{% macro test(jtwig) %}{% endmacro %}"); assertThat(result.matched, is(true)); MacroNode macroNode = result.valueStack.pop(); assertThat(macroNode.getMacroName().getIdentifier(), is("test")); assertThat(macroNode.getMacroArgumentNames().size(), is(1)); assertThat(macroNode.getMacroArgumentNames().iterator().next(), is("jtwig")); } }
@Test public void binaryOperationExpression() throws Exception { ParsingResult<Expression> result = parse(underTest.ExpressionRule(), "2 + 2"); assertThat(result.matched, is(true)); Expression expression = result.valueStack.pop(); assertThat(expression, instanceOf(BinaryOperationExpression.class)); BinaryOperationExpression exp = (BinaryOperationExpression) expression; assertThat(exp.getLeftOperand(), instanceOf(ConstantExpression.class)); assertThat(exp.getRightOperand(), instanceOf(ConstantExpression.class)); assertThat(exp.getBinaryOperator(), instanceOf(SumOperator.class)); }
@Test public void TernaryUnaryExpression() throws Exception { ParsingResult<Expression> result = parse(underTest.ExpressionRule(), "not(2 == 2) ? not 1 : 2"); assertThat(result.matched, is(true)); Expression expression = result.valueStack.pop(); assertThat(expression, instanceOf(TernaryOperationExpression.class)); TernaryOperationExpression exp = (TernaryOperationExpression) expression; assertThat(exp.getFirstExpression(), instanceOf(UnaryOperationExpression.class)); assertThat(exp.getSecondExpression(), instanceOf(UnaryOperationExpression.class)); assertThat(exp.getThirdExpression(), instanceOf(ConstantExpression.class)); } }