From a9d08a921368b19b01ec4b83b2385f0d76d50054 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 29 Jan 2024 17:28:01 -0800 Subject: [PATCH] noodling on sexp --- justfile | 4 ++-- src/primitives.rs | 1 + src/sequence.rs | 4 ++++ src/test/mod.rs | 5 +++++ src/test/sexp.rs | 9 +++++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/justfile b/justfile index a9606b0..3898ba2 100644 --- a/justfile +++ b/justfile @@ -2,5 +2,5 @@ _default: just --list -test: - cargo nextest run +test *args: + cargo nextest run {{args}} diff --git a/src/primitives.rs b/src/primitives.rs index a6f74d8..9011d8b 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -49,6 +49,7 @@ pub fn identifier(input: &str) -> ParseResult<&str, String, ()> { } pub fn whitespace(input: &str) -> ParseResult<&str, char, ()> { + println!("whitespace: `{input}`"); match input.chars().next() { Some(ch) if ch.is_whitespace() => Ok((ch, &input[1..])), _ => Err(((), input)), diff --git a/src/sequence.rs b/src/sequence.rs index 7fa4aac..791d756 100644 --- a/src/sequence.rs +++ b/src/sequence.rs @@ -12,9 +12,13 @@ pub fn surrounded_by( surrounding: impl Parser, ) -> impl Parser { move |input| { + println!("surrounded_by"); let (_result1, rest1) = surrounding.parse(input)?; + println!("A"); let (result2, rest2) = main.parse(rest1)?; + println!("B"); let (_result3, rest3) = surrounding.parse(rest2)?; + println!("C"); Ok((result2, rest3)) } } diff --git a/src/test/mod.rs b/src/test/mod.rs index 94e1aa1..e0cdbcc 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -67,4 +67,9 @@ fn test_repeated() { let output = parser.parse("bongo bongo bongo bongo"); let output = output.unwrap(); assert_eq!(output.0, vec!["bongo", "bongo", "bongo", "bongo"]); + + let bongos = repeated(literal("bongo")); + let output = bongos.parse("tra la la").unwrap(); + assert_eq!(output.0.len(), 0); + assert_eq!(output.1, "tra la la"); } diff --git a/src/test/sexp.rs b/src/test/sexp.rs index 25552d2..234a463 100644 --- a/src/test/sexp.rs +++ b/src/test/sexp.rs @@ -48,6 +48,7 @@ fn test_parse_atom() { } fn parse_expr(input: &str) -> ParseResult<&str, Expr, ()> { + println!("parse_expr: {input}"); choice((parse_list, parse_atom.map(Expr::Atom))).parse(input) } @@ -60,9 +61,17 @@ fn parse_list(input: &str) -> ParseResult<&str, Expr, ()> { ) .then_ignore(literal_char(')')) .map(Expr::List) + .surrounded_by(repeated(whitespace)) .parse(input) } +#[test] +fn test_parse_list() { + let output = parse_list.parse("\n(1 2 (1 2) 9999 3)").unwrap(); + assert_eq!(output.1, ""); + +} + #[test] fn test_parse_sexp() { let output = parse_expr("(add 1 2)").unwrap();