added parse test

This commit is contained in:
Erin 2022-07-26 20:03:58 +02:00 committed by ondra05
parent 8518098e89
commit ec4c8d168a
2 changed files with 35 additions and 6 deletions

View file

@ -18,7 +18,7 @@ pub enum Token<'a> {
Quote, Quote,
#[token("#")] #[token("#")]
Octothrope, Octothorpe,
// Values // Values
#[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))] #[regex("\"(\\.|[^\"])*\"", (lex_slice::<1, 1>))]
@ -56,7 +56,7 @@ impl<'a> Display for Token<'a> {
Token::RightParen => write!(f, ")"), Token::RightParen => write!(f, ")"),
Token::Dot => write!(f, "."), Token::Dot => write!(f, "."),
Token::Quote => write!(f, "'"), Token::Quote => write!(f, "'"),
Token::Octothrope => write!(f, "#"), Token::Octothorpe => write!(f, "#"),
Token::String(s) => write!(f, "\"{s}\""), Token::String(s) => write!(f, "\"{s}\""),
Token::Number(n) => write!(f, "{n}"), Token::Number(n) => write!(f, "{n}"),
Token::Symbol(sym) => write!(f, "{sym}"), Token::Symbol(sym) => write!(f, "{sym}"),
@ -82,7 +82,7 @@ mod tests {
LeftParen, LeftParen,
RightParen, RightParen,
Quote, Quote,
Octothrope, Octothorpe,
String("ABLE"), String("ABLE"),
Symbol("corp"), Symbol("corp"),
Keyword("wisp"), Keyword("wisp"),

View file

@ -26,7 +26,7 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Spanned<Expr<'a>>>, Error = Simple
.repeated() .repeated()
.delimited_by(just(Token::LeftParen), just(Token::RightParen)); .delimited_by(just(Token::LeftParen), just(Token::RightParen));
let vector = just(Token::Octothrope) let vector = just(Token::Octothorpe)
.ignore_then(list.clone()) .ignore_then(list.clone())
.map(Expr::Vector); .map(Expr::Vector);
@ -60,7 +60,36 @@ fn parser<'a>() -> impl Parser<Token<'a>, Vec<Spanned<Expr<'a>>>, Error = Simple
mod tests { mod tests {
use super::*; use super::*;
fn assert_parse<'a>(src: &'a str, expected: &'a [Spanned<Expr<'a>>]) { #[test]
assert_eq!(parse(src).unwrap(), expected); 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,
)]
);
} }
} }