Skip to main content

PrecedenceTable

Struct PrecedenceTable 

Source
pub struct PrecedenceTable { /* private fields */ }
Expand description

A runtime-extensible operator precedence table for the expression parser.

The built-in table handles +, -, *, /, %, and ^. Additional infix operators with custom precedence levels can be registered at runtime without modifying the parser source.

Named operators (multi-character strings such as "and", "or", "mod", "xor") are supported alongside single-character operators. Named operators are matched as identifiers during infix parsing — after the left operand is parsed, the parser checks whether the next token matches any registered named operator before falling back to single-character matching.

Higher precedence numbers bind more tightly (e.g. * before +). Right-associative operators (currently only ^) are marked separately.

The parse_with_table function uses a PrecedenceTable instead of the hardcoded op_precedence / op_right_associative functions.

Implementations§

Source§

impl PrecedenceTable

Source

pub fn default_table() -> Self

Creates the default table matching the built-in parser behaviour.

Source

pub fn empty() -> Self

Creates an empty table (no operators registered).

Source

pub fn register_op( &mut self, op: impl Into<String>, precedence: u8, right_associative: bool, )

Registers a new infix operator by name (single- or multi-character).

  • op: any string key; single chars such as '+' are converted to a one-character string. Multi-char keys like "and" or "mod" are matched as identifiers during infix parsing.
  • precedence: binding strength; higher binds tighter.
  • right_associative: true for right-to-left evaluation (like ^).
Source

pub fn register(&mut self, op: char, precedence: u8, right_associative: bool)

Registers a single-character infix operator.

Convenience alias for register_op with a char argument; preserves backward compatibility with the previous API.

Source

pub fn precedence(&self, op: char) -> Option<u8>

Returns the precedence of a single-char operator, or None if not registered.

Source

pub fn precedence_str(&self, op: &str) -> Option<u8>

Returns the precedence of any operator (single- or multi-char).

Source

pub fn is_right_associative(&self, op: char) -> bool

Returns true if a single-char operator is right-associative.

Source

pub fn is_right_associative_str(&self, op: &str) -> bool

Returns true if any operator (single- or multi-char) is right-associative.

Source

pub fn named_ops(&self) -> impl Iterator<Item = &str>

Returns all registered multi-character operator names (length > 1).

Used by the infix parser to attempt named-operator matching before falling back to single-character operators.

Source

pub fn register_unary_op(&mut self, prefix: impl Into<String>, kind: SymbolKind)

Registers a prefix unary operator (e.g. "!", "~", "not").

When the parser encounters this prefix in atom position, it consumes it, recursively parses the operand, and wraps it in a DAG node whose SymbolKind is kind. The prefix is matched literally from the current position (after whitespace).

Source

pub fn unary_ops(&self) -> impl Iterator<Item = (&str, &SymbolKind)>

Iterator over all registered prefix unary operators.

Yields (prefix, kind) pairs. Use this to inspect what custom unary operators are active without modifying the table.

Trait Implementations§

Source§

impl Clone for PrecedenceTable

Source§

fn clone(&self) -> PrecedenceTable

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 Debug for PrecedenceTable

Source§

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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

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

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,