#![feature(assert_matches)] #![allow(dead_code)] //TODO eventually turn this off mod bnf; mod combinators; mod parser; mod primitives; mod sequence; use parser::{ParseResult, Parser}; 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::combinators::one_or_more; 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_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), ""))); } }