A | B != B | A - cats-parse

maybe, remove this | method. just use orElse

Asked Sep 29 '21 07:09
avatar timzaak
timzaak

6 Answer:

While it is true that | is not commutative (a.backtrack | b.backtrack would be), having an infix operator is very nice.

Do you have any other suggestions?

1
Answered Mar 27 '21 at 17:38
avatar  of johnynek
johnynek

maybe | would act like oneOf.

I just write graphql schema parser. Parser2.scala. there are some user experience to share: 1. need insert lots of whitespaces, maybe it's a good idea to create ParserContext, just like fastparse to handle whitespaces. 2. so much backtrack, and ? always need backtrack. maybe it's my code fault, there would have nice way to avoid using so much backtrack. 3. P[A] ~ (P[B].surround(whitespaces).backtrack.? ~ P[C].surround(whitespaces).backtrack.?) will read whitespaces twice when P[B] fails. but I don't know how to avoid.

1
Answered Mar 28 '21 at 09:54
avatar  of timzaak
timzaak

PEGs use / for their non-commutative "or" for this reason - would that fit here?

1
Answered Mar 28 '21 at 12:23
avatar  of ritschwumm
ritschwumm

Sorry I'm slow to reply on the weekend.

You definitely don't always need backtrack with ?. I'll rewrite some of your examples without backtrack and it might give you a feel for how to do it.

A big design property of this library is to encourage you to write parsers avoiding backtracking since that can easily result in accidental exponential complexity and also it generally results in poor descriptions of what went wrong.

That said, we could probably do with better educational materials to help people get started.

1
Answered Mar 29 '21 at 04:32
avatar  of johnynek
johnynek

Also note | does act like oneOf. It is a synonym. oneOf is also not commutative, it matches left to right.

1
Answered Mar 29 '21 at 04:32
avatar  of johnynek
johnynek

@johnynek thanks. And I'm sorry that I misunderstand the usage of |. It's good.

1
Answered Mar 29 '21 at 08:47
avatar  of timzaak
timzaak