pub struct Parser<PN: Eq + Hash + Copy + Display + Debug, TN: Eq + Copy + Display + Debug> { /* private fields */ }Expand description
The Parser is instantiated with an Alphabet and Grammar, and provides the
parse method to directly compile a String to an AST.
ยงExamples
Simple example
// Define an enum for our tokens
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Token { X }
// Define an enum for our procedures
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum Proc { AllTheX }
// Implement std::fmt::Display for our two enums for error message generation
impl std::fmt::Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::X => write!(f, "x")
}
}
}
impl std::fmt::Display for Proc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AllTheX => write!(f, "all the x!")
}
}
}
// import our tokens for convenience (this is not required)
use Token::*;
use Proc::*;
// Define alphabet and grammar and create parser
let parser = Parser::new(
alphabet! {
X => "x"
},
grammar! {
#AllTheX => (X)*
}
);
// Parse a test string
let result = parser.parse(AllTheX, "xxxxxxxx".to_string());
assert!(result.is_ok())
For a detailed explanation on how to define alphabets and grammars, see alphabet and grammar.
Implementationsยง
Sourceยงimpl<PN: Eq + Hash + Copy + Display + Debug, TN: Eq + Copy + Display + Debug> Parser<PN, TN>
impl<PN: Eq + Hash + Copy + Display + Debug, TN: Eq + Copy + Display + Debug> Parser<PN, TN>
Sourcepub fn parse(&self, proc: PN, text: String) -> Result<AST<PN>, ParsingError>
pub fn parse(&self, proc: PN, text: String) -> Result<AST<PN>, ParsingError>
Parse a String, according to some proc defined in the grammar of this parser,
to an AST.
Returns a Result with ParsingError as the error type.
ยงExamples
Parsing a string
// Given the grammar contains a procedure Proc::Number
let result = parser.parse(Proc::Number, "3453".to_string());For more details, see Parser
Trait Implementationsยง
Auto Trait Implementationsยง
impl<PN, TN> Freeze for Parser<PN, TN>where
TN: Freeze,
impl<PN, TN> RefUnwindSafe for Parser<PN, TN>where
TN: RefUnwindSafe,
PN: RefUnwindSafe,
impl<PN, TN> Send for Parser<PN, TN>
impl<PN, TN> Sync for Parser<PN, TN>
impl<PN, TN> Unpin for Parser<PN, TN>
impl<PN, TN> UnwindSafe for Parser<PN, TN>where
PN: UnwindSafe,
TN: UnwindSafe,
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more