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
221
222
223
224
225
226
227
use std::cmp::Ordering;
use once_cell::sync::OnceCell;
use super::{
expression::{Expression, ExpressionKind},
ops::{BinaryOp, UnaryPostfixOp, UnaryPrefixOp},
};
type PrecedenceMatcher = fn(&ExpressionKind) -> bool;
static PRECEDENCE: OnceCell<Vec<PrecedenceMatcher>> = OnceCell::new();
/// The position occupied by a child expression within its parent operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChildPosition {
Left,
Right,
Operand,
}
/// Whether `child` needs parentheses when printed in `position` beneath
/// `parent`.
///
/// At equal precedence, the child's position decides whether the parser would
/// reconstruct the same AST. In particular, the right child of a
/// left-associative binary operator must retain its parentheses.
pub fn needs_parentheses(
parent: &ExpressionKind,
child: &Expression,
position: ChildPosition,
) -> bool {
let Some(parent_precedence) = precedence(parent) else {
return false;
};
let Some(child_precedence) = precedence(&child.kind) else {
return false;
};
match child_precedence.cmp(&parent_precedence) {
Ordering::Less => false,
Ordering::Greater => true,
Ordering::Equal => equal_precedence_child(parent) != Some(position),
}
}
/// Return the precedence of an operator-like expression.
///
/// `None` means the expression is self-delimiting: a literal, reference,
/// function call, or another form whose surrounding syntax already closes it.
fn precedence(expression: &ExpressionKind) -> Option<usize> {
PRECEDENCE
.get_or_init(|| {
vec![
// | operator=(MINUS | PLUS) expression
|expression| {
matches!(
expression,
ExpressionKind::UnaryPrefixOperator(unary)
if matches!(
unary.operator,
UnaryPrefixOp::Minus | UnaryPrefixOp::Plus
)
)
},
// | expression '@' ...
// | expression truncation_unit
// | expression DOT simpleIdentifier
// | expression LBRACKET expression RBRACKET
//
// These closed postfix forms can be chained without
// parentheses, so they deliberately share one entry.
|expression| match expression {
ExpressionKind::TsTrunc(_)
| ExpressionKind::FieldLookup(_)
| ExpressionKind::IndexAccess(_) => true,
ExpressionKind::BinaryOperator(binary) => binary.operator == BinaryOp::Dot,
_ => false,
},
// | expression (ASTERISK | SLASH | PERCENT) expression
|expression| {
matches!(
expression,
ExpressionKind::BinaryOperator(binary)
if matches!(
binary.operator,
BinaryOp::Multiply | BinaryOp::Divide | BinaryOp::Modulo
)
)
},
// | expression (PLUS | MINUS) expression
|expression| {
matches!(
expression,
ExpressionKind::BinaryOperator(binary)
if matches!(binary.operator, BinaryOp::Add | BinaryOp::Subtract)
)
},
// | operator=(RANGE_INCLUSIVE | RANGE) expression
|expression| {
matches!(
expression,
ExpressionKind::UnaryPrefixOperator(unary)
if matches!(
unary.operator,
UnaryPrefixOp::RangeInclusive | UnaryPrefixOp::Range
)
)
},
// | expression operator=RANGE
|expression| {
matches!(
expression,
ExpressionKind::UnaryPostfixOperator(unary)
if unary.operator == UnaryPostfixOp::Range
)
},
// | expression (RANGE_INCLUSIVE | RANGE) expression
|expression| {
matches!(
expression,
ExpressionKind::BinaryOperator(binary)
if matches!(
binary.operator,
BinaryOp::RangeInclusive | BinaryOp::Range
)
)
},
// | expression AS hamelintype
|expression| match expression {
ExpressionKind::Cast(_) => true,
ExpressionKind::BinaryOperator(binary) => binary.operator == BinaryOp::As,
_ => false,
},
// | expression comparison_operator expression
|expression| {
matches!(
expression,
ExpressionKind::BinaryOperator(binary)
if matches!(
binary.operator,
BinaryOp::Equal
| BinaryOp::NotEqual
| BinaryOp::LessThan
| BinaryOp::LessThanOrEqual
| BinaryOp::GreaterThan
| BinaryOp::GreaterThanOrEqual
| BinaryOp::Is
| BinaryOp::IsNot
| BinaryOp::In
| BinaryOp::NotIn
)
)
},
// | NOT expression
|expression| {
matches!(
expression,
ExpressionKind::UnaryPrefixOperator(unary)
if unary.operator == UnaryPrefixOp::Not
)
},
// | expression AND expression
|expression| {
matches!(
expression,
ExpressionKind::BinaryOperator(binary)
if binary.operator == BinaryOp::And
)
},
// | expression OR expression
|expression| {
matches!(
expression,
ExpressionKind::BinaryOperator(binary)
if binary.operator == BinaryOp::Or
)
},
// | expression COLON expression
|expression| match expression {
ExpressionKind::PairLiteral(_) => true,
ExpressionKind::BinaryOperator(binary) => binary.operator == BinaryOp::Colon,
_ => false,
},
// | lambdaParams ARROW expression
|expression| matches!(expression, ExpressionKind::Lambda(_)),
]
})
.iter()
.enumerate()
.find_map(|(index, matches)| matches(expression).then_some(index))
}
/// Return the child position that naturally retains the same AST when its
/// precedence equals its parent's precedence.
fn equal_precedence_child(parent: &ExpressionKind) -> Option<ChildPosition> {
match parent {
ExpressionKind::UnaryPrefixOperator(_)
| ExpressionKind::UnaryPostfixOperator(_)
| ExpressionKind::TsTrunc(_)
| ExpressionKind::FieldLookup(_)
| ExpressionKind::IndexAccess(_)
| ExpressionKind::Cast(_) => Some(ChildPosition::Operand),
ExpressionKind::BinaryOperator(_) | ExpressionKind::PairLiteral(_) => {
Some(ChildPosition::Left)
}
ExpressionKind::Lambda(_) => Some(ChildPosition::Right),
ExpressionKind::IntLiteral(_)
| ExpressionKind::DecimalLiteral(_)
| ExpressionKind::ScientificLiteral(_)
| ExpressionKind::DoubleLiteral(_)
| ExpressionKind::BooleanLiteral(_)
| ExpressionKind::StringLiteral(_)
| ExpressionKind::BinaryLiteral(_)
| ExpressionKind::NullLiteral(_)
| ExpressionKind::ArrayLiteral(_)
| ExpressionKind::TupleLiteral(_)
| ExpressionKind::StructLiteral(_)
| ExpressionKind::FieldReference(_)
| ExpressionKind::TemplateParameter(_)
| ExpressionKind::FunctionCall(_)
| ExpressionKind::IntervalLiteral(_)
| ExpressionKind::RowsLiteral(_)
| ExpressionKind::UnboundRangeLiteral(_)
| ExpressionKind::Error(_) => None,
}
}