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.
This commit is contained in:
Ryan Chandler 2022-08-09 00:00:14 +01:00
parent 06cbbeff40
commit 9ab82059a1
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
4 changed files with 13 additions and 0 deletions

View File

@ -749,6 +749,7 @@ fn identifier_to_keyword(ident: &str) -> Option<TokenKind> {
"return" => TokenKind::Return,
"static" => TokenKind::Static,
"switch" => TokenKind::Switch,
"throw" => TokenKind::Throw,
"trait" => TokenKind::Trait,
"true" | "TRUE" => TokenKind::True,
"try" => TokenKind::Try,

View File

@ -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",

View File

@ -394,6 +394,9 @@ pub enum Expression {
condition: Box<Self>,
arms: Vec<MatchArm>,
},
Throw {
value: Box<Expression>,
},
}
#[derive(Debug, PartialEq, Clone, Serialize)]

View File

@ -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();