From 798eeee48852df499892fe96a42158ba1c4d70b0 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 21 Apr 2023 21:15:18 -0400 Subject: [PATCH] Add a test for sexp with quoted atom containing parenthesis. --- src/parser/sexp.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/parser/sexp.rs b/src/parser/sexp.rs index 25a866a..ea4a5a6 100644 --- a/src/parser/sexp.rs +++ b/src/parser/sexp.rs @@ -124,7 +124,7 @@ fn quoted_atom<'s>(input: &'s str) -> Res<&'s str, Token<'s>> { let (remaining, _) = tag(r#"""#)(input)?; let (remaining, _) = escaped( take_till1(|c| match c { - '\\' | '"' | ')' => true, + '\\' | '"' => true, _ => false, }), '\\', @@ -215,4 +215,40 @@ mod tests { r#""foo""# ) } + + #[test] + fn quoted_containing_paren() { + let input = r#" (foo "b(a)r" baz ) "#; + let (remaining, parsed) = sexp_with_padding(input).expect("Parse the input"); + assert_eq!(remaining, ""); + assert!(match parsed { + Token::List(_) => true, + _ => false, + }); + let children = match parsed { + Token::List(children) => children, + _ => panic!("Should be a list."), + }; + assert_eq!( + match children.first() { + Some(Token::Atom(body)) => *body, + _ => panic!("First child should be an atom."), + }, + r#"foo"# + ); + assert_eq!( + match children.iter().nth(1) { + Some(Token::Atom(body)) => *body, + _ => panic!("Second child should be an atom."), + }, + r#""b(a)r""# + ); + assert_eq!( + match children.iter().nth(2) { + Some(Token::Atom(body)) => *body, + _ => panic!("Third child should be an atom."), + }, + r#"baz"# + ); + } }