parser: support left and right shift op

This commit is contained in:
Ryan Chandler 2022-09-16 15:09:10 +01:00
parent fb8ebd2484
commit 274fc7c0d6
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
2 changed files with 20 additions and 0 deletions

View File

@ -641,6 +641,8 @@ pub enum InfixOp {
MulAssign,
SubAssign,
DivAssign,
LeftShift,
RightShift,
}
impl From<TokenKind> for InfixOp {
@ -671,6 +673,8 @@ impl From<TokenKind> for InfixOp {
TokenKind::AsteriskEqual => Self::MulAssign,
TokenKind::MinusEquals => Self::SubAssign,
TokenKind::SlashEquals => Self::DivAssign,
TokenKind::LeftShift => Self::LeftShift,
TokenKind::RightShift => Self::RightShift,
_ => unreachable!(),
}
}

View File

@ -2257,6 +2257,8 @@ fn is_infix(t: &TokenKind) -> bool {
matches!(
t,
TokenKind::Pow
| TokenKind::LeftShift
| TokenKind::RightShift
| TokenKind::Percent
| TokenKind::Instanceof
| TokenKind::Asterisk
@ -4364,6 +4366,20 @@ mod tests {
]);
}
#[test]
fn left_shift() {
assert_ast("<?php 6 << 2;", &[
expr!(Expression::Infix { lhs: Box::new(Expression::Int { i: 6 }), op: InfixOp::LeftShift, rhs: Box::new(Expression::Int { i: 2 }) })
]);
}
#[test]
fn right_shift() {
assert_ast("<?php 6 >> 2;", &[
expr!(Expression::Infix { lhs: Box::new(Expression::Int { i: 6 }), op: InfixOp::RightShift, rhs: Box::new(Expression::Int { i: 2 }) })
]);
}
fn assert_ast(source: &str, expected: &[Statement]) {
let mut lexer = Lexer::new(None);
let tokens = lexer.tokenize(source).unwrap();