pub fn sym<'a, I>(t: I) -> Parser<'a, I, I>
Expand description
Success when current input symbol equals t
.
Examples found in repository?
More examples
examples/simple.rs (line 4)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn example<'a>() -> Parser<'a, u8, Vec<u8>> {
(sym(b'<') * none_of(b">").repeat(0..) - sym(b'>'))
>> |tag| {
(call(example) | none_of(b"<>").repeat(0..))
- seq(b"</") - take(tag.len()).convert(move |t| if t == tag { Ok(()) } else { Err(()) })
- sym(b'>')
}
}
fn main() {
let input = b"abcde";
let parser = sym(b'a') * none_of(b"AB") - sym(b'c') + seq(b"de");
let output = parser.parse(input);
// assert_eq!(output, Ok( (b'b', &b"de"[..]) ) );
println!("{:?}", output);
println!("{:?}", example().parse("<app>bcb</app>".as_bytes()));
}
examples/duration.rs (line 33)
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 71
fn date_part() -> Parser<u8, (Option<f32>, Option<f32>, Option<f32>, Option<f32>)> {
((number() - sym(b'Y')).opt()
+ (number() - sym(b'M')).opt()
+ (number() - sym(b'W')).opt()
+ (number() - sym(b'D')).opt())
.map(|(((years, months), weeks), days)| (years, months, weeks, days))
}
fn time_part() -> Parser<u8, (Option<f32>, Option<f32>, Option<f32>)> {
sym(b'T')
* ((number() - sym(b'H')).opt()
+ (number() - sym(b'M')).opt()
+ (number() - sym(b'S')).opt())
.map(|((hours, minutes), seconds)| (hours, minutes, seconds))
}
fn parser() -> Parser<u8, Duration> {
sym(b'P')
* (time_part().map(|(hours, minutes, seconds)| Duration {
years: None,
months: None,
weeks: None,
days: None,
hours,
minutes,
seconds,
}) | (date_part() + time_part()).map(|(date_elements, time_elements)| {
let (years, months, weeks, days) = date_elements;
let (hours, minutes, seconds) = time_elements;
Duration {
years,
months,
weeks,
days,
hours,
minutes,
seconds,
}
}))
}
examples/json.rs (line 23)
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 number<'a>() -> Parser<'a, u8, f64> {
let integer = one_of(b"123456789") - one_of(b"0123456789").repeat(0..) | sym(b'0');
let frac = sym(b'.') + one_of(b"0123456789").repeat(1..);
let exp = one_of(b"eE") + one_of(b"+-").opt() + one_of(b"0123456789").repeat(1..);
let number = sym(b'-').opt() + integer + frac.opt() + exp.opt();
number
.collect()
.convert(str::from_utf8)
.convert(f64::from_str)
}
fn string<'a>() -> Parser<'a, u8, String> {
let special_char = sym(b'\\')
| sym(b'/')
| sym(b'"')
| sym(b'b').map(|_| b'\x08')
| sym(b'f').map(|_| b'\x0C')
| sym(b'n').map(|_| b'\n')
| sym(b'r').map(|_| b'\r')
| sym(b't').map(|_| b'\t');
let escape_sequence = sym(b'\\') * special_char;
let char_string = (none_of(b"\\\"") | escape_sequence)
.repeat(1..)
.convert(String::from_utf8);
let utf16_char = seq(b"\\u")
* is_a(hex_digit)
.repeat(4)
.convert(String::from_utf8)
.convert(|digits| u16::from_str_radix(&digits, 16));
let utf16_string = utf16_char.repeat(1..).map(|chars| {
decode_utf16(chars)
.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
.collect::<String>()
});
let string = sym(b'"') * (char_string | utf16_string).repeat(0..) - sym(b'"');
string.map(|strings| strings.concat())
}
fn array<'a>() -> Parser<'a, u8, Vec<JsonValue>> {
let elems = list(call(value), sym(b',') * space());
sym(b'[') * space() * elems - sym(b']')
}
fn object<'a>() -> Parser<'a, u8, HashMap<String, JsonValue>> {
let member = string() - space() - sym(b':') - space() + call(value);
let members = list(member, sym(b',') * space());
let obj = sym(b'{') * space() * members - sym(b'}');
obj.map(|members| members.into_iter().collect::<HashMap<_, _>>())
}
examples/json_char.rs (line 23)
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 number<'a>() -> Parser<'a, char, f64> {
let integer = one_of("123456789") - one_of("0123456789").repeat(0..) | sym('0');
let frac = sym('.') + one_of("0123456789").repeat(1..);
let exp = one_of("eE") + one_of("+-").opt() + one_of("0123456789").repeat(1..);
let number = sym('-').opt() + integer + frac.opt() + exp.opt();
number
.collect()
.map(String::from_iter)
.convert(|s| f64::from_str(&s))
}
fn string<'a>() -> Parser<'a, char, String> {
let special_char = sym('\\')
| sym('/')
| sym('"')
| sym('b').map(|_| '\x08')
| sym('f').map(|_| '\x0C')
| sym('n').map(|_| '\n')
| sym('r').map(|_| '\r')
| sym('t').map(|_| '\t');
let escape_sequence = sym('\\') * special_char;
let char_string = (none_of("\\\"") | escape_sequence)
.repeat(1..)
.map(String::from_iter);
let utf16_char = tag("\\u")
* is_a(|c: char| c.is_digit(16))
.repeat(4)
.map(String::from_iter)
.convert(|digits| u16::from_str_radix(&digits, 16));
let utf16_string = utf16_char.repeat(1..).map(|chars| {
decode_utf16(chars)
.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
.collect::<String>()
});
let string = sym('"') * (char_string | utf16_string).repeat(0..) - sym('"');
string.map(|strings| strings.concat())
}
fn array<'a>() -> Parser<'a, char, Vec<JsonValue>> {
let elems = list(call(value), sym(',') * space());
sym('[') * space() * elems - sym(']')
}
fn object<'a>() -> Parser<'a, char, HashMap<String, JsonValue>> {
let member = string() - space() - sym(':') - space() + call(value);
let members = list(member, sym(',') * space());
let obj = sym('{') * space() * members - sym('}');
obj.map(|members| members.into_iter().collect::<HashMap<_, _>>())
}
examples/json_file.rs (line 25)
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 71 72
fn number<'a>() -> Parser<'a, u8, f64> {
let integer = one_of(b"123456789") - one_of(b"0123456789").repeat(0..) | sym(b'0');
let frac = sym(b'.') + one_of(b"0123456789").repeat(1..);
let exp = one_of(b"eE") + one_of(b"+-").opt() + one_of(b"0123456789").repeat(1..);
let number = sym(b'-').opt() + integer + frac.opt() + exp.opt();
number
.collect()
.convert(str::from_utf8)
.convert(f64::from_str)
}
fn string<'a>() -> Parser<'a, u8, String> {
let special_char = sym(b'\\')
| sym(b'/')
| sym(b'"')
| sym(b'b').map(|_| b'\x08')
| sym(b'f').map(|_| b'\x0C')
| sym(b'n').map(|_| b'\n')
| sym(b'r').map(|_| b'\r')
| sym(b't').map(|_| b'\t');
let escape_sequence = sym(b'\\') * special_char;
let char_string = (none_of(b"\\\"") | escape_sequence)
.repeat(1..)
.convert(String::from_utf8);
let utf16_char = seq(b"\\u")
* is_a(hex_digit)
.repeat(4)
.convert(String::from_utf8)
.convert(|digits| u16::from_str_radix(&digits, 16));
let utf16_string = utf16_char.repeat(1..).map(|chars| {
decode_utf16(chars)
.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
.collect::<String>()
});
let string = sym(b'"') * (char_string | utf16_string).repeat(0..) - sym(b'"');
string.map(|strings| strings.concat())
}
fn array<'a>() -> Parser<'a, u8, Vec<JsonValue>> {
let elems = list(call(value), sym(b',') * space());
sym(b'[') * space() * elems - sym(b']')
}
fn object<'a>() -> Parser<'a, u8, HashMap<String, JsonValue>> {
let member = string() - space() - sym(b':') - space() + call(value);
let members = list(member, sym(b',') * space());
let obj = sym(b'{') * space() * members - sym(b'}');
obj.map(|members| members.into_iter().collect::<HashMap<_, _>>())
}
Additional examples can be found in: