When designing parsers as a reusable library (rather than a bespoke parser for a single purpose), we have the choice to make push parser and pull parser.
Push Parsers—Events are generated by the API in the form of callback functions like startDocument()
and endDocument()
and are beyond the programmer’s control. We as programmers could handle the events, but the generation of events is beyond our control.
Pull Parsers - Events are generated when we call some API. Example shown below. So we, as programmers, can decide when to generate events. 1
Push:
if (myChar == '(')
handler.handleOpenParen(); // push the open paren to the handler
Pull:
Token token = parser.next(); // pull the next token from the parser
Push vs pull can be seen as a special instance of internal vs external iterator.
Pros and cons
Some advantages of a pull parser include loose coupling, less memory usage, and ease of use from the user side. 2