From 9ab82059a14deba8021390840ca81176a7e67944 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Tue, 9 Aug 2022 00:00:14 +0100 Subject: [PATCH] parser: support `throw` expressions It's worth noting that a `throw` will always be an expression in the Trunk parser. PHP-Parser will convert standalone `throw` expressions into a dedicated `throw` statement for backwards-compatibility reasons. --- trunk_lexer/src/lexer.rs | 1 + trunk_lexer/src/token.rs | 2 ++ trunk_parser/src/ast.rs | 3 +++ trunk_parser/src/parser/mod.rs | 7 +++++++ 4 files changed, 13 insertions(+) diff --git a/trunk_lexer/src/lexer.rs b/trunk_lexer/src/lexer.rs index b475652..9340adb 100644 --- a/trunk_lexer/src/lexer.rs +++ b/trunk_lexer/src/lexer.rs @@ -749,6 +749,7 @@ fn identifier_to_keyword(ident: &str) -> Option { "return" => TokenKind::Return, "static" => TokenKind::Static, "switch" => TokenKind::Switch, + "throw" => TokenKind::Throw, "trait" => TokenKind::Trait, "true" | "TRUE" => TokenKind::True, "try" => TokenKind::Try, diff --git a/trunk_lexer/src/token.rs b/trunk_lexer/src/token.rs index d7b5f1c..ce01b83 100644 --- a/trunk_lexer/src/token.rs +++ b/trunk_lexer/src/token.rs @@ -125,6 +125,7 @@ pub enum TokenKind { Slash, Static, Switch, + Throw, Trait, TripleEquals, True, @@ -261,6 +262,7 @@ impl Display for TokenKind { Self::Slash => "/", Self::Static => "static", Self::Switch => "switch", + Self::Throw => "throw", Self::Trait => "trait", Self::TripleEquals => "===", Self::True => "true", diff --git a/trunk_parser/src/ast.rs b/trunk_parser/src/ast.rs index 9292bc3..e2d2150 100644 --- a/trunk_parser/src/ast.rs +++ b/trunk_parser/src/ast.rs @@ -394,6 +394,9 @@ pub enum Expression { condition: Box, arms: Vec, }, + Throw { + value: Box, + }, } #[derive(Debug, PartialEq, Clone, Serialize)] diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index 765e745..357ed15 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -959,6 +959,13 @@ impl Parser { self.skip_comments(); let mut lhs = match &self.current.kind { + TokenKind::Throw => { + self.next(); + + let value = self.expression(0)?; + + Expression::Throw { value: Box::new(value) } + }, TokenKind::Clone => { self.next();