diff --git a/phpast/samples/return_types.php b/phpast/samples/return_types.php new file mode 100644 index 0000000..d1a32e3 --- /dev/null +++ b/phpast/samples/return_types.php @@ -0,0 +1,3 @@ +, body: Block, + return_type: Option, }, Class { name: Identifier, diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index aad7e1c..1642c9f 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -238,7 +238,13 @@ impl Parser { expect!(self, TokenKind::RightParen, "expected )"); - // TODO: Support return types here. + let mut return_type = None; + + if self.current.kind == TokenKind::Colon { + self.next(); + + return_type = Some(self.type_string()?); + } expect!(self, TokenKind::LeftBrace, "expected {"); @@ -250,7 +256,7 @@ impl Parser { expect!(self, TokenKind::RightBrace, "expected }"); - Statement::Function { name: name.into(), params, body } + Statement::Function { name: name.into(), params, body, return_type } }, TokenKind::Var => { self.next(); @@ -307,7 +313,7 @@ impl Parser { let s = self.statement()?; let statement = match s { - Statement::Function { name, params, body } => { + Statement::Function { name, params, body, .. } => { Statement::Method { name, params, body, flags: vec![] } }, Statement::Var { var } => { @@ -502,6 +508,7 @@ mod tests { name: $name.to_string().into(), params: $params.to_vec().into_iter().map(|p: &str| Param::from(p)).collect::>(), body: $body.to_vec(), + return_type: None, } }; } @@ -689,7 +696,8 @@ mod tests { r#type: Some(Type::Plain("string".into())) } ], - body: vec![] + body: vec![], + return_type: None, } ]); } @@ -705,7 +713,8 @@ mod tests { r#type: Some(Type::Nullable("string".into())) } ], - body: vec![] + body: vec![], + return_type: None, } ]); } @@ -724,8 +733,9 @@ mod tests { ])) } ], - body: vec![] - } + body: vec![], + return_type: None, + }, ]); } @@ -743,11 +753,23 @@ mod tests { ])) } ], - body: vec![] + body: vec![], + return_type: None, } ]); } + #[test] + fn function_return_types() { + assert_ast("