parser: support types on class var statements

This commit is contained in:
Ryan Chandler 2022-07-24 19:41:05 +01:00
parent a1a00c6f8f
commit 2948a2e0af
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
3 changed files with 17 additions and 3 deletions

View File

@ -0,0 +1,6 @@
<?php
class Foo {
var $bar;
var string $baz;
}

View File

@ -72,9 +72,11 @@ pub enum Statement {
InlineHtml(String),
Var {
var: String,
r#type: Option<Type>,
},
Property {
var: String,
r#type: Option<Type>,
},
Function {
name: Identifier,

View File

@ -261,11 +261,17 @@ impl Parser {
TokenKind::Var => {
self.next();
let mut var_type = None;
if ! matches!(self.current.kind, TokenKind::Variable(_)) {
var_type = Some(self.type_string()?);
}
let var = expect!(self, TokenKind::Variable(i), i, "expected variable name");
expect!(self, TokenKind::SemiColon, "expected semi-colon");
Statement::Var { var }
Statement::Var { var, r#type: var_type }
},
TokenKind::SemiColon => {
self.next();
@ -316,8 +322,8 @@ impl Parser {
Statement::Function { name, params, body, .. } => {
Statement::Method { name, params, body, flags: vec![] }
},
Statement::Var { var } => {
Statement::Property { var }
Statement::Var { var, r#type } => {
Statement::Property { var, r#type }
},
Statement::Method { .. } | Statement::Comment { .. } => s,
_ => {