V
- the result type of the parserpublic abstract class ListeningParser<V> extends BaseParser<V>
EventBus
Basic usage of this parser is as follows:
A "listening method" is a method which obeys the following conditions:
public
;Note that the return values of this method can be anything; generally,
though, such methods return void
. Also note that these methods will
not only receive objects of the exact type they have subscribed to, but any
subtype as well; a method accepting a Number
, for instance, will also
receive Integer
s and Double
s.
An example class would be:
public class MyListener { private String value; @Subscribe public void setValue(final String value) { this.value = value; } }
In your parser you could then do:
Rule myRule() { return sequence(oneOrMore('a'), postRaw(match())); }
The parser class also has two other methods to post events: one taking a
ValueBuilder
as an argument and another taking a Var
as an
argument. These methods will extract the values from both of these classes
and feed it to the event bus.
For more details on how the bus work, see the documentation for this class
, along with Guava's user guide article.
ValueBuilder
Modifier and Type | Field and Description |
---|---|
protected EventBus |
bus |
ANY, EMPTY, EOI, NOTHING
Constructor and Description |
---|
ListeningParser() |
Modifier and Type | Method and Description |
---|---|
<T> boolean |
post(ValueBuilder<T> builder)
Post a value on the bus from a
ValueBuilder |
<T> boolean |
post(Var<T> var)
Post a value on the bus from a
Var |
boolean |
postRaw(Object object)
"Raw" post to the bus
|
boolean |
register(Object listener)
Register a listener to the event bus
|
boolean |
unregister(Object listener)
Unregister a listener from this parser's
EventBus |
ACTION, alpha, anyOf, anyOf, anyOf, asciiChars, bit, ch, charRange, cr, crlf, ctl, digit, dquote, empty, eof, firstOf, firstOf, fromCharArray, fromCharLiteral, fromStringLiteral, hexDigit, hexDigitLowerCase, hexDigitUpperCase, hTab, ignoreCase, ignoreCase, ignoreCase, join, join, lf, noneOf, noneOf, nTimes, octet, oneOrMore, oneOrMore, optional, optional, regex, repeat, repeat, sequence, sequence, sp, string, string, test, test, testNot, testNot, toRule, toRules, trie, trie, trieIgnoreCase, trieIgnoreCase, unicodeChar, unicodeRange, vchar, wsp, zeroOrMore, zeroOrMore
currentChar, currentIndex, drop, drop, dup, getContext, hasError, inPredicate, match, matchedChar, matchEnd, matchLength, matchOrDefault, matchRange, matchStart, peek, peek, peekAs, peekAs, poke, poke, pop, pop, popAs, popAs, position, push, push, setContext, swap, swap
protected final EventBus bus
public final boolean register(@Nonnull Object listener)
The canonical use of this method is to register a listener once you have created your parser, as in:
final MyParser parser = Parboiled.createParser(MyParser.class); parser.register(myListener);
It is also usable as an action; you can write rules such as:
Rule myRule() { return sequence(other(), register(...)); }
listener
- the listenerEventBus.register(Object)
public final boolean unregister(@Nonnull Object listener)
EventBus
In a similar fashion as register(Object)
, you can use this
method as an action.
listener
- the listener to removepublic final <T> boolean post(@Nonnull ValueBuilder<T> builder)
ValueBuilder
This method will build
the value, post
the result on the bus and then reset
the
value builder.
T
- the value type produced by the builderbuilder
- the value buildertrue
public final <T> boolean post(@Nonnull Var<T> var)
Var
This method will get
the value of the associated
var and post it on the bus.
Notes:
T
- value type of the varvar
- the var to use