Dmitry Kandalov
08/27/2023, 2:26 PMoneOrMore parser does capture multiple space-separated tokens, but it expects them to be directly adjacent to one another. In other words, itâs expecting input like REMOVE foofoofoo instead of REMOVE foo bar because thereâs no parser defined to handle the space between the tokens. To solve this, you can explicitly specify that youâre expecting space-separated tokens. Hereâs how you can modify the Remove parser to handle multiple space-separated tokens:
private val Remove: ExprFactory = { parser ->
inOrder(
token("REMOVE"),
oneOrMore(
inOrder(
Tokens.space.optional(),
UpdateAttributeName(parser)
).skipFirst()
)
).skipFirst().map { names ->
Expr { item ->
val attributeNames = names.map { it.eval(item) }
item.item.filterKeys { it !in attributeNames }
}
}
}
Hereâs a breakdown of the changes:
1. Inside the oneOrMore parser, I wrapped the UpdateAttributeName(parser) with another inOrder parser.
2. This new inOrder parser starts with Tokens.space.optional(), which tries to parse a space but doesnât fail if there isnât one.
3. I used .skipFirst() to make sure the optional space doesnât get included in the parsed result.
Now, Remove should be able to parse inputs like REMOVE foo bar.