You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The API with the PrattParser I think has some egonomic issues that can make life difficult with the borrow checker when one's parser has to hold some state.
Consider the following from my real world parser:
error[E0524]: two closures require unique access to `*self` at the same time
--> crates/compiler/src/parse.rs:370:25
|
217 | .map_primary(|primary| match primary.as_rule() {
| --------- first closure is constructed here
...
220 | let expr = self.parse_atom(inner.next().unwrap())?;
| ---- first borrow occurs due to use of `*self` in closure
...
370 | .map_prefix(|op, rhs| match op.as_rule() {
| ---------- ^^^^^^^^^ second closure is constructed here
| |
| first borrow later used by call
...
382 | .map(|e| self.parse_expr(e.into_inner()).unwrap());
| ---- second borrow occurs due to use of `*self` in closure
This happens because the parser itself has to hold state (in this case variable bindings), and that mutable state needs to be borrowed by the relevant closures. But the compiler really isn't going to like receiving multiple callbacks which borrow against the same state (in this case self but could be anything).
I have as a workaround the use of an Rc<RefCell around the mutable variable state (variable binding context) but this feels like a really bad cop-out.
I think the answer here is that PrattParser itself should be able to take as an construction argument some kind of arbitrary mutable context state parameter. And then this would get passed into each of the closures used as arguments to the map_<xxx> functions.
Otherwise, I can't really think of a way to make this API work without resorting to RefCell etc. The compiler is always going to think there's multiple borrows happening, even though in reality the PrattParser will only be using one at a time. I think that's a bit of a design problem with the API as it stands.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
The API with the
PrattParserI think has some egonomic issues that can make life difficult with the borrow checker when one's parser has to hold some state.Consider the following from my real world parser:
This happens because the parser itself has to hold state (in this case variable bindings), and that mutable state needs to be borrowed by the relevant closures. But the compiler really isn't going to like receiving multiple callbacks which borrow against the same state (in this case
selfbut could be anything).I have as a workaround the use of an
Rc<RefCellaround the mutable variable state (variable binding context) but this feels like a really bad cop-out.I think the answer here is that
PrattParseritself should be able to take as an construction argument some kind of arbitrary mutable context state parameter. And then this would get passed into each of the closures used as arguments to themap_<xxx>functions.Otherwise, I can't really think of a way to make this API work without resorting to
RefCelletc. The compiler is always going to think there's multiple borrows happening, even though in reality the PrattParser will only be using one at a time. I think that's a bit of a design problem with the API as it stands.All reactions