From c5e4632c15e54ab1f4e6c2690998480f0d9bb2d6 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Tue, 13 Sep 2022 00:46:37 +0100 Subject: [PATCH 1/2] parser: support global statements --- trunk_lexer/src/lexer.rs | 1 + trunk_lexer/src/token.rs | 2 ++ trunk_parser/src/ast.rs | 3 +++ trunk_parser/src/parser/mod.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/trunk_lexer/src/lexer.rs b/trunk_lexer/src/lexer.rs index 11887f3..82fdcd6 100644 --- a/trunk_lexer/src/lexer.rs +++ b/trunk_lexer/src/lexer.rs @@ -828,6 +828,7 @@ impl Lexer { #[allow(dead_code)] fn identifier_to_keyword(ident: &str) -> Option { Some(match ident { + "global" => TokenKind::Global, "match" => TokenKind::Match, "abstract" => TokenKind::Abstract, "array" => TokenKind::Array, diff --git a/trunk_lexer/src/token.rs b/trunk_lexer/src/token.rs index 99da139..58f6807 100644 --- a/trunk_lexer/src/token.rs +++ b/trunk_lexer/src/token.rs @@ -9,6 +9,7 @@ pub enum OpenTagKind { #[derive(Debug, PartialEq, Clone)] pub enum TokenKind { + Global, Abstract, Ampersand, And, @@ -300,6 +301,7 @@ impl Display for TokenKind { Self::Variable(var) => &var[..], Self::Yield => "yield", Self::While => "while", + Self::Global => "global", _ => todo!("format token: {:?}", self), } ) diff --git a/trunk_parser/src/ast.rs b/trunk_parser/src/ast.rs index f58acd6..9a07530 100644 --- a/trunk_parser/src/ast.rs +++ b/trunk_parser/src/ast.rs @@ -297,6 +297,9 @@ pub enum Statement { Block { body: Block, }, + Global { + vars: Vec, + }, Noop, } diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index d2133a9..45369cd 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -169,6 +169,19 @@ impl Parser { self.skip_comments(); let statement = match &self.current.kind { + TokenKind::Global => { + self.next(); + + let mut vars = vec![]; + while self.current.kind != TokenKind::SemiColon { + vars.push(self.var()?.into()); + + self.optional_comma()?; + } + + self.semi()?; + Statement::Global { vars } + }, TokenKind::Static if matches!(self.peek.kind, TokenKind::Variable(_)) => { self.next(); @@ -3305,6 +3318,20 @@ mod tests { ); } + #[test] + fn global_statements() { + assert_ast(" Date: Tue, 13 Sep 2022 00:46:58 +0100 Subject: [PATCH 2/2] chore: format --- trunk_parser/src/parser/mod.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index 45369cd..7e65477 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -181,7 +181,7 @@ impl Parser { self.semi()?; Statement::Global { vars } - }, + } TokenKind::Static if matches!(self.peek.kind, TokenKind::Variable(_)) => { self.next(); @@ -3320,16 +3320,22 @@ mod tests { #[test] fn global_statements() { - assert_ast("