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

	fn rest_str<'a>() -> Parser<'a, &'a str> {
		any().repeat(1..).collect()
	}
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)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
fn main() {
	// Informal, Spanish-language movie database format
	let input = "\
Título: Abre los ojos
Año: 1997
Director: Alejandro Amenábar

Título: Amores Perros
Director: Alejandro González Iñárritu
Año: 2000

Título: La montaña sagrada
Año: 1973
Director: Alejandro Jodorowsky
";

	enum DataLine<'a> {
		Title(&'a str),
		Director(&'a str),
		Year(i32),
	}

	fn positive<'a>() -> Parser<'a, i32> {
		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
		let digit = one_of("0123456789");
		let integer = digit.discard().repeat(1..);
		integer.collect().convert(|x| x.parse::<i32>())
	}

	fn rest_str<'a>() -> Parser<'a, &'a str> {
		any().repeat(1..).collect()
	}

	fn separator<'a>() -> Parser<'a, ()> {
		seq(": ").discard()
	}

	let parser = (seq("Título") * separator() * rest_str().map(|s| DataLine::Title(s)))
		| (seq("Director") * separator() * rest_str().map(|s| DataLine::Director(s)))
		| (seq("Año") * separator() * positive().map(|i| DataLine::Year(i)));

	{
		let mut title_opt: Option<&str> = None;
		let mut year_opt: Option<i32> = None;
		let mut director_opt: Option<&str> = None;

		for line in input.lines() {
			if !line.is_empty() {
				// Skip blank lines without parsing
				// Parse line
				match parser.parse_str(line).unwrap() {
					DataLine::Title(s) => title_opt = Some(s),
					DataLine::Director(s) => director_opt = Some(s),
					DataLine::Year(s) => year_opt = Some(s),
				}
				// When all three line types have been collected, print them
				if let (Some(title), Some(year), Some(director)) =
					(title_opt, year_opt, director_opt)
				{
					println!("Title: {}\nDirector: {}\nYear: {}\n", title, director, year);
					(title_opt, year_opt, director_opt) = (None, None, None);
				}
			}
		}
	}
}
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)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
fn main() {
	// Informal, Spanish-language movie database format
	let input = "\
Título: Abre los ojos
Año: 1997
Director: Alejandro Amenábar

Título: Amores Perros
Director: Alejandro González Iñárritu
Año: 2000

Título: La montaña sagrada
Año: 1973
Director: Alejandro Jodorowsky
";

	enum DataLine<'a> {
		Title(&'a str),
		Director(&'a str),
		Year(i32),
	}

	fn positive<'a>() -> Parser<'a, i32> {
		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
		let digit = one_of("0123456789");
		let integer = digit.discard().repeat(1..);
		integer.collect().convert(|x| x.parse::<i32>())
	}

	fn rest_str<'a>() -> Parser<'a, &'a str> {
		any().repeat(1..).collect()
	}

	fn separator<'a>() -> Parser<'a, ()> {
		seq(": ").discard()
	}

	let parser = (seq("Título") * separator() * rest_str().map(|s| DataLine::Title(s)))
		| (seq("Director") * separator() * rest_str().map(|s| DataLine::Director(s)))
		| (seq("Año") * separator() * positive().map(|i| DataLine::Year(i)));

	{
		let mut title_opt: Option<&str> = None;
		let mut year_opt: Option<i32> = None;
		let mut director_opt: Option<&str> = None;

		for line in input.lines() {
			if !line.is_empty() {
				// Skip blank lines without parsing
				// Parse line
				match parser.parse_str(line).unwrap() {
					DataLine::Title(s) => title_opt = Some(s),
					DataLine::Director(s) => director_opt = Some(s),
					DataLine::Year(s) => year_opt = Some(s),
				}
				// When all three line types have been collected, print them
				if let (Some(title), Some(year), Some(director)) =
					(title_opt, year_opt, director_opt)
				{
					println!("Title: {}\nDirector: {}\nYear: {}\n", title, director, year);
					(title_opt, year_opt, director_opt) = (None, None, None);
				}
			}
		}
	}
}
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
28
29
30
31
32
	fn positive<'a>() -> Parser<'a, i32> {
		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
		let digit = one_of("0123456789");
		let integer = digit.discard().repeat(1..);
		integer.collect().convert(|x| x.parse::<i32>())
	}
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
28
29
30
31
32
33
34
35
36
37
38
39
40
	fn positive<'a>() -> Parser<'a, i32> {
		//		let integer = (one_of("123456789") - one_of("0123456789").repeat(0..)) | sym(b'0'); // TODO
		let digit = one_of("0123456789");
		let integer = digit.discard().repeat(1..);
		integer.collect().convert(|x| x.parse::<i32>())
	}

	fn rest_str<'a>() -> Parser<'a, &'a str> {
		any().repeat(1..).collect()
	}

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

	fn rest_str<'a>() -> Parser<'a, &'a str> {
		any().repeat(1..).collect()
	}
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

§

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)

§

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)

§

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)

§

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)

§

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

§

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

§

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)

§

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)

§

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

§

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

§

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.

§

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

§

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)

§

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)

§

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> !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>,

§

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>,

§

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.