|
1 |
| -//! Routines the parser uses to classify AST nodes |
2 |
| -
|
3 |
| -// Predicates on exprs and stmts that the pretty-printer and parser use |
| 1 | +//! Routines the parser and pretty-printer use to classify AST nodes. |
4 | 2 |
|
5 | 3 | use crate::{ast, token::Delimiter};
|
6 | 4 |
|
7 |
| -/// Does this expression require a semicolon to be treated |
8 |
| -/// as a statement? The negation of this: 'can this expression |
9 |
| -/// be used as a statement without a semicolon' -- is used |
10 |
| -/// as an early-bail-out in the parser so that, for instance, |
11 |
| -/// if true {...} else {...} |
12 |
| -/// |x| 5 |
13 |
| -/// isn't parsed as (if true {...} else {...} | x) | 5 |
| 5 | +/// Does this expression require a semicolon to be treated as a statement? |
| 6 | +/// |
| 7 | +/// The negation of this: "can this expression be used as a statement without a |
| 8 | +/// semicolon" -- is used as an early bail-out in the parser so that, for |
| 9 | +/// instance, |
| 10 | +/// |
| 11 | +/// ```ignore (illustrative) |
| 12 | +/// if true {...} else {...} |
| 13 | +/// |x| 5 |
| 14 | +/// ``` |
| 15 | +/// |
| 16 | +/// isn't parsed as `(if true {...} else {...} | x) | 5`. |
| 17 | +/// |
| 18 | +/// Nearly the same early bail-out also occurs in the right-hand side of match |
| 19 | +/// arms: |
| 20 | +/// |
| 21 | +/// ```ignore (illustrative) |
| 22 | +/// match i { |
| 23 | +/// 0 => if true {...} else {...} |
| 24 | +/// | x => {} |
| 25 | +/// } |
| 26 | +/// ``` |
| 27 | +/// |
| 28 | +/// Here the `|` is a leading vert in a second match arm. It is not a binary |
| 29 | +/// operator with the If as its left operand. If the first arm were some other |
| 30 | +/// expression for which `expr_requires_semi_to_be_stmt` returns true, then the |
| 31 | +/// `|` on the next line would be a binary operator (leading to a parse error). |
| 32 | +/// |
| 33 | +/// The statement case and the match-arm case are "nearly" the same early |
| 34 | +/// bail-out because of 1 edge case. Macro calls with brace delimiter terminate |
| 35 | +/// a statement without a semicolon, but do not terminate a match-arm without |
| 36 | +/// comma. |
| 37 | +/// |
| 38 | +/// ```ignore (illustrative) |
| 39 | +/// m! {} - 1; // two statements: a macro call followed by -1 literal |
| 40 | +/// |
| 41 | +/// match () { |
| 42 | +/// _ => m! {} - 1, // binary subtraction operator |
| 43 | +/// } |
| 44 | +/// ``` |
14 | 45 | pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
|
15 | 46 | !matches!(
|
16 | 47 | e.kind,
|
|
0 commit comments