From 0e23b9fa82d95ca365523afac554a4fb6d461a23 Mon Sep 17 00:00:00 2001 From: John Turner Date: Tue, 21 Oct 2025 20:48:00 -0400 Subject: impl some basic parsers and combinators --- tests/sexpr.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/sexpr.rs (limited to 'tests') diff --git a/tests/sexpr.rs b/tests/sexpr.rs new file mode 100644 index 0000000..651d534 --- /dev/null +++ b/tests/sexpr.rs @@ -0,0 +1,66 @@ +#![allow(dead_code)] + +use mon::{ + alpha1, alphanumeric, alphanumeric1, input::InputIter, numeric1, tag, whitespace, Parser, + ParserResult, +}; + +#[derive(Debug)] +enum Sexpr { + List(Vec), + Atom(String), + String(String), + Int(i64), +} + +fn atom<'a>() -> impl Parser<&'a str, Output = Sexpr> { + alpha1() + .and(alphanumeric()) + .recognize() + .map(|output: &str| Sexpr::Atom(output.to_string())) +} + +fn string<'a>() -> impl Parser<&'a str, Output = Sexpr> { + alphanumeric1() + .delimited_by(tag("\""), tag("\"")) + .map(|output: &str| Sexpr::String(output.to_string())) +} + +fn int<'a>() -> impl Parser<&'a str, Output = Sexpr> { + numeric1().map(|output: &str| Sexpr::Int(output.parse().unwrap())) +} + +fn sexpr<'a>(it: InputIter<&'a str>) -> ParserResult<&'a str, Sexpr> { + sexpr + .separated_list(whitespace()) + .delimited_by(tag("("), tag(")")) + .map(|output| Sexpr::List(output)) + .or(atom()) + .or(string()) + .or(int()) + .parse(it) +} + +#[test] +fn test_atom() { + let input = "atom"; + let it = InputIter::new(input); + + atom().check_finished(it).unwrap(); +} + +#[test] +fn test_string() { + let input = r#""string""#; + let it = InputIter::new(input); + + string().check_finished(it).unwrap() +} + +#[test] +fn test_sexpr() { + let input = r#"(let ((a "hello") (b 2)))"#; + let it = InputIter::new(input); + + dbg!(sexpr(it).unwrap()); +} -- cgit v1.2.3