@Override public String get(Context<Object> context) { return context.getMatch(); }
@Override public Position get(Context<Object> context) { return context.getPosition(); }
/** * Creates a new processor instance with the given {@link org.pegdown.Extensions}, parsing timeout and plugins. * * @param options the flags of the extensions to enable as a bitmask * @param maxParsingTimeInMillis the parsing timeout * @param plugins the plugins to use */ public PegDownProcessor(int options, long maxParsingTimeInMillis, PegDownPlugins plugins) { this(Parboiled.createParser(Parser.class, options, maxParsingTimeInMillis, Parser.DefaultParseRunnerProvider, plugins)); }
public static void main(String... args) { Parser templateParser = Parboiled.createParser(Parser.class); ParsingResult<Object> result = new ReportingParseRunner<>(templateParser.Unit()).run(input2); ImmutableList<Object> copy = ImmutableList.copyOf(result.valueStack.iterator()); if (!copy.isEmpty()) { Unit unit = (Unit) copy.get(0); Unit balance = Balancing.balance(unit); System.out.println(balance); } if (result.hasErrors()) { System.err.println(ErrorUtils.printParseErrors(result.parseErrors)); } // System.out.println(ParseTreeUtils.printNodeTree(result)); } }
@Cached public Rule TaskListItem(Rule itemStart, SuperNodeTaskItemCreator itemNodeCreator) { StringBuilderVar block = new StringBuilderVar(); StringBuilderVar taskListMarker = new StringBuilderVar(); StringBuilderVar temp = new StringBuilderVar(); Var<Boolean> tight = new Var<Boolean>(false); Var<Integer> taskType = new Var<Integer>(0); Var<SuperNode> tightFirstItem = new Var<SuperNode>(); return Sequence( push(getContext().getCurrentIndex()), FirstOf(CrossedOut(BlankLine(), block), tight.set(true)), CrossedOut(itemStart, block), Optional(CrossedOut(Sequence(FirstOf(Sequence("[ ]", taskType.set(1)), Sequence(FirstOf("[x]","[X]"), taskType.set(2))), OneOrMore(Spacechar())), taskListMarker)), block.append(taskListMarker.getString()), Line(block), TestNotItem(), Line(temp), block.append(temp.getString()) && temp.clearContents() ), tight.get() ? push(tightFirstItem.setAndGet(itemNodeCreator.create(parseListBlock(block), taskType.get(), taskListMarker.getString()))) && taskListMarker.clearContents() : fixFirstItem((SuperNode) peek(1)) && push(itemNodeCreator.create(parseListBlock(block.appended('\n')), taskType.get(), taskListMarker.getString())) && taskListMarker.clearContents(), ZeroOrMore( push(getContext().getCurrentIndex()), FirstOf(Sequence(Test(BlankLine()), tight.set(false)), tight.set(true)), ListItemIndentedBlocks(block),
private static void printContext(Object caller, Context<Object> context) { String message = ErrorUtils.printErrorMessage("%s:%s:%s", "", context.getCurrentIndex(), context.getCurrentIndex() + 1, context.getInputBuffer()); System.err.println("*** " + caller + message + "\n\t" + Joiner.on("\n\t - ").join(context.getValueStack())); } }
@Override public Object get(Context<Object> context) { return context.getValueStack().pop(); }
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()); }
public Rule TableRow() { Var<Boolean> leadingPipe = new Var<Boolean>(Boolean.FALSE); return NodeSequence( push(new TableRowNode()), Optional('|', leadingPipe.set(Boolean.TRUE)), OneOrMore(TableCell(), addAsChild()), leadingPipe.get() || ((Node) peek()).getChildren().size() > 1 || getContext().getInputBuffer().charAt(matchEnd() - 1) == '|', Sp(), Newline() ); }
@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) { Object value = get(context); context.getValueStack().push(value); return true; } }
public Rule SingleQuoted() { return NodeSequence( !Character.isLetter(getContext().getInputBuffer().charAt(getContext().getCurrentIndex() - 1)), '\'', push(new QuotedNode(QuotedNode.Type.Single)), OneOrMore(TestNot(SingleQuoteEnd()), Inline(), addAsChild()), SingleQuoteEnd() ); }
/** * Returns the current index in the input buffer. * * @return the current index */ public int currentIndex() { check(); return context.getCurrentIndex(); }
public Rule NodeSequence(Object... nodeRules) { return Sequence( push(getContext().getCurrentIndex()), Sequence(nodeRules), setIndices() ); }
/** * <p>Returns the end location of the rule immediately preceding the action expression that is currently * being evaluated. This call can only be used in actions that are part of a Sequence rule and are not at first * position in this Sequence.</p> * * @return the end index of the context immediately preceding current action, i.e. the index of the character * immediately following the last matched character */ public int matchEnd() { check(); return context.getMatchEndIndex(); }
@Cached public Rule ListItem(Rule itemStart, SuperNodeCreator itemNodeCreator) { StringBuilderVar block = new StringBuilderVar(); StringBuilderVar temp = new StringBuilderVar(); Var<Boolean> tight = new Var<Boolean>(false); Var<SuperNode> tightFirstItem = new Var<SuperNode>(); return Sequence( push(getContext().getCurrentIndex()), FirstOf(CrossedOut(BlankLine(), block), tight.set(true)), CrossedOut(itemStart, block), Line(block), ZeroOrMore( TestNotItem(), Line(temp), block.append(temp.getString()) && temp.clearContents() ), tight.get() ? push(tightFirstItem.setAndGet(itemNodeCreator.create(parseListBlock(block)))) : fixFirstItem((SuperNode) peek(1)) && push(itemNodeCreator.create(parseListBlock(block.appended('\n')))), ZeroOrMore( push(getContext().getCurrentIndex()), FirstOf(Sequence(Test(BlankLine()), tight.set(false)), tight.set(true)), ListItemIndentedBlocks(block), (tight.get() ? push(parseListBlock(block)) : ((!ext(FORCELISTITEMPARA)) || tightFirstItem.isNotSet() || wrapFirstItemInPara(tightFirstItem.get())) &&
/** * Add a plugin parser. This should either implement {@link InlinePluginParser} or {@link BlockPluginParser}, * or both. The parser will be enhanced by parboiled before its rules are extracted and registered here. * * @param pluginParser the plugin parser class. * @param arguments the arguments to pass to the constructor of that class. */ public Builder withPlugin(Class<? extends BaseParser<Object>> pluginParser, Object... arguments) { // First, check that the parser implements one of the parser interfaces if (!(InlinePluginParser.class.isAssignableFrom(pluginParser) || BlockPluginParser.class.isAssignableFrom(pluginParser))) { throw new IllegalArgumentException("Parser plugin must implement a parser plugin interface to be useful"); } BaseParser<Object> parser = Parboiled.createParser(pluginParser, arguments); if (parser instanceof InlinePluginParser) { withInlinePluginRules(((InlinePluginParser) parser).inlinePluginRules()); } if (parser instanceof BlockPluginParser) { withBlockPluginRules(((BlockPluginParser) parser).blockPluginRules()); } return this; }
@Override public final T get(Context<Object> context) { @SuppressWarnings("unchecked") B builder = (B) context.getValueStack().pop(); return build(builder); }
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; }
@Override public final boolean run(Context<Object> context) { B builder = builder(); context.getValueStack().push(builder); return true; }