Blank comment and string-literal spans, preserving every offset. One output character per input character: non-newline characters inside a blanked span become spaces and newlines are kept, so positions and line numbers computed against the masked text are valid against the original.
(text: str)
| 116 | |
| 117 | |
| 118 | def _scan_sql(text: str) -> tuple[str, list[tuple[int, int]]]: |
| 119 | """Blank comment and string-literal spans, preserving every offset. |
| 120 | |
| 121 | One output character per input character: non-newline characters inside |
| 122 | a blanked span become spaces and newlines are kept, so positions and |
| 123 | line numbers computed against the masked text are valid against the |
| 124 | original. Double-quoted and bracket-delimited identifiers are preserved |
| 125 | verbatim (they carry recoverable routine names); single-quoted strings, |
| 126 | line comments, and (nesting-aware) block comments are blanked. Used by |
| 127 | the routine-recovery scan so CREATE PROCEDURE/FUNCTION DDL reachable |
| 128 | only through a comment or a single-quoted string cannot fabricate a |
| 129 | routine node when an unrelated parse error arms recovery. |
| 130 | |
| 131 | Returns (masked_text, ident_spans) where ident_spans holds the [start, |
| 132 | end) of every PRESERVED delimited identifier: preserved spans keep their |
| 133 | text verbatim, so DDL keywords inside one are still visible in the |
| 134 | masked text, and the recovery scan must skip a match that starts there |
| 135 | (identifier data, not DDL — 'SELECT 1 AS [CREATE PROCEDURE x pending]' |
| 136 | must not mint a routine). |
| 137 | """ |
| 138 | ident_spans: list[tuple[int, int]] = [] |
| 139 | out: list[str] = [] |
| 140 | i, n = 0, len(text) |
| 141 | |
| 142 | def _blank(upto: int) -> int: |
| 143 | """Blank [i, upto), keeping newlines; return upto.""" |
| 144 | for c in text[i:upto]: |
| 145 | out.append("\n" if c == "\n" else " ") |
| 146 | return upto |
| 147 | |
| 148 | def _blank_tail_and_carry(start: int) -> int: |
| 149 | """Blank from start to end-of-line, carrying comment state forward. |
| 150 | |
| 151 | The union-of-readings blank for an ambiguous stretch: the rest of the |
| 152 | line is blanked outright; if the raw text of that stretch leaves a /* |
| 153 | unclosed on its own line (the maximum comment depth any reading could |
| 154 | be left holding), blanking continues, nesting-aware, to the closing |
| 155 | */ or EOF. Where a carry closes MID-line the same rule applies to the |
| 156 | remainder of that line — under the reading where the carry never |
| 157 | opened, that whole line may be a comment or a string, so emitting the |
| 158 | post-*/ text verbatim exposed it (found by differential fuzzing). |
| 159 | Repeats until a line ends with no carry pending. Appends one output |
| 160 | character per input character; returns the resume index. |
| 161 | """ |
| 162 | k = start |
| 163 | while True: |
| 164 | eol = text.find("\n", k) |
| 165 | eol = n if eol == -1 else eol |
| 166 | depth = 0 |
| 167 | m2 = k |
| 168 | while m2 < eol: |
| 169 | if text.startswith("/*", m2): |
| 170 | depth += 1 |
| 171 | m2 += 2 |
| 172 | elif text.startswith("*/", m2): |
| 173 | if depth: |
| 174 | depth -= 1 |
| 175 | m2 += 2 |
no test coverage detected