|
| 1 | +//! Python's cursor intrinsic scan over quotes, comments and identifier boundaries. |
| 2 | +
|
| 3 | +use sqlbuild_sqltext::sql_scan::main::comment_end::comment_end; |
| 4 | +use sqlbuild_sqltext::sql_scan::main::non_code_end::non_code_end; |
| 5 | +use sqlbuild_sqltext::sql_scan::models::QuotePolicy; |
| 6 | + |
| 7 | +const INTRINSIC_NAMES: [&[u8]; 2] = [b"__cursor_start", b"__cursor_end"]; |
| 8 | +/// Only dollar quotes: Python's own quote scan does not double backticks, so quotes run here. |
| 9 | +const DOLLAR_POLICY: QuotePolicy = QuotePolicy { |
| 10 | + backtick_identifiers: false, |
| 11 | + single_quote_backslash_escapes: false, |
| 12 | + double_quote_backslash_escapes: false, |
| 13 | + dollar_quotes: true, |
| 14 | +}; |
| 15 | + |
| 16 | +/// The next scan offset after `index`, or `Err` where Python finds an intrinsic or raises. |
| 17 | +pub(crate) fn step(bytes: &[u8], index: usize) -> Result<usize, ()> { |
| 18 | + if let Some(end) = comment_end(bytes, index).map_err(|_| ())? { |
| 19 | + return Ok(end); |
| 20 | + } |
| 21 | + match bytes[index] { |
| 22 | + quote @ (b'\'' | b'"' | b'`') => return quoted_end(bytes, index, quote), |
| 23 | + b'$' => { |
| 24 | + return Ok(non_code_end(bytes, index, DOLLAR_POLICY) |
| 25 | + .map_err(|_| ())? |
| 26 | + .unwrap_or(index + 1)); |
| 27 | + } |
| 28 | + _ => {} |
| 29 | + } |
| 30 | + for name in INTRINSIC_NAMES { |
| 31 | + if bytes[index..].starts_with(name) { |
| 32 | + let end: usize = index + name.len(); |
| 33 | + let before: Option<u8> = index.checked_sub(1).map(|previous| bytes[previous]); |
| 34 | + if !continues_identifier(before)? && !continues_identifier(bytes.get(end).copied())? { |
| 35 | + return Err(()); |
| 36 | + } |
| 37 | + return Ok(end); |
| 38 | + } |
| 39 | + } |
| 40 | + Ok(index + 1) |
| 41 | +} |
| 42 | + |
| 43 | +/// Python's quoted text end: `'` and `"` double to escape, backticks do not. |
| 44 | +fn quoted_end(bytes: &[u8], start: usize, quote: u8) -> Result<usize, ()> { |
| 45 | + let mut index: usize = start + 1; |
| 46 | + loop { |
| 47 | + index += bytes[index..] |
| 48 | + .iter() |
| 49 | + .position(|byte| *byte == quote) |
| 50 | + .ok_or(())?; |
| 51 | + if quote != b'`' && bytes.get(index + 1) == Some("e) { |
| 52 | + index += 2; |
| 53 | + continue; |
| 54 | + } |
| 55 | + return Ok(index + 1); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Python's `isalnum() or _`; non-ASCII neighbours defer to Python's Unicode rules. |
| 60 | +fn continues_identifier(byte: Option<u8>) -> Result<bool, ()> { |
| 61 | + match byte { |
| 62 | + None => Ok(false), |
| 63 | + Some(byte) if !byte.is_ascii() => Err(()), |
| 64 | + Some(byte) => Ok(byte.is_ascii_alphanumeric() || byte == b'_'), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Python's `any(name in sql for name in _INTRINSIC_NAMES)`. |
| 69 | +pub(crate) fn contains_intrinsic_name(bytes: &[u8]) -> bool { |
| 70 | + for name in INTRINSIC_NAMES { |
| 71 | + if bytes.windows(name.len()).any(|window| window == name) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + } |
| 75 | + false |
| 76 | +} |
0 commit comments