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
impl PrecedenceTable
Sourcepub fn default_table() -> Self
pub fn default_table() -> Self
Creates the default table matching the built-in parser behaviour.
Sourcepub fn register_op(
&mut self,
op: impl Into<String>,
precedence: u8,
right_associative: bool,
)
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:truefor right-to-left evaluation (like^).
Sourcepub fn register(&mut self, op: char, precedence: u8, right_associative: bool)
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.
Sourcepub fn precedence(&self, op: char) -> Option<u8>
pub fn precedence(&self, op: char) -> Option<u8>
Returns the precedence of a single-char operator, or None if not registered.
Sourcepub fn precedence_str(&self, op: &str) -> Option<u8>
pub fn precedence_str(&self, op: &str) -> Option<u8>
Returns the precedence of any operator (single- or multi-char).
Sourcepub fn is_right_associative(&self, op: char) -> bool
pub fn is_right_associative(&self, op: char) -> bool
Returns true if a single-char operator is right-associative.
Sourcepub fn is_right_associative_str(&self, op: &str) -> bool
pub fn is_right_associative_str(&self, op: &str) -> bool
Returns true if any operator (single- or multi-char) is right-associative.
Sourcepub fn named_ops(&self) -> impl Iterator<Item = &str>
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.
Sourcepub fn register_unary_op(&mut self, prefix: impl Into<String>, kind: SymbolKind)
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).
Sourcepub fn unary_ops(&self) -> impl Iterator<Item = (&str, &SymbolKind)>
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
impl Clone for PrecedenceTable
Source§fn clone(&self) -> PrecedenceTable
fn clone(&self) -> PrecedenceTable
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more