3 ms·
This is a reasonable pattern commonly used to implement recursive-descent parsers, where you have a parsing function for each language construct but they all ne
by skybrian 7y ago
This is a reasonable pattern commonly used to implement recursive-descent parsers, where you have a parsing function for each language construct but they all need to access common state (such as the lexer and the error output). Passing this common state as separate parameters becomes tedious, so it makes sense to group them into a struct.
It's not all that well-motivated in the article, though, since helper functions aren't in themselves a code smell. You can just make them private. It's also not strictly necessary to convert the helper functions into methods on the object holding the state, though this can be convenient.
Also, the state object is an implementation detail and should usually be private. The public API of of the parser can be a pure function.