From 12c7c14c393b1097947b8fe76238e12525caf17f Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Wed, 7 Dec 2022 00:28:30 +0000 Subject: [PATCH] parser: support bodyless else statements --- src/parser/mod.rs | 13 +++- tests/fixtures/0266/ast.txt | 41 +++++++++++++ tests/fixtures/0266/code.php | 7 +++ tests/fixtures/0266/tokens.txt | 106 +++++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/0266/ast.txt create mode 100644 tests/fixtures/0266/code.php create mode 100644 tests/fixtures/0266/tokens.txt diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 680fabe..5f9f733 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -825,11 +825,18 @@ impl Parser { self.skip(state, TokenKind::Else)?; - self.left_brace(state)?; + let r#else; + if state.current.kind == TokenKind::LeftBrace { + self.left_brace(state)?; - let r#else = self.block(state, &TokenKind::RightBrace)?; + r#else = self.block(state, &TokenKind::RightBrace)?; - self.right_brace(state)?; + self.right_brace(state)?; + } else { + r#else = vec![ + self.statement(state)? + ]; + } Statement::If { condition, diff --git a/tests/fixtures/0266/ast.txt b/tests/fixtures/0266/ast.txt new file mode 100644 index 0000000..c68b122 --- /dev/null +++ b/tests/fixtures/0266/ast.txt @@ -0,0 +1,41 @@ +[ + If { + condition: Variable( + Variable { + start: ( + 3, + 5, + ), + name: "foo", + end: ( + 3, + 9, + ), + }, + ), + then: [], + else_ifs: [], + else: Some( + [ + If { + condition: Variable( + Variable { + start: ( + 5, + 12, + ), + name: "bar", + end: ( + 5, + 16, + ), + }, + ), + then: [], + else_ifs: [], + else: None, + }, + ], + ), + }, +] diff --git a/tests/fixtures/0266/code.php b/tests/fixtures/0266/code.php new file mode 100644 index 0000000..6ad224a --- /dev/null +++ b/tests/fixtures/0266/code.php @@ -0,0 +1,7 @@ +