Skip to content

Commit 1e2e500

Browse files
committed
boolean; null; list; object
1 parent 4b1062c commit 1e2e500

File tree

1 file changed

+75
-4
lines changed

1 file changed

+75
-4
lines changed

src/parser.rs

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,17 @@ make_parser!(
371371
);
372372

373373
make_parser!(
374-
ValueParser(input: char, variable: &bool) -> Value {
374+
ValueParser(input: char, constant: &bool) -> Value {
375375

376376
let mut constants = try(FloatValue::new())
377-
.or(IntValue::new());
378-
379-
if *variable {
377+
.or(IntValue::new())
378+
.or(BooleanValue::new())
379+
.or(NullValue::new())
380+
//.or(EnumValue::new())
381+
.or(ListValue::new(constant))
382+
.or(ObjectValue::new(constant));
383+
384+
if *constant {
380385
VariableParser::new()
381386
.map(Value::Variable)
382387
.or(constants)
@@ -416,6 +421,7 @@ make_parser!(
416421
}
417422
})
418423
.map(Value::Int)
424+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
419425
.parse_lazy(input)
420426
}
421427
);
@@ -467,6 +473,7 @@ make_parser!(
467473
result.parse::<f32>().unwrap()
468474
})
469475
.map(Value::Float)
476+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
470477
.parse_lazy(input)
471478
}
472479
);
@@ -501,6 +508,66 @@ make_parser!(
501508
}
502509
);
503510

511+
make_parser!(
512+
BooleanValue(input: char) -> Value {
513+
string("true").map(|_| Value::Boolean(true))
514+
.or(string("false").map(|_| Value::Boolean(false)))
515+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
516+
.parse_lazy(input)
517+
}
518+
);
519+
520+
make_parser!(
521+
NullValue(input: char) -> Value {
522+
string("null")
523+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
524+
.map(|_| Value::Null)
525+
.parse_lazy(input)
526+
}
527+
);
528+
529+
// TODO enum parser: Name but not true or false or null
530+
531+
make_parser!(
532+
ListValue(input: char, constant: &bool) -> Value {
533+
between(char('['), char(']'), many(ValueParser::new(constant)))
534+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
535+
.map(Value::List)
536+
.parse_lazy(input)
537+
}
538+
);
539+
540+
make_parser!(
541+
ObjectField(input: char, constant: &bool) -> (String, Value) {
542+
NameParser::new()
543+
.skip(char(':'))
544+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
545+
.and(ValueParser::new(constant))
546+
.parse_lazy(input)
547+
}
548+
);
549+
550+
make_parser!(
551+
ObjectValue(input: char, constant: &bool) -> Value {
552+
between(char('{'), char('}'), many::<Vec<_>,_>(ObjectField::new(constant)))
553+
.skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
554+
.map(|fields| {
555+
let mut result = HashMap::new();
556+
557+
for (name,value) in fields.into_iter() {
558+
// TODO complain about same name fields?
559+
result.insert(name, value);
560+
}
561+
562+
Value::Object(result)
563+
})
564+
.parse_lazy(input)
565+
}
566+
);
567+
568+
// TODO .skip(many::<Vec<_>,_>(or(WhiteSpace::new(), LineTerminator::new(&true))))
569+
// TODO add commas to this thing
570+
504571
make_parser!(
505572
Alias(input: char) -> Name {
506573
NameParser::new()
@@ -509,6 +576,7 @@ make_parser!(
509576
.parse_lazy(input)
510577
}
511578
);
579+
512580
#[cfg(test)]
513581
mod tests {
514582
use super::*;
@@ -636,4 +704,7 @@ mod tests {
636704
assert_successful_parse!(FloatValue, "10e+1", Value::Float(10.0e1));
637705
assert_successful_parse!(FloatValue, "10.0e-1", Value::Float(10.0e-1));
638706
}
707+
708+
// TODO add tests for object value
709+
// TODO add tests for list values
639710
}

0 commit comments

Comments
 (0)