ruff/crates/ruff_python_parser/src
Charlie Marsh f16e780e0a
Add an implicit concatenation flag to string and bytes constants (#6512)
## Summary

Per the discussion in
https://github.com/astral-sh/ruff/discussions/6183, this PR adds an
`implicit_concatenated` flag to the string and bytes constant variants.
It's not actually _used_ anywhere as of this PR, but it is covered by
the tests.

Specifically, we now use a struct for the string and bytes cases, along
with the `Expr::FString` node. That struct holds the value, plus the
flag:

```rust
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Constant {
    Str(StringConstant),
    Bytes(BytesConstant),
    ...
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StringConstant {
    /// The string value as resolved by the parser (i.e., without quotes, or escape sequences, or
    /// implicit concatenations).
    pub value: String,
    /// Whether the string contains multiple string tokens that were implicitly concatenated.
    pub implicit_concatenated: bool,
}

impl Deref for StringConstant {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        self.value.as_str()
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BytesConstant {
    /// The bytes value as resolved by the parser (i.e., without quotes, or escape sequences, or
    /// implicit concatenations).
    pub value: Vec<u8>,
    /// Whether the string contains multiple string tokens that were implicitly concatenated.
    pub implicit_concatenated: bool,
}

impl Deref for BytesConstant {
    type Target = [u8];
    fn deref(&self) -> &Self::Target {
        self.value.as_slice()
    }
}
```

## Test Plan

`cargo test`
2023-08-14 13:46:54 +00:00
..
lexer Pull in RustPython parser (#6099) 2023-07-27 09:29:11 +00:00
snapshots Add an implicit concatenation flag to string and bytes constants (#6512) 2023-08-14 13:46:54 +00:00
context.rs Remove `Parse` trait (#6235) 2023-08-01 18:35:03 +02:00
function.rs Introduce an `Arguments` AST node for function calls and class definitions (#6259) 2023-08-02 10:01:13 -04:00
lexer.rs Rename `Magic*` to `IpyEscape*` (#6395) 2023-08-09 13:28:18 +00:00
lib.rs Use `Jupyter` mode while parsing Notebook files (#5552) 2023-08-05 00:32:07 +00:00
parser.rs Remove `allow(pedantic)` from formatter (#6549) 2023-08-14 14:02:06 +02:00
python.lalrpop Rename `Magic*` to `IpyEscape*` (#6395) 2023-08-09 13:28:18 +00:00
python.rs Rename `Magic*` to `IpyEscape*` (#6395) 2023-08-09 13:28:18 +00:00
soft_keywords.rs Replace `.map_or(false, $closure)` with `.is_some_and(closure)` (#6244) 2023-08-01 19:29:42 +02:00
string.rs Add an implicit concatenation flag to string and bytes constants (#6512) 2023-08-14 13:46:54 +00:00
token.rs Rename `Magic*` to `IpyEscape*` (#6395) 2023-08-09 13:28:18 +00:00
typing.rs Replace `.map_or(false, $closure)` with `.is_some_and(closure)` (#6244) 2023-08-01 19:29:42 +02:00