#![feature(assert_matches)] #![allow(dead_code)] //TODO eventually turn this off mod bnf; mod parser; mod primitives; mod sequence; use parser::{ParseResult, Parser}; fn map(parser: P, map_fn: F) -> impl Parser where P: Parser, F: Fn(O1) -> O2, { move |input| { parser .parse(input) .map(|(result, rest)| (map_fn(result), rest)) } } fn pred(parser: P, pred_fn: F) -> impl Parser where P: Parser, F: Fn(&O) -> bool, { move |input| { parser.parse(input).and_then(|(result, rest)| { if pred_fn(&result) { Ok((result, rest)) } else { Err(rest) } }) } } fn zero_or_more(parser: P) -> impl Parser, I> where P: Parser, I: Copy, { move |mut input| { let mut results = Vec::new(); while let Ok((item, rest)) = parser.parse(input) { results.push(item); input = rest; } Ok((results, input)) } } fn one_or_more(parser: P) -> impl Parser, I> where P: Parser + 'static, I: Copy + 'static, O: 'static, { let parser = std::rc::Rc::new(parser); parser .clone() .then(zero_or_more(parser)) .map(|(first, rest)| { let mut output = vec![first]; output.extend(rest.into_iter()); output }) } fn any_char(input: &str) -> ParseResult<&str, char, &str> { match input.chars().next() { Some(ch) => Ok((ch, &input[ch.len_utf8()..])), None => Err(input), } } fn choice(parser1: P1, parser2: P2) -> impl Parser where P1: Parser, P2: Parser, I: Copy, { move |input| match parser1.parse(input) { ok @ Ok(..) => ok, Err(_e) => parser2.parse(input), } } #[cfg(test)] mod tests { use super::*; use crate::primitives::literal; use std::assert_matches::assert_matches; use std::collections::HashMap; #[test] fn test_parsing() { let output = literal("a")("a yolo"); assert_matches!(output.unwrap(), ("a", " yolo")); } #[test] fn test_map() { let lit_a = literal("a"); let output = lit_a.map(|s| s.to_uppercase()).parse("a yolo"); assert_matches!(output.unwrap(), (s, " yolo") if s == "A"); } #[test] fn test_one_or_more() { let p = one_or_more(literal("bongo ")); let input = "bongo bongo bongo bongo bongo "; assert_matches!(p.parse(input), Ok((v, "")) if v.len() == 5); let input = "bongo ecks"; assert_matches!(p.parse(input), Ok((v, "ecks")) if v.len() == 1); } #[test] fn test_choice() { let p = choice(literal("gnostika").to(1), one_or_more(literal(" ")).to(2)); assert_eq!(p.parse("gnostika twentynine"), Ok((1, " twentynine"))); } /* * JSON BNF * ::= ::= | | | | | ::= "[" [] {"," }* "]" ::= "{" [] {"," }* "}" ::= ":" */ #[derive(Debug, Clone)] 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_true = literal("true").to(JsonValue::Bool(true)); let json_false = literal("false").to(JsonValue::Bool(false)); let json_value = choice(json_null, choice(json_true, json_false)); assert_matches!(json_value.parse("true"), Ok((JsonValue::Bool(true), ""))); } }