Parser

Struct Parser 

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

Source

pub fn new(alphabet: Alphabet<TN>, grammar: Grammar<PN, TN>) -> Self

Constructs a new parser from an Alphabet and a Grammar.

For a complete usage example, see Parser.

See also: Alphabet, Grammar.

Source

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ยง

Sourceยง

impl<PN: Debug + Eq + Hash + Copy + Display + Debug, TN: Debug + Eq + Copy + Display + Debug> Debug for Parser<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 Parser<PN, TN>
where TN: Freeze,

ยง

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

ยง

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

ยง

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

ยง

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

ยง

impl<PN, TN> UnwindSafe for Parser<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))