2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::kw;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
use crate::ast::{self, BinOpKind};
|
2019-10-11 12:46:32 +02:00
|
|
|
use crate::token::{self, BinOpToken, Token};
|
2015-10-15 15:51:30 +03:00
|
|
|
|
|
|
|
|
/// Associative operator with precedence.
|
|
|
|
|
///
|
|
|
|
|
/// This is the enum which specifies operator precedence and fixity to the parser.
|
2020-06-22 21:01:49 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2015-10-15 15:51:30 +03:00
|
|
|
pub enum AssocOp {
|
|
|
|
|
/// `+`
|
|
|
|
|
Add,
|
|
|
|
|
/// `-`
|
|
|
|
|
Subtract,
|
|
|
|
|
/// `*`
|
|
|
|
|
Multiply,
|
|
|
|
|
/// `/`
|
|
|
|
|
Divide,
|
|
|
|
|
/// `%`
|
|
|
|
|
Modulus,
|
|
|
|
|
/// `&&`
|
|
|
|
|
LAnd,
|
|
|
|
|
/// `||`
|
|
|
|
|
LOr,
|
|
|
|
|
/// `^`
|
|
|
|
|
BitXor,
|
|
|
|
|
/// `&`
|
|
|
|
|
BitAnd,
|
|
|
|
|
/// `|`
|
|
|
|
|
BitOr,
|
|
|
|
|
/// `<<`
|
|
|
|
|
ShiftLeft,
|
|
|
|
|
/// `>>`
|
|
|
|
|
ShiftRight,
|
|
|
|
|
/// `==`
|
|
|
|
|
Equal,
|
|
|
|
|
/// `<`
|
|
|
|
|
Less,
|
|
|
|
|
/// `<=`
|
|
|
|
|
LessEqual,
|
|
|
|
|
/// `!=`
|
|
|
|
|
NotEqual,
|
|
|
|
|
/// `>`
|
|
|
|
|
Greater,
|
|
|
|
|
/// `>=`
|
|
|
|
|
GreaterEqual,
|
|
|
|
|
/// `=`
|
|
|
|
|
Assign,
|
2024-12-19 16:24:06 +11:00
|
|
|
/// `?=` where ? is one of the assignable BinOps
|
|
|
|
|
AssignOp(BinOpKind),
|
2015-10-15 15:51:30 +03:00
|
|
|
/// `as`
|
|
|
|
|
As,
|
|
|
|
|
/// `..` range
|
2015-12-03 05:37:48 +03:00
|
|
|
DotDot,
|
2017-09-19 05:40:04 +00:00
|
|
|
/// `..=` range
|
|
|
|
|
DotDotEq,
|
2015-10-15 15:51:30 +03:00
|
|
|
}
|
|
|
|
|
|
2018-03-21 01:58:25 +03:00
|
|
|
#[derive(PartialEq, Debug)]
|
2015-10-15 15:51:30 +03:00
|
|
|
pub enum Fixity {
|
|
|
|
|
/// The operator is left-associative
|
|
|
|
|
Left,
|
|
|
|
|
/// The operator is right-associative
|
|
|
|
|
Right,
|
|
|
|
|
/// The operator is not associative
|
|
|
|
|
None,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AssocOp {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Creates a new AssocOP from a token
|
2019-10-15 22:48:13 +02:00
|
|
|
pub fn from_token(t: &Token) -> Option<AssocOp> {
|
2019-02-07 02:33:01 +09:00
|
|
|
use AssocOp::*;
|
2019-06-08 19:45:12 +03:00
|
|
|
match t.kind {
|
2019-05-24 02:04:56 +03:00
|
|
|
token::Eq => Some(Assign),
|
2024-12-19 16:24:06 +11:00
|
|
|
token::BinOpEq(BinOpToken::Plus) => Some(AssignOp(BinOpKind::Add)),
|
|
|
|
|
token::BinOpEq(BinOpToken::Minus) => Some(AssignOp(BinOpKind::Sub)),
|
|
|
|
|
token::BinOpEq(BinOpToken::Star) => Some(AssignOp(BinOpKind::Mul)),
|
|
|
|
|
token::BinOpEq(BinOpToken::Slash) => Some(AssignOp(BinOpKind::Div)),
|
|
|
|
|
token::BinOpEq(BinOpToken::Percent) => Some(AssignOp(BinOpKind::Rem)),
|
|
|
|
|
token::BinOpEq(BinOpToken::Caret) => Some(AssignOp(BinOpKind::BitXor)),
|
|
|
|
|
token::BinOpEq(BinOpToken::And) => Some(AssignOp(BinOpKind::BitAnd)),
|
|
|
|
|
token::BinOpEq(BinOpToken::Or) => Some(AssignOp(BinOpKind::BitOr)),
|
|
|
|
|
token::BinOpEq(BinOpToken::Shl) => Some(AssignOp(BinOpKind::Shl)),
|
|
|
|
|
token::BinOpEq(BinOpToken::Shr) => Some(AssignOp(BinOpKind::Shr)),
|
|
|
|
|
token::BinOp(BinOpToken::Plus) => Some(Add),
|
|
|
|
|
token::BinOp(BinOpToken::Minus) => Some(Subtract),
|
2019-05-24 02:04:56 +03:00
|
|
|
token::BinOp(BinOpToken::Star) => Some(Multiply),
|
|
|
|
|
token::BinOp(BinOpToken::Slash) => Some(Divide),
|
|
|
|
|
token::BinOp(BinOpToken::Percent) => Some(Modulus),
|
|
|
|
|
token::BinOp(BinOpToken::Caret) => Some(BitXor),
|
2024-12-19 16:24:06 +11:00
|
|
|
token::BinOp(BinOpToken::And) => Some(BitAnd),
|
2019-05-24 02:04:56 +03:00
|
|
|
token::BinOp(BinOpToken::Or) => Some(BitOr),
|
2024-12-19 16:24:06 +11:00
|
|
|
token::BinOp(BinOpToken::Shl) => Some(ShiftLeft),
|
|
|
|
|
token::BinOp(BinOpToken::Shr) => Some(ShiftRight),
|
2019-05-24 02:04:56 +03:00
|
|
|
token::Lt => Some(Less),
|
|
|
|
|
token::Le => Some(LessEqual),
|
|
|
|
|
token::Ge => Some(GreaterEqual),
|
|
|
|
|
token::Gt => Some(Greater),
|
|
|
|
|
token::EqEq => Some(Equal),
|
|
|
|
|
token::Ne => Some(NotEqual),
|
|
|
|
|
token::AndAnd => Some(LAnd),
|
|
|
|
|
token::OrOr => Some(LOr),
|
|
|
|
|
token::DotDot => Some(DotDot),
|
|
|
|
|
token::DotDotEq => Some(DotDotEq),
|
2017-11-05 00:46:41 +01:00
|
|
|
// DotDotDot is no longer supported, but we need some way to display the error
|
2019-05-24 02:04:56 +03:00
|
|
|
token::DotDotDot => Some(DotDotEq),
|
2019-08-11 23:37:05 +02:00
|
|
|
// `<-` should probably be `< -`
|
|
|
|
|
token::LArrow => Some(Less),
|
2019-05-11 17:41:37 +03:00
|
|
|
_ if t.is_keyword(kw::As) => Some(As),
|
2015-10-15 15:51:30 +03:00
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Creates a new AssocOp from ast::BinOpKind.
|
2016-02-08 13:16:12 +01:00
|
|
|
pub fn from_ast_binop(op: BinOpKind) -> Self {
|
2019-02-07 02:33:01 +09:00
|
|
|
use AssocOp::*;
|
2015-10-15 15:51:30 +03:00
|
|
|
match op {
|
2016-02-08 13:16:12 +01:00
|
|
|
BinOpKind::Lt => Less,
|
|
|
|
|
BinOpKind::Gt => Greater,
|
|
|
|
|
BinOpKind::Le => LessEqual,
|
|
|
|
|
BinOpKind::Ge => GreaterEqual,
|
|
|
|
|
BinOpKind::Eq => Equal,
|
|
|
|
|
BinOpKind::Ne => NotEqual,
|
|
|
|
|
BinOpKind::Mul => Multiply,
|
|
|
|
|
BinOpKind::Div => Divide,
|
|
|
|
|
BinOpKind::Rem => Modulus,
|
|
|
|
|
BinOpKind::Add => Add,
|
|
|
|
|
BinOpKind::Sub => Subtract,
|
|
|
|
|
BinOpKind::Shl => ShiftLeft,
|
|
|
|
|
BinOpKind::Shr => ShiftRight,
|
|
|
|
|
BinOpKind::BitAnd => BitAnd,
|
|
|
|
|
BinOpKind::BitXor => BitXor,
|
|
|
|
|
BinOpKind::BitOr => BitOr,
|
|
|
|
|
BinOpKind::And => LAnd,
|
|
|
|
|
BinOpKind::Or => LOr,
|
2015-10-15 15:51:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets the precedence of this operator
|
2024-11-28 12:47:18 -08:00
|
|
|
pub fn precedence(&self) -> ExprPrecedence {
|
2019-02-07 02:33:01 +09:00
|
|
|
use AssocOp::*;
|
2015-10-15 15:51:30 +03:00
|
|
|
match *self {
|
2024-11-28 12:47:18 -08:00
|
|
|
As => ExprPrecedence::Cast,
|
|
|
|
|
Multiply | Divide | Modulus => ExprPrecedence::Product,
|
|
|
|
|
Add | Subtract => ExprPrecedence::Sum,
|
|
|
|
|
ShiftLeft | ShiftRight => ExprPrecedence::Shift,
|
|
|
|
|
BitAnd => ExprPrecedence::BitAnd,
|
|
|
|
|
BitXor => ExprPrecedence::BitXor,
|
|
|
|
|
BitOr => ExprPrecedence::BitOr,
|
|
|
|
|
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => ExprPrecedence::Compare,
|
|
|
|
|
LAnd => ExprPrecedence::LAnd,
|
|
|
|
|
LOr => ExprPrecedence::LOr,
|
|
|
|
|
DotDot | DotDotEq => ExprPrecedence::Range,
|
|
|
|
|
Assign | AssignOp(_) => ExprPrecedence::Assign,
|
2015-10-15 15:51:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets the fixity of this operator
|
|
|
|
|
pub fn fixity(&self) -> Fixity {
|
2019-02-07 02:33:01 +09:00
|
|
|
use AssocOp::*;
|
2015-10-15 15:51:30 +03:00
|
|
|
// NOTE: it is a bug to have an operators that has same precedence but different fixities!
|
|
|
|
|
match *self {
|
2019-05-06 14:53:22 +01:00
|
|
|
Assign | AssignOp(_) => Fixity::Right,
|
2015-10-15 15:51:30 +03:00
|
|
|
As | Multiply | Divide | Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd
|
2024-12-20 19:50:42 -08:00
|
|
|
| BitXor | BitOr | LAnd | LOr => Fixity::Left,
|
|
|
|
|
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual | DotDot | DotDotEq => {
|
|
|
|
|
Fixity::None
|
|
|
|
|
}
|
2015-10-15 15:51:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_comparison(&self) -> bool {
|
2019-02-07 02:33:01 +09:00
|
|
|
use AssocOp::*;
|
2015-10-15 15:51:30 +03:00
|
|
|
match *self {
|
|
|
|
|
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual => true,
|
2019-05-06 14:53:22 +01:00
|
|
|
Assign | AssignOp(_) | As | Multiply | Divide | Modulus | Add | Subtract
|
2022-11-16 21:46:06 +01:00
|
|
|
| ShiftLeft | ShiftRight | BitAnd | BitXor | BitOr | LAnd | LOr | DotDot | DotDotEq => {
|
|
|
|
|
false
|
|
|
|
|
}
|
2015-10-15 15:51:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-16 22:42:06 +03:00
|
|
|
pub fn is_assign_like(&self) -> bool {
|
2019-02-07 02:33:01 +09:00
|
|
|
use AssocOp::*;
|
2015-10-16 22:42:06 +03:00
|
|
|
match *self {
|
2019-05-06 14:53:22 +01:00
|
|
|
Assign | AssignOp(_) => true,
|
2015-10-16 22:42:06 +03:00
|
|
|
Less | Greater | LessEqual | GreaterEqual | Equal | NotEqual | As | Multiply
|
|
|
|
|
| Divide | Modulus | Add | Subtract | ShiftLeft | ShiftRight | BitAnd | BitXor
|
2022-11-16 21:46:06 +01:00
|
|
|
| BitOr | LAnd | LOr | DotDot | DotDotEq => false,
|
2015-10-16 22:42:06 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 13:16:12 +01:00
|
|
|
pub fn to_ast_binop(&self) -> Option<BinOpKind> {
|
2019-02-07 02:33:01 +09:00
|
|
|
use AssocOp::*;
|
2015-10-15 15:51:30 +03:00
|
|
|
match *self {
|
2016-02-08 13:16:12 +01:00
|
|
|
Less => Some(BinOpKind::Lt),
|
|
|
|
|
Greater => Some(BinOpKind::Gt),
|
|
|
|
|
LessEqual => Some(BinOpKind::Le),
|
|
|
|
|
GreaterEqual => Some(BinOpKind::Ge),
|
|
|
|
|
Equal => Some(BinOpKind::Eq),
|
|
|
|
|
NotEqual => Some(BinOpKind::Ne),
|
|
|
|
|
Multiply => Some(BinOpKind::Mul),
|
|
|
|
|
Divide => Some(BinOpKind::Div),
|
|
|
|
|
Modulus => Some(BinOpKind::Rem),
|
|
|
|
|
Add => Some(BinOpKind::Add),
|
|
|
|
|
Subtract => Some(BinOpKind::Sub),
|
|
|
|
|
ShiftLeft => Some(BinOpKind::Shl),
|
|
|
|
|
ShiftRight => Some(BinOpKind::Shr),
|
|
|
|
|
BitAnd => Some(BinOpKind::BitAnd),
|
|
|
|
|
BitXor => Some(BinOpKind::BitXor),
|
|
|
|
|
BitOr => Some(BinOpKind::BitOr),
|
|
|
|
|
LAnd => Some(BinOpKind::And),
|
|
|
|
|
LOr => Some(BinOpKind::Or),
|
2022-11-16 21:46:06 +01:00
|
|
|
Assign | AssignOp(_) | As | DotDot | DotDotEq => None,
|
2015-10-15 15:51:30 +03:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-22 19:37:23 -07:00
|
|
|
|
2019-05-06 16:00:21 -07:00
|
|
|
/// This operator could be used to follow a block unambiguously.
|
|
|
|
|
///
|
|
|
|
|
/// This is used for error recovery at the moment, providing a suggestion to wrap blocks with
|
|
|
|
|
/// parentheses while having a high degree of confidence on the correctness of the suggestion.
|
2019-04-22 19:37:23 -07:00
|
|
|
pub fn can_continue_expr_unambiguously(&self) -> bool {
|
|
|
|
|
use AssocOp::*;
|
2021-11-06 01:31:32 +01:00
|
|
|
matches!(
|
|
|
|
|
self,
|
2019-04-22 19:37:23 -07:00
|
|
|
BitXor | // `{ 42 } ^ 3`
|
|
|
|
|
Assign | // `{ 42 } = { 42 }`
|
|
|
|
|
Divide | // `{ 42 } / 42`
|
|
|
|
|
Modulus | // `{ 42 } % 2`
|
|
|
|
|
ShiftRight | // `{ 42 } >> 2`
|
|
|
|
|
LessEqual | // `{ 42 } <= 3`
|
|
|
|
|
Greater | // `{ 42 } > 3`
|
|
|
|
|
GreaterEqual | // `{ 42 } >= 3`
|
|
|
|
|
AssignOp(_) | // `{ 42 } +=`
|
|
|
|
|
// Equal | // `{ 42 } == { 42 }` Accepting these here would regress incorrect
|
2022-11-16 21:46:06 +01:00
|
|
|
// NotEqual | // `{ 42 } != { 42 } struct literals parser recovery.
|
|
|
|
|
As // `{ 42 } as usize`
|
2021-11-06 01:31:32 +01:00
|
|
|
)
|
2019-04-22 19:37:23 -07:00
|
|
|
}
|
2015-10-15 15:51:30 +03:00
|
|
|
}
|
2017-07-20 16:53:56 -04:00
|
|
|
|
2024-11-28 12:47:18 -08:00
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd)]
|
|
|
|
|
pub enum ExprPrecedence {
|
2024-12-02 17:17:37 -08:00
|
|
|
// return, break, yield, closures
|
2024-11-28 12:47:18 -08:00
|
|
|
Jump,
|
|
|
|
|
// = += -= *= /= %= &= |= ^= <<= >>=
|
|
|
|
|
Assign,
|
|
|
|
|
// .. ..=
|
|
|
|
|
Range,
|
|
|
|
|
// ||
|
|
|
|
|
LOr,
|
|
|
|
|
// &&
|
|
|
|
|
LAnd,
|
|
|
|
|
// == != < > <= >=
|
|
|
|
|
Compare,
|
|
|
|
|
// |
|
|
|
|
|
BitOr,
|
|
|
|
|
// ^
|
|
|
|
|
BitXor,
|
|
|
|
|
// &
|
|
|
|
|
BitAnd,
|
|
|
|
|
// << >>
|
|
|
|
|
Shift,
|
|
|
|
|
// + -
|
|
|
|
|
Sum,
|
|
|
|
|
// * / %
|
|
|
|
|
Product,
|
|
|
|
|
// as
|
|
|
|
|
Cast,
|
|
|
|
|
// unary - * ! & &mut
|
|
|
|
|
Prefix,
|
|
|
|
|
// paths, loops, function calls, array indexing, field expressions, method calls
|
|
|
|
|
Unambiguous,
|
|
|
|
|
}
|
2017-07-20 16:53:56 -04:00
|
|
|
|
2021-10-17 12:04:01 +02:00
|
|
|
/// In `let p = e`, operators with precedence `<=` this one requires parentheses in `e`.
|
2024-11-28 12:47:18 -08:00
|
|
|
pub fn prec_let_scrutinee_needs_par() -> ExprPrecedence {
|
|
|
|
|
ExprPrecedence::LAnd
|
2019-06-18 01:28:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Suppose we have `let _ = e` and the `order` of `e`.
|
2021-10-17 12:04:01 +02:00
|
|
|
/// Is the `order` such that `e` in `let _ = e` needs parentheses when it is on the RHS?
|
2019-06-18 01:28:20 +02:00
|
|
|
///
|
|
|
|
|
/// Conversely, suppose that we have `(let _ = a) OP b` and `order` is that of `OP`.
|
|
|
|
|
/// Can we print this as `let _ = a OP b`?
|
2024-11-28 12:47:18 -08:00
|
|
|
pub fn needs_par_as_let_scrutinee(order: ExprPrecedence) -> bool {
|
|
|
|
|
order <= prec_let_scrutinee_needs_par()
|
2019-06-18 01:28:20 +02:00
|
|
|
}
|
2018-01-15 15:09:39 -08:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Expressions that syntactically contain an "exterior" struct literal i.e., not surrounded by any
|
|
|
|
|
/// parens or other delimiters, e.g., `X { y: 1 }`, `X { y: 1 }.method()`, `foo == X { y: 1 }` and
|
2017-07-20 16:53:56 -04:00
|
|
|
/// `X { y: 1 } == foo` all do, but `(X { y: 1 }) == foo` does not.
|
|
|
|
|
pub fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &value.kind {
|
2017-07-20 16:53:56 -04:00
|
|
|
ast::ExprKind::Struct(..) => true,
|
|
|
|
|
|
2022-11-16 19:26:38 +00:00
|
|
|
ast::ExprKind::Assign(lhs, rhs, _)
|
|
|
|
|
| ast::ExprKind::AssignOp(_, lhs, rhs)
|
|
|
|
|
| ast::ExprKind::Binary(_, lhs, rhs) => {
|
2017-07-20 16:53:56 -04:00
|
|
|
// X { y: 1 } + X { y: 2 }
|
2022-11-29 11:01:17 +00:00
|
|
|
contains_exterior_struct_lit(lhs) || contains_exterior_struct_lit(rhs)
|
2017-07-20 16:53:56 -04:00
|
|
|
}
|
2023-04-25 18:59:16 +00:00
|
|
|
ast::ExprKind::Await(x, _)
|
2022-11-16 19:26:38 +00:00
|
|
|
| ast::ExprKind::Unary(_, x)
|
|
|
|
|
| ast::ExprKind::Cast(x, _)
|
|
|
|
|
| ast::ExprKind::Type(x, _)
|
|
|
|
|
| ast::ExprKind::Field(x, _)
|
2024-04-02 19:40:12 -04:00
|
|
|
| ast::ExprKind::Index(x, _, _)
|
|
|
|
|
| ast::ExprKind::Match(x, _, ast::MatchKind::Postfix) => {
|
2017-07-20 16:53:56 -04:00
|
|
|
// &X { y: 1 }, X { y: 1 }.y
|
2022-11-29 11:01:17 +00:00
|
|
|
contains_exterior_struct_lit(x)
|
2017-07-20 16:53:56 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-16 19:26:38 +00:00
|
|
|
ast::ExprKind::MethodCall(box ast::MethodCall { receiver, .. }) => {
|
2017-07-20 16:53:56 -04:00
|
|
|
// X { y: 1 }.bar(...)
|
2022-11-29 11:01:17 +00:00
|
|
|
contains_exterior_struct_lit(receiver)
|
2017-07-20 16:53:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|