From a639641ed2432fc1c01f7487bea517ef4394514a Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 8 Aug 2024 13:11:32 -0400 Subject: [PATCH] Parse strings and string concatentation --- dust-lang/src/parse.rs | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/dust-lang/src/parse.rs b/dust-lang/src/parse.rs index 1d373d8..252c477 100644 --- a/dust-lang/src/parse.rs +++ b/dust-lang/src/parse.rs @@ -206,6 +206,11 @@ impl<'src> Parser<'src> { Ok(Node::new(Statement::Identifier(identifier), span)) } + (Token::String(string), span) => { + self.next_token()?; + + Ok(Node::new(Statement::Constant(Value::string(string)), span)) + } (Token::LeftParenthesis, left_span) => { self.next_token()?; @@ -379,6 +384,47 @@ mod tests { use super::*; + #[test] + fn string_concatenation() { + let input = "\"Hello, \" + \"World!\""; + + assert_eq!( + parse(input), + Ok(AbstractSyntaxTree { + nodes: [Node::new( + Statement::Add( + Box::new(Node::new( + Statement::Constant(Value::string("Hello, ")), + (0, 9) + )), + Box::new(Node::new( + Statement::Constant(Value::string("World!")), + (12, 20) + )) + ), + (0, 20) + )] + .into() + }) + ); + } + + #[test] + fn string() { + let input = "\"Hello, World!\""; + + assert_eq!( + parse(input), + Ok(AbstractSyntaxTree { + nodes: [Node::new( + Statement::Constant(Value::string("Hello, World!")), + (0, 15) + )] + .into() + }) + ); + } + #[test] fn boolean() { let input = "true";