Struct Parser

Source
pub struct Parser<'a> { /* private fields */ }
Expand description

A parser for a mathematical expression.

The Parser builds an Abstract Syntax Tree (effectively one large ASTNode) by processing a program’s expressive tokens (in the case, via the Lexer). Unlike the Lexer, the Parser does not programatically build one node of the AST at a time; rather, a single request to parse a program will retrun the entire tree of the program. Like the Lexer, this tree is useless unless further processed; for instance, by a traverser (the Interpreter).

We expect the Parser to be agnostic to the significance of each Token in the program, instead concerned only with the relationships between Tokens. As such, the Parser is built to refrain from analyzing anything but the positions of tokens in a program.

Building an AST with the Parser requires only a Lexer.

let lexer = Lexer::from("1 + 3 - 2");
let mut parser = Parser::from(lexer);
let tree: ASTNode = parser.parse().unwrap();

Implementations§

Source§

impl<'a> Parser<'a>

Source

pub fn from(lexer: Lexer<'_>) -> Parser<'_>

Creates a Parser from a Lexer.

Source

pub fn parse(&mut self) -> Result<ASTNode, ProgramError>

Generates an AST by parsing the Tokens of a program, given by a Lexer.

Auto Trait Implementations§

§

impl<'a> Freeze for Parser<'a>

§

impl<'a> RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> UnwindSafe for Parser<'a>

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.