<T> List<T> popAll(Class... classes) { ValueStack<Object> valueStack = getContext().getValueStack(); List<T> result = new ArrayList<T>(); while (!valueStack.isEmpty() && isInstance(classes, valueStack.peek())) { result.add((T) valueStack.pop()); } if (!valueStack.isEmpty() && valueStack.peek() == MARKER) { valueStack.pop(); } Collections.reverse(result); return result; }
@SuppressWarnings({"ConstantConditions"}) public void createNode() { if (!nodeSuppressed) { node = new NodeImpl<V>(matcher, getSubNodes(), startIndex, currentIndex, valueStack.isEmpty() ? null : valueStack.peek(), hasError); if (parent != null) { parent.subNodes = parent.subNodes.prepend(node); } } }
@Test public void set() throws Exception { ParsingResult<SetNode> result = parse(underTest.NodeRule(), "{% do 1 + 123 %}"); assertThat(result.matched, is(true)); assertThat(result.valueStack.isEmpty(), is(false)); }
@Test public void set() throws Exception { ParsingResult<SetNode> result = parse(underTest.NodeRule(), "{% set a = 123 %}"); assertThat(result.matched, is(true)); assertThat(result.valueStack.isEmpty(), is(false)); } }
@Override public boolean run(Context ctx) { List contents = (List) pop(); Selector selector = (Selector) pop(); String comment = null; if (!ctx.getValueStack().isEmpty() && peek() instanceof String) { comment = (String) pop(); comment = comment.trim(); // get rid of the extra comments between rules while (!ctx.getValueStack().isEmpty() && peek() instanceof String) { pop(); } } final Stream stream = contents.stream(); Map<Boolean, List> splitContents = (Map<Boolean, List>) stream.collect( Collectors.partitioningBy( x -> x instanceof CssRule)); List<Property> properties = splitContents.get(Boolean.FALSE); List<CssRule> subRules = splitContents.get(Boolean.TRUE); final CssRule rule = new CssRule(selector, properties, comment); rule.nestedRules = subRules; push(rule); return true; } });
/** * Creates a new ParsingResult. * * @param matched true if the rule matched the input * @param parseTreeRoot the parse tree root node * @param valueStack the value stack of the parsing run * @param parseErrors the list of parse errors * @param inputBuffer the input buffer */ public ParsingResult(boolean matched, Node<V> parseTreeRoot, ValueStack<V> valueStack, List<ParseError> parseErrors, InputBuffer inputBuffer) { this.matched = matched; this.parseTreeRoot = parseTreeRoot; this.valueStack = checkArgNotNull(valueStack, "valueStack"); this.resultValue = valueStack.isEmpty() ? null : valueStack.peek(); this.parseErrors = checkArgNotNull(parseErrors, "parseErrors"); this.inputBuffer = checkArgNotNull(inputBuffer, "inputBuffer"); }