Replace ast::TokenKind::BinOp{,Eq} and remove BinOpToken.

`BinOpToken` is badly named, because it only covers the assignable
binary ops and excludes comparisons and `&&`/`||`. Its use in
`ast::TokenKind` does allow a small amount of code sharing, but it's a
clumsy factoring.

This commit removes `ast::TokenKind::BinOp{,Eq}`, replacing each one
with 10 individual variants. This makes `ast::TokenKind` more similar to
`rustc_lexer::TokenKind`, which has individual variants for all
operators.

Although the number of lines of code increases, the number of chars
decreases due to the frequent use of shorter names like `token::Plus`
instead of `token::BinOp(BinOpToken::Plus)`.
This commit is contained in:
Nicholas Nethercote 2024-12-20 07:28:16 +11:00
parent 7c4a55c2ac
commit 2a1e2e9632
19 changed files with 352 additions and 309 deletions

View file

@ -1,4 +1,4 @@
use rustc_ast::token::{self, BinOpToken, Delimiter, IdentIsRaw};
use rustc_ast::token::{self, Delimiter, IdentIsRaw};
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast_pretty::pprust::PrintState;
use rustc_ast_pretty::pprust::state::State as Printer;
@ -137,14 +137,9 @@ fn print_tts(printer: &mut Printer<'_>, tts: &TokenStream) {
(Dollar, token::Ident(..)) => (false, DollarIdent),
(DollarIdent, token::Colon) => (false, DollarIdentColon),
(DollarIdentColon, token::Ident(..)) => (false, Other),
(
DollarParen,
token::BinOp(BinOpToken::Plus | BinOpToken::Star) | token::Question,
) => (false, Other),
(DollarParen, token::Plus | token::Star | token::Question) => (false, Other),
(DollarParen, _) => (false, DollarParenSep),
(DollarParenSep, token::BinOp(BinOpToken::Plus | BinOpToken::Star)) => {
(false, Other)
}
(DollarParenSep, token::Plus | token::Star) => (false, Other),
(Pound, token::Not) => (false, PoundBang),
(_, token::Ident(symbol, IdentIsRaw::No))
if !usually_needs_space_between_keyword_and_open_delim(*symbol, tt.span) =>

View file

@ -12,7 +12,7 @@
use std::collections::HashMap;
use std::panic::{AssertUnwindSafe, catch_unwind};
use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
use rustc_ast::token::{Delimiter, Token, TokenKind};
use rustc_ast::tokenstream::{TokenStream, TokenStreamIter, TokenTree};
use rustc_ast::{ast, ptr};
use rustc_ast_pretty::pprust;
@ -841,7 +841,7 @@ impl MacroArgParser {
match tok {
TokenTree::Token(
Token {
kind: TokenKind::BinOp(BinOpToken::Plus),
kind: TokenKind::Plus,
..
},
_,
@ -855,7 +855,7 @@ impl MacroArgParser {
)
| TokenTree::Token(
Token {
kind: TokenKind::BinOp(BinOpToken::Star),
kind: TokenKind::Star,
..
},
_,
@ -1090,12 +1090,30 @@ fn force_space_before(tok: &TokenKind) -> bool {
| TokenKind::OrOr
| TokenKind::Not
| TokenKind::Tilde
| TokenKind::BinOpEq(_)
| TokenKind::PlusEq
| TokenKind::MinusEq
| TokenKind::StarEq
| TokenKind::SlashEq
| TokenKind::PercentEq
| TokenKind::CaretEq
| TokenKind::AndEq
| TokenKind::OrEq
| TokenKind::ShlEq
| TokenKind::ShrEq
| TokenKind::At
| TokenKind::RArrow
| TokenKind::LArrow
| TokenKind::FatArrow
| TokenKind::BinOp(_)
| TokenKind::Plus
| TokenKind::Minus
| TokenKind::Star
| TokenKind::Slash
| TokenKind::Percent
| TokenKind::Caret
| TokenKind::And
| TokenKind::Or
| TokenKind::Shl
| TokenKind::Shr
| TokenKind::Pound
| TokenKind::Dollar => true,
_ => false,
@ -1114,7 +1132,7 @@ fn next_space(tok: &TokenKind) -> SpaceState {
match tok {
TokenKind::Not
| TokenKind::BinOp(BinOpToken::And)
| TokenKind::And
| TokenKind::Tilde
| TokenKind::At
| TokenKind::Comma