2022-10-15 23:36:04 -07:00
|
|
|
#![allow(dead_code)] //TODO eventually turn this off
|
2022-10-16 01:37:51 -07:00
|
|
|
mod bnf;
|
2022-10-16 19:21:43 -07:00
|
|
|
mod choice;
|
2022-10-16 19:16:21 -07:00
|
|
|
mod combinators;
|
2022-10-16 18:57:10 -07:00
|
|
|
mod parser;
|
2022-10-16 19:07:58 -07:00
|
|
|
mod primitives;
|
2022-10-16 17:33:10 -07:00
|
|
|
mod sequence;
|
2022-10-16 01:37:51 -07:00
|
|
|
|
2022-10-21 16:19:41 -07:00
|
|
|
pub use parser::{ParseResult, Parser, ParserInput};
|
2022-10-16 01:29:48 -07:00
|
|
|
|
2022-10-10 00:13:39 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-10-16 19:21:43 -07:00
|
|
|
use crate::choice::choice;
|
2022-10-19 22:06:10 -07:00
|
|
|
use crate::combinators::repeated;
|
2022-10-17 00:47:19 -07:00
|
|
|
use crate::primitives::{any_char, literal, literal_char, one_of, pred};
|
2022-10-16 21:36:23 -07:00
|
|
|
use crate::sequence::seq;
|
2022-10-10 00:13:39 -07:00
|
|
|
|
|
|
|
#[test]
|
2022-10-15 23:41:22 -07:00
|
|
|
fn test_parsing() {
|
2022-10-15 23:36:04 -07:00
|
|
|
let output = literal("a")("a yolo");
|
2022-10-16 19:21:43 -07:00
|
|
|
assert_eq!(output.unwrap(), ("a", " yolo"));
|
2022-10-16 01:29:48 -07:00
|
|
|
}
|
2022-10-16 01:36:20 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JSON BNF
|
|
|
|
* <JSON> ::= <value>
|
|
|
|
<value> ::= <object> | <array> | <boolean> | <string> | <number> | <null>
|
|
|
|
<array> ::= "[" [<value>] {"," <value>}* "]"
|
|
|
|
<object> ::= "{" [<property>] {"," <property>}* "}"
|
|
|
|
<property> ::= <string> ":" <value>
|
|
|
|
*/
|
2022-10-16 19:21:43 -07:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2022-10-16 01:42:03 -07:00
|
|
|
enum JsonValue {
|
|
|
|
Null,
|
|
|
|
Bool(bool),
|
|
|
|
Str(String),
|
|
|
|
Num(f64),
|
|
|
|
Array(Vec<JsonValue>),
|
2022-10-21 18:58:00 -07:00
|
|
|
Object(Vec<(String, JsonValue)>),
|
2022-10-16 01:42:03 -07:00
|
|
|
}
|
2022-10-16 01:36:20 -07:00
|
|
|
|
2022-10-19 22:35:13 -07:00
|
|
|
trait JsonParser<'a, T>: Parser<&'a str, T, &'a str> {}
|
|
|
|
impl<'a, T, P> JsonParser<'a, T> for P where P: Parser<&'a str, T, &'a str> {}
|
|
|
|
|
|
|
|
fn json_null<'a>() -> impl JsonParser<'a, JsonValue> {
|
|
|
|
literal("null").to(JsonValue::Null)
|
|
|
|
}
|
|
|
|
fn json_bool<'a>() -> impl JsonParser<'a, JsonValue> {
|
|
|
|
choice((
|
2022-10-16 21:36:23 -07:00
|
|
|
literal("true").to(JsonValue::Bool(true)),
|
|
|
|
literal("false").to(JsonValue::Bool(false)),
|
2022-10-19 22:35:13 -07:00
|
|
|
))
|
|
|
|
}
|
2022-10-16 01:36:20 -07:00
|
|
|
|
2022-10-19 22:35:13 -07:00
|
|
|
fn json_number() -> impl JsonParser<'static, JsonValue> {
|
2022-10-17 01:26:33 -07:00
|
|
|
let digit = || one_of("1234567890");
|
2022-10-19 22:23:52 -07:00
|
|
|
let digits = || repeated(digit()).at_least(1);
|
|
|
|
|
2022-10-17 01:26:33 -07:00
|
|
|
let json_number_inner = choice((
|
2022-10-19 22:23:52 -07:00
|
|
|
seq((digits(), literal(".").ignore_then(digits()).optional())).map(
|
|
|
|
|(mut digits, maybe_decimal)| {
|
|
|
|
if let Some(decimal_digits) = maybe_decimal {
|
|
|
|
digits.push(".");
|
|
|
|
digits.extend(decimal_digits.into_iter());
|
|
|
|
}
|
|
|
|
digits.into_iter().collect::<String>()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
literal(".").ignore_then(digits()).map(|decimal_digits| {
|
2022-10-17 01:26:33 -07:00
|
|
|
let mut d = vec!["."];
|
2022-10-19 22:23:52 -07:00
|
|
|
d.extend(decimal_digits.into_iter());
|
2022-10-17 01:26:33 -07:00
|
|
|
d.into_iter().collect::<String>()
|
|
|
|
}),
|
|
|
|
))
|
|
|
|
.map(|digits| digits.parse::<f64>().unwrap());
|
|
|
|
|
2022-10-19 22:35:13 -07:00
|
|
|
literal("-")
|
|
|
|
.optional()
|
|
|
|
.then(json_number_inner)
|
|
|
|
.map(|(maybe_sign, mut val)| {
|
|
|
|
if maybe_sign.is_some() {
|
|
|
|
val *= -1.0;
|
|
|
|
}
|
|
|
|
JsonValue::Num(val)
|
|
|
|
})
|
|
|
|
}
|
2022-10-17 01:26:33 -07:00
|
|
|
|
2022-10-21 18:58:00 -07:00
|
|
|
fn json_string_raw() -> impl JsonParser<'static, String> {
|
2022-10-19 22:35:13 -07:00
|
|
|
seq((
|
|
|
|
literal_char('"'),
|
2022-10-20 18:10:13 -07:00
|
|
|
repeated(pred(any_char, |ch| *ch != '"')),
|
2022-10-19 22:35:13 -07:00
|
|
|
literal_char('"'),
|
|
|
|
))
|
2022-10-21 18:58:00 -07:00
|
|
|
.map(|(_, s, _)| s.iter().cloned().collect::<String>())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn json_string() -> impl JsonParser<'static, JsonValue> {
|
|
|
|
json_string_raw().map(JsonValue::Str)
|
2022-10-19 22:35:13 -07:00
|
|
|
}
|
|
|
|
|
2022-10-20 17:55:46 -07:00
|
|
|
fn whitespace() -> impl JsonParser<'static, ()> {
|
|
|
|
repeated(literal_char(' ')).to(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn json_array() -> impl JsonParser<'static, JsonValue> {
|
2022-10-21 17:04:16 -07:00
|
|
|
move |input| {
|
2022-10-21 17:38:08 -07:00
|
|
|
let val = json_value().surrounded_by(whitespace());
|
2022-10-21 17:04:16 -07:00
|
|
|
|
|
|
|
repeated(val)
|
|
|
|
.separated_by(literal(","), false)
|
|
|
|
.delimited(literal_char('['), literal_char(']'))
|
|
|
|
.map(JsonValue::Array)
|
|
|
|
.parse(input)
|
|
|
|
}
|
2022-10-20 17:55:46 -07:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:58:00 -07:00
|
|
|
fn json_object() -> impl JsonParser<'static, JsonValue> {
|
|
|
|
move |input| {
|
|
|
|
let kv = json_string_raw()
|
|
|
|
.surrounded_by(whitespace())
|
|
|
|
.then_ignore(literal_char(':'))
|
|
|
|
.then(json_value().surrounded_by(whitespace()));
|
|
|
|
|
|
|
|
repeated(kv)
|
|
|
|
.separated_by(literal_char(','), false)
|
|
|
|
.delimited(literal_char('{'), literal_char('}'))
|
|
|
|
.map(JsonValue::Object)
|
|
|
|
.parse(input)
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 17:38:08 -07:00
|
|
|
|
2022-10-19 22:35:13 -07:00
|
|
|
fn json_value() -> impl JsonParser<'static, JsonValue> {
|
2022-10-21 15:53:26 -07:00
|
|
|
choice((
|
|
|
|
json_null(),
|
|
|
|
json_bool(),
|
|
|
|
json_number(),
|
|
|
|
json_string(),
|
|
|
|
json_array(),
|
2022-10-21 18:58:00 -07:00
|
|
|
json_object(),
|
2022-10-21 15:53:26 -07:00
|
|
|
))
|
2022-10-19 22:35:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-10-21 15:53:26 -07:00
|
|
|
fn parse_json_primitives() {
|
2022-10-20 18:10:13 -07:00
|
|
|
assert_eq!(
|
|
|
|
json_string().parse(r#""yolo swagg""#).unwrap(),
|
|
|
|
(JsonValue::Str("yolo swagg".into()), "")
|
|
|
|
);
|
2022-10-21 15:53:26 -07:00
|
|
|
|
2022-10-17 01:42:42 -07:00
|
|
|
assert_eq!(
|
2022-10-19 22:35:13 -07:00
|
|
|
json_number().parse("-383").unwrap().0,
|
2022-10-17 01:42:42 -07:00
|
|
|
JsonValue::Num(-383f64)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-10-19 22:35:13 -07:00
|
|
|
json_number().parse("-.383").unwrap().0,
|
2022-10-17 01:42:42 -07:00
|
|
|
JsonValue::Num(-0.383)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-10-19 22:35:13 -07:00
|
|
|
json_number().parse(".383").unwrap().0,
|
|
|
|
JsonValue::Num(0.383)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
json_number().parse("-1.383").unwrap().0,
|
2022-10-17 01:42:42 -07:00
|
|
|
JsonValue::Num(-1.383)
|
|
|
|
);
|
2022-10-21 15:53:26 -07:00
|
|
|
}
|
2022-10-17 00:47:19 -07:00
|
|
|
|
2022-10-21 15:53:26 -07:00
|
|
|
#[test]
|
|
|
|
fn parse_json_array() {
|
|
|
|
assert!(json_array().parse(r#"[ 4, 9, "ara",]"#).is_err());
|
2022-10-21 17:04:16 -07:00
|
|
|
assert_eq!(
|
|
|
|
json_array().parse("[[],[]]").unwrap().0,
|
|
|
|
JsonValue::Array(vec![JsonValue::Array(vec![]), JsonValue::Array(vec![])])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
json_array().parse(r#"[ 4, 9, "foo" ]"#).unwrap(),
|
|
|
|
(
|
|
|
|
JsonValue::Array(vec![
|
|
|
|
JsonValue::Num(4.),
|
|
|
|
JsonValue::Num(9.0),
|
|
|
|
JsonValue::Str("foo".to_string())
|
|
|
|
]),
|
|
|
|
""
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-10-21 18:58:00 -07:00
|
|
|
json_array().parse(r#"[8,null,[],5],{}"#).unwrap(),
|
2022-10-21 17:04:16 -07:00
|
|
|
(
|
|
|
|
JsonValue::Array(vec![
|
|
|
|
JsonValue::Num(8.),
|
|
|
|
JsonValue::Null,
|
|
|
|
JsonValue::Array(vec![]),
|
2022-10-21 18:58:00 -07:00
|
|
|
JsonValue::Num(5.),
|
2022-10-21 17:04:16 -07:00
|
|
|
]),
|
2022-10-21 18:58:00 -07:00
|
|
|
",{}"
|
2022-10-21 17:04:16 -07:00
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(json_value().parse("true"), Ok((JsonValue::Bool(true), "")));
|
2022-10-16 01:36:20 -07:00
|
|
|
}
|
2022-10-21 18:58:00 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_json() {
|
|
|
|
assert_eq!(
|
|
|
|
json_object().parse(r#"{ "a": 23}"#).unwrap().0,
|
|
|
|
JsonValue::Object(vec![("a".into(), JsonValue::Num(23.))])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
json_object().parse(r#"{}"#).unwrap().0,
|
|
|
|
JsonValue::Object(vec![])
|
|
|
|
);
|
|
|
|
}
|
2022-10-10 00:13:39 -07:00
|
|
|
}
|