mirror of
https://github.com/danog/parser.git
synced 2024-11-30 04:29:13 +01:00
parser: support prefix expressions like !
This commit is contained in:
parent
08e7c99f25
commit
d955cd28da
3
phpast/samples/bang.php
Normal file
3
phpast/samples/bang.php
Normal file
@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
! $foo;
|
@ -273,6 +273,7 @@ pub enum Expression {
|
||||
Bool(bool),
|
||||
ArrayIndex(Box<Self>, Option<Box<Self>>),
|
||||
Null,
|
||||
BooleanNot(Box<Self>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
|
@ -912,6 +912,16 @@ impl Parser {
|
||||
|
||||
Expression::New(Box::new(target), args)
|
||||
},
|
||||
_ if is_prefix(&self.current.kind) => {
|
||||
let op = self.current.kind.clone();
|
||||
|
||||
self.next();
|
||||
|
||||
let rbp = prefix_binding_power(&op);
|
||||
let rhs = self.expression(rbp)?;
|
||||
|
||||
prefix(&op, rhs)
|
||||
},
|
||||
_ => todo!("expr lhs: {:?}", self.current.kind),
|
||||
};
|
||||
|
||||
@ -1077,6 +1087,27 @@ impl Parser {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_prefix(op: &TokenKind) -> bool {
|
||||
match op {
|
||||
TokenKind::Bang => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
fn prefix_binding_power(op: &TokenKind) -> u8 {
|
||||
match op {
|
||||
TokenKind::Bang => 99,
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
fn prefix(op: &TokenKind, rhs: Expression) -> Expression {
|
||||
match op {
|
||||
TokenKind::Bang => Expression::BooleanNot(Box::new(rhs)),
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
fn infix(lhs: Expression, op: TokenKind, rhs: Expression) -> Expression {
|
||||
if op == TokenKind::Equals {
|
||||
return Expression::Assign(Box::new(lhs), Box::new(rhs));
|
||||
|
Loading…
Reference in New Issue
Block a user