parser: support constant string expressions and concats

This commit is contained in:
Ryan Chandler 2022-07-25 20:27:17 +01:00
parent be40cd61c4
commit 0cb97f3f66
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 20 additions and 0 deletions

View File

@ -231,6 +231,7 @@ pub enum Expression {
Closure(Vec<Param>, Option<Type>, Block),
ArrowFunction(Vec<Param>, Option<Type>, Box<Self>),
New(Box<Self>, Vec<Self>),
ConstantString(String),
}
#[derive(Debug, Clone, PartialEq, Serialize)]
@ -245,6 +246,7 @@ pub enum InfixOp {
Sub,
Div,
Mul,
Concat,
LessThan,
}
@ -256,6 +258,7 @@ impl From<TokenKind> for InfixOp {
TokenKind::Asterisk => Self::Mul,
TokenKind::Slash => Self::Div,
TokenKind::LessThan => Self::LessThan,
TokenKind::Dot => Self::Concat,
_ => unreachable!()
}
}

View File

@ -694,6 +694,7 @@ impl Parser {
TokenKind::Variable(v) => Expression::Variable(v.to_string()),
TokenKind::Int(i) => Expression::Int(*i),
TokenKind::Identifier(i) => Expression::Identifier(i.to_string()),
TokenKind::ConstantString(s) => Expression::ConstantString(s.to_string()),
TokenKind::LeftParen => {
self.next();
@ -913,6 +914,7 @@ fn infix_binding_power(t: &TokenKind) -> Option<(u8, u8)> {
Some(match t {
TokenKind::Asterisk | TokenKind::Slash => (13, 14),
TokenKind::Plus | TokenKind::Minus => (11, 12),
TokenKind::Dot => (11, 11),
TokenKind::LessThan => (9, 10),
TokenKind::Equals => (2, 1),
_ => return None,
@ -1054,6 +1056,21 @@ mod tests {
]);
}
#[test]
fn concat() {
assert_ast("<?php 'foo' . 'bar' . 'baz';", &[
expr!(Expression::Infix(
Box::new(Expression::ConstantString("foo".into())),
InfixOp::Concat,
Box::new(Expression::Infix(
Box::new(Expression::ConstantString("bar".into())),
InfixOp::Concat,
Box::new(Expression::ConstantString("baz".into())),
))
))
]);
}
#[test]
fn empty_fn() {
assert_ast("<?php function foo() {}", &[