Skip to content

fix: stop stripping backslashes from BQL string literals - #1798

Merged
robcohen merged 3 commits into
mainfrom
fix/bql-string-literal-escapes
Jul 16, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/bql-string-literal-escapes

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

subst('[ ,]*\)$', ...) failed with "unopened group" because the BQL string-literal parser treated \ as a C-style escape introducer and dropped it (just('\\').ignore_then(any())) — the regex engine received [ ,]*)$. Reported with a real-world query template from the mkshp Obsidian plugin that works against beanquery 0.2.0.

The upstream contract

beanquery's grammar is quote-stripping ONLY:

string = /\"[^\"]*\"|\'(?:[^\']|\'\')*\'/     (bql.ebnf)
def string(value): return value[1:-1]           (parser semantics)

A backslash is an ordinary byte. The only structural escape is SQL-style '' in single-quoted strings — which continues the string but stays verbatim in the value (upstream's own parser_test.py pins 'rainy''day' → rainy''day). Double-quoted strings have no escapes at all.

Changes

  • string_literal() reimplements exactly that grammar, with the upstream rules quoted in its doc comment.
  • The paren-nesting pre-scan (nesting_exceeds_limit) lexed strings with the OLD backslash rule — its lexing must agree with the parser's (canonical-discipline drift rule), so it now uses the same semantics, and a drift-guard test pins the historically-breaking shape (a lone trailing backslash used to make the scan swallow the closing quote and desynchronize its paren count).
  • The completions tokenizer already terminated "..." at the first quote, so it becomes more consistent, not less.

Compatibility

Strictly compat-improving: no existing test relied on the invented escapes (full query + LSP suites pass unchanged), and \"-style escapes never worked in beanquery either, so no real query used them.

Tests

The issue's minimal repro; the full 3-level Obsidian template; backslash-as-literal in both quote styles including lone trailing backslash; the upstream '' verbatim pin; scan/parser lexing agreement.

Closes #1797

🤖 Generated with Claude Code

The chumsky string-literal parser treated backslash as a C-style
escape introducer and DROPPED it (`just('\\').ignore_then(any())`),
so subst('[ ,]*\)$', ...) reached the regex engine as `[ ,]*)$` —
"unopened group" on a regex that works in beanquery.

Upstream's grammar is quote-stripping only:

    string = /\"[^\"]*\"|\'(?:[^\']|\'\')*\'/     (bql.ebnf)
    def string(value): return value[1:-1]         (semantics)

A backslash is an ordinary byte; the only structural escape is the
SQL-style doubled '' in single-quoted strings, which continues the
string but stays VERBATIM in the value (pinned by upstream's own
parser_test.py: 'rainy''day' -> rainy''day). Both string_literal()
and the paren-nesting pre-scan (whose string lexing must agree, per
the canonical-discipline drift rule) now implement exactly that.

No existing test relied on the invented escapes (full query + LSP
suites pass unchanged). New regression tests: the issue's minimal
repro, the full real-world Obsidian-plugin template, backslash-as-
literal in both quote styles including a lone trailing backslash,
the upstream '' pin, and a scan/parser lexing agreement guard.

Closes #1797

Co-Authored-By: Claude Fable 5 <[email protected]>
@github-actions

github-actions Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 29.8ms ± 0.7ms 23.6 MB
beancount 640.5ms ± 9.5ms 50.7 MB
Comparison 21.5x faster 2.1x less

vs main branch (2026-07-15)

Metric main this PR Change
Time 32.8ms 29.8ms -9.1% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes BQL string literal parsing in rustledger-query to match upstream beanquery semantics (quote stripping only): backslashes are treated as ordinary bytes, and single-quoted strings support SQL-style '' continuation without interpreting it.

Changes:

  • Reimplemented string_literal() to stop consuming backslashes as escapes and to preserve '' verbatim in single-quoted strings.
  • Updated the parenthesis nesting pre-scan (nesting_exceeds_limit) to lex string literals consistently with the parser (avoiding scan/parser drift).
  • Added regression tests covering the reported subst() regex case, real-world nested templates, and scan/parser agreement.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
crates/rustledger-query/src/parser.rs Aligns parser + nesting pre-scan with upstream beanquery string-literal semantics (no backslash escapes; '' continues single-quoted strings).
crates/rustledger-query/tests/string_literal_escapes_test.rs Adds targeted regressions for #1797 and drift-guard coverage for scan/parser agreement.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rustledger-query/src/parser.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen and others added 2 commits July 16, 2026 03:07
Review comment: the ''-continuation parser allocated a String per
character. The value is verbatim anyway, so match the body with
ignored() pieces and take one to_slice() of the input.

Co-Authored-By: Claude Fable 5 <[email protected]>
The review caught the drift guard being decoration: the pre-scan's
only observable is the MAX_NESTING_DEPTH rejection, and the guard
inputs never approached it — the OLD divergent scanner passed all
three asserts. Replaced with straddling guards: 150 real parens
after a trailing-backslash string must hit the nesting cap (a
scanner regressing to backslash-escaping swallows them — and on
wasm32 the pre-scan is the only stack protection), and 150 parens
inside a ''-continued string must NOT (a scanner ending the string
at the first quote falsely rejects the query).

Also from the review: pins for the quote-run edge family of the
upstream regex ('' empty, four-quotes, trailing '' before the
close, three-quotes rejected); tokenize_bql documented as a
DELIBERATELY divergent heuristic tokenizer with cross-references at
both sites; the double-quoted arm uses to_slice like the
single-quoted one.

Co-Authored-By: Claude Fable 5 <[email protected]>
@codecov

codecov Bot commented Jul 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 1e524de into main Jul 16, 2026
50 checks passed
@robcohen
robcohen deleted the fix/bql-string-literal-escapes branch July 16, 2026 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: subst() silently strips backslash-escapes from regex string literals, breaking valid regexes

2 participants