@Override public Object get(Context<Object> context) { return context.getValueStack().pop(); }
@Override public final boolean run(Context<Object> context) { V value = extractor.get(context); @SuppressWarnings("unchecked") B builder = (B) context.getValueStack().peek(); specify(builder, value); return true; }
@Override public final boolean run(Context<Object> context) { B builder = builder(); context.getValueStack().push(builder); return true; }
<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; }
@Override public boolean run(Context<CfgElement> context) { final ValueStack<CfgElement> stack = context.getValueStack(); if (!context.hasError()) { int size = stack.size(); switch (size) { case 1: CfgElement elemKey = stack.pop(); parsedProps.setProperty(unescape(elemKey.getText()), ""); cfgFile.getElements().add(new PairElement(elemKey)); break; case 2: // NOTE: stack popping order below is important! final CfgElement elemValue = stack.pop(); elemKey = stack.pop(); parsedProps.setProperty(unescape(elemKey.getText()), unescape(elemValue.getText())); cfgFile.getElements().add(new PairElement(elemKey, elemValue)); break; default: throw new IllegalStateException(String.format("Cannot manage %d values on the parsing stack", size)); } } else { stack.clear(); } return true; }
@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); } } }
@Override public boolean run(final Context<BaseTemplate> context) { ValueStack<BaseTemplate> stack = context.getValueStack(); if (stack.size() > 1) { BaseTemplate body = pop(); ((Block) section.get()).inverse(body); } return addToline(section.get()); } }
@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)); } }
/** * 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"); }
@Override public boolean run(final Context<BaseTemplate> context) { ValueStack<BaseTemplate> stack = context.getValueStack(); if (stack.size() > 1) { BaseTemplate body = pop(); ((Block) section.get()).body(body); } return addToline(section.get()); } }).label("block");
@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)); }
@Override public final T get(Context<Object> context) { @SuppressWarnings("unchecked") B builder = (B) context.getValueStack().pop(); return build(builder); }
@Override public final boolean run(Context<Object> context) { Object value = get(context); context.getValueStack().push(value); return true; } }
boolean setListItemIndices() { SuperNode listItem = (SuperNode) getContext().getValueStack().peek(); List<Node> children = listItem.getChildren(); listItem.setStartIndex(children.get(0).getStartIndex()); listItem.setEndIndex(children.get(children.size() - 1).getEndIndex()); return true; }
/** * Creates CSV value. * * @return An CSV value. */ protected CsvReaderValue createCsvReaderValue() { CSVParserSettings parserSettings = null; switch (getContext().getValueStack().size()) { case 1: parserSettings = new CSVParserSettings((String) pop()); break; case 2: parserSettings = new CSVParserSettings((String) pop(1), (char) pop()); break; case 8: parserSettings = new CSVParserSettings((String) pop(7), (char) pop(6), (String) pop(5), (boolean) pop(4), peek(3) instanceof NullValue ? null : (Character) pop(3), (char) pop(2), (boolean) pop(1), peek() instanceof NullValue ? null : (String) pop()); break; default: throw new RuntimeException("Unsupported number of parameters, should not happen ever."); } return new CsvReaderValue(parserSettings); }
@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; } });
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 boolean run(Context ctx) { String match = match(); if (match.endsWith("k") || match.endsWith("M") || match.endsWith("G")) { match = scaleValueToString(match); } ctx.getValueStack().push(new Value.Literal(match)); return true; } });
/** * Returns the value the given number of elements below the top of the value stack without removing it. * * @param down the number of elements to skip (0 being equivalent to peek()) * @return the value * @throws IllegalArgumentException if the stack does not contain enough elements to perform this operation */ public V peek(int down) { check(); return context.getValueStack().peek(down); }
/** * Creates CSV value. * * @return An CSV value. */ protected CsvReaderValue createCsvReaderValue() { CSVParserSettings parserSettings = null; switch (getContext().getValueStack().size()) { case 1: parserSettings = new CSVParserSettings((String) pop()); break; case 2: parserSettings = new CSVParserSettings((String) pop(1), (char) pop()); break; case 8: parserSettings = new CSVParserSettings((String) pop(7), (char) pop(6), (String) pop(5), (boolean) pop(4), peek(3) instanceof NullValue ? null : (Character) pop(3), (char) pop(2), (boolean) pop(1), peek() instanceof NullValue ? null : (String) pop()); break; default: throw new RuntimeException("Unsupported number of parameters, should not happen ever."); } return new CsvReaderValue(parserSettings); }