parser: support bodyless else statements

This commit is contained in:
Ryan Chandler 2022-12-07 00:28:30 +00:00
parent 385ff84e18
commit 12c7c14c39
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
4 changed files with 164 additions and 3 deletions

View File

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

41
tests/fixtures/0266/ast.txt vendored Normal file
View File

@ -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,
},
],
),
},
]

7
tests/fixtures/0266/code.php vendored Normal file
View File

@ -0,0 +1,7 @@
<?php
if ($foo) {
} else if ($bar) {
}

106
tests/fixtures/0266/tokens.txt vendored Normal file
View File

@ -0,0 +1,106 @@
[
Token {
kind: OpenTag(
Full,
),
span: (
1,
1,
),
},
Token {
kind: If,
span: (
3,
1,
),
},
Token {
kind: LeftParen,
span: (
3,
4,
),
},
Token {
kind: Variable(
"foo",
),
span: (
3,
5,
),
},
Token {
kind: RightParen,
span: (
3,
9,
),
},
Token {
kind: LeftBrace,
span: (
3,
11,
),
},
Token {
kind: RightBrace,
span: (
5,
1,
),
},
Token {
kind: Else,
span: (
5,
3,
),
},
Token {
kind: If,
span: (
5,
8,
),
},
Token {
kind: LeftParen,
span: (
5,
11,
),
},
Token {
kind: Variable(
"bar",
),
span: (
5,
12,
),
},
Token {
kind: RightParen,
span: (
5,
16,
),
},
Token {
kind: LeftBrace,
span: (
5,
18,
),
},
Token {
kind: RightBrace,
span: (
7,
1,
),
},
]