Struct ParseResult
pub struct ParseResult {
pub tree: Arc<ParsedTree>,
pub table: Arc<ParseTable>,
}Expand description
A finished parse — self-describing: the owned tree plus the
table its data_ids index, so a Cursor resolves display names from the
result alone. Both fields are Arc (O(1) clone, Send, C-expressible).
Fields§
§tree: Arc<ParsedTree>§table: Arc<ParseTable>Implementations§
§impl ParseResult
impl ParseResult
pub fn fold<F: Fold>(&self, f: &mut F) -> F::Value
pub fn fold<F: Fold>(&self, f: &mut F) -> F::Value
Fold the tree bottom-up through a Fold algebra. Iterative — safe
for trees of any depth (a left-leaning 1+1+1+... chain folds fine).
pub fn eval<T>(&self, f: impl Fn(&EvalNode<'_, T>) -> T) -> T
pub fn eval<T>(&self, f: impl Fn(&EvalNode<'_, T>) -> T) -> T
Evaluate the tree top-down: f receives the root as an EvalNode
and recurses by calling EvalNode::eval on the children it needs.
Pinning the value type on the binding usually suffices
(let value: f64 = result.eval(|n| ...)). One known exception: an arm
applying a unary operator to a child ("neg" => -n.eval(0)) defeats
closure inference — annotate the parameter then, as below.
use hyperlark::{EvalNode, Lark, LarkOptions};
let parser = Lark::from_lark_source(
r#"
?start: sum
?sum: product | sum "+" product -> add | sum "-" product -> sub
?product: atom | product "*" atom -> mul | product "/" atom -> div
?atom: NUMBER -> number | "-" atom -> neg | "(" sum ")"
%import common.NUMBER
%import common.WS_INLINE
%ignore WS_INLINE
"#,
LarkOptions::default(),
).unwrap();
let result = parser.parse("2 + 3 * 4").unwrap();
let value = result.eval(|n: &EvalNode<f64>| match n.name() {
"add" => n.eval(0) + n.eval(1),
"sub" => n.eval(0) - n.eval(1),
"mul" => n.eval(0) * n.eval(1),
"div" => n.eval(0) / n.eval(1),
"neg" => -n.eval(0),
"number" => n.eval(0),
"NUMBER" => n.text().parse().unwrap(),
other => unreachable!("unexpected node: {other}"),
});
assert_eq!(value, 14.0);§impl ParseResult
impl ParseResult
pub fn cursor_at<'a>(&'a self, node: &'a NodeValue) -> Cursor<'a>
pub fn cursor_at<'a>(&'a self, node: &'a NodeValue) -> Cursor<'a>
A cursor at an arbitrary node of this result (tree-cursor bindings resolve hit positions back into the walk surface).
pub fn find_data<'a>(&'a self, name: &str) -> impl Iterator<Item = Cursor<'a>>
pub fn find_data<'a>(&'a self, name: &str) -> impl Iterator<Item = Cursor<'a>>
Every subtree whose rule (display) name is name, as Cursors —
lark’s Tree.find_data, in the same bottom-up order: innermost
matches come before their parents. An unknown name yields nothing.
The Earley ambiguity='explicit' sentinels (_ambig/_iambig/
_inter) are not resolvable by name here; walk them via Cursor.
pub fn find_token<'a>(&'a self, name: &str) -> impl Iterator<Item = &'a Token>
pub fn find_token<'a>(&'a self, name: &str) -> impl Iterator<Item = &'a Token>
Every token leaf of terminal type name, in document order — lark’s
scan_values filtered by type. An unknown terminal yields nothing.
Trait Implementations§
§impl Clone for ParseResult
impl Clone for ParseResult
§fn clone(&self) -> ParseResult
fn clone(&self) -> ParseResult
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more