Remove token::{Open,Close}Delim.

By replacing them with `{Open,Close}{Param,Brace,Bracket,Invisible}`.

PR #137902 made `ast::TokenKind` more like `lexer::TokenKind` by
replacing the compound `BinOp{,Eq}(BinOpToken)` variants with fieldless
variants `Plus`, `Minus`, `Star`, etc. This commit does a similar thing
with delimiters. It also makes `ast::TokenKind` more similar to
`parser::TokenType`.

This requires a few new methods:
- `TokenKind::is_{,open_,close_}delim()` replace various kinds of
  pattern matches.
- `Delimiter::as_{open,close}_token_kind` are used to convert
  `Delimiter` values to `TokenKind`.

Despite these additions, it's a net reduction in lines of code. This is
because e.g. `token::OpenParen` is so much shorter than
`token::OpenDelim(Delimiter::Parenthesis)` that many multi-line forms
reduce to single line forms. And many places where the number of lines
doesn't change are still easier to read, just because the names are
shorter, e.g.:
```
-   } else if self.token != token::CloseDelim(Delimiter::Brace) {
+   } else if self.token != token::CloseBrace {
```
This commit is contained in:
Nicholas Nethercote
2025-04-16 16:13:50 +10:00
parent a15cce2690
commit bf8ce32558
30 changed files with 456 additions and 498 deletions

View File

@@ -2573,14 +2573,14 @@ fn look_ahead() {
// Current position is the `fn`.
look(&p, 0, token::Ident(kw::Fn, raw_no));
look(&p, 1, token::Ident(sym_f, raw_no));
look(&p, 2, token::OpenDelim(Delimiter::Parenthesis));
look(&p, 2, token::OpenParen);
look(&p, 3, token::Ident(sym_x, raw_no));
look(&p, 4, token::Colon);
look(&p, 5, token::Ident(sym::u32, raw_no));
look(&p, 6, token::CloseDelim(Delimiter::Parenthesis));
look(&p, 7, token::OpenDelim(Delimiter::Brace));
look(&p, 6, token::CloseParen);
look(&p, 7, token::OpenBrace);
look(&p, 8, token::Ident(sym_x, raw_no));
look(&p, 9, token::CloseDelim(Delimiter::Brace));
look(&p, 9, token::CloseBrace);
look(&p, 10, token::Ident(kw::Struct, raw_no));
look(&p, 11, token::Ident(sym_S, raw_no));
look(&p, 12, token::Semi);
@@ -2597,10 +2597,10 @@ fn look_ahead() {
look(&p, 0, token::Ident(sym_x, raw_no));
look(&p, 1, token::Colon);
look(&p, 2, token::Ident(sym::u32, raw_no));
look(&p, 3, token::CloseDelim(Delimiter::Parenthesis));
look(&p, 4, token::OpenDelim(Delimiter::Brace));
look(&p, 3, token::CloseParen);
look(&p, 4, token::OpenBrace);
look(&p, 5, token::Ident(sym_x, raw_no));
look(&p, 6, token::CloseDelim(Delimiter::Brace));
look(&p, 6, token::CloseBrace);
look(&p, 7, token::Ident(kw::Struct, raw_no));
look(&p, 8, token::Ident(sym_S, raw_no));
look(&p, 9, token::Semi);
@@ -2652,18 +2652,18 @@ fn look_ahead_non_outermost_stream() {
}
look(&p, 0, token::Ident(kw::Fn, raw_no));
look(&p, 1, token::Ident(sym_f, raw_no));
look(&p, 2, token::OpenDelim(Delimiter::Parenthesis));
look(&p, 2, token::OpenParen);
look(&p, 3, token::Ident(sym_x, raw_no));
look(&p, 4, token::Colon);
look(&p, 5, token::Ident(sym::u32, raw_no));
look(&p, 6, token::CloseDelim(Delimiter::Parenthesis));
look(&p, 7, token::OpenDelim(Delimiter::Brace));
look(&p, 6, token::CloseParen);
look(&p, 7, token::OpenBrace);
look(&p, 8, token::Ident(sym_x, raw_no));
look(&p, 9, token::CloseDelim(Delimiter::Brace));
look(&p, 9, token::CloseBrace);
look(&p, 10, token::Ident(kw::Struct, raw_no));
look(&p, 11, token::Ident(sym_S, raw_no));
look(&p, 12, token::Semi);
look(&p, 13, token::CloseDelim(Delimiter::Brace));
look(&p, 13, token::CloseBrace);
// Any lookahead past the end of the token stream returns `Eof`.
look(&p, 14, token::Eof);
look(&p, 15, token::Eof);
@@ -2723,9 +2723,7 @@ fn debug_lookahead() {
\"f\",
No,
),
OpenDelim(
Parenthesis,
),
OpenParen,
Ident(
\"x\",
No,
@@ -2735,9 +2733,7 @@ fn debug_lookahead() {
\"u32\",
No,
),
CloseDelim(
Parenthesis,
),
CloseParen,
],
approx_token_stream_pos: 0,
..
@@ -2768,9 +2764,7 @@ fn debug_lookahead() {
\"f\",
No,
),
OpenDelim(
Parenthesis,
),
OpenParen,
Ident(
\"x\",
No,
@@ -2780,19 +2774,13 @@ fn debug_lookahead() {
\"u32\",
No,
),
CloseDelim(
Parenthesis,
),
OpenDelim(
Brace,
),
CloseParen,
OpenBrace,
Ident(
\"x\",
No,
),
CloseDelim(
Brace,
),
CloseBrace,
Ident(
\"struct\",
No,
@@ -2817,9 +2805,7 @@ fn debug_lookahead() {
&format!("{:#?}", p.debug_lookahead(1)),
"Parser {
prev_token: Token {
kind: OpenDelim(
Brace,
),
kind: OpenBrace,
span: Span {
lo: BytePos(
13,
@@ -2844,9 +2830,7 @@ fn debug_lookahead() {
&format!("{:#?}", p.debug_lookahead(4)),
"Parser {
prev_token: Token {
kind: OpenDelim(
Brace,
),
kind: OpenBrace,
span: Span {
lo: BytePos(
13,
@@ -2862,9 +2846,7 @@ fn debug_lookahead() {
\"x\",
No,
),
CloseDelim(
Brace,
),
CloseBrace,
Ident(
\"struct\",
No,