parser: support long ternary expressions

This commit is contained in:
Ryan Chandler 2022-07-28 19:45:00 +01:00
parent ed12a5c5f3
commit 81199e5d5d
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 12 additions and 0 deletions

View File

@ -282,6 +282,7 @@ pub enum Expression {
Null,
BooleanNot(Box<Self>),
MagicConst(MagicConst),
Ternary(Box<Self>, Box<Self>, Box<Self>)
}
#[derive(Debug, Eq, PartialEq, Clone, Serialize)]

View File

@ -1059,6 +1059,16 @@ impl Parser {
Expression::ArrayIndex(Box::new(lhs), Some(Box::new(index)))
}
},
TokenKind::Question => {
// TODO: Handle short-hand ternaries here too.
let then = self.expression(0)?;
expect!(self, TokenKind::Colon, "expected :");
let otherwise = self.expression(0)?;
Expression::Ternary(Box::new(lhs), Box::new(then), Box::new(otherwise))
},
TokenKind::DoubleColon => {
match self.current.kind.clone() {
TokenKind::Variable(_) => {
@ -1206,6 +1216,7 @@ fn infix_binding_power(t: &TokenKind) -> Option<(u8, u8)> {
fn postfix_binding_power(t: &TokenKind) -> Option<u8> {
Some(match t {
TokenKind::Question => 20,
TokenKind::LeftParen | TokenKind::LeftBracket => 19,
TokenKind::Arrow | TokenKind::DoubleColon => 18,
_ => return None