Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

idea for safe repetition on Parser0 #344

Open
johnynek opened this issue Jan 3, 2022 · 0 comments
Open

idea for safe repetition on Parser0 #344

johnynek opened this issue Jan 3, 2022 · 0 comments

Comments

@johnynek
Copy link
Collaborator

johnynek commented Jan 3, 2022

We don't allow p.rep on a p: Parser0[_] because if you parse nothing and that is valid, at parse time you would loop infinitely not making progress.

Another idea could be:

def safeRep: Parser0[(NonEmptyList[A], Boolean)] =
  (Parser.index, this, Parser.index)
    .tupled
    .flatMap {
      case (s, a, e) =>
         if (s == e) {
           // No progress, stop
           Parser.pure((NonEmptyList(a, Nil), false))
         }
         else safeRep.?.map {
           case None => (NonEmptyList(a, Nil), true)
           case Some((tail, r)) => (a :: tail, r)
         }
     }

the boolean tells if the last item made progress or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant