pub fn seq<'a, 'b: 'a, I>(tag: &'b [I]) -> Parser<'a, I, &'a [I]>
Expand description
Success when sequence of symbols matches current input.
Examples found in repository?
examples/simple.rs (line 7)
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()));
}
More examples
examples/whitespace.rs (line 53)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
fn container<'a>() -> Parser<'a, u8, Container> {
seq(b"Container\n")
* (indented() | empty().map(|()| vec![]))
.repeat(1..)
.map(|lines| {
lines
.into_iter()
.filter(|line| line.len() > 0)
.fold(vec![], |accum, line| {
accum
.into_iter()
.chain(line.into_iter().chain(vec![b'\n'].into_iter()))
.collect()
})
})
.map(|deden| subcontainer().parse(&deden).expect("subcont"))
.map(|(containers, contents)| Container {
containers,
contents,
})
}
examples/json.rs (line 46)
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 73 74 75 76 77 78 79 80 81
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<_, _>>())
}
fn value<'a>() -> Parser<'a, u8, JsonValue> {
(seq(b"null").map(|_| JsonValue::Null)
| seq(b"true").map(|_| JsonValue::Bool(true))
| seq(b"false").map(|_| JsonValue::Bool(false))
| number().map(|num| JsonValue::Num(num))
| string().map(|text| JsonValue::Str(text))
| array().map(|arr| JsonValue::Array(arr))
| object().map(|obj| JsonValue::Object(obj)))
- space()
}
examples/json_file.rs (line 48)
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 73 74 75 76 77 78 79 80 81 82 83
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<_, _>>())
}
fn value<'a>() -> Parser<'a, u8, JsonValue> {
(seq(b"null").map(|_| JsonValue::Null)
| seq(b"true").map(|_| JsonValue::Bool(true))
| seq(b"false").map(|_| JsonValue::Bool(false))
| number().map(|num| JsonValue::Num(num))
| string().map(|text| JsonValue::Str(text))
| array().map(|arr| JsonValue::Array(arr))
| object().map(|obj| JsonValue::Object(obj)))
- space()
}