rust-parser-combinator/src/lib.rs

154 lines
4.6 KiB
Rust
Raw Normal View History

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-16 19:21:43 -07:00
pub use parser::{ParseResult, Parser};
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-16 01:42:03 -07:00
use std::collections::HashMap;
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>),
Object(HashMap<String, JsonValue>),
}
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-20 18:10:13 -07:00
fn json_string() -> impl JsonParser<'static, JsonValue> {
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-20 18:10:13 -07:00
.map(|(_, s, _)| JsonValue::Str(s.iter().cloned().collect::<String>()))
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-20 23:37:46 -07:00
let val = json_value().delimited(whitespace(), whitespace());
2022-10-20 17:55:46 -07:00
2022-10-20 18:10:13 -07:00
repeated(val)
2022-10-20 23:37:46 -07:00
.separated_by(literal(","), false)
2022-10-20 18:10:13 -07:00
.delimited(literal_char('['), literal_char(']'))
2022-10-20 17:55:46 -07:00
.map(JsonValue::Array)
}
2022-10-19 22:35:13 -07:00
fn json_value() -> impl JsonParser<'static, JsonValue> {
choice((json_null(), json_bool(), json_number(), json_string()))
}
#[test]
fn parse_json() {
2022-10-20 18:10:13 -07:00
assert_eq!(
json_string().parse(r#""yolo swagg""#).unwrap(),
(JsonValue::Str("yolo swagg".into()), "")
);
2022-10-20 23:37:46 -07:00
assert!(json_array().parse(r#"[ 4, 9, "ara",]"#).is_err());
2022-10-20 18:10:13 -07:00
assert_eq!(
2022-10-20 23:37:46 -07:00
json_array().parse(r#"[ 4, 9, "foo" ]"#).unwrap(),
2022-10-20 18:10:13 -07:00
(
JsonValue::Array(vec![
JsonValue::Num(4.),
JsonValue::Num(9.0),
JsonValue::Str("foo".to_string())
]),
""
)
);
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-17 00:47:19 -07:00
2022-10-19 22:35:13 -07:00
assert_eq!(json_value().parse("true"), Ok((JsonValue::Bool(true), "")));
2022-10-16 01:36:20 -07:00
}
2022-10-10 00:13:39 -07:00
}