parser: support basic new expressions

This commit is contained in:
Ryan Chandler 2022-07-25 19:15:09 +01:00
parent b0ce1bdd30
commit 570433de1c
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
3 changed files with 34 additions and 1 deletions

7
phpast/samples/new.php Normal file
View File

@ -0,0 +1,7 @@
<?php
class Foo {}
$foo = new Foo;
$bar = new Foo(123);

View File

@ -229,7 +229,8 @@ pub enum Expression {
Assign(Box<Self>, Box<Self>),
Array(Vec<ArrayItem>),
Closure(Vec<Param>, Option<Type>, Block),
ArrowFunction(Vec<Param>, Option<Type>, Box<Self>)
ArrowFunction(Vec<Param>, Option<Type>, Box<Self>),
New(Box<Self>, Vec<Self>),
}
#[derive(Debug, Clone, PartialEq, Serialize)]

View File

@ -783,6 +783,31 @@ impl Parser {
Expression::ArrowFunction(params, return_type, Box::new(value))
},
TokenKind::New => {
self.next();
// TODO: Support dynamic instantiation targets here.
let target = self.expression(20)?;
let mut args = vec![];
if self.current.kind == TokenKind::LeftParen {
expect!(self, TokenKind::LeftParen, "expected (");
while self.current.kind != TokenKind::RightParen {
let value = self.expression(0)?;
args.push(value);
if self.current.kind == TokenKind::Comma {
self.next();
}
}
expect!(self, TokenKind::RightParen, "expected )");
}
Expression::New(Box::new(target), args)
},
_ => todo!("expr lhs: {:?}", self.current.kind),
};