Skip to main content

ParseResult

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

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

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

pub fn root(&self) -> Cursor<'_>

A cursor at the tree root.

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>>

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>

Every token leaf of terminal type name, in document order — lark’s scan_values filtered by type. An unknown terminal yields nothing.

pub fn pretty(&self) -> String

Indented multi-line rendering of the tree — lark’s Tree.pretty (two-space indent; a rule with a single leaf child renders inline). The walk is iterative, so tree depth is not stack-bounded.

Trait Implementations§

§

impl Clone for ParseResult

§

fn clone(&self) -> ParseResult

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for ParseResult

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.