-
Notifications
You must be signed in to change notification settings - Fork 53
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
Why is Parser0#repSep not possible? #205
Comments
A combinator to repeat a def rep0sep0[A](data: Parser0[A], separator: Parser[Any]): Parser0[List[A]] =
(data.? ~ (separator *> data).rep0).map { case (a, as) => a ++: as }
def rep0sep[A](data: Parser0[A], separator: Parser[Any]): Parser0[NonEmptyList[A]] = (data ~ (separator *> data).rep0).map {
case (a, as) => NonEmptyList(a, as)
} Then your use case becomes val sep = char('/')
val part = until0(charIn("/?#"))
val path = sep.? *> rep0sep0(part, sep)
val _path = path.string |
We could add a function like this. I'd be happy to do that. |
By the way, I think a totally empty path is not a valid path is it? So, even this function isn't great since it will match nothing. It seems like a better way to write this parser would be to consider a few cases:
In this way, you still get a |
I thought about adding this code... but then I realized that we probably want to be a bit more thoughtful since we have min and max accepting versions of repSep, we probably want to mirror that. But then, if you repeat 2 or more times with a sep that is non-empty you get a In this way, you have |
I have some difficulties understanding why we need |
Unbounded repetition on a Parser0 isn't safe: it can just run forever allocating more memory parsing an infinite number of items making no progress. This is not an uncommon error when working with Parser combinators. The blog post linked in the read me of this repo covers this: https://posco.medium.com/designing-a-parsing-library-in-scala-d5076de52536 |
Hi all,
I'm trying to port scala-uri to cats-parse.
One difficulty I'm facing is with parsing path parts including empty ones:
What I would like to do is something like this (simplified)
but this doesn't work, as
Parser0
doesn't haverepSep
orrepSep0
defined.I understand that
Parser0#rep
is problematic, as one could easily run the empty parser infinitely, but in my naive thinking this problem shouldn't exist withrepSep
, right?Or is there a nicer way to do this in cats-parse?
The text was updated successfully, but these errors were encountered: