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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use cas_error::Error;
use crate::parser::{
ast::{
assign::{Assign as AssignExpr, AssignTarget},
expr::Expr,
range::{Range as RangeExpr, RangeKind},
unary::Unary,
},
error::NonFatal,
fmt::{Latex, fmt_pow},
token::{
op::{
AssignOp,
Associativity,
BinOp,
BinOpKind,
Precedence,
},
Assign,
RangeClosed,
RangeHalfOpen,
},
Parse,
Parser,
ParseResult,
};
use std::{fmt, ops::Range};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// A binary operator, including assignment.
#[derive(Debug, Clone, PartialEq, Eq)]
enum BinOpExt {
/// A binary operator, such as `+` or `*`.
Op(BinOp),
/// Implicit multiplication, such as `2x` or `x(x + 1)`.
///
/// This is not a real operator, but it is treated as one for the purposes of parsing.
ImplicitMultiplication,
/// An assignment operator, such as `+=` or `/=`.
Assign(AssignOp),
/// A range operator, such as `..` or `..=`.
Range(RangeKind)
}
impl BinOpExt {
/// Returns the precedence of the binary operator.
fn precedence(&self) -> Precedence {
match self {
BinOpExt::Op(op) => op.precedence(),
BinOpExt::ImplicitMultiplication => Precedence::Factor,
BinOpExt::Assign(_) => Precedence::Assign,
BinOpExt::Range(_) => Precedence::Range,
}
}
}
impl From<BinOp> for BinOpExt {
fn from(op: BinOp) -> Self {
BinOpExt::Op(op)
}
}
impl From<AssignOp> for BinOpExt {
fn from(op: AssignOp) -> Self {
BinOpExt::Assign(op)
}
}
/// A binary expression, such as `1 + 2`. Binary expressions can include nested expressions.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Binary {
/// The left-hand side of the binary expression.
pub lhs: Box<Expr>,
/// The operator of the binary expression.
pub op: BinOp,
/// The right-hand side of the binary expression.
pub rhs: Box<Expr>,
/// The region of the source code that this binary expression was parsed from.
pub span: Range<usize>,
}
impl Binary {
/// Returns the span of the binary expression.
pub fn span(&self) -> Range<usize> {
self.span.clone()
}
/// After parsing the left-hand-side, the operator, and the right-hand-side of a potential
/// binary expression, parse ahead to see if the right-hand-side is incomplete.
///
/// If we are parsing the expression `1 + 2 * 3`, we will first parse the left-hand-side `1`,
/// then the operator `+`, then the right-hand-side `2`. However, before we build the
/// corresponding AST node, we should check if the operator after `2` has higher precedence
/// than `+` (if it exists).
///
/// If it does, we should parse the expression starting with `2` first, so that we get `2 * 3`
/// as the right-hand-side to the `1 +` node. This works by calling into [`Self::parse_expr`]
/// again, but with `rhs` (`2` in this case) as the `lhs` argument.
///
/// If it does not (such as in the expression `3 * 2 + 1`), we build the AST node `3 * 2`
/// first. Then, [`Self::parse_expr`] will pick up the `+ 1` part of the expression, and
/// build the AST node `3 * 2 + 1`.
///
/// Implicit multiplication is also handled here. In an expression such as `1 + 2x y`, the
/// first call to [`Self::parse_expr`] will parse `1 + 2`. However, there is no operator after
/// `2`, so instead, we assume an implicit multiplication operator, because multiplication has
/// higher precedence than addition, then continue with the same procedure as if the operator
/// did exist.
///
/// There is one distinction we must make when parsing implicit multiplication. Since we're
/// essentially creating multiplication out of thin air, [`Self::complete_rhs`] can get into an
/// infinite loop if we're not careful!
///
/// Consider the expression `1 + 2x`. The first call to [`Self::complete_rhs`] will contain the
/// left-hand-side `1`, the operator `+`, and the right-hand-side `2`. Since there is no
/// operator after `2`, we assume implicit multiplication (higher precedence than `+`), and
/// successfully parse `2x`. At this point, we've returned to the first call to
/// [`Self::complete_rhs`], where the right-hand-side is now `2x`. However, there is still no
/// operator after `2x`, so we assume implicit multiplication again, but parse nothing;
/// [`Self::parse_expr`] would simply return `2x` as-is, and we would return to the first call
/// to [`Self::complete_rhs`] again.
///
/// Thankfully, the solution is simple: just check if `2x` is returned as-is! If so, there is
/// no expression after `2x`, so we should break out of the loop and return the AST node
/// `1 + 2x`. This is the purpose of the `changed` boolean returned by [`Self::parse_expr`].
fn complete_rhs(
input: &mut Parser,
recoverable_errors: &mut Vec<Error>,
lhs: Expr,
op: BinOpExt,
mut rhs: Expr
) -> Result<Expr, Vec<Error>> {
let precedence = op.precedence();
loop {
// before creating the `lhs op rhs` node, we should check the precedence of the
// following operator, if any
// this is because we can't parse an expression like `3 + 4 * 5`, as (3 + 4) * 5
// clone the input stream to emulate peeking
let mut input_ahead = input.clone();
if let Ok(next_op) = input_ahead.try_parse::<BinOp>().forward_errors(recoverable_errors) {
if next_op.precedence() > precedence || next_op.associativity() == Associativity::Right {
// this operator has a higher precedence or it is right associative, so we should
// parse its expression starting with `rhs` first
rhs = Self::parse_expr(input, recoverable_errors, rhs, next_op.precedence())?.0;
} else {
// this operator has lower precedence, or equal precedence and
// left-associativity; this is in scenarios like:
// `1 * 2 + 3` or `1 * 2 * 3`
// prec(+) < prec(*), prec(*) == prec(*)
//
// so just break out of the loop and let `lhs` become `1 * 2`
// we will parse this operator on the next iteration of the outside loop
break;
}
} else if input_ahead.try_parse::<Assign>().is_ok() {
// assignment is right-associative, so we should parse its expression starting with
// `rhs` first
if Precedence::Assign >= precedence {
rhs = Self::parse_expr(input, recoverable_errors, rhs, Precedence::Assign)?.0;
} else {
break;
}
} else {
// there is no operator; check if there is a valid expression after
// if there is, this could be implicit multiplication
//
// first, check if the previous operator has higher or equal precedence; if so, we
// cannot give priority to implicit multiplication
if precedence >= BinOpKind::Mul.precedence() {
break;
}
// then check if there is significant whitespace after `rhs`; if there is, we cannot
// parse implicit multiplication, as that would be confusing
input_ahead.advance_past_non_significant_whitespace();
if let Some(token) = input_ahead.current_token() {
if token.kind.is_significant_whitespace() {
break;
}
}
let (expr, changed) = Self::parse_expr(input, recoverable_errors, rhs, BinOpKind::Mul.precedence())?;
// `rhs = expr;` must happen in all cases, even if `changed` is false, otherwise it
// would've been moved into `Self::parse_expr` above
rhs = expr;
if !changed {
break;
}
}
}
// create the binary node representing `lhs op rhs`
let (start_span, end_span) = (lhs.span().start, rhs.span().end);
match op {
BinOpExt::Op(op) => Ok(Expr::Binary(Binary {
lhs: Box::new(lhs),
op,
rhs: Box::new(rhs),
span: start_span..end_span,
})),
BinOpExt::ImplicitMultiplication => {
let op_span = lhs.span().end..rhs.span().start;
Ok(Expr::Binary(Binary {
lhs: Box::new(lhs),
op: BinOp {
kind: BinOpKind::Mul,
implicit: true,
span: op_span,
},
rhs: Box::new(rhs),
span: start_span..end_span,
}))
},
BinOpExt::Assign(op) => Ok(Expr::Assign(AssignExpr {
target: AssignTarget::try_from_with_op(lhs, &op).forward_errors(recoverable_errors)?,
op,
value: Box::new(rhs),
span: start_span..end_span,
})),
BinOpExt::Range(kind) => Ok(Expr::Range(RangeExpr {
start: Box::new(lhs),
end: Box::new(rhs),
kind,
span: start_span..end_span,
})),
}
}
/// After parsing the left-hand-side of a potential binary expression, parse ahead to see if
/// there is a binary operator and a right-hand-side.
///
/// See [`Self::complete_rhs`] for more information about the return value of this function.
pub(crate) fn parse_expr(
input: &mut Parser,
recoverable_errors: &mut Vec<Error>,
mut lhs: Expr,
precedence: Precedence
) -> Result<(Expr, bool), Vec<Error>> {
let mut changed = false;
loop {
let mut input_ahead = input.clone();
if let Ok(op) = input_ahead.try_parse_then::<BinOp, _>(|bin_op, input| {
if bin_op.precedence() >= precedence {
ParseResult::Ok(())
} else {
ParseResult::Unrecoverable(vec![input.error(NonFatal)])
}
}).forward_errors(recoverable_errors) {
input.set_cursor(&input_ahead);
let rhs = Unary::parse_or_lower(input, recoverable_errors)?;
lhs = Self::complete_rhs(input, recoverable_errors, lhs, op.into(), rhs)?;
} else if let Ok(assign) = input_ahead.try_parse_then::<AssignOp, _>(|_, input| {
if Precedence::Assign >= precedence {
ParseResult::Ok(())
} else {
ParseResult::Unrecoverable(vec![input.error(NonFatal)])
}
}).forward_errors(recoverable_errors) {
// assignment is also a binary expression, however it requires special handling
// because not all expressions are valid as the left-hand side of an assignment
// expression, and there is some syntax is only valid in the context of an
// assignment expression (i.e. function headers)
input.set_cursor(&input_ahead);
let rhs = Unary::parse_or_lower(input, recoverable_errors)?;
lhs = Self::complete_rhs(input, recoverable_errors, lhs, assign.into(), rhs)?;
} else if input_ahead.try_parse_then::<RangeHalfOpen, _>(|_, input| {
if Precedence::Range >= precedence {
ParseResult::Ok(())
} else {
ParseResult::Unrecoverable(vec![input.error(NonFatal)])
}
}).is_ok() {
// range expressions are also binary expressions, but they require special handling
// because they are not valid in all contexts
input.set_cursor(&input_ahead);
let rhs = Unary::parse_or_lower(input, recoverable_errors)?;
lhs = Self::complete_rhs(
input,
recoverable_errors,
lhs,
BinOpExt::Range(RangeKind::HalfOpen),
rhs,
)?;
} else if input_ahead.try_parse_then::<RangeClosed, _>(|_, input| {
if Precedence::Range >= precedence {
ParseResult::Ok(())
} else {
ParseResult::Unrecoverable(vec![input.error(NonFatal)])
}
}).is_ok() {
input.set_cursor(&input_ahead);
let rhs = Unary::parse_or_lower(input, recoverable_errors)?;
lhs = Self::complete_rhs(
input,
recoverable_errors,
lhs,
BinOpExt::Range(RangeKind::Closed),
rhs,
)?;
} else if BinOpKind::Mul.precedence() >= precedence {
// implicit multiplication test
// do not continue if there is significant whitespace after `lhs`
input_ahead.advance_past_non_significant_whitespace();
if let Some(token) = input_ahead.current_token() {
if token.kind.is_significant_whitespace() {
break;
}
}
// ensure that we get here because there is *no* operator, not because the operator
// has lower precedence
if input_ahead.try_parse_then::<BinOp, _>(|op, input| {
if op.precedence() > BinOpKind::Mul.precedence() {
ParseResult::Unrecoverable(vec![input.error(NonFatal)])
} else {
ParseResult::Ok(())
}
}).is_ok() {
break;
}
// if there is no expression, there is no implicit multiplication and all our
// attempts to parse a binary expression fail
let mut inner_recoverable_errors = Vec::new();
let Ok(rhs) = Unary::parse_or_lower(&mut input_ahead, &mut inner_recoverable_errors) else {
break;
};
if rhs.is_implicit_mul_target() {
// TODO: this can be refactored with input.try_parse_with_fn once Try trait is
// stabilized, which would make ParseResult so much nicer to work with
// add the recoverable errors from the inner parser to the outer parser, since
// we know now implicit multiplication is the correct branch to take
recoverable_errors.extend(inner_recoverable_errors);
input.set_cursor(&input_ahead);
lhs = Self::complete_rhs(
input,
recoverable_errors,
lhs,
BinOpExt::ImplicitMultiplication,
rhs,
)?;
} else {
break;
}
} else {
break;
}
changed = true;
}
Ok((lhs, changed))
}
}
impl<'source> Parse<'source> for Binary {
fn std_parse(
input: &mut Parser<'source>,
recoverable_errors: &mut Vec<Error>
) -> Result<Self, Vec<Error>> {
match input.try_parse().forward_errors(recoverable_errors)? {
Expr::Binary(binary) => Ok(binary),
_ => todo!(),
}
}
}
impl std::fmt::Display for Binary {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.lhs.fmt(f)?;
self.op.fmt(f)?;
self.rhs.fmt(f)
}
}
impl Latex for Binary {
fn fmt_latex(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.op.kind {
BinOpKind::Exp => fmt_pow(f, Some(&*self.lhs), Some(&*self.rhs)),
BinOpKind::Div => {
write!(f, "\\frac{{")?;
self.lhs.fmt_latex(f)?;
write!(f, "}}{{")?;
self.rhs.innermost().fmt_latex(f)?;
write!(f, "}}")
},
_ => {
self.lhs.fmt_latex(f)?;
self.op.fmt_latex(f)?;
self.rhs.fmt_latex(f)
},
}
}
}