parser: reorganise poor expression handling

This commit is contained in:
Ryan Chandler 2022-07-28 15:52:45 +01:00
parent 30afcce2d5
commit a6fc1b58f1
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
3 changed files with 45 additions and 12 deletions

View File

@ -1,4 +1,3 @@
<?php
$foo['foo'] = 'bar';
$foo['foo']['bar'] = 'baz';
$foo['foo'];

View File

@ -709,20 +709,42 @@ impl Parser {
}
let mut lhs = match &self.current.kind {
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::True => Expression::Bool(true),
TokenKind::False => Expression::Bool(false),
TokenKind::Variable(v) => {
let e = Expression::Variable(v.to_string());
self.next();
e
},
TokenKind::Int(i) => {
let e = Expression::Int(*i);
self.next();
e
},
TokenKind::Identifier(i) => {
let e = Expression::Identifier(i.to_string());
self.next();
e
},
TokenKind::ConstantString(s) => {
let e = Expression::ConstantString(s.to_string());
self.next();
e
},
TokenKind::True => {
let e = Expression::Bool(true);
self.next();
e
},
TokenKind::False => {
let e = Expression::Bool(false);
self.next();
e
},
TokenKind::LeftParen => {
self.next();
let e = self.expression(0)?;
if self.current.kind != TokenKind::RightParen {
return Err(ParseError::ExpectedToken("expected right paren".into(), self.current.span));
}
self.rparen()?;
e
},
@ -748,6 +770,8 @@ impl Parser {
}
}
self.rbracket()?;
Expression::Array(items)
},
TokenKind::Function => {
@ -901,7 +925,7 @@ impl Parser {
return Ok(lhs);
}
self.next();
// self.next();
loop {
let kind = match &self.current {

View File

@ -29,4 +29,14 @@ impl Parser {
expect!(self, TokenKind::RightParen, (), "expected )");
Ok(())
}
pub(crate) fn lbracket(&mut self) -> ParseResult<()> {
expect!(self, TokenKind::LeftBracket, (), "expected [");
Ok(())
}
pub(crate) fn rbracket(&mut self) -> ParseResult<()> {
expect!(self, TokenKind::RightBracket, (), "expected ]");
Ok(())
}
}