-
I'm attempting to use the PrattParser, but I also want my AST nodes to be allocated in an arena. The problem is that the arena reference needs to be captured in the closures that I pass to the PrattParser; this works fine for simple expressions, but not for recursive ones. fn to_ast<'a>(arena: &'a mut Arena<Node<'a>>, expression: Pairs<Rule>) -> &'a ast::Node<'a> {
PRATT_PARSER
.map_primary(|primary| {
let location: TokenLocation = primary.as_span().into();
match primary.as_rule() {
// ERROR: Captured variable cannot escape `FnMut` closure body
Rule::expr => to_ast(arena, primary.into_inner()),
Rule::integer => {
let value = primary.as_str().parse::<i64>().unwrap();
arena.new_node(ast::Node {
location,
value: ast::NodeValue::ConstI64(value),
})
}
_ => todo!("to_ast: {:?}", primary),
}
})
// etc...details omitted Any ideas how to get around this limitation? Right now the only option I can think of is to not use PrattParser, and write my own operator-precedence algorithm that doesn't use closures. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Never mind, I figured it out :) Arenas don't need to be mutable references, something I did not expect. |
Beta Was this translation helpful? Give feedback.
Never mind, I figured it out :) Arenas don't need to be mutable references, something I did not expect.