Merge pull request #61 from ryangjchandler/fix/empty-array-items

This commit is contained in:
Ryan Chandler 2022-09-13 11:42:57 +01:00 committed by GitHub
commit d70b2685ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View File

@ -388,6 +388,7 @@ pub struct Use {
#[derive(Debug, PartialEq, Clone)]
pub enum Expression {
Static,
Empty,
ErrorSuppress {
expr: Box<Self>,
},

View File

@ -1505,6 +1505,15 @@ impl Parser {
self.skip_comments();
while self.current.kind != TokenKind::RightBracket {
if self.current.kind == TokenKind::Comma {
items.push(ArrayItem {
key: None,
value: Expression::Empty,
});
self.next();
continue;
}
let mut key = None;
let mut value = self.expression(0)?;
@ -3421,6 +3430,33 @@ mod tests {
);
}
#[test]
fn array_empty_items() {
assert_ast(
"<?php [1, 2, , 4];",
&[expr!(Expression::Array {
items: vec![
ArrayItem {
key: None,
value: Expression::Int { i: 1 },
},
ArrayItem {
key: None,
value: Expression::Int { i: 2 },
},
ArrayItem {
key: None,
value: Expression::Empty,
},
ArrayItem {
key: None,
value: Expression::Int { i: 4 },
},
]
})],
)
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();