Skip to content

Alternative to trim parser or how to discard unwanted input? #149

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

You must be logged in to vote

Thank you for the kind words and the question with the great examples.

This is not something that is provided out of the box from this package. I don't think trim is a good stand-in here, because it consumes the opening and closing params an arbitrary amount of times. Furthermore, it also passes if the before and after characters are entirely absent.

Probably the easiest solution that comes closest to your version 2 would be to add a helper extension, like so:

extension SurroundedParserExtension<T> on Parser<T> {
  Parser<T> surroundedBy(Parser<void> left, [Parser<void>? right]) =>
      [left, this, right ?? left].toSequenceParser().pick(1).cast<T>();
}

final parserV2 = digit().map(int.…

Replies: 3 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
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
Converted from issue

This discussion was converted from issue #123 on June 09, 2023 12:15.