[][src]Enum lip::ParseResult

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

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

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 of Ok

input: &'a strlocation: Locationoutput: Outputstate: State
Err

Fields of Err

message: Stringfrom: Locationto: Locationstate: State

Methods

impl<'a, T, S: Clone> ParseResult<'a, T, S>[src]

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

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

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

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.

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

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

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

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

Trait Implementations

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

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

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

impl<'a, Output, State> StructuralEq for ParseResult<'a, Output, State>[src]

impl<'a, Output, State> StructuralPartialEq for ParseResult<'a, Output, State>[src]

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.