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), InlineHtml(String),
Var { Var {
var: String, var: String,
r#type: Option<Type>,
}, },
Property { Property {
var: String, var: String,
r#type: Option<Type>,
}, },
Function { Function {
name: Identifier, name: Identifier,

View File

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