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§
Implementations§
source§impl<'a, T, S: Clone + 'a> ParseResult<'a, T, S>
impl<'a, T, S: Clone + 'a> ParseResult<'a, T, S>
sourcepub fn map<U, F: FnOnce(T) -> U>(self, func: F) -> ParseResult<'a, U, S>
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.
sourcepub fn map_with_state<U, F: FnOnce(T, S) -> U>(
self,
func: F
) -> ParseResult<'a, U, S>
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.
sourcepub fn map_err<F: FnOnce(String) -> String>(
self,
func: F
) -> ParseResult<'a, T, S>
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.
sourcepub fn and_then<U, F: FnOnce(&'a str, T, Location, S) -> ParseResult<'a, U, S>>(
self,
func: F
) -> ParseResult<'a, U, S>
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.
pub fn backtrackable(self) -> ParseResult<'a, T, S>
Trait Implementations§
source§impl<'a, A, S> From<ParseResult<'a, A, S>> for StdParseResult<'a, A, S>
impl<'a, A, S> From<ParseResult<'a, A, S>> for StdParseResult<'a, A, S>
source§fn from(val: ParseResult<'a, A, S>) -> Self
fn from(val: ParseResult<'a, A, S>) -> Self
source§impl<'a, A, S> From<Result<ParseOk<'a, A, S>, ParseErr<S>>> for ParseResult<'a, A, S>
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>
fn from(result: StdParseResult<'a, A, S>) -> ParseResult<'a, A, S>
source§impl<'a, Output: PartialEq, State: PartialEq> PartialEq<ParseResult<'a, Output, State>> for ParseResult<'a, Output, State>
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
fn eq(&self, other: &ParseResult<'a, Output, State>) -> bool
self
and other
values to be equal, and is used
by ==
.