parser: support <=> op

This commit is contained in:
Ryan Chandler 2022-09-16 15:16:23 +01:00
parent a7d74e6b2d
commit a6388b3c57
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
5 changed files with 18 additions and 1 deletions

View File

@ -488,6 +488,10 @@ impl Lexer {
self.skip(2);
TokenKind::LeftShift
}
[b'<', b'=', b'>', ..] => {
self.skip(3);
TokenKind::Spaceship
}
[b'<', b'=', ..] => {
self.skip(2);
TokenKind::LessThanEquals

View File

@ -32,6 +32,7 @@ pub enum TokenKind {
BangEquals,
AngledLeftRight,
BangDoubleEquals,
Spaceship,
BoolCast,
BooleanCast,
BooleanAnd,
@ -344,6 +345,8 @@ impl Display for TokenKind {
Self::Yield => "yield",
Self::While => "while",
Self::Global => "global",
Self::AngledLeftRight => "<>",
Self::Spaceship => "<=>",
_ => todo!("format token: {:?}", self),
};
write!(f, "{}", s)

View File

@ -646,6 +646,7 @@ pub enum InfixOp {
BitwiseAnd,
BitwiseOr,
BitwiseXor,
Spaceship,
}
impl From<TokenKind> for InfixOp {
@ -681,6 +682,7 @@ impl From<TokenKind> for InfixOp {
TokenKind::Ampersand => Self::BitwiseAnd,
TokenKind::Pipe => Self::BitwiseOr,
TokenKind::Caret => Self::BitwiseXor,
TokenKind::Spaceship => Self::Spaceship,
_ => unreachable!(),
}
}

View File

@ -2257,6 +2257,7 @@ fn is_infix(t: &TokenKind) -> bool {
matches!(
t,
TokenKind::Pow
| TokenKind::Spaceship
| TokenKind::LeftShift
| TokenKind::RightShift
| TokenKind::Ampersand
@ -4412,6 +4413,13 @@ mod tests {
]);
}
#[test]
fn spaceship() {
assert_ast("<?php 6 <=> 2;", &[
expr!(Expression::Infix { lhs: Box::new(Expression::Int { i: 6 }), op: InfixOp::Spaceship, 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();

View File

@ -56,7 +56,7 @@ impl Precedence {
LeftShift | RightShift => Self::BitShift,
Dot => Self::Concat,
LessThan | LessThanEquals | GreaterThan | GreaterThanEquals => Self::LtGt,
DoubleEquals | BangEquals | TripleEquals | BangDoubleEquals | AngledLeftRight => Self::Equality,
DoubleEquals | BangEquals | TripleEquals | BangDoubleEquals | AngledLeftRight | Spaceship => Self::Equality,
Ampersand => Self::BitwiseAnd,
Caret => Self::BitwiseXor,
Pipe => Self::BitwiseOr,