mirror of
https://github.com/danog/parser.git
synced 2024-11-30 04:29:13 +01:00
parser: support basic new expressions
This commit is contained in:
parent
b0ce1bdd30
commit
570433de1c
7
phpast/samples/new.php
Normal file
7
phpast/samples/new.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
class Foo {}
|
||||
|
||||
$foo = new Foo;
|
||||
|
||||
$bar = new Foo(123);
|
@ -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)]
|
||||
|
@ -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),
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user