about summary refs log tree commit diff
path: root/compiler/rustc_ast/src/util/parser.rs
blob: 98b1fc52ed74721953ee9c38bee3c36391bc83fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use rustc_span::kw;

use crate::ast::{self, BinOpKind, RangeLimits};
use crate::token::{self, Token};

/// Associative operator.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum AssocOp {
    /// A binary op.
    Binary(BinOpKind),
    /// `?=` where ? is one of the assignable BinOps
    AssignOp(BinOpKind),
    /// `=`
    Assign,
    /// `as`
    Cast,
    /// `..` or `..=` range
    Range(RangeLimits),
}

#[derive(PartialEq, Debug)]
pub enum Fixity {
    /// The operator is left-associative
    Left,
    /// The operator is right-associative
    Right,
    /// The operator is not associative
    None,
}

impl AssocOp {
    /// Creates a new AssocOp from a token.
    pub fn from_token(t: &Token) -> Option<AssocOp> {
        use AssocOp::*;
        match t.kind {
            token::Eq => Some(Assign),
            token::Plus => Some(Binary(BinOpKind::Add)),
            token::Minus => Some(Binary(BinOpKind::Sub)),
            token::Star => Some(Binary(BinOpKind::Mul)),
            token::Slash => Some(Binary(BinOpKind::Div)),
            token::Percent => Some(Binary(BinOpKind::Rem)),
            token::Caret => Some(Binary(BinOpKind::BitXor)),
            token::And => Some(Binary(BinOpKind::BitAnd)),
            token::Or => Some(Binary(BinOpKind::BitOr)),
            token::Shl => Some(Binary(BinOpKind::Shl)),
            token::Shr => Some(Binary(BinOpKind::Shr)),
            token::PlusEq => Some(AssignOp(BinOpKind::Add)),
            token::MinusEq => Some(AssignOp(BinOpKind::Sub)),
            token::StarEq => Some(AssignOp(BinOpKind::Mul)),
            token::SlashEq => Some(AssignOp(BinOpKind::Div)),
            token::PercentEq => Some(AssignOp(BinOpKind::Rem)),
            token::CaretEq => Some(AssignOp(BinOpKind::BitXor)),
            token::AndEq => Some(AssignOp(BinOpKind::BitAnd)),
            token::OrEq => Some(AssignOp(BinOpKind::BitOr)),
            token::ShlEq => Some(AssignOp(BinOpKind::Shl)),
            token::ShrEq => Some(AssignOp(BinOpKind::Shr)),
            token::Lt => Some(Binary(BinOpKind::Lt)),
            token::Le => Some(Binary(BinOpKind::Le)),
            token::Ge => Some(Binary(BinOpKind::Ge)),
            token::Gt => Some(Binary(BinOpKind::Gt)),
            token::EqEq => Some(Binary(BinOpKind::Eq)),
            token::Ne => Some(Binary(BinOpKind::Ne)),
            token::AndAnd => Some(Binary(BinOpKind::And)),
            token::OrOr => Some(Binary(BinOpKind::Or)),
            token::DotDot => Some(Range(RangeLimits::HalfOpen)),
            // DotDotDot is no longer supported, but we need some way to display the error
            token::DotDotEq | token::DotDotDot => Some(Range(RangeLimits::Closed)),
            // `<-` should probably be `< -`
            token::LArrow => Some(Binary(BinOpKind::Lt)),
            _ if t.is_keyword(kw::As) => Some(Cast),
            _ => None,
        }
    }

    /// Gets the precedence of this operator
    pub fn precedence(&self) -> ExprPrecedence {
        use AssocOp::*;
        match *self {
            Cast => ExprPrecedence::Cast,
            Binary(bin_op) => bin_op.precedence(),
            Range(_) => ExprPrecedence::Range,
            Assign | AssignOp(_) => ExprPrecedence::Assign,
        }
    }

    /// Gets the fixity of this operator
    pub fn fixity(&self) -> Fixity {
        use AssocOp::*;
        // NOTE: it is a bug to have an operators that has same precedence but different fixities!
        match *self {
            Assign | AssignOp(_) => Fixity::Right,
            Binary(binop) => binop.fixity(),
            Cast => Fixity::Left,
            Range(_) => Fixity::None,
        }
    }

    pub fn is_comparison(&self) -> bool {
        use AssocOp::*;
        match *self {
            Binary(binop) => binop.is_comparison(),
            Assign | AssignOp(_) | Cast | Range(_) => false,
        }
    }

    pub fn is_assign_like(&self) -> bool {
        use AssocOp::*;
        match *self {
            Assign | AssignOp(_) => true,
            Cast | Binary(_) | Range(_) => false,
        }
    }

    /// 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.
    pub fn can_continue_expr_unambiguously(&self) -> bool {
        use AssocOp::*;
        use BinOpKind::*;
        matches!(
            self,
            Assign | // `{ 42 } = { 42 }`
            Binary(
                BitXor | // `{ 42 } ^ 3`
                Div | // `{ 42 } / 42`
                Rem | // `{ 42 } % 2`
                Shr | // `{ 42 } >> 2`
                Le | // `{ 42 } <= 3`
                Gt | // `{ 42 } > 3`
                Ge   // `{ 42 } >= 3`
            ) |
            AssignOp(_) | // `{ 42 } +=`
            // Equal | // `{ 42 } == { 42 }`    Accepting these here would regress incorrect
            // NotEqual | // `{ 42 } != { 42 }  struct literals parser recovery.
            Cast // `{ 42 } as usize`
        )
    }
}

#[derive(Clone, Copy, PartialEq, PartialOrd)]
pub enum ExprPrecedence {
    // return, break, yield, closures
    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,
}

/// In `let p = e`, operators with precedence `<=` this one requires parentheses in `e`.
pub fn prec_let_scrutinee_needs_par() -> ExprPrecedence {
    ExprPrecedence::LAnd
}

/// Suppose we have `let _ = e` and the `order` of `e`.
/// Is the `order` such that `e` in `let _ = e` needs parentheses when it is on the RHS?
///
/// Conversely, suppose that we have `(let _ = a) OP b` and `order` is that of `OP`.
/// Can we print this as `let _ = a OP b`?
pub fn needs_par_as_let_scrutinee(order: ExprPrecedence) -> bool {
    order <= prec_let_scrutinee_needs_par()
}

/// 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
/// `X { y: 1 } == foo` all do, but `(X { y: 1 }) == foo` does not.
pub fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
    match &value.kind {
        ast::ExprKind::Struct(..) => true,

        ast::ExprKind::Assign(lhs, rhs, _)
        | ast::ExprKind::AssignOp(_, lhs, rhs)
        | ast::ExprKind::Binary(_, lhs, rhs) => {
            // X { y: 1 } + X { y: 2 }
            contains_exterior_struct_lit(lhs) || contains_exterior_struct_lit(rhs)
        }
        ast::ExprKind::Await(x, _)
        | ast::ExprKind::Unary(_, x)
        | ast::ExprKind::Cast(x, _)
        | ast::ExprKind::Type(x, _)
        | ast::ExprKind::Field(x, _)
        | ast::ExprKind::Index(x, _, _)
        | ast::ExprKind::Match(x, _, ast::MatchKind::Postfix) => {
            // &X { y: 1 }, X { y: 1 }.y
            contains_exterior_struct_lit(x)
        }

        ast::ExprKind::MethodCall(box ast::MethodCall { receiver, .. }) => {
            // X { y: 1 }.bar(...)
            contains_exterior_struct_lit(receiver)
        }

        _ => false,
    }
}