Comments (12)
@johnynek thanks, I will rewrite my code. It's a very useful advice.
from cats-parse.
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?
from cats-parse.
maybe |
would act like oneOf
.
I just write graphql schema parser.
Parser2.scala.
there are some user experience to share:
- need insert lots of whitespaces, maybe it's a good idea to create ParserContext, just like fastparse to handle whitespaces.
- so much
backtrack
, and?
always needbacktrack
. maybe it's my code fault, there would have nice way to avoid using so much backtrack. 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.
from cats-parse.
PEGs use /
for their non-commutative "or" for this reason - would that fit here?
from cats-parse.
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.
from cats-parse.
Also note | does act like oneOf. It is a synonym. oneOf is also not commutative, it matches left to right.
from cats-parse.
@johnynek thanks. And I'm sorry that I misunderstand the usage of |
. It's good.
from cats-parse.
intuitively, i'd expect |
to be commutative, too and therefore rename it to /
. or is this a problem concerning operator precedence?
from cats-parse.
related to #184
from cats-parse.
While /
is a good suggestion, the cost of breaking compatibility is significant... I can imagine adding /
as another synonym of orElse
but then we have three equivalent methods: orElse, /
and |
.
I'd like to take it a bit slow.
from cats-parse.
Hey @timzaak you have quite a bit of nice parsing code. One thing you don't seem to be using is "soft products", A normal product a ~ b
will have an arresting failure if a parses and b has any failure at all... So, once a parses you expect b MUST come next. A soft product only fails if at least one has an arresting failure. So, a.soft ~ b
will not be an arresting failure if a parses but b has an epsilon error (it fails to parse without a partial parse).
So, for instance:
pa ~ (pb.surround(whitespaces).backtrack.? ~ pc.surround(whitespaces).backtrack.?)
we can write instead as:
val hasB = pb ~ (whitespaces.soft *> pc).?
pa ~ (whitespaces.soft *> (hasB | pc)).?
So, now, we are very precise in the backtracking: only over the whitespace part. We are not erasing an arresting error as we did above: see pb.surround(whitespaces).backtrack
what if you hit something in pb
that could not parse: an arresting failure indicates a partial parse which we generally strive to be a real syntax error. By backtracking there, we are hiding from the user the real cause of the problem and presenting them with a confusing error when instead we wind up hitting the None
branches in ?
. This is the key goal of avoiding backtracking: it very easily hides the true syntax error from the user.
Does this help @timzaak ?
from cats-parse.
@johnynek thanks again, I rewrite the code, and it looks good to me now.
from cats-parse.
Related Issues (20)
- docs can give warnings, which causes publish to fail
- orElse not working as expected HOT 3
- Either x or not y HOT 6
- Recursive parser creates infinite loop. HOT 1
- A parser string interpolator
- Backtracking with context HOT 1
- Rep same char as surrounded by HOT 5
- idea for safe repetition on Parser0
- RadixNode and stringIn are a bit slow
- Is it possible to support Scala Native? HOT 6
- alternate design for voiding
- use java.util.BitSet on scalajs
- `p1.? ~ p2` != `(p1 ~ p2) | p2` for error reporting purposes HOT 9
- Remove isScalaJs/isScalaJvm on minor version update
- identifier parser HOT 2
- flakey test: X cats.parse.ParserTest.a.flatMap(b) composes as expected parser00
- add json string parsing
- add a withString combinator
- Should `Parser` have a typeclass? HOT 2
- set up github CI to run benchmarks on release
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cats-parse.