[−][src]Trait autumn::combinators::ParserExt
Combinators that can be used on all parsers
Provided methods
fn or<P: Parser<T, E>>(self, other: P) -> Or<Self, P, E>
Evaluate two alternative parsers producing all successful parses from both
fn alpha_or_beta() -> impl Parser<String> { "alpha".or("beta").copy_string() }
fn map<V, F: Fn(T) -> V>(self, map: F) -> Map<Self, F, T, E>
Map the output of a parser to a new value and type
use std::str::FromStr; fn integer() -> impl Parser<i32> { digit.multiple().copy_string().map(|i| FromStr::from_str(&i).unwrap()) }
fn and_then<V, Q: Parser<V, E>, F: Fn(T) -> Q>(
self,
map: F
) -> AndThen<Self, F, T, E>
self,
map: F
) -> AndThen<Self, F, T, E>
Apply an additional parser dependant on the result of the preceding parser
use std::str::FromStr; enum Token { Identifier(String), Literal(i32), } use Token::*; fn identifier() -> impl Parser<String> { alphabetic.and(alphanumeric.multiple().maybe()).copy_string() } fn literal() -> impl Parser<i32> { "-".maybe() .and(digit.multiple()) .copy_string() .map(|i| FromStr::from_str(&i).unwrap()) } fn tagged_token() -> impl Parser<Token> { "identifier" .or("literal") .drop(space) .copy_string() .and_then(|parsed| { match parsed.as_str() { "identifier" => identifier().map(Identifier).boxed(), "literal" => literal().map(Literal).boxed(), _ => unreachable!() } }) }
fn on_failure<P: Parser<T, E>>(self, other: P) -> OnFailure<Self, P, E>
Apply an alternative parser if the preceding parser produces errors or no successful parses
fn failing_parser() -> impl Parser<String, &'static str> { "hello" .and("world") .copy_string() .on_none(error(String::new(), "Not hello world")) .on_failure(error(String::new(), "Caught error")) }
fn on_none<P: Parser<T, E>>(self, other: P) -> OnNone<Self, P, E>
Apply an alternative parser if the preceding parser produces no successful parses
fn parse_handle_none() -> impl Parser<String, &'static str> { "hello" .and("world") .copy_string() .on_none(error(String::new(), "Not hello world")) }
fn drop<V, P: Parser<V, E>>(self, other: P) -> Drop<Self, P, V, E>
Apply an additional parser and discard the result
fn drop_trailing_whitespace() -> impl Parser<String> { "hello".drop(space).copy_string() }
fn skip<V, P: Parser<V, E>>(self, keep: P) -> Skip<Self, P, T, E>
Apply an additional parser and take its output instead
fn drop_leading_whitespace() -> impl Parser<String> { space.skip("hello").copy_string() }
fn condition<F: Fn(&T) -> bool>(self, condition: F) -> Condition<Self, F, E>
Only parse successfully if the output of the parser satisfies a given condition
use std::str::FromStr; fn foutry_two() -> impl Parser<i32> { digit .multiple() .copy_string() .map(|i| FromStr::from_str(&i).unwrap()) .condition(|i| *i == 42) }
fn end(self) -> End<Self, E>
Only parse successfully if there is no text remaining in the input after a parse
fn single_line() -> impl Parser<String> { any_character .str_condition(|s| !s.chars().any(|c| c == '\n')) .drop("\n") .copy_string() .end() }
fn catch(self) -> Catch<Self, E>
Transform all exceptions in the result into errors, moving the start of the associated range to the start of the given parser.
See exceptions.
fn meta(self) -> MetaMap<Self, E>
Wrap the output of a parser with the location of the input parsed
fn identifier_location() -> impl Parser<Meta<String, Span>> { alphabetic .and(alphanumeric.multiple().maybe()) .copy_string() .meta() }
fn to_list(self) -> ListMap<Self, E>
Wrap the value of a parser in a list
use std::str::FromStr; fn parse_integers() -> impl Parser<List<i32>> { "-".maybe() .and(digit.multiple()) .copy_string() .map(|s| FromStr::from_str(&s).unwrap()) .to_list() .multiple() }