From 9353c8fab1bab18e22ddcdfac9959304d885efd0 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 16 Sep 2022 10:09:29 +0100 Subject: [PATCH 1/3] lexer: produce single dollar tokens --- trunk_lexer/src/lexer.rs | 8 ++++++-- trunk_lexer/src/token.rs | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/trunk_lexer/src/lexer.rs b/trunk_lexer/src/lexer.rs index 5bba1c4..23f4b95 100644 --- a/trunk_lexer/src/lexer.rs +++ b/trunk_lexer/src/lexer.rs @@ -663,7 +663,11 @@ impl Lexer { } } - TokenKind::Variable(buffer.into()) + if buffer.is_empty() { + TokenKind::Dollar + } else { + TokenKind::Variable(buffer.into()) + } } fn tokenize_number( @@ -1066,7 +1070,7 @@ function"#, #[test] fn sigils() { - assert_tokens("", &[open!(), TokenKind::Arrow]); + assert_tokens(" $", &[open!(), TokenKind::Arrow, TokenKind::Dollar]); } #[test] diff --git a/trunk_lexer/src/token.rs b/trunk_lexer/src/token.rs index 3fc7b0c..47ca78c 100644 --- a/trunk_lexer/src/token.rs +++ b/trunk_lexer/src/token.rs @@ -11,6 +11,7 @@ pub enum OpenTagKind { #[derive(Debug, PartialEq, Clone)] pub enum TokenKind { + Dollar, HaltCompiler, Readonly, Global, @@ -174,6 +175,7 @@ impl Default for Token { impl Display for TokenKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let s = match self { + Self::Dollar => "$", Self::HaltCompiler => "__halt_compiler", Self::Readonly => "readonly", Self::AsteriskEqual => "*=", From 5cc76b4f3d8f5ff46a9cf38ea3fec48d1d498f9a Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 16 Sep 2022 10:20:38 +0100 Subject: [PATCH 2/3] parser: support braced variables --- trunk_parser/src/ast.rs | 3 +++ trunk_parser/src/parser/mod.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/trunk_parser/src/ast.rs b/trunk_parser/src/ast.rs index ab3506a..3e18ca5 100644 --- a/trunk_parser/src/ast.rs +++ b/trunk_parser/src/ast.rs @@ -442,6 +442,9 @@ pub enum Expression { Variable { name: ByteString, }, + DynamicVariable { + name: Box, + }, Infix { lhs: Box, op: InfixOp, diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index 016a086..14c1bf2 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -1929,7 +1929,23 @@ impl Parser { let rhs = self.expression(rpred)?; prefix(&op, rhs) - } + }, + TokenKind::Dollar => { + self.next(); + + match self.current.kind { + TokenKind::LeftBrace => { + self.next(); + + let name = self.expression(Precedence::Lowest)?; + + self.rbrace()?; + + Expression::DynamicVariable { name: Box::new(name) } + }, + _ => todo!(), + } + }, _ => todo!( "expr lhs: {:?}, line {} col {}", self.current.kind, @@ -4200,6 +4216,15 @@ mod tests { ); } + #[test] + fn braced_variables() { + assert_ast(" Date: Fri, 16 Sep 2022 10:21:37 +0100 Subject: [PATCH 3/3] parser: add test case for call inside braced var --- trunk_lexer/src/lexer.rs | 5 ++++- trunk_parser/src/parser/mod.rs | 33 ++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/trunk_lexer/src/lexer.rs b/trunk_lexer/src/lexer.rs index 23f4b95..069a0c2 100644 --- a/trunk_lexer/src/lexer.rs +++ b/trunk_lexer/src/lexer.rs @@ -1070,7 +1070,10 @@ function"#, #[test] fn sigils() { - assert_tokens(" $", &[open!(), TokenKind::Arrow, TokenKind::Dollar]); + assert_tokens( + " $", + &[open!(), TokenKind::Arrow, TokenKind::Dollar], + ); } #[test] diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index 14c1bf2..5200fa1 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -1929,7 +1929,7 @@ impl Parser { let rhs = self.expression(rpred)?; prefix(&op, rhs) - }, + } TokenKind::Dollar => { self.next(); @@ -1941,11 +1941,13 @@ impl Parser { self.rbrace()?; - Expression::DynamicVariable { name: Box::new(name) } - }, + Expression::DynamicVariable { + name: Box::new(name), + } + } _ => todo!(), } - }, + } _ => todo!( "expr lhs: {:?}, line {} col {}", self.current.kind, @@ -4218,11 +4220,24 @@ mod tests { #[test] fn braced_variables() { - assert_ast("