Parser

Struct Parser 

Source
pub struct Parser<'a, O>(/* private fields */);
Expand description

Being wrapped in this struct guarantees that the parser within will only match valid UTF-8 strings.

Implementations§

Source§

impl<'a, O> Parser<'a, O>

Source

pub fn new<P>(parse: P) -> Self
where P: Fn(&'a [u8], usize) -> Result<(O, usize)> + 'a,

Create new parser.

Source

pub fn collect(self) -> Parser<'a, &'a str>
where O: 'a,

Collect all matched input symbols.

Examples found in repository?
examples/utf8_mixed.rs (line 23)
22	fn rest_as_str<'a>() -> utf8::Parser<'a, &'a str> {
23		utf8::any().repeat(0..).collect()
24	}
More examples
Hide additional examples
examples/utf8.rs (line 31)
27	fn positive<'a>() -> Parser<'a, i32> {
28		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
29		let digit = one_of("0123456789");
30		let integer = digit.discard().repeat(1..);
31		integer.collect().convert(|x| x.parse::<i32>())
32	}
33
34	fn rest_str<'a>() -> Parser<'a, &'a str> {
35		any().repeat(1..).collect()
36	}
Source

pub fn parse(&self, input: &'a [u8]) -> Result<O>

Apply the parser to parse input.

Source

pub fn parse_at(&self, input: &'a [u8], start: usize) -> Result<(O, usize)>

Parse input at specified byte position.

Source

pub fn parse_str(&self, input: &'a str) -> Result<O>

Apply the parser to parse input.

Examples found in repository?
examples/utf8.rs (line 55)
5fn main() {
6	// Informal, Spanish-language movie database format
7	let input = "\
8Título: Abre los ojos
9Año: 1997
10Director: Alejandro Amenábar
11
12Título: Amores Perros
13Director: Alejandro González Iñárritu
14Año: 2000
15
16Título: La montaña sagrada
17Año: 1973
18Director: Alejandro Jodorowsky
19";
20
21	enum DataLine<'a> {
22		Title(&'a str),
23		Director(&'a str),
24		Year(i32),
25	}
26
27	fn positive<'a>() -> Parser<'a, i32> {
28		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
29		let digit = one_of("0123456789");
30		let integer = digit.discard().repeat(1..);
31		integer.collect().convert(|x| x.parse::<i32>())
32	}
33
34	fn rest_str<'a>() -> Parser<'a, &'a str> {
35		any().repeat(1..).collect()
36	}
37
38	fn separator<'a>() -> Parser<'a, ()> {
39		seq(": ").discard()
40	}
41
42	let parser = (seq("Título") * separator() * rest_str().map(|s| DataLine::Title(s)))
43		| (seq("Director") * separator() * rest_str().map(|s| DataLine::Director(s)))
44		| (seq("Año") * separator() * positive().map(|i| DataLine::Year(i)));
45
46	{
47		let mut title_opt: Option<&str> = None;
48		let mut year_opt: Option<i32> = None;
49		let mut director_opt: Option<&str> = None;
50
51		for line in input.lines() {
52			if !line.is_empty() {
53				// Skip blank lines without parsing
54				// Parse line
55				match parser.parse_str(line).unwrap() {
56					DataLine::Title(s) => title_opt = Some(s),
57					DataLine::Director(s) => director_opt = Some(s),
58					DataLine::Year(s) => year_opt = Some(s),
59				}
60				// When all three line types have been collected, print them
61				if let (Some(title), Some(year), Some(director)) =
62					(title_opt, year_opt, director_opt)
63				{
64					println!("Title: {}\nDirector: {}\nYear: {}\n", title, director, year);
65					(title_opt, year_opt, director_opt) = (None, None, None);
66				}
67			}
68		}
69	}
70}
Source

pub fn map<U, F>(self, f: F) -> Parser<'a, U>
where F: Fn(O) -> U + 'a, O: 'a, U: 'a,

Convert parser result to desired value.

Examples found in repository?
examples/utf8.rs (line 42)
5fn main() {
6	// Informal, Spanish-language movie database format
7	let input = "\
8Título: Abre los ojos
9Año: 1997
10Director: Alejandro Amenábar
11
12Título: Amores Perros
13Director: Alejandro González Iñárritu
14Año: 2000
15
16Título: La montaña sagrada
17Año: 1973
18Director: Alejandro Jodorowsky
19";
20
21	enum DataLine<'a> {
22		Title(&'a str),
23		Director(&'a str),
24		Year(i32),
25	}
26
27	fn positive<'a>() -> Parser<'a, i32> {
28		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
29		let digit = one_of("0123456789");
30		let integer = digit.discard().repeat(1..);
31		integer.collect().convert(|x| x.parse::<i32>())
32	}
33
34	fn rest_str<'a>() -> Parser<'a, &'a str> {
35		any().repeat(1..).collect()
36	}
37
38	fn separator<'a>() -> Parser<'a, ()> {
39		seq(": ").discard()
40	}
41
42	let parser = (seq("Título") * separator() * rest_str().map(|s| DataLine::Title(s)))
43		| (seq("Director") * separator() * rest_str().map(|s| DataLine::Director(s)))
44		| (seq("Año") * separator() * positive().map(|i| DataLine::Year(i)));
45
46	{
47		let mut title_opt: Option<&str> = None;
48		let mut year_opt: Option<i32> = None;
49		let mut director_opt: Option<&str> = None;
50
51		for line in input.lines() {
52			if !line.is_empty() {
53				// Skip blank lines without parsing
54				// Parse line
55				match parser.parse_str(line).unwrap() {
56					DataLine::Title(s) => title_opt = Some(s),
57					DataLine::Director(s) => director_opt = Some(s),
58					DataLine::Year(s) => year_opt = Some(s),
59				}
60				// When all three line types have been collected, print them
61				if let (Some(title), Some(year), Some(director)) =
62					(title_opt, year_opt, director_opt)
63				{
64					println!("Title: {}\nDirector: {}\nYear: {}\n", title, director, year);
65					(title_opt, year_opt, director_opt) = (None, None, None);
66				}
67			}
68		}
69	}
70}
Source

pub fn convert<U, E, F>(self, f: F) -> Parser<'a, U>
where F: Fn(O) -> Result<U, E> + 'a, E: Debug, O: 'a, U: 'a,

Convert parser result to desired value, fail in case of conversion error.

Examples found in repository?
examples/utf8.rs (line 31)
27	fn positive<'a>() -> Parser<'a, i32> {
28		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
29		let digit = one_of("0123456789");
30		let integer = digit.discard().repeat(1..);
31		integer.collect().convert(|x| x.parse::<i32>())
32	}
Source

pub fn cache(self) -> Self
where O: Clone + 'a,

Cache parser output result to speed up backtracking.

Source

pub fn pos(self) -> Parser<'a, usize>
where O: 'a,

Get input position after matching parser.

Source

pub fn discard(self) -> Parser<'a, ()>
where O: 'a,

Discard parser output.

Examples found in repository?
examples/utf8.rs (line 30)
27	fn positive<'a>() -> Parser<'a, i32> {
28		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
29		let digit = one_of("0123456789");
30		let integer = digit.discard().repeat(1..);
31		integer.collect().convert(|x| x.parse::<i32>())
32	}
33
34	fn rest_str<'a>() -> Parser<'a, &'a str> {
35		any().repeat(1..).collect()
36	}
37
38	fn separator<'a>() -> Parser<'a, ()> {
39		seq(": ").discard()
40	}
Source

pub fn opt(self) -> Parser<'a, Option<O>>
where O: 'a,

Make parser optional.

Source

pub fn repeat<R>(self, range: R) -> Parser<'a, Vec<O>>
where R: RangeArgument<usize> + Debug + 'a, O: 'a,

p.repeat(5) repeat p exactly 5 times p.repeat(0..) repeat p zero or more times p.repeat(1..) repeat p one or more times p.repeat(1..4) match p at least 1 and at most 3 times

Examples found in repository?
examples/utf8_mixed.rs (line 23)
22	fn rest_as_str<'a>() -> utf8::Parser<'a, &'a str> {
23		utf8::any().repeat(0..).collect()
24	}
More examples
Hide additional examples
examples/utf8.rs (line 30)
27	fn positive<'a>() -> Parser<'a, i32> {
28		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
29		let digit = one_of("0123456789");
30		let integer = digit.discard().repeat(1..);
31		integer.collect().convert(|x| x.parse::<i32>())
32	}
33
34	fn rest_str<'a>() -> Parser<'a, &'a str> {
35		any().repeat(1..).collect()
36	}
Source

pub fn name(self, name: &'a str) -> Self
where O: 'a,

Give parser a name to identify parsing errors.

Source

pub fn expect(self, name: &'a str) -> Self
where O: 'a,

Mark parser as expected, abort early when failed in ordered choice.

Trait Implementations§

Source§

impl<'a, Left: 'a, Right: 'a> Add<Parser<'a, Right>> for Parser<'a, Left>

Sequence reserve value

Source§

type Output = Parser<'a, (Left, Right)>

The resulting type after applying the + operator.
Source§

fn add(self, other: Parser<'a, Right>) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a, Left: 'a, Right: 'a> Add<Parser<'a, Right>> for Parser<'a, u8, Left>

Sequence reserve value (but degrade to non-utf8 parser)

Source§

type Output = Parser<'a, u8, (Left, Right)>

The resulting type after applying the + operator.
Source§

fn add(self, other: Parser<'a, Right>) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a, Left: 'a, Right: 'a> Add<Parser<'a, u8, Right>> for Parser<'a, Left>

Sequence reserve value (but degrade to non-utf8 parser)

Source§

type Output = Parser<'a, u8, (Left, Right)>

The resulting type after applying the + operator.
Source§

fn add(self, other: Parser<'a, u8, Right>) -> Self::Output

Performs the + operation. Read more
Source§

impl<'a, O: 'a> BitOr<Parser<'a, O>> for Parser<'a, u8, O>

Ordered choice (but degrade to non-utf8 parser)

Source§

type Output = Parser<'a, u8, O>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: Parser<'a, O>) -> Self::Output

Performs the | operation. Read more
Source§

impl<'a, O: 'a> BitOr<Parser<'a, u8, O>> for Parser<'a, O>

Ordered choice (but degrade to non-utf8 parser)

Source§

type Output = Parser<'a, u8, O>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: Parser<'a, u8, O>) -> Self::Output

Performs the | operation. Read more
Source§

impl<'a, O: 'a> BitOr for Parser<'a, O>

Ordered choice

Source§

type Output = Parser<'a, O>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: Self) -> Self

Performs the | operation. Read more
Source§

impl<'a, O> From<Parser<'a, O>> for Parser<'a, u8, O>

Source§

fn from(parser: Parser<'a, O>) -> Self

Converts to this type from the input type.
Source§

impl<'a, Left: 'a, Right: 'a> Mul<Parser<'a, Right>> for Parser<'a, Left>

Sequence discard first value

Source§

type Output = Parser<'a, Right>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Parser<'a, Right>) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a, Left: 'a, Right: 'a> Mul<Parser<'a, Right>> for Parser<'a, u8, Left>

Sequence discard first value (but degrade to non-utf8 parser)

Source§

type Output = Parser<'a, u8, Right>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Parser<'a, Right>) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a, Left: 'a, Right: 'a> Mul<Parser<'a, u8, Right>> for Parser<'a, Left>

Sequence discard first value (but degrade to non-utf8 parser)

Source§

type Output = Parser<'a, u8, Right>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Parser<'a, u8, Right>) -> Self::Output

Performs the * operation. Read more
Source§

impl<'a, O: 'a> Neg for Parser<'a, O>

And predicate

Source§

type Output = Parser<'a, bool>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<'a, O: 'a> Not for Parser<'a, O>

Not predicate

Source§

type Output = Parser<'a, bool>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<'a, O: 'a, U: 'a, F: Fn(O) -> Parser<'a, U> + 'a> Shr<F> for Parser<'a, O>

Chain two parsers where the second parser depends on the first’s result.

Source§

type Output = Parser<'a, U>

The resulting type after applying the >> operator.
Source§

fn shr(self, other: F) -> Self::Output

Performs the >> operation. Read more
Source§

impl<'a, Left: 'a, Right: 'a> Sub<Parser<'a, Right>> for Parser<'a, Left>

Sequence discard second value

Source§

type Output = Parser<'a, Left>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Parser<'a, Right>) -> Self::Output

Performs the - operation. Read more
Source§

impl<'a, Left: 'a, Right: 'a> Sub<Parser<'a, Right>> for Parser<'a, u8, Left>

Sequence discard second value (but degrade to non-utf8 parser)

Source§

type Output = Parser<'a, u8, Left>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Parser<'a, Right>) -> Self::Output

Performs the - operation. Read more
Source§

impl<'a, Left: 'a, Right: 'a> Sub<Parser<'a, u8, Right>> for Parser<'a, Left>

Sequence discard second value (but degrade to non-utf8 parser)

Source§

type Output = Parser<'a, u8, Left>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Parser<'a, u8, Right>) -> Self::Output

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<'a, O> Freeze for Parser<'a, O>

§

impl<'a, O> !RefUnwindSafe for Parser<'a, O>

§

impl<'a, O> !Send for Parser<'a, O>

§

impl<'a, O> !Sync for Parser<'a, O>

§

impl<'a, O> Unpin for Parser<'a, O>

§

impl<'a, O> !UnwindSafe for Parser<'a, O>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Source§

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.