parser/lexer: support <> op

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

View File

@ -492,6 +492,10 @@ impl Lexer {
self.skip(2);
TokenKind::LessThanEquals
}
[b'<', b'>', ..] => {
self.skip(2);
TokenKind::AngledLeftRight
},
[b'<', ..] => {
self.next();
TokenKind::LessThan

View File

@ -30,6 +30,7 @@ pub enum TokenKind {
Attribute,
Bang,
BangEquals,
AngledLeftRight,
BangDoubleEquals,
BoolCast,
BooleanCast,

View File

@ -664,7 +664,7 @@ impl From<TokenKind> for InfixOp {
TokenKind::DotEquals => Self::ConcatAssign,
TokenKind::DoubleEquals => Self::Equals,
TokenKind::TripleEquals => Self::Identical,
TokenKind::BangEquals => Self::NotEquals,
TokenKind::BangEquals | TokenKind::AngledLeftRight => Self::NotEquals,
TokenKind::BangDoubleEquals => Self::NotIdentical,
TokenKind::BooleanAnd => Self::And,
TokenKind::BooleanOr => Self::Or,

View File

@ -2277,6 +2277,7 @@ fn is_infix(t: &TokenKind) -> bool {
| TokenKind::TripleEquals
| TokenKind::BangEquals
| TokenKind::BangDoubleEquals
| TokenKind::AngledLeftRight
| TokenKind::Question
| TokenKind::QuestionColon
| TokenKind::BooleanAnd
@ -4404,6 +4405,13 @@ mod tests {
]);
}
#[test]
fn angled_not_equal() {
assert_ast("<?php 6 <> 2;", &[
expr!(Expression::Infix { lhs: Box::new(Expression::Int { i: 6 }), op: InfixOp::NotEquals, 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 => Self::Equality,
DoubleEquals | BangEquals | TripleEquals | BangDoubleEquals | AngledLeftRight => Self::Equality,
Ampersand => Self::BitwiseAnd,
Caret => Self::BitwiseXor,
Pipe => Self::BitwiseOr,