diff options
| author | John Turner <jturner.usa@gmail.com> | 2025-10-21 20:48:00 -0400 |
|---|---|---|
| committer | John Turner <jturner.usa@gmail.com> | 2025-10-21 20:48:21 -0400 |
| commit | 0e23b9fa82d95ca365523afac554a4fb6d461a23 (patch) | |
| tree | 296fc7fc3e8208198b7e87adf4a016a9ea93aedf /tests | |
| parent | 876855e826bf3c7bd47b822fb41fd91ab46ad7e5 (diff) | |
| download | mon-0e23b9fa82d95ca365523afac554a4fb6d461a23.tar.gz | |
impl some basic parsers and combinators
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/sexpr.rs | 66 |
1 files changed, 66 insertions, 0 deletions
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<Sexpr>), + 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()); +} |
