Skip to content

Commit 45e2acd

Browse files
committed
format; parse argument(s); parse directive(s);
1 parent 0faf82d commit 45e2acd

File tree

1 file changed

+101
-6
lines changed

1 file changed

+101
-6
lines changed

src/parser.rs

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,30 @@ pub struct Directive {
9797
arguments: Vec<Argument>,
9898
}
9999

100+
impl Directive {
101+
fn new(name: Name, arguments: Vec<Argument>) -> Directive {
102+
Directive {
103+
name: name,
104+
arguments: arguments,
105+
}
106+
}
107+
}
108+
100109
#[derive(Debug,PartialEq,Clone)]
101110
pub struct Argument {
102111
name: Name,
103112
value: Value,
104113
}
105114

115+
impl Argument {
116+
fn new(name: Name, value: Value) -> Argument {
117+
Argument {
118+
name: name,
119+
value: value,
120+
}
121+
}
122+
}
123+
106124
#[derive(Debug,PartialEq,Clone)]
107125
pub enum Selection {
108126
Field(Field),
@@ -544,9 +562,9 @@ make_parser!(
544562

545563
fn hex_digit_to_u8(input: char) -> u8 {
546564
match input {
547-
'0' ... '9' => (input as u8) - ('0' as u8),
548-
'a' ... 'f' => (input as u8) - ('a' as u8) + 10,
549-
'A' ... 'F' => (input as u8) - ('A' as u8) + 10,
565+
'0'...'9' => (input as u8) - ('0' as u8),
566+
'a'...'f' => (input as u8) - ('a' as u8) + 10,
567+
'A'...'F' => (input as u8) - ('A' as u8) + 10,
550568
_ => 0,
551569
}
552570
}
@@ -674,6 +692,47 @@ make_parser!(
674692
}
675693
);
676694

695+
make_parser!(
696+
Directives(input: char) -> Vec<Directive> {
697+
many::<Vec<_>,_>(DirectiveParser::new())
698+
.parse_lazy(input)
699+
}
700+
);
701+
702+
make_parser!(
703+
DirectiveParser(input: char) -> Directive {
704+
char('@')
705+
.with(NameParser::new())
706+
.and(optional(Arguments::new()))
707+
.map(|(name,opt_args)| {
708+
Directive::new(name, match opt_args {
709+
Some(args) => args,
710+
None => Vec::new(),
711+
})
712+
})
713+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
714+
.parse_lazy(input)
715+
}
716+
);
717+
718+
make_parser!(
719+
Arguments(input: char) -> Vec<Argument> {
720+
between(char('('), char(')'), many::<Vec<_>,_>(ArgumentParser::new()))
721+
.parse_lazy(input)
722+
}
723+
);
724+
725+
make_parser!(
726+
ArgumentParser(input: char) -> Argument {
727+
NameParser::new()
728+
.skip(char(':'))
729+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
730+
.and(ValueParser::new(&false))
731+
.map(|(name,value)| Argument::new(name,value))
732+
.parse_lazy(input)
733+
}
734+
);
735+
677736
// TODO .skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
678737
// TODO add commas to this thing
679738

@@ -918,8 +977,44 @@ mod tests {
918977
assert_successful_parse!(StringValue, r#""""#, Value::String(String::from("")));
919978

920979
// strings with random stuff in it
921-
assert_successful_parse!(StringValue, r#""hello world""#, Value::String(String::from("hello world")));
922-
assert_successful_parse!(StringValue, r#""hello \u0025""#, Value::String(String::from("hello %")));
923-
assert_successful_parse!(StringValue, r#""hello\n\u0025""#, Value::String(String::from("hello\n%")));
980+
assert_successful_parse!(StringValue,
981+
r#""hello world""#,
982+
Value::String(String::from("hello world")));
983+
assert_successful_parse!(StringValue,
984+
r#""hello \u0025""#,
985+
Value::String(String::from("hello %")));
986+
assert_successful_parse!(StringValue,
987+
r#""hello\n\u0025""#,
988+
Value::String(String::from("hello\n%")));
989+
}
990+
991+
#[test]
992+
fn test_parse_argument() {
993+
assert_successful_parse!(ArgumentParser,
994+
"x:1",
995+
Argument::new(String::from("x"), Value::Int(1)));
996+
}
997+
998+
#[test]
999+
fn test_parse_directive() {
1000+
// directive no arguments
1001+
assert_successful_parse!(DirectiveParser,
1002+
"@dir",
1003+
Directive::new(String::from("dir"), Vec::new()));
1004+
1005+
// directive with arguments
1006+
assert_successful_parse!(DirectiveParser,
1007+
"@dir(x:1)",
1008+
Directive::new(String::from("dir"),
1009+
vec![Argument::new(String::from("x"), Value::Int(1))]));
1010+
}
1011+
1012+
#[test]
1013+
fn test_parse_directives() {
1014+
assert_successful_parse!(Directives,
1015+
"@dir\n@dir2(x:1)",
1016+
vec![Directive::new(String::from("dir"), Vec::new()),
1017+
Directive::new(String::from("dir2"),
1018+
vec![Argument::new(String::from("x"), Value::Int(1))])]);
9241019
}
9251020
}

0 commit comments

Comments
 (0)