-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve
print_tts
by changing tokenstream::Spacing
.
`proc_macro::Spacing` only appears on `Punct` tokens, where: - `Joint` means "the next token follows immediately and is a `Punct`. - `Alone` means "the next token doesn't follow immediately *or* it follows immediately and is not a `Punct`. `tokenstream::Spacing` appears on all `TokenTree::Token` instances, both punct and non-punct, where: - `Joint` means "the next token follows immediately and is a punct" (i.e. satisfies `is_op`). - `Alone` means "the next token doesn't follow immediately *or* it follows immediately and is not a punct". The fact that `Alone` is used for two different cases is awkward. This commit replaces `tokenstream::Spacing` with a new type `FollowedBy` that separates those two cases: - `Space` means "the next token doesn't follow immediately" - `Op` means "the next token follows immediately and is a punct". - `Other` means "the next token follows immediately and is not a punct". The mapping from old to new is: - `Joint` -> `Op` - `Alone` -> `Space` or `Other`, depending on the situation. We can use `FollowedBy` to *drastically* improve the output of `print_tts`. For example, this: ``` stringify!(let a: Vec<u32> = vec![];) ``` currently produces this string: ``` let a : Vec < u32 > = vec! [] ; ``` With this PR, it now produces this string: ``` let a: Vec<u32> = vec![] ; ``` (The space after the `]` is because `TokenTree::Delimited` currently doesn't have `FollowedBy` information. Adding this as a follow-up should be straightforward.) The new `print_tts` doesn't replicate original code perfectly. E.g. multiple space characters will be condensed into a single space character. But it's much improved. `print_tts` still produces the old, uglier output for code produced by proc macros. Because we have to translate the generated code from `Spacing` to the more expressive `Followed`, which results in too much `FollowedBy::Space` usage and no `FollowedBy::Other` usage. So `tt_prepend_space` still exists and is used by `print_tts` in conjunction with the `FollowedBy` field. This change will also help with the removal of `Token::Interpolated`. Currently interpolated tokens are pretty-printed nicely via AST pretty printing. `Token::Interpolated` removal will mean they get printed with `print_tts`. Without this change, that would result in much uglier output for code produced by decl macro expansions. With this change, AST pretty printing and `print_tts` produce similar results.
- Loading branch information
1 parent
00e93e1
commit a6942db
Showing
59 changed files
with
490 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.