Struct GrammarBuilder

Source
pub struct GrammarBuilder<PN: Eq + Hash + Copy + Debug + Display, TN: PartialEq + Copy + Debug + Display> { /* private fields */ }
Expand description

A utility for dynamically building grammars.

If you only want to create a static grammar that won’t change over the runtime of your program, consider using the [grammar] macro instead.

§Examples

Creating a simple grammar

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Token {
	Word,
	Space,
	Dot
}
 
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum Proc {
	Sentence,
	Text
}
 
// Though not shown here, both Token an Proc need to implement std::fmt::Display so that
// human-readable error messages can be generated
 
use sprout::parse::{GrammarBuilder, ProcErrorBehavior};
 
use Token::*;
use Proc::*;
 
let mut grammar_builder = GrammarBuilder::new();
 
// Start a primtive procedure definition
// See documentation for ProcErrorBehavior
grammar_builder.define(Sentence, ProcErrorBehavior::Primitive);
 
// Add a token to the current procedure
grammar_builder.token(Word);
 
// Start a repeat block with a minimum of 0 repetitions
grammar_builder.start_repeat(0);
 
grammar_builder.token(Space);
grammar_builder.token(Word);
 
// end the repeat block
grammar_builder.end();
 
grammar_builder.token(Dot);
 
// Start the next (non-primitive) procedure definition
grammar_builder.define(Text, ProcErrorBehavior::Default);
 
// Add a reference to another procedure to the current procedure
grammar_builder.proc(Sentence);
 
grammar_builder.start_repeat(0);
grammar_builder.token(Space);
grammar_builder.proc(Sentence);
grammar_builder.end();
 
// Extract the finished grammar
let grammar = grammar_builder.complete();

See method documentation for more options.

Implementations§

Source§

impl<PN: Eq + Hash + Copy + Debug + Display, TN: PartialEq + Copy + Debug + Display> GrammarBuilder<PN, TN>

Source

pub fn new() -> Self

Source

pub fn define(&mut self, proc: PN, error_behavior: ProcErrorBehavior)

Start a new procedure definition.

for a detailed description of the options for error_behavior, see ProcErrorBehavior.

Will panic if there are any unfinished repeats, optionals, or choices.

Source

pub fn token(&mut self, token: TN)

Add a token to the current procedure definition.

Will panic if no procedure definition was started yet.

Source

pub fn signature(&mut self, token: TN)

Add a signature token to the current procedure definition. Signatures give the current procedure priority over others even if it only matches up to this point.

Will panic if no procedure definition was started yet.

Source

pub fn proc(&mut self, proc: PN)

Add a procedure to the current procedure definition.

Will panic if no procedure definition was started yet.

Source

pub fn start_repeat(&mut self, min: u32)

Start a repeat block with a minimum of min repeats.

Will panic if no procedure definition was started yet.

Source

pub fn start_optional(&mut self)

Start an optional block.

Will panic if no procedure definition was started yet.

Source

pub fn start_choice(&mut self)

Start a choice block.

Will panic if no procedure definition was started yet.

Source

pub fn next_choice_path(&mut self)

Within a choice block, finish the current choice path and start defining the next one.

Will panic when not within a choice block or if no procedure definition was started yet.

Source

pub fn end(&mut self)

End the current block (repeat/optional/choice).

Will panic when not within a block or if no procedure definition was started yet.

Source

pub fn complete(self) -> Grammar<PN, TN>

Complete the grammar build and extract the constructed Grammar.

Trait Implementations§

Source§

impl<PN: Debug + Eq + Hash + Copy + Debug + Display, TN: Debug + PartialEq + Copy + Debug + Display> Debug for GrammarBuilder<PN, TN>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<PN, TN> Freeze for GrammarBuilder<PN, TN>
where PN: Freeze,

§

impl<PN, TN> RefUnwindSafe for GrammarBuilder<PN, TN>

§

impl<PN, TN> Send for GrammarBuilder<PN, TN>
where PN: Send, TN: Send,

§

impl<PN, TN> Sync for GrammarBuilder<PN, TN>
where PN: Sync, TN: Sync,

§

impl<PN, TN> Unpin for GrammarBuilder<PN, TN>
where PN: Unpin, TN: Unpin,

§

impl<PN, TN> UnwindSafe for GrammarBuilder<PN, TN>
where PN: UnwindSafe, TN: UnwindSafe,

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

impl<T> TupleTree<T, ()> for T

Source§

const SIZE: Size

Source§

fn descendants(_indirect_level: usize) -> usize

Source§

fn height() -> usize

Source§

fn preorder(self, f: &mut impl FnMut(Visit<T>))

Source§

fn preorder_with_size_hint(self, f: &mut impl FnMut(Visit<T>, Size))

Source§

fn postorder(self, f: &mut impl FnMut(Visit<T>))

Source§

fn postorder_with_size_hint(self, f: &mut impl FnMut(Visit<T>, Size))