Function sym

Source
pub fn sym<'a, I>(t: I) -> Parser<'a, I, I>
where I: Clone + PartialEq + Display,
Expand description

Success when current input symbol equals t.

Examples found in repository?
examples/whitespace.rs (line 19)
18fn linebreak<'a>() -> Parser<'a, u8, ()> {
19	sym(b'\r').opt() * sym(b'\n').discard()
20}
21
22fn indented<'a>() -> Parser<'a, u8, Vec<u8>> {
23	sym(b'\t') * none_of(b"\n\r").repeat(1..) - linebreak()
24}
More examples
Hide additional examples
examples/simple.rs (line 5)
3fn main() {
4	let input = b"abcde";
5	let parser = sym(b'a') * none_of(b"AB") - sym(b'c') + seq(b"de");
6	let output = parser.parse(input);
7	// assert_eq!(output, Ok( (b'b', &b"de"[..]) ) );
8	println!("{:?}", output);
9}
examples/duration.rs (line 33)
32fn date_part() -> Parser<u8, (Option<f32>, Option<f32>, Option<f32>, Option<f32>)> {
33	((number() - sym(b'Y')).opt()
34		+ (number() - sym(b'M')).opt()
35		+ (number() - sym(b'W')).opt()
36		+ (number() - sym(b'D')).opt())
37	.map(|(((years, months), weeks), days)| (years, months, weeks, days))
38}
39
40fn time_part() -> Parser<u8, (Option<f32>, Option<f32>, Option<f32>)> {
41	sym(b'T')
42		* ((number() - sym(b'H')).opt()
43			+ (number() - sym(b'M')).opt()
44			+ (number() - sym(b'S')).opt())
45		.map(|((hours, minutes), seconds)| (hours, minutes, seconds))
46}
47
48fn parser() -> Parser<u8, Duration> {
49	sym(b'P')
50		* (time_part().map(|(hours, minutes, seconds)| Duration {
51			years: None,
52			months: None,
53			weeks: None,
54			days: None,
55			hours,
56			minutes,
57			seconds,
58		}) | (date_part() + time_part()).map(|(date_elements, time_elements)| {
59			let (years, months, weeks, days) = date_elements;
60			let (hours, minutes, seconds) = time_elements;
61			Duration {
62				years,
63				months,
64				weeks,
65				days,
66				hours,
67				minutes,
68				seconds,
69			}
70		}))
71}
examples/json.rs (line 23)
22fn number<'a>() -> Parser<'a, u8, f64> {
23	let integer = one_of(b"123456789") - one_of(b"0123456789").repeat(0..) | sym(b'0');
24	let frac = sym(b'.') + one_of(b"0123456789").repeat(1..);
25	let exp = one_of(b"eE") + one_of(b"+-").opt() + one_of(b"0123456789").repeat(1..);
26	let number = sym(b'-').opt() + integer + frac.opt() + exp.opt();
27	number.collect().convert(str::from_utf8).convert(f64::from_str)
28}
29
30fn string<'a>() -> Parser<'a, u8, String> {
31	let special_char = sym(b'\\') | sym(b'/') | sym(b'"')
32		| sym(b'b').map(|_|b'\x08') | sym(b'f').map(|_|b'\x0C')
33		| sym(b'n').map(|_|b'\n') | sym(b'r').map(|_|b'\r') | sym(b't').map(|_|b'\t');
34	let escape_sequence = sym(b'\\') * special_char;
35	let char_string = (none_of(b"\\\"") | escape_sequence).repeat(1..).convert(String::from_utf8);
36	let utf16_char = seq(b"\\u") * is_a(hex_digit).repeat(4).convert(String::from_utf8).convert(|digits|u16::from_str_radix(&digits, 16));
37	let utf16_string = utf16_char.repeat(1..).map(|chars|decode_utf16(chars).map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)).collect::<String>());
38	let string = sym(b'"') * (char_string | utf16_string).repeat(0..) - sym(b'"');
39	string.map(|strings| strings.concat())
40}
41
42fn array<'a>() -> Parser<'a, u8, Vec<JsonValue>> {
43	let elems = list(call(value), sym(b',') * space());
44	sym(b'[') * space() * elems - sym(b']')
45}
46
47fn object<'a>() -> Parser<'a, u8, HashMap<String, JsonValue>> {
48	let member = string() - space() - sym(b':') - space() + call(value);
49	let members = list(member, sym(b',') * space());
50	let obj = sym(b'{') * space() * members - sym(b'}');
51	obj.map(|members| members.into_iter().collect::<HashMap<_, _>>())
52}
examples/json_char.rs (line 23)
22fn number<'a>() -> Parser<'a, char, f64> {
23	let integer = one_of("123456789") - one_of("0123456789").repeat(0..) | sym('0');
24	let frac = sym('.') + one_of("0123456789").repeat(1..);
25	let exp = one_of("eE") + one_of("+-").opt() + one_of("0123456789").repeat(1..);
26	let number = sym('-').opt() + integer + frac.opt() + exp.opt();
27	number
28		.collect()
29		.map(String::from_iter)
30		.convert(|s| f64::from_str(&s))
31}
32
33fn string<'a>() -> Parser<'a, char, String> {
34	let special_char = sym('\\')
35		| sym('/')
36		| sym('"')
37		| sym('b').map(|_| '\x08')
38		| sym('f').map(|_| '\x0C')
39		| sym('n').map(|_| '\n')
40		| sym('r').map(|_| '\r')
41		| sym('t').map(|_| '\t');
42	let escape_sequence = sym('\\') * special_char;
43	let char_string = (none_of("\\\"") | escape_sequence)
44		.repeat(1..)
45		.map(String::from_iter);
46	let utf16_char = tag("\\u") * is_a(|c: char| c.is_digit(16))
47		.repeat(4)
48		.map(String::from_iter)
49		.convert(|digits| u16::from_str_radix(&digits, 16));
50	let utf16_string = utf16_char.repeat(1..).map(|chars| {
51		decode_utf16(chars)
52			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
53			.collect::<String>()
54	});
55	let string = sym('"') * (char_string | utf16_string).repeat(0..) - sym('"');
56	string.map(|strings| strings.concat())
57}
58
59fn array<'a>() -> Parser<'a, char, Vec<JsonValue>> {
60	let elems = list(call(value), sym(',') * space());
61	sym('[') * space() * elems - sym(']')
62}
63
64fn object<'a>() -> Parser<'a, char, HashMap<String, JsonValue>> {
65	let member = string() - space() - sym(':') - space() + call(value);
66	let members = list(member, sym(',') * space());
67	let obj = sym('{') * space() * members - sym('}');
68	obj.map(|members| members.into_iter().collect::<HashMap<_, _>>())
69}