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>
impl<PN: Eq + Hash + Copy + Debug + Display, TN: PartialEq + Copy + Debug + Display> GrammarBuilder<PN, TN>
pub fn new() -> Self
Sourcepub fn define(&mut self, proc: PN, error_behavior: ProcErrorBehavior)
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.
Sourcepub fn token(&mut self, token: TN)
pub fn token(&mut self, token: TN)
Add a token to the current procedure definition.
Will panic if no procedure definition was started yet.
Sourcepub fn signature(&mut self, token: TN)
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.
Sourcepub fn proc(&mut self, proc: PN)
pub fn proc(&mut self, proc: PN)
Add a procedure to the current procedure definition.
Will panic if no procedure definition was started yet.
Sourcepub fn start_repeat(&mut self, min: u32)
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.
Sourcepub fn start_optional(&mut self)
pub fn start_optional(&mut self)
Start an optional block.
Will panic if no procedure definition was started yet.
Sourcepub fn start_choice(&mut self)
pub fn start_choice(&mut self)
Start a choice block.
Will panic if no procedure definition was started yet.
Sourcepub fn next_choice_path(&mut self)
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.