Skip to content

Greedy parse problem #146

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

You must be logged in to vote

The reason this doesn't work for the whitespace before the closing is that char("*").neg().plus() reads over the whitespace and to right before the asterisk. You can fix this like so:

final parser = seq3(
  char("*"),
  seq2(
    whitespace().not(),
    [seq2(whitespace(), char('*')), char('*')]
        .toChoiceParser()
        .neg()
        .plus(),
  ).flatten(),
  char("*"),
);

Easier and maybe closer to what you want is the following:

final parser = seq3(
  char("*"),
  char('*').neg().plus().flatten(),
  char("*"),
).where((sequence) => sequence.second.trim() == sequence.second);

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
Converted from issue

This discussion was converted from issue #145 on March 10, 2023 13:29.