ShiftReduceParser

Struct ShiftReduceParser 

Source
pub struct ShiftReduceParser<K, T> { /* private fields */ }
Expand description

The empty shift rule simply fails immediately with an error. The intention is that, starting from this rule, we can build up a complete shift rule.

Implementations§

Source§

impl<K, T> ShiftReduceParser<K, T>

Source

pub fn new() -> Self

Trait Implementations§

Source§

impl<K: PartialEq, T> ShiftReduceRule for ShiftReduceParser<K, T>

Source§

type Kind = K

Identifies the kind of tokens being processed by this rule.
Source§

type Term = T

Identifies the type of terms being produced by this rule.
Source§

fn shift( &mut self, _token: Token<K>, _stack: &mut Vec<T>, ) -> Result<Option<T>, ()>

A shift rule reads the next token and decides what to do. For example, it can return a completed term or it can begin a new term.
Source§

fn reduce(&self, _parent: T, _child: T) -> Result<T, ()>

A _reduction rule combines a sub-term into its enclosing term. For example, when parsing arithmetic expressions we might reduce 1 + 2*x with y to form 1 + 2*x + y.
Source§

fn apply<F: Fn(Self::Term, Self::Term) -> Result<Self::Term, ()>>( self, rule: F, ) -> ShiftApplyRule<Self::Kind, F, Self>

Apply a given reduction as necessary when reducing terms.
Source§

fn first(self, term: Self::Term) -> ShiftFirstRule<Self::Kind, Self::Term, Self>

Source§

fn terminate<F>( self, kind: Self::Kind, rule: F, ) -> ShiftTerminalRule<Self::Kind, F, Self>
where F: Fn(Token<Self::Kind>) -> Self::Term,

Handle a terminating token kind using a given parser. For example, a token representing a number can be handled directly using an appropriate parser (e.g. usize::parse()).
Source§

fn skip(self, kind: Self::Kind) -> ShiftSkipRule<Self::Kind, Self::Term, Self>

Skip all occurrences of the given token. For example, if our language supports WhiteSpace tokens then we might want to skip them and, likewise, for comments, etc.
Source§

fn open( self, kind: Self::Kind, default: Self::Term, ) -> ShiftOpenRule<Self::Kind, Self::Term, Self>

Begin parsing a new compound term when the given token is encountered. The new term is initialised with a given default value. For example, when parsing an S-expression (i.e. lisp), then upon encountering a ( we start a new empty list.
Source§

fn close( self, kind: Self::Kind, ) -> ShiftCloseRule<Self::Kind, fn(Self::Term) -> Self::Term, Self>

Finish parsing a compound a compound term when the given token is encountered. When this happens, the current stack element is popped off the stack and either reduced into the element below, or returned. For example, when parsing an S-expression (i.e. lisp), the token ) signifies the end of a list.
Source§

fn close_with<F>( self, kind: Self::Kind, rule: F, ) -> ShiftCloseRule<Self::Kind, F, Self>
where F: Fn(Self::Term) -> Self::Term,

Finish parsing a compound a compound term when the given token is encountered. When this happens, the current stack element is popped off the stack and either reduced into the element below, or returned. For example, when parsing an S-expression (i.e. lisp), the token ) signifies the end of a list.
Source§

fn update<F: Fn(Self::Term) -> Self::Term>( self, kind: Self::Kind, rule: F, ) -> ShiftUpdateRule<Self::Kind, F, Self>

Update the current state (i.e. topmost stack term) based on a given token.
Source§

fn update_with<F: Fn(Self::Term) -> Result<(bool, Self::Term), ()>>( self, kind: Self::Kind, rule: F, ) -> ShiftUpdateWithRule<Self::Kind, F, Self>

Update the current state (i.e. topmost stack item) based on a given token. If the given rule returns None, then no action is taken. Otherwise, the topmost stack item is replaced with that returned.
Source§

fn update_as<F: Fn(&mut Self::Term) -> Result<bool, ()>>( self, kind: Self::Kind, rule: F, ) -> ShiftUpdateAsRule<Self::Kind, F, Self>

Update the current state (i.e. topmost stack item) based on a given token. If the given rule returns false, then the rule did not apply and other rules should be considered.
Source§

fn parse<I>(&mut self, input: I) -> Result<Self::Term, ()>
where I: IntoIterator<Item = Token<Self::Kind>>,

Parse a given token stream and construct a corresponding term (or report an error).

Auto Trait Implementations§

§

impl<K, T> Freeze for ShiftReduceParser<K, T>

§

impl<K, T> RefUnwindSafe for ShiftReduceParser<K, T>

§

impl<K, T> Send for ShiftReduceParser<K, T>
where K: Send, T: Send,

§

impl<K, T> Sync for ShiftReduceParser<K, T>
where K: Sync, T: Sync,

§

impl<K, T> Unpin for ShiftReduceParser<K, T>
where K: Unpin, T: Unpin,

§

impl<K, T> UnwindSafe for ShiftReduceParser<K, T>
where K: UnwindSafe, T: 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.