Enum lip::ParseResult

source ·
pub enum ParseResult<'a, Output, State> {
    Ok {
        input: &'a str,
        location: Location,
        output: Output,
        state: State,
        committed: bool,
    },
    Err {
        message: String,
        from: Location,
        to: Location,
        state: State,
        committed: bool,
    },
}
Expand description

Records the result of the parser.

Similar to the std::result::Result type, outputs ParseResult::Ok if parsing succeeds and ParseResult::Err if parsing fails.

ParseResult::Ok contains:

  • input: the part of source string left after parsing finished
  • location: the Location of the end of parse,
  • output: the output of the parsing
  • state: the final state of the parser
  • committed: If committed is true, then the parser has succeeded and has committed to this parse. If a parser after this fails, other parser alternatives will not be attempted

ParseResult::Err contains:

  • message: the message explaining what and why the parse failed
  • from: the starting Location of the error
  • to: the ending Location of the error
  • state: the final state of the parser

A word about state: Sometimes you want to keep track of extra information outside the current location and the rest of the source string. For example, you may want to put different symbols encountered into a symbol table as a replacement mapping after parsing finished or keep track of the current instruction index. This library uses state to capture any kind of extra information like the ones we mentioned above during parsing. The only requirement on state is implementation of the Clone trait. Here’s an example state:

#[derive(Clone, Debug)]
pub struct State {
  symbol_table: HashMap<String, usize>, // build up a symbol table
  instruction_index: usize, // count which instruction we are at
  variable_index: usize, // count how many variables we encountered
}

To update state during and after parsing, use update_state.

Variants§

§

Ok

Fields

§input: &'a str
§location: Location
§output: Output
§state: State
§committed: bool
§

Err

Fields

§message: String
§state: State
§committed: bool

Implementations§

source§

impl<'a, T, S: Clone + 'a> ParseResult<'a, T, S>

source

pub fn map<U, F: FnOnce(T) -> U>(self, func: F) -> ParseResult<'a, U, S>

Map the parse output to a new value if parse succeeds. Otherwise, return error as usual.

source

pub fn map_with_state<U, F: FnOnce(T, S) -> U>( self, func: F ) -> ParseResult<'a, U, S>

Map the parse output to a new value if parse succeeds. The map function is supplied both the output and the state of the parser. Otherwise, return error as usual.

source

pub fn map_err<F: FnOnce(String) -> String>( self, func: F ) -> ParseResult<'a, T, S>

Map the error message to a new message if parse fails. Otherwise, return output as usual.

source

pub fn and_then<U, F: FnOnce(&'a str, T, Location, S) -> ParseResult<'a, U, S>>( self, func: F ) -> ParseResult<'a, U, S>

Returns a new parser which is given the current output if parse succeeds. Otherwise, return error as usual.

source

pub fn backtrackable(self) -> ParseResult<'a, T, S>

Trait Implementations§

source§

impl<'a, Output: Debug, State: Debug> Debug for ParseResult<'a, Output, State>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, A, S> From<ParseResult<'a, A, S>> for StdParseResult<'a, A, S>

source§

fn from(val: ParseResult<'a, A, S>) -> Self

Converts to this type from the input type.
source§

impl<'a, A, S> From<Result<ParseOk<'a, A, S>, ParseErr<S>>> for ParseResult<'a, A, S>

source§

fn from(result: StdParseResult<'a, A, S>) -> ParseResult<'a, A, S>

Converts to this type from the input type.
source§

impl<'a, Output: PartialEq, State: PartialEq> PartialEq<ParseResult<'a, Output, State>> for ParseResult<'a, Output, State>

source§

fn eq(&self, other: &ParseResult<'a, Output, State>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, Output: Eq, State: Eq> Eq for ParseResult<'a, Output, State>

source§

impl<'a, Output, State> StructuralEq for ParseResult<'a, Output, State>

source§

impl<'a, Output, State> StructuralPartialEq for ParseResult<'a, Output, State>

Auto Trait Implementations§

§

impl<'a, Output, State> RefUnwindSafe for ParseResult<'a, Output, State>where Output: RefUnwindSafe, State: RefUnwindSafe,

§

impl<'a, Output, State> Send for ParseResult<'a, Output, State>where Output: Send, State: Send,

§

impl<'a, Output, State> Sync for ParseResult<'a, Output, State>where Output: Sync, State: Sync,

§

impl<'a, Output, State> Unpin for ParseResult<'a, Output, State>where Output: Unpin, State: Unpin,

§

impl<'a, Output, State> UnwindSafe for ParseResult<'a, Output, State>where Output: UnwindSafe, State: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.