Skip to content

How to parametrize a parser? #108

Answered by renggli
venkatd asked this question in Q&A
Discussion options

You must be logged in to vote

Traditionally the parsing and resolution would be done separately. That means you create a generic parser that would extract all user-names and then in a second step you would check that list against your list of valid usernames.

That said, with PetitParser creating dynamic parsers is very cheap. There is nothing that speaks against creating something like this:

Parser<Token<String>> getUserNamesParser(List<String> usernames) => usernames
    .map((username) => '@$username'.toParser())
    .toChoiceParser()
    .token();

If you parser is large and complex and you don't want to create everything from scratch each time, you could even modify the existing one: Keep a settable reference that …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by renggli
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants