-
Notifications
You must be signed in to change notification settings - Fork 1
Parse Event Listener
It is possible to listen for parse events that are posted during the parse process. To do this, simply extend the ParseEventListener class and overwrite the corresponding handler methods. Finally, the listener must be registered at the parser.
class MyListener : ParseEventListener<Unit>() {
override fun beforeParse(event: PreParseEvent<Unit>) {
// ...
}
override fun beforeMatch(event: PreMatchEvent<Unit>) {
// ...
}
override fun afterMatchSuccess(event: MatchSuccessEvent<Unit>) {
// ...
}
override fun afterMatchFailure(event: MatchFailureEvent<Unit>) {
// ...
}
override fun afterParse(event: PostParseEvent<Unit>) {
// ...
}
}
val myParser = Parser(/* ... */).apply {
registerListener(MyListener())
}
This event is posted at the very beginning of a parser run. It holds the current rule context.
This event is posted just before a rule is run (i.e. its match
function is called). It holds the current rule context.
This event is posted right after a rule has succesfully matched (i.e. its match
function returned true
). It holds the current rule context.
This event is posted right after a rule failed to match (i.e. its match
function returned false
). It holds the current rule context.
This event is posted at the very ending of a parser run, regardless of whether it was successful or not. It holds the parse result.
It is also possible to post and handle your own events.
You have to add a function to the listener that is annotated with @org.greenrobot.eventbus.Subscribe
.
The event to listen to may have any type. For more information about the Eventbus in use see greenrobot/EventBus.
class MyListener : ParseEventListener<Unit>() {
@Subscribe
override fun listenToStrings(event: String) {
// ...
}
@Subscribe
override fun listenToInts(event: Int) {
// ...
}
}
Then, inside your rule functions, you may post events like this:
open class MyGrammar : AbstractGrammar<Unit>() {
override fun start() = sequence(
post("myStringEvent"),
post(42)
)
}
See Rules#post for more details about how to post events.