Skip to main content

call

Function call 

Source
pub fn call<'a, I, O, F>(parser_factory: F) -> Parser<'a, I, O>
where O: 'a, F: Fn() -> Parser<'a, I, O> + 'a,
Expand description

Call a parser factory, can be used to create recursive parsers.

Examples found in repository?
examples/json_char.rs (line 60)
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}
More examples
Hide additional examples
examples/json.rs (line 43)
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/whitespace.rs (line 36)
34fn subcontainer<'a>() -> Parser<'a, u8, (Vec<Container>, Vec<String>)> {
35	(
36		call(container).map(|ctr| TmpContainerOrContent::Container(ctr)) |
37		content().map(|ctn| TmpContainerOrContent::Content(ctn))
38	).repeat(1..).map(
39		|tmp| {
40			tmp.into_iter().fold(
41				(vec![], vec![]),
42				|acc, x| match x {
43					TmpContainerOrContent::Container(ct) => (
44						acc.0.into_iter().chain(vec![ct].into_iter()).collect(),
45						acc.1,
46					),
47					TmpContainerOrContent::Content(cn) => (
48						acc.0,
49						acc.1.into_iter().chain(vec![cn].into_iter()).collect(),
50					),
51				}
52			)
53		}
54	)
55}
56
57fn container<'a>() -> Parser<'a, u8, Container> {
58	seq(b"Container\n") *
59	(
60		indented() |
61		empty().map(|()| vec![])
62	).repeat(1..).map(
63		|lines| lines.into_iter().filter(
64			|line| line.len() > 0
65		).fold(
66			vec![],
67			|accum, line| accum.into_iter().chain(
68				line.into_iter().chain(vec![b'\n'].into_iter())
69			).collect()
70		)
71	).map(|deden| {
72		subcontainer().parse(&deden).expect("subcont")
73	}).map(|(containers, contents)| Container { containers, contents })
74}
75
76fn mylang<'a>() -> Parser<'a, u8, Vec<Container>> {
77	(
78		whitespace() *
79		list(
80			call(container),
81			whitespace()
82		)
83	)
84}