Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: support empty array items
  • Loading branch information
ryangjchandler committed Sep 13, 2022
commit 61c04bb61655c8ed27a8617d6a551f6ab6b01b88
1 change: 1 addition & 0 deletions trunk_parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ pub struct Use {
#[derive(Debug, PartialEq, Clone)]
pub enum Expression {
Static,
Empty,
ErrorSuppress {
expr: Box<Self>,
},
Expand Down
30 changes: 30 additions & 0 deletions trunk_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,12 @@ 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)?;

Expand Down Expand Up @@ -3421,6 +3427,30 @@ 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();
Expand Down