parser: add support for static arrow functions

This commit is contained in:
Ryan Chandler 2022-09-15 00:53:33 +01:00
parent cddeff2901
commit 05aff40752
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 20 additions and 1 deletions

View File

@ -460,6 +460,7 @@ pub enum Expression {
return_type: Option<Type>,
expr: Box<Self>,
by_ref: bool,
r#static: bool,
},
New {
target: Box<Self>,

View File

@ -1632,7 +1632,7 @@ impl Parser {
Expression::Array { items }
}
TokenKind::Static if matches!(self.peek.kind, TokenKind::Function) => {
TokenKind::Static if matches!(self.peek.kind, TokenKind::Function | TokenKind::Fn) => {
self.next();
match self.expression(Precedence::Lowest)? {
@ -1651,6 +1651,7 @@ impl Parser {
by_ref,
r#static: true,
},
Expression::ArrowFunction { params, return_type, expr, by_ref, .. } => Expression::ArrowFunction { params, return_type, expr, by_ref, r#static: true },
_ => unreachable!(),
}
}
@ -1771,6 +1772,7 @@ impl Parser {
return_type,
expr: Box::new(value),
by_ref,
r#static: false,
}
}
TokenKind::New => {
@ -3802,6 +3804,7 @@ mod tests {
return_type: None,
expr: Box::new(Expression::Null),
by_ref: false,
r#static: false,
})],
);
}
@ -3886,6 +3889,7 @@ mod tests {
return_type: None,
expr: Box::new(Expression::Null),
by_ref: false,
r#static: false,
})],
);
}
@ -3950,10 +3954,24 @@ mod tests {
expr: Box::new(Expression::Null),
return_type: None,
by_ref: true,
r#static: false,
})],
);
}
#[test]
fn static_arrow_functions() {
assert_ast("<?php static fn () => null;", &[
expr!(Expression::ArrowFunction {
params: vec![],
expr: Box::new(Expression::Null),
return_type: None,
by_ref: false,
r#static: true,
})
]);
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();