#![allow(dead_code)] //TODO eventually turn this off mod bnf; mod choice; mod combinators; mod parser; mod primitives; mod sequence; pub use parser::{ParseResult, Parser}; #[cfg(test)] mod tests { use super::*; use crate::choice::choice; use crate::primitives::{any_char, literal, literal_char, pred}; use crate::sequence::seq; use std::collections::HashMap; #[test] fn test_parsing() { let output = literal("a")("a yolo"); assert_eq!(output.unwrap(), ("a", " yolo")); } /* * JSON BNF * ::= ::= | | | | | ::= "[" [] {"," }* "]" ::= "{" [] {"," }* "}" ::= ":" */ #[derive(Debug, Clone, PartialEq)] enum JsonValue { Null, Bool(bool), Str(String), Num(f64), Array(Vec), Object(HashMap), } #[test] fn parse_json() { let json_null = literal("null").to(JsonValue::Null); let json_bool = choice(( literal("true").to(JsonValue::Bool(true)), literal("false").to(JsonValue::Bool(false)), )); let json_string = seq(( literal_char('"'), pred(any_char, |ch| *ch != '"'), literal_char('"'), )) .map(|(_, s, _)| JsonValue::Str(s.to_string())); let json_value = choice((json_null, json_bool, json_string)); assert_eq!(json_value.parse("true"), Ok((JsonValue::Bool(true), ""))); } }