|
| 1 | +use bitflags::bitflags; |
| 2 | + |
| 3 | +use ruff_text_size::TextSize; |
| 4 | + |
| 5 | +bitflags! { |
| 6 | + #[derive(Debug)] |
| 7 | + pub(crate) struct FStringContextFlags: u8 { |
| 8 | + /// The current f-string is a triple-quoted f-string i.e., the number of |
| 9 | + /// opening quotes is 3. If this flag is not set, the number of opening |
| 10 | + /// quotes is 1. |
| 11 | + const TRIPLE = 1 << 0; |
| 12 | + |
| 13 | + /// The current f-string is a double-quoted f-string. If this flag is not |
| 14 | + /// set, the current f-string is a single-quoted f-string. |
| 15 | + const DOUBLE = 1 << 1; |
| 16 | + |
| 17 | + /// The current f-string is a raw f-string i.e., prefixed with `r`/`R`. |
| 18 | + /// If this flag is not set, the current f-string is a normal f-string. |
| 19 | + const RAW = 1 << 2; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/// The context representing the current f-string that the lexer is in. |
| 24 | +#[derive(Debug)] |
| 25 | +pub(crate) struct FStringContext { |
| 26 | + flags: FStringContextFlags, |
| 27 | + |
| 28 | + /// The level of nesting for the lexer when it entered the current f-string. |
| 29 | + /// The nesting level includes all kinds of parentheses i.e., round, square, |
| 30 | + /// and curly. |
| 31 | + nesting: u32, |
| 32 | + |
| 33 | + /// The current depth of format spec for the current f-string. This is because |
| 34 | + /// there can be multiple format specs nested for the same f-string. |
| 35 | + /// For example, `{a:{b:{c}}}` has 3 format specs. |
| 36 | + format_spec_depth: u32, |
| 37 | +} |
| 38 | + |
| 39 | +impl FStringContext { |
| 40 | + pub(crate) const fn new(flags: FStringContextFlags, nesting: u32) -> Self { |
| 41 | + Self { |
| 42 | + flags, |
| 43 | + nesting, |
| 44 | + format_spec_depth: 0, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pub(crate) const fn nesting(&self) -> u32 { |
| 49 | + self.nesting |
| 50 | + } |
| 51 | + |
| 52 | + /// Returns the quote character for the current f-string. |
| 53 | + pub(crate) const fn quote_char(&self) -> char { |
| 54 | + if self.flags.contains(FStringContextFlags::DOUBLE) { |
| 55 | + '"' |
| 56 | + } else { |
| 57 | + '\'' |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Returns the number of quotes for the current f-string. |
| 62 | + pub(crate) const fn quote_size(&self) -> TextSize { |
| 63 | + if self.is_triple_quoted() { |
| 64 | + TextSize::new(3) |
| 65 | + } else { |
| 66 | + TextSize::new(1) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Returns the triple quotes for the current f-string if it is a triple-quoted |
| 71 | + /// f-string, `None` otherwise. |
| 72 | + pub(crate) const fn triple_quotes(&self) -> Option<&'static str> { |
| 73 | + if self.is_triple_quoted() { |
| 74 | + if self.flags.contains(FStringContextFlags::DOUBLE) { |
| 75 | + Some(r#"""""#) |
| 76 | + } else { |
| 77 | + Some("'''") |
| 78 | + } |
| 79 | + } else { |
| 80 | + None |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// Returns `true` if the current f-string is a raw f-string. |
| 85 | + pub(crate) const fn is_raw_string(&self) -> bool { |
| 86 | + self.flags.contains(FStringContextFlags::RAW) |
| 87 | + } |
| 88 | + |
| 89 | + /// Returns `true` if the current f-string is a triple-quoted f-string. |
| 90 | + pub(crate) const fn is_triple_quoted(&self) -> bool { |
| 91 | + self.flags.contains(FStringContextFlags::TRIPLE) |
| 92 | + } |
| 93 | + |
| 94 | + /// Calculates the number of open parentheses for the current f-string |
| 95 | + /// based on the current level of nesting for the lexer. |
| 96 | + const fn open_parentheses_count(&self, current_nesting: u32) -> u32 { |
| 97 | + current_nesting.saturating_sub(self.nesting) |
| 98 | + } |
| 99 | + |
| 100 | + /// Returns `true` if the lexer is in a f-string expression i.e., between |
| 101 | + /// two curly braces. |
| 102 | + pub(crate) const fn is_in_expression(&self, current_nesting: u32) -> bool { |
| 103 | + self.open_parentheses_count(current_nesting) > self.format_spec_depth |
| 104 | + } |
| 105 | + |
| 106 | + /// Returns `true` if the lexer is in a f-string format spec i.e., after a colon. |
| 107 | + pub(crate) const fn is_in_format_spec(&self, current_nesting: u32) -> bool { |
| 108 | + self.format_spec_depth > 0 && !self.is_in_expression(current_nesting) |
| 109 | + } |
| 110 | + |
| 111 | + /// Returns `true` if the context is in a valid position to start format spec |
| 112 | + /// i.e., at the same level of nesting as the opening parentheses token. |
| 113 | + /// Increments the format spec depth if it is. |
| 114 | + /// |
| 115 | + /// This assumes that the current character for the lexer is a colon (`:`). |
| 116 | + pub(crate) fn try_start_format_spec(&mut self, current_nesting: u32) -> bool { |
| 117 | + if self |
| 118 | + .open_parentheses_count(current_nesting) |
| 119 | + .saturating_sub(self.format_spec_depth) |
| 120 | + == 1 |
| 121 | + { |
| 122 | + self.format_spec_depth += 1; |
| 123 | + true |
| 124 | + } else { |
| 125 | + false |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /// Decrements the format spec depth unconditionally. |
| 130 | + pub(crate) fn end_format_spec(&mut self) { |
| 131 | + self.format_spec_depth = self.format_spec_depth.saturating_sub(1); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/// The f-strings stack is used to keep track of all the f-strings that the |
| 136 | +/// lexer encounters. This is necessary because f-strings can be nested. |
| 137 | +#[derive(Debug, Default)] |
| 138 | +pub(crate) struct FStrings { |
| 139 | + stack: Vec<FStringContext>, |
| 140 | +} |
| 141 | + |
| 142 | +impl FStrings { |
| 143 | + pub(crate) fn push(&mut self, context: FStringContext) { |
| 144 | + self.stack.push(context); |
| 145 | + } |
| 146 | + |
| 147 | + pub(crate) fn pop(&mut self) -> Option<FStringContext> { |
| 148 | + self.stack.pop() |
| 149 | + } |
| 150 | + |
| 151 | + pub(crate) fn current(&self) -> Option<&FStringContext> { |
| 152 | + self.stack.last() |
| 153 | + } |
| 154 | + |
| 155 | + pub(crate) fn current_mut(&mut self) -> Option<&mut FStringContext> { |
| 156 | + self.stack.last_mut() |
| 157 | + } |
| 158 | +} |
0 commit comments