parser: support arrow functions returning by ref

This commit is contained in:
Ryan Chandler 2022-09-15 00:47:51 +01:00
parent af11ff39c8
commit 4338b83254
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 25 additions and 2 deletions

View File

@ -459,6 +459,7 @@ pub enum Expression {
params: Vec<Param>,
return_type: Option<Type>,
expr: Box<Self>,
by_ref: bool,
},
New {
target: Box<Self>,

View File

@ -1730,6 +1730,13 @@ impl Parser {
TokenKind::Fn => {
self.next();
let by_ref = if self.current.kind == TokenKind::Ampersand {
self.next();
true
} else {
false
};
self.lparen()?;
let params = self.param_list()?;
@ -1752,6 +1759,7 @@ impl Parser {
params,
return_type,
expr: Box::new(value),
by_ref,
}
}
TokenKind::New => {
@ -3781,7 +3789,8 @@ mod tests {
&[expr!(Expression::ArrowFunction {
params: vec![],
return_type: None,
expr: Box::new(Expression::Null)
expr: Box::new(Expression::Null),
by_ref: false,
})],
);
}
@ -3864,7 +3873,8 @@ mod tests {
by_ref: true,
}],
return_type: None,
expr: Box::new(Expression::Null)
expr: Box::new(Expression::Null),
by_ref: false,
})],
);
}
@ -3915,6 +3925,18 @@ mod tests {
]);
}
#[test]
fn arrow_functions_returning_by_ref() {
assert_ast("<?php fn &() => null;", &[
expr!(Expression::ArrowFunction {
params: vec![],
expr: Box::new(Expression::Null),
return_type: None,
by_ref: true,
})
]);
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();