Skip to main content

Rule

Struct Rule 

Source
pub struct Rule {
    pub parent_node: Option<Rc<RefCell<Value>>>,
    pub child_node: Value,
    /* private fields */
}
Expand description

A rule as the parse loop sees it.

Its state lives behind an Rc that it shares with every snapshot taken of it, so snapshot() is a pointer copy. The copy happens instead on the next write, and only while a snapshot is still holding the current value — so a rule that is snapshotted several times between writes pays for one copy, not several, and a rule nobody snapshotted pays for none. Reads and writes both go through Deref, so rule.state and rule.state = .. are unchanged at every call site.

Fields§

§parent_node: Option<Rc<RefCell<Value>>>

Not shared with snapshots, which do not carry it.

§child_node: Value

The completed child’s node, likewise not shared. A Value is 72 bytes, which was a third of a RuleSnapshot, and copy-on-write copied all of it on the next write to any field. Nothing reads this through a snapshot: every use in the engine and in the plugin repos is rule.child_node on a live rule.

Undefined when the child shared this rule’s node cell, so that this field is never a second handle on this rule’s own accumulator (#195); Rule::child_value answers from the node in that case.

Implementations§

Source§

impl Rule

Source

pub fn n_mut(&mut self) -> &mut HashMap<String, i32>

Mutable access to the per-rule counters and state. Copies only when a snapshot is still holding the current value.

Source

pub fn u_mut(&mut self) -> &mut HashMap<String, Value>

Source

pub fn k_mut(&mut self) -> &mut HashMap<String, Value>

Source

pub fn new(name: impl Into<RuleName>, initial_node: Value) -> Self

Source

pub fn with_shared_node( name: impl Into<RuleName>, node: Rc<RefCell<Value>>, ) -> Self

Source

pub fn o0(&self) -> Option<&Token>

Source

pub fn o1(&self) -> Option<&Token>

Source

pub fn c0(&self) -> Option<&Token>

Source

pub fn c1(&self) -> Option<&Token>

Source

pub fn os(&self) -> usize

Source

pub fn cs(&self) -> usize

Source

pub fn resolve_open_value( &mut self, index: usize, context: &mut Context, ) -> Value

Resolve a matched opening token’s eager or lazy semantic value without exposing the temporary token clone needed by Rust’s borrow rules.

Source

pub fn resolve_close_value( &mut self, index: usize, context: &mut Context, ) -> Value

Resolve a matched closing token’s eager or lazy semantic value.

Source

pub fn eq(&self, counter: &str, limit: i32) -> bool

Counter comparisons use zero for an unset counter, matching the canonical engine. exist distinguishes unset from explicitly zero.

Source

pub fn lt(&self, counter: &str, limit: i32) -> bool

Source

pub fn gt(&self, counter: &str, limit: i32) -> bool

Source

pub fn lte(&self, counter: &str, limit: i32) -> bool

Source

pub fn gte(&self, counter: &str, limit: i32) -> bool

Source

pub fn exist(&self, counter: &str) -> bool

Source

pub fn snapshot(&self) -> Rc<RuleSnapshot>

The rule’s state as it stands, shared rather than copied. The copy, if one is still needed, happens on the rule’s next write.

Source

pub fn child_value(&self) -> Value

The completed child’s value, as TypeScript’s rule.child.node reads: child_node when the child had a node of its own, and this rule’s own node when the child shared this rule’s cell (see Rule::accept_child_node, which is crate-private, so this is a name and not a link). Read this rather than child_node wherever the shared case must be seen as a value.

Source

pub fn has_child_value(&self) -> bool

Whether Rule::child_value is defined.

Trait Implementations§

Source§

impl Clone for Rule

Source§

fn clone(&self) -> Self

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

impl Deref for Rule

Source§

type Target = RuleSnapshot

The resulting type after dereferencing.
Source§

fn deref(&self) -> &RuleSnapshot

Dereferences the value.
Source§

impl DerefMut for Rule

Every write to a rule’s shared state goes through here, which is what makes the copy happen on write rather than on snapshot.

The obvious next step — stop copying at all, so a snapshot aliases the live rule the way TypeScript’s and Go’s rule handles do — was measured here by making this return an aliasing pointer. It is faster where it works: a 512-character palindrome drops from 31.7M to 24.6M instructions, and a 16K-term adder from 72.7 ms to 57.8 ms. But next_rule and the parent and child links then point at rules that point back, and an Rc cycle is never freed: peak memory for the benchmark set goes from 71 MB to 1214 MB, and a 32K-character palindrome slows from 111 ms to 270 ms once the leak outweighs the saving. Doing it properly needs Weak on every back-reference and an upgrade on every traversal, which spends some of the same 1.3x it is chasing. Worth knowing before anyone tries it again.

Source§

fn deref_mut(&mut self) -> &mut RuleSnapshot

Mutably dereferences the value.
Source§

impl Display for Rule

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Rule

§

impl !Send for Rule

§

impl !Sync for Rule

§

impl !UnwindSafe for Rule

§

impl Freeze for Rule

§

impl Unpin for Rule

§

impl UnsafeUnpin for Rule

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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, !>

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.