mirror of
https://github.com/danog/parser.git
synced 2024-11-30 04:29:13 +01:00
parser: support <=> op
This commit is contained in:
parent
a7d74e6b2d
commit
a6388b3c57
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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!(),
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user