1
2
3
4
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
use ast::ASTNode;
use ast::ASTNode::*;
use exp::parse_exp;
use name::parse_name;
named!(pub parse_fieldlist<ASTNode>, map!(
map!(do_parse!(
a: parse_field
>> b: many0!(preceded!(parse_fieldsep, parse_field))
>> (a,b)
), |(a, mut b): (_, Vec <ASTNode>) | { b.insert(0, a); b }),
ASTNode::FieldList));
named!(parse_field<ASTNode>, ws!(alt!(
do_parse!(
n: delimited!(tag!("["), ws!(parse_exp), tag!("]"))
>> ws!(tag!("="))
>> e: parse_exp
>> (astb!(FieldAssign, n, e)))|
do_parse!(
n: parse_name
>> ws!(tag!("="))
>> e: parse_exp
>> (astb!(FieldAssign, n, e)))|
map!(map!(parse_exp, Box::new), ASTNode::FieldSingle)
)));
named!(parse_fieldsep, alt!(tag!(",") | tag!(";")));
#[cfg(test)]
mod tests {
use ast::ASTNode::*;
ast_valid!(parse_fieldsep_1, parse_fieldsep, ";");
ast_valid!(parse_fieldsep_2, parse_fieldsep, ",");
ast_test!(parse_field_assign_1, parse_field, " [ true ] = true ",
astb!(FieldAssign, ast!(Bool, true), ast!(Bool, true)));
ast_test!(parse_field_assign_2, parse_field, "[true]=nil",
astb!(FieldAssign, ast!(Bool, true), ast!(Nil)));
ast_test!(parse_field_assign_3, parse_field, "is=true",
astb!(FieldAssign, ast!(Name, "is".into()), ast!(Bool, true)));
ast_test!(parse_field_single_1, parse_field, "true",
astb!(FieldSingle, ast!(Bool, true)));
}