parser: support braced variables

This commit is contained in:
Ryan Chandler 2022-09-16 10:20:38 +01:00
parent 9353c8fab1
commit 5cc76b4f3d
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 29 additions and 1 deletions

View File

@ -442,6 +442,9 @@ pub enum Expression {
Variable {
name: ByteString,
},
DynamicVariable {
name: Box<Self>,
},
Infix {
lhs: Box<Self>,
op: InfixOp,

View File

@ -1929,7 +1929,23 @@ impl Parser {
let rhs = self.expression(rpred)?;
prefix(&op, rhs)
}
},
TokenKind::Dollar => {
self.next();
match self.current.kind {
TokenKind::LeftBrace => {
self.next();
let name = self.expression(Precedence::Lowest)?;
self.rbrace()?;
Expression::DynamicVariable { name: Box::new(name) }
},
_ => todo!(),
}
},
_ => todo!(
"expr lhs: {:?}, line {} col {}",
self.current.kind,
@ -4200,6 +4216,15 @@ mod tests {
);
}
#[test]
fn braced_variables() {
assert_ast("<?php ${'foo'};", &[
expr!(Expression::DynamicVariable {
name: Box::new(Expression::ConstantString { value: "foo".into() })
})
]);
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();