Start tracking quoting style in the AST (#10298)

This PR modifies our AST so that nodes for string literals, bytes literals and f-strings all retain the following information:
- The quoting style used (double or single quotes)
- Whether the string is triple-quoted or not
- Whether the string is raw or not

This PR is a followup to #10256. Like with that PR, this PR does not, in itself, fix any bugs. However, it means that we will have the necessary information to preserve quoting style and rawness of strings in the `ExprGenerator` in a followup PR, which will allow us to provide a fix for https://github.com/astral-sh/ruff/issues/7799.

The information is recorded on the AST nodes using a bitflag field on each node, similarly to how we recorded the information on `Tok::String`, `Tok::FStringStart` and `Tok::FStringMiddle` tokens in #10298. Rather than reusing the bitflag I used for the tokens, however, I decided to create a custom bitflag for each AST node.

Using different bitflags for each node allows us to make invalid states unrepresentable: it is valid to set a `u` prefix on a string literal, but not on a bytes literal or an f-string. It also allows us to have better debug representations for each AST node modified in this PR.
This commit is contained in:
Alex Waygood 2024-03-08 19:11:47 +00:00 committed by GitHub
parent 965adbed4b
commit 1d97f27335
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
81 changed files with 4733 additions and 3756 deletions

View file

@ -1267,7 +1267,7 @@ impl<'a> Generator<'a> {
}
fn unparse_string_literal(&mut self, string_literal: &ast::StringLiteral) {
if string_literal.unicode {
if string_literal.flags.is_u_string() {
self.p("u");
}
self.p_str_repr(&string_literal.value);