Skip to main content

RuleBuilder

Struct RuleBuilder 

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

Builder for parser rules

Implementations§

Source§

impl RuleBuilder

Source

pub fn new(name: &str) -> Self

Source

pub fn parse(&self, rule_name: &str) -> Combinator

Reference another rule by name

Source

pub fn lit(&self, s: &str) -> Combinator

Match a literal string exactly (e.g., “if”, “===”, “+”)

Source

pub fn char(&self, c: char) -> Combinator

Match a single specific character

Source

pub fn digit(&self) -> Combinator

Match any decimal digit (0-9)

Source

pub fn hex_digit(&self) -> Combinator

Match any hexadecimal digit (0-9, a-f, A-F)

Source

pub fn alpha(&self) -> Combinator

Match any alphabetic character (a-z, A-Z)

Source

pub fn alpha_num(&self) -> Combinator

Match any alphanumeric character (a-z, A-Z, 0-9)

Source

pub fn ws(&self) -> Combinator

Match any whitespace character (space, tab, newline, etc.)

Source

pub fn ident_start(&self) -> Combinator

Match identifier start character (a-z, A-Z, _, $)

Source

pub fn ident_cont(&self) -> Combinator

Match identifier continue character (a-z, A-Z, 0-9, _, $)

Source

pub fn any_char(&self) -> Combinator

Match any single character

Source

pub fn range(&self, from: char, to: char) -> Combinator

Match a character in the given range (inclusive)

Source

pub fn not_followed_by(&self, inner: Combinator) -> Combinator

Negative lookahead: succeed if inner does NOT match, consume nothing

Source

pub fn followed_by(&self, inner: Combinator) -> Combinator

Positive lookahead: succeed if inner matches, consume nothing

Source

pub fn capture(&self, inner: Combinator) -> Combinator

Capture the matched text as a string token

Source

pub fn memoize(&self, id: usize, inner: Combinator) -> Combinator

Memoize the result of parsing to avoid exponential backtracking.

When a memoized combinator is tried at a position, the result is cached. If the same combinator is tried again at the same position (due to backtracking), the cached result is returned instead of re-parsing.

Use this for rules that:

  1. Appear in multiple Choice alternatives
  2. Contain recursion
  3. Are frequently backtracked

The id parameter must be unique across all memoization points.

Source

pub fn sequence<T: IntoCombinatorsVec>(&self, items: T) -> Combinator

Sequence of combinators

Source

pub fn choice<T: IntoCombinatorsVec>(&self, items: T) -> Combinator

Ordered choice (first match wins, auto-backtrack)

Source

pub fn zero_or_more(&self, inner: Combinator) -> Combinator

Zero or more

Source

pub fn one_or_more(&self, inner: Combinator) -> Combinator

One or more

Source

pub fn optional(&self, inner: Combinator) -> Combinator

Optional (zero or one)

Source

pub fn skip(&self, inner: Combinator) -> Combinator

Parse but discard result

Source

pub fn separated_by( &self, item: Combinator, separator: Combinator, ) -> Combinator

Separated list: item (sep item)*

Source

pub fn separated_by_trailing( &self, item: Combinator, separator: Combinator, ) -> Combinator

Separated list with optional trailing separator

Source

pub fn pratt<F>(&self, operand: Combinator, f: F) -> Combinator

Pratt expression parsing

Trait Implementations§

Source§

impl Debug for RuleBuilder

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