pub struct Parser<T> {
pub expectation: String,
/* private fields */
}Expand description
A Parser has a function that consumes input and returns an object of type Output.
Fields§
§expectation: StringImplementations§
Source§impl<T> Parser<T>where
T: 'static + Clone,
impl<T> Parser<T>where
T: 'static + Clone,
Sourcepub fn new(
parser: impl Fn(&str) -> Output<T> + 'static,
expectation: impl ToString,
) -> Self
pub fn new( parser: impl Fn(&str) -> Output<T> + 'static, expectation: impl ToString, ) -> Self
Create a new parser from a function that returns an Output. This is mainly used to define the atomic combinators
pub fn expects(self, expectation: impl ToString) -> Self
Sourcepub fn parse(&self, input: &str) -> Result<T, Error>
pub fn parse(&self, input: &str) -> Result<T, Error>
This parses a string using this combinator, and returns a result containing either the successfully lexed and parsed data, or an error containing info about the failure.
Examples found in repository?
50fn main() {
51 println!(
52 "{:#?}",
53 json().parse(
54 r#"
55{
56 "testing" : null,
57 "recursion" : {
58 "WOW": 1.2345
59 },
60 "array": [1, 2, {"test": "123"}, 4],
61 "test": "testing"
62}
63"#
64 )
65 );
66}More examples
4fn main() {
5 println!(
6 "{:?}",
7 (token() * (..)).parse(
8 r#"
9
10h123213ey
11jasdf2212eude
12"hey jude dude"
131234 jude
14
15
16fn testing() {
17 println("hello world!");
18}
19
20
21struct Point {
22 fn testing() {
23 println!(
24 "yo dude", 3, []
25 );
26 }
27}
28
29"#
30 )
31 );
32}40fn main() {
41 println!(
42 "{:#?}",
43 (markup() * (1..)).parse(
44 r#"
45
46row {
47 column1 {
48 width { 5 }
49 height { 10 }
50 }
51
52 column2 {
53 width { 5 }
54 height { 10 }
55 }
56
57 column3 {
58 width { 5 }
59 height { 10 }
60 }
61
62 columnlist {
63 - 1
64 - 2
65 - 3
66 }
67}
68
69"#
70 )
71 );
72}47fn main() {
48 println!(
49 "{:#?}",
50 paragraph().parse(
51 r#"
52
53I look at you all see the love there thats sleeping,
54While my guitar gently weeps.
55I look at the floor and I see it needs sweeping
56Still my guitar gently weeps.
57I dont know why nobody told you
58How to unfold your love.
59I dont know how someone controlled you.
60They bought and sold you.
61I look at the world and I notice its turning
62While my guitar gently weeps.
63With every mistake we must surely be learning,
64Still my guitar gently weeps.
65"#
66 )
67 );
68}Sourcepub fn parse_internal(&self, input: &str) -> Output<T>
pub fn parse_internal(&self, input: &str) -> Output<T>
This is used by the atomic combinators for things like control flow and passing the output of one parser into another.
Sourcepub fn map<O>(self, map_fn: fn(T) -> O) -> Parser<O>where
O: 'static + Clone,
pub fn map<O>(self, map_fn: fn(T) -> O) -> Parser<O>where
O: 'static + Clone,
This method takes a function that takes the output of this Parser, and converts it to the output of another data type. This allows us to lex our input as we parse it.
Sourcepub fn convert<O, E>(self, convert_fn: fn(T) -> Result<O, E>) -> Parser<O>where
O: 'static + Clone,
E: 'static,
pub fn convert<O, E>(self, convert_fn: fn(T) -> Result<O, E>) -> Parser<O>where
O: 'static + Clone,
E: 'static,
This method takes a function that takes the output of this Parser, and TRIES to convert it to the output of another data type. If the given function returns an Err, this parser fails.
Sourcepub fn prefixes<O>(self, operand: Parser<O>) -> Parser<O>where
O: 'static + Clone,
pub fn prefixes<O>(self, operand: Parser<O>) -> Parser<O>where
O: 'static + Clone,
This parser “prefixes” another. When the returned parser is used, it will require this parser and the operand parser to succeed, and return the result of the second.
Sourcepub fn suffix<O>(self, operand: Parser<O>) -> Parser<T>where
O: 'static + Clone,
pub fn suffix<O>(self, operand: Parser<O>) -> Parser<T>where
O: 'static + Clone,
This parser will use the operand as a “suffix”. The parser will only succeed if the “suffix” parser succeeds afterwards, but the input of the “suffix” parser will be discarded.
Sourcepub fn is(self) -> Parser<()>
pub fn is(self) -> Parser<()>
This method returns a parser that does not consume input, but succeeds if this parser succeeds. This can be used to make assertions for our input.
Examples found in repository?
More examples
Sourcepub fn isnt(self) -> Parser<()>
pub fn isnt(self) -> Parser<()>
This method returns a parser that does not consume input, but succeeds if this parser does not succeed. This can be used to make assertions for our input.
Trait Implementations§
Source§impl<A: 'static + Clone, B: 'static + Clone> BitAnd<Parser<B>> for Parser<A>
The & operator can be used as an alternative to the .and method
impl<A: 'static + Clone, B: 'static + Clone> BitAnd<Parser<B>> for Parser<A>
The & operator can be used as an alternative to the .and method
Source§impl<T: 'static + Clone> BitOr for Parser<T>
The | operator can be used as an alternative to the .or method
impl<T: 'static + Clone> BitOr for Parser<T>
The | operator can be used as an alternative to the .or method
Source§impl<O, T, E> BitXor<fn(T) -> Result<O, E>> for Parser<T>
The ^ operator is used as an alternative to the .convert method.
impl<O, T, E> BitXor<fn(T) -> Result<O, E>> for Parser<T>
The ^ operator is used as an alternative to the .convert method.
Source§impl<T: 'static + Clone, R: RangeBounds<usize>> Mul<R> for Parser<T>
A parser can be multiplied by a range as an alternative to .repeat.
Here’s an example: sym('a') * (..7)
impl<T: 'static + Clone, R: RangeBounds<usize>> Mul<R> for Parser<T>
A parser can be multiplied by a range as an alternative to .repeat.
Here’s an example: sym('a') * (..7)
Source§impl<T: 'static + Clone> Not for Parser<T>
The ! operator can be used as an alternative to the .not method
impl<T: 'static + Clone> Not for Parser<T>
The ! operator can be used as an alternative to the .not method
Source§impl<T: 'static + Clone, S: ToString> Rem<S> for Parser<T>
The | operator can be used as an alternative to the .or method
impl<T: 'static + Clone, S: ToString> Rem<S> for Parser<T>
The | operator can be used as an alternative to the .or method
Source§impl<A: 'static + Clone, B: 'static + Clone> Shl<Parser<B>> for Parser<A>
Discard the consumed data of the RHS
impl<A: 'static + Clone, B: 'static + Clone> Shl<Parser<B>> for Parser<A>
Discard the consumed data of the RHS
Source§impl<A: 'static + Clone, B: 'static + Clone> Shr<Parser<B>> for Parser<A>
Discard the consumed data of the LHS
impl<A: 'static + Clone, B: 'static + Clone> Shr<Parser<B>> for Parser<A>
Discard the consumed data of the LHS