Files
codebase-memory-mcp/tools/tree-sitter-sql/grammar.js
T
Martin VogelandKerf d9b22e0b7f fix(grammar): stop flagging valid SQLite migrations as partially parsed
Valid SQLite migration files were reported parse_partial because the
vendored SQL grammar (DerekStride/tree-sitter-sql @ 851e9cb257ba) rejects
SQLite syntax they use: PRAGMA, AUTOINCREMENT, a trigger's BEGIN ... END
body, a column named `key` after the first column, WITHOUT ROWID, an
unparenthesized trigger WHEN, INSERT OR IGNORE, and CREATE VIRTUAL TABLE
... USING fts5(...). Each became an ERROR node; the trigger's error region
in atuin's ai_session schema also swallowed the CREATE TABLE after it.

The `key` case is lexical: after a comma inside a column list the grammar
also accepts MySQL's `KEY name (cols)` index, so `key` lexes as that
keyword there and `key TEXT NOT NULL` became a malformed index.

Fix: sql becomes a self-maintained fork of the pin we already vendored,
following the pascal model. tools/tree-sitter-sql/ holds upstream's
grammar.js + grammar/ (blob-identical to the pin and to upstream main @
3c99273f9b32, where its corpus lives) and one patch,
patches/sqlite-dialect.patch, touching grammar/ only:
- PRAGMA [schema.]name [= value | (value)] (new pragma_statement)
- AUTOINCREMENT as keyword_auto_increment, upstream's own change
  (c042cde1e2a6, 2026-11-08, after our pin)
- `key` accepted as a column name; the type keywords that take no
  arguments are accepted as a MySQL index name so `KEY name (name)` stays
  an index
- WITHOUT ROWID
- a trigger body as the existing block node, after an optional
  unparenthesized WHEN (precedence keeps Postgres `WHEN (...) EXECUTE
  FUNCTION` trees unchanged)
- INSERT OR {ABORT | FAIL | IGNORE | REPLACE | ROLLBACK}
- CREATE VIRTUAL TABLE [IF NOT EXISTS] name USING module(args), aliased
  to create_table (new module_argument node), so the table is extracted
  as a Table like any other
Upstream node types keep their shape: upstream's 511 corpus cases pass
unchanged; test/corpus/sqlite.txt adds 11 (9 fail on the unpatched
grammar, 2 controls pass on both). The vendored scanner.c is kept as is
(it carries our start_tag leak fix 0e61867d9, now listed under the
MANIFEST's "Local source patches"; upstream fixed the same leak after our
pin in ee2f84f9b857).

Provenance: without the patch, tree-sitter-cli 0.25.10 regenerates
upstream's shipped parser.c byte-for-byte except line 1 (upstream's
generator stamp has no version), and grammar.json, node-types.json and
tree_sitter/*.h byte-identical. Patched: three generations are
byte-identical (parser.c SHA-256 3c64e5c4...7887); ABI 15; states 30,622
-> 31,081, symbols 770 -> 782; 0 non-ASCII bytes, no string literal with
a space, one comment, the same call-position identifiers as before. Node
types only gain entries (pragma_statement, module_argument, four
keywords; create_table gains a `module` field).

Proof, indexed from scratch with isolated caches (main 6d34ca15 vs this
change): dani-garcia/vaultwarden @ 58a980f8 migrations/sqlite (116
files) parse_partial 3 -> 0, graph identical (303 nodes, 446 edges);
atuinsh/atuin @ 2535161e *.sql (69 files) 19 -> 7: all 12 valid SQLite
files clean; the 7 left are the invalid `ALTER TABLE ... DROP INDEX`
down migration and the 2 MySQL + 4 Postgres files, same ranges as
before; graph +2 Table nodes (the fts5 table and the table the trigger
error swallowed), +2 DEFINES, +3 USAGE lineage edges. No newly flagged
file. Control: helm @ 9b88efd6 indexes to an identical graph (12,251
nodes, 39,561 edges).

Registered like pascal: MANIFEST.md fork rows + notes (ABI tally
recounted, unchanged), the audit FORKS map, THIRD_PARTY.md, vendored
checksums. LICENSE stays upstream's MIT byte-identical.

Tests: parse_coverage carries the two SQLite tests from #2246 as written
(sqlite_valid_migration_not_flagged, red before this change, and the
sqlite_malformed_still_flagged control) and adds `key` columns, WITHOUT
ROWID, a WHEN trigger with INSERT OR IGNORE plus an fts5 table (asserted
as a Table), PRAGMA forms, and two controls: MySQL's KEY index still
clean, and ALTER TABLE ... DROP INDEX still flagged.

Distilled from #2246.

Co-authored-by: Kerf <kerfern@hotmail.com>
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2026-11-11 19:44:47 +01:00

127 lines
4.0 KiB
JavaScript

/*
* cbm self-maintained fork of tree-sitter-sql (SQLite migration syntax, #2246)
*
* Copyright (c) 2021 Derek Stride (tree-sitter-sql, MIT)
* Copyright (c) 2026 DeusData (the "cbm fork (#2246)" hunks in grammar/)
*
* The VENDORED parser ships upstream's MIT LICENSE byte-identical; our copyright
* for the patch lives here, with the source, and in THIRD_PARTY.md.
*
* Fork provenance (pinned; the commit this repo vendored before the fork):
* DerekStride/tree-sitter-sql @ 851e9cb257ba7c66cc8c14214a31c44d2f1e954e
* (v0.3.11; upstream's gh-pages "deploy: 3c99273f9b32" commit, whose grammar
* sources are byte-identical to main @ 3c99273f9b32a00e38a737e8a0dd37397f931916)
* grammar.js -> this file = upstream + this header
* grammar/*.js -> upstream + patches/sqlite-dialect.patch
* src/scanner.c -> upstream + the start_tag leak fix listed under "Local
* source patches" in internal/cbm/vendored/grammars/MANIFEST.md
*
* The patch (and nothing else) differs from upstream. Upstream rejected forms
* that valid SQLite migrations use, so those files became ERROR nodes and were
* flagged parse_partial: PRAGMA; AUTOINCREMENT (upstream's own c042cde1e2a6,
* after our pin); a column named `key` after the first column; WITHOUT ROWID;
* a trigger's BEGIN ... END body and its unparenthesized WHEN; INSERT OR
* <conflict>; CREATE VIRTUAL TABLE ... USING module(...), aliased to
* create_table. Every upstream node keeps its type and shape; the new nodes
* are pragma_statement and module_argument.
* On an upstream bump: re-fetch the pinned grammar.js and grammar/, re-apply the
* patch, regenerate, re-run test/corpus plus upstream's corpus -- and drop each
* hunk upstream has absorbed.
*
* Regenerate with exactly this CLI version (ABI 15, the runtime ceiling; the
* output is pinned by SHA-256 in scripts/vendored-checksums.txt):
* cd tools/tree-sitter-sql
* npx tree-sitter-cli@0.25.10 generate
* npx tree-sitter-cli@0.25.10 test
* cp src/parser.c ../../internal/cbm/vendored/grammars/sql/parser.c
*/
import keyword_rules from "./grammar/keywords.js";
import type_rules from "./grammar/types.js";
import column_list_rules from "./grammar/column-lists.js";
import expression_rules from "./grammar/expressions.js";
import transaction_rules from "./grammar/transactions.js";
import statement_rules from "./grammar/statements/index.js";
export default grammar({
name: 'sql',
extras: $ => [
/\s\n/,
/\s/,
$.comment,
$.marginalia,
],
externals: $ => [
$._dollar_quoted_string_start_tag,
$._dollar_quoted_string_end_tag,
$._dollar_quoted_string,
],
conflicts: $ => [
[$.object_reference, $._qualified_field],
[$.field, $._qualified_field],
[$._column, $._qualified_field],
[$.object_reference],
[$.between_expression, $.binary_expression],
[$.time],
[$.timestamp],
],
precedences: $ => [
[
'binary_is',
'unary_not',
'binary_exp',
'binary_times',
'binary_plus',
'unary_other',
'binary_other',
'binary_in',
'binary_compare',
'binary_relation',
'pattern_matching',
'between',
'clause_connective',
'clause_disjunctive',
],
],
word: $ => $._identifier,
rules: {
program: $ => seq(
// any number of transactions, statements, or blocks with a terminating ;
repeat(
seq(
choice(
$.transaction,
$.statement,
$.block,
),
';',
),
),
// optionally, a single statement without a terminating ;
optional(
$.statement,
),
),
comment: _ => /--.*/,
// https://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment
marginalia: _ => /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//,
...keyword_rules,
...type_rules,
...column_list_rules,
...expression_rules,
...transaction_rules,
...statement_rules,
}
});