From ec4c8d168ade0e52e8afd33a05ae8326343fc4be Mon Sep 17 00:00:00 2001 From: Erin Date: Tue, 26 Jul 2022 20:03:58 +0200 Subject: [PATCH] added parse test --- src/syntax/lexer.rs | 6 +++--- src/syntax/parser.rs | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/syntax/lexer.rs b/src/syntax/lexer.rs index e84617b..aeb1392 100644 --- a/src/syntax/lexer.rs +++ b/src/syntax/lexer.rs @@ -18,7 +18,7 @@ pub enum Token<'a> { Quote, #[token("#")] - Octothrope, + Octothorpe, // Values #[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))] @@ -56,7 +56,7 @@ impl<'a> Display for Token<'a> { Token::RightParen => write!(f, ")"), Token::Dot => write!(f, "."), Token::Quote => write!(f, "'"), - Token::Octothrope => write!(f, "#"), + Token::Octothorpe => write!(f, "#"), Token::String(s) => write!(f, "\"{s}\""), Token::Number(n) => write!(f, "{n}"), Token::Symbol(sym) => write!(f, "{sym}"), @@ -82,7 +82,7 @@ mod tests { LeftParen, RightParen, Quote, - Octothrope, + Octothorpe, String("ABLE"), Symbol("corp"), Keyword("wisp"), diff --git a/src/syntax/parser.rs b/src/syntax/parser.rs index c08cd5e..bc16f55 100644 --- a/src/syntax/parser.rs +++ b/src/syntax/parser.rs @@ -26,7 +26,7 @@ fn parser<'a>() -> impl Parser, Vec>>, Error = Simple .repeated() .delimited_by(just(Token::LeftParen), just(Token::RightParen)); - let vector = just(Token::Octothrope) + let vector = just(Token::Octothorpe) .ignore_then(list.clone()) .map(Expr::Vector); @@ -60,7 +60,36 @@ fn parser<'a>() -> impl Parser, Vec>>, Error = Simple mod tests { use super::*; - fn assert_parse<'a>(src: &'a str, expected: &'a [Spanned>]) { - assert_eq!(parse(src).unwrap(), expected); + #[test] + fn function() { + assert_eq!( + parse(r#"(defun hello (name) (log (concat "Hello, " name "!")))"#).unwrap(), + &[Spanned::new( + Expr::List(vec![ + Spanned::new(Expr::Symbol("defun".into()), 1..6), + Spanned::new(Expr::Symbol("hello".into()), 7..12), + Spanned::new( + Expr::List(vec![Spanned::new(Expr::Symbol("name".into()), 14..18),]), + 13..19, + ), + Spanned::new( + Expr::List(vec![ + Spanned::new(Expr::Symbol("log".into()), 21..24), + Spanned::new( + Expr::List(vec![ + Spanned::new(Expr::Symbol("concat".into()), 26..32), + Spanned::new(Expr::String("Hello, ".into()), 33..42), + Spanned::new(Expr::Symbol("name".into()), 43..47), + Spanned::new(Expr::String("!".into()), 48..51), + ]), + 48..51 + ) + ]), + 20..53 + ) + ]), + 0..54, + )] + ); } }