@willbooster/tree-sitter-sql 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +159 -2
- package/common/common.mjs +52 -0
- package/package.json +71 -4
- package/sqlite/grammar.js +1266 -0
- package/sqlite/package.json +4 -0
- package/sqlite/src/node-types.json +7639 -0
- package/tree-sitter-sqlite.wasm +0 -0
- package/tree-sitter.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 WillBooster Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,160 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @willbooster/tree-sitter-sql
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-sql)
|
|
4
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-sql)
|
|
5
|
+
[](https://github.com/WillBooster/tree-sitter-sql/actions/workflows/test.yml)
|
|
6
|
+
[](https://github.com/WillBooster/tree-sitter-sql/actions/workflows/test-rust.yml)
|
|
7
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
8
|
+
[](https://crates.io/crates/willbooster-tree-sitter-sql)
|
|
9
|
+
|
|
10
|
+
SQL grammars for [tree-sitter](https://github.com/tree-sitter/tree-sitter), generated with the
|
|
11
|
+
[WillBooster/tree-sitter](https://github.com/WillBooster/tree-sitter) generator. The parsers carry that fork's own ABI
|
|
12
|
+
number and load with that fork's runtime, version 17.0.1 or later in the 17.x series.
|
|
13
|
+
|
|
14
|
+
The `sqlite` grammar parses the SQL dialect of [SQLite](https://sqlite.org/lang.html) 3.54 in its default build. Its
|
|
15
|
+
rules follow SQLite's own grammar (`src/parse.y`) and tokenizer (`src/tokenize.c`), which are in the public domain:
|
|
16
|
+
input that they reject, which SQLite reports as a syntax error, an unrecognized token, or incomplete input, yields
|
|
17
|
+
`ERROR` or `MISSING` nodes, except for
|
|
18
|
+
|
|
19
|
+
- `OVER` as an alias without `AS` before a WINDOW clause after a closing parenthesis other than a function call's, such
|
|
20
|
+
as `SELECT (1) OVER WINDOW w AS ()`, which SQLite's tokenizer reads as a keyword by the token before it;
|
|
21
|
+
- a NUL character, which ends the input for SQLite but is an error here;
|
|
22
|
+
- input that is not valid UTF-8 or UTF-16, which SQLite reads byte by byte.
|
|
23
|
+
|
|
24
|
+
SQLite's parser also reports errors of other kinds while it builds a statement, from comparing names (a trigger's
|
|
25
|
+
schema with its table's), combining constraints (DEFAULT on a generated column, AUTOINCREMENT on a WITHOUT ROWID table),
|
|
26
|
+
and looking up the schema or the limits of the connection. The grammar rejects some of them where they shape a single
|
|
27
|
+
clause, such as an unknown join type, an ORDER BY clause before UNION, NULLS FIRST in an index, or a qualified table name
|
|
28
|
+
in a trigger outside the temp schema, but it accepts the others.
|
|
29
|
+
|
|
30
|
+
`common/common.mjs` holds the lexical rules that SQL dialects share, for the grammars of other dialects to build on.
|
|
31
|
+
|
|
32
|
+
## Support Scope
|
|
33
|
+
|
|
34
|
+
This repository maintains only full parsing: valid input yields a correct tree, and invalid input yields `ERROR` or
|
|
35
|
+
`MISSING` nodes without crashing, hanging, or taking more than linear time. No queries (highlights, locals, injections,
|
|
36
|
+
tags) are shipped. Node types, field names, and the trees that error recovery produces may change; such changes are
|
|
37
|
+
released as new major versions.
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
The npm package ships `tree-sitter-sqlite.wasm` for
|
|
42
|
+
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 17.0.1 or later in
|
|
43
|
+
the 17.x series, which runs in Node.js, Bun, browsers, and Cloudflare Workers. In Node.js and Bun, load the grammar from
|
|
44
|
+
its path:
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
import { fileURLToPath } from 'node:url';
|
|
48
|
+
|
|
49
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
50
|
+
|
|
51
|
+
await Parser.init();
|
|
52
|
+
const parser = new Parser();
|
|
53
|
+
const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-sql/tree-sitter-sqlite.wasm'));
|
|
54
|
+
parser.setLanguage(await Language.load(wasmPath));
|
|
55
|
+
const tree = parser.parse('SELECT a FROM t WHERE b = 1;\n');
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
In browsers, serve both `.wasm` files and load them by URL (the example uses Vite's `?url` imports):
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import sqliteUrl from '@willbooster/tree-sitter-sql/tree-sitter-sqlite.wasm?url';
|
|
62
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
63
|
+
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
|
|
64
|
+
|
|
65
|
+
await Parser.init({ locateFile: () => runtimeUrl });
|
|
66
|
+
const parser = new Parser();
|
|
67
|
+
parser.setLanguage(await Language.load(sqliteUrl));
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Cloudflare Workers do not allow compiling Wasm at run time, so import both `.wasm` files as modules, with or without
|
|
71
|
+
Node.js compatibility:
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
import sqlite from '@willbooster/tree-sitter-sql/tree-sitter-sqlite.wasm';
|
|
75
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
76
|
+
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
|
|
77
|
+
|
|
78
|
+
await Parser.init({ wasmModule: runtime });
|
|
79
|
+
const parser = new Parser();
|
|
80
|
+
parser.setLanguage(await Language.load(sqlite));
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The package also ships the grammar definitions (`common/common.mjs` and `sqlite/grammar.js`, with the `package.json`
|
|
84
|
+
file that marks the latter as an ES module) and the node types in `sqlite/src/node-types.json`.
|
|
85
|
+
|
|
86
|
+
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-sql) and on
|
|
87
|
+
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
|
|
88
|
+
fuzzed with:
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
cargo add willbooster-tree-sitter@17.0.1 --rename tree-sitter
|
|
92
|
+
cargo add willbooster-tree-sitter-sql --rename tree-sitter-sql
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```rust
|
|
96
|
+
let mut parser = tree_sitter::Parser::new();
|
|
97
|
+
parser.set_language(&tree_sitter_sql::LANGUAGE_SQLITE.into())?;
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The crate also exports `SQLITE_NODE_TYPES`.
|
|
101
|
+
|
|
102
|
+
## Development
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
mise install
|
|
106
|
+
bun install --frozen-lockfile
|
|
107
|
+
bun run test/ci-setup # installs Chromium for the browser test
|
|
108
|
+
bun run build/ci
|
|
109
|
+
bun run test
|
|
110
|
+
script/parse-examples
|
|
111
|
+
cargo test
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
|
|
115
|
+
WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
|
|
116
|
+
not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
|
|
117
|
+
with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary
|
|
118
|
+
that runs here. Run other CLI commands through it as well (e.g. `script/tree-sitter parse --grammar-path sqlite
|
|
119
|
+
file.sql`).
|
|
120
|
+
|
|
121
|
+
`bun run generate` records a fresh generation profile from the `test/corpus` cases, then generates compact parser
|
|
122
|
+
tables. It would also profile files tracked in `examples/`, but this repository tracks none. The parser also embeds
|
|
123
|
+
metadata from `tree-sitter.json`. After changing a grammar, the scanner, `tree-sitter.json`, or a corpus case,
|
|
124
|
+
regenerate and commit `sqlite/src/`. Profiles in `.tmp/generation-profiles/` are temporary and must not be committed.
|
|
125
|
+
`bun run build-wasm`, `bun run build/ci`, and the release build regenerate the parser before compiling it.
|
|
126
|
+
|
|
127
|
+
`bun run test` runs:
|
|
128
|
+
|
|
129
|
+
- the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
|
|
130
|
+
- a check that the real-world files in `examples/` fail to parse exactly as listed in `script/known-failures.txt`. The
|
|
131
|
+
first run clones the repositories. The example repositories are pinned to commits in `script/parse-examples`. After a
|
|
132
|
+
grammar change or a moved pin alters that list, `script/parse-examples` rewrites it; review its diff before
|
|
133
|
+
committing. Each listed file is also rejected by SQLite;
|
|
134
|
+
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear time,
|
|
135
|
+
since consumers parse files while they are being edited, and that the scanner reads the input a bounded number of
|
|
136
|
+
times. It loads the Wasm build through @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after
|
|
137
|
+
regenerating the parser;
|
|
138
|
+
- a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
|
|
139
|
+
`willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
|
|
140
|
+
tests, the fuzzer, and the CLI on the latter;
|
|
141
|
+
- tests that load the Wasm build with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
|
|
142
|
+
Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
|
|
143
|
+
|
|
144
|
+
The tests compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared by every checkout;
|
|
145
|
+
`script/parse-examples` builds a parser of its own in `.tmp/` for each run and deletes it afterwards.
|
|
146
|
+
|
|
147
|
+
CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parser and the scanner natively, and
|
|
148
|
+
fuzzes the parser with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
|
|
149
|
+
|
|
150
|
+
When changing the grammar, compare what it accepts with SQLite itself: a valid statement must parse without errors, and
|
|
151
|
+
a statement that SQLite rejects with a syntax error must yield `ERROR` or `MISSING` nodes. SQL from SQLite's test suite
|
|
152
|
+
(`test/*.test`), and that SQL with tokens deleted, inserted, replaced, or swapped, make good inputs for the comparison.
|
|
153
|
+
To check the grouping of operators, evaluate random constant expressions in SQLite both as written and with every node of
|
|
154
|
+
their tree enclosed in parentheses.
|
|
155
|
+
|
|
156
|
+
### References
|
|
157
|
+
|
|
158
|
+
- [SQL As Understood By SQLite](https://sqlite.org/lang.html)
|
|
159
|
+
- [SQLite's grammar (`src/parse.y`)](https://sqlite.org/src/file/src/parse.y) and
|
|
160
|
+
[tokenizer (`src/tokenize.c`)](https://sqlite.org/src/file/src/tokenize.c)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/* oxlint-disable no-control-regex -- Tokens exclude NUL characters, which end the input of C parsers. */
|
|
3
|
+
|
|
4
|
+
/** @import {RuleOrLiteral} from '@willbooster/web-tree-sitter/dsl' */
|
|
5
|
+
|
|
6
|
+
// The CLI injects the DSL before loading the grammar that imports this module.
|
|
7
|
+
const { alias, optional, repeat, seq } = /** @type {typeof globalThis & import('@willbooster/web-tree-sitter/dsl').DSL} */ (
|
|
8
|
+
globalThis
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
// SQL keywords are ASCII words that match without regard to case. A pattern with the `i` flag would also match the
|
|
12
|
+
// Unicode characters that fold to an ASCII letter (e.g. U+212A KELVIN SIGN for `k`), which SQL dialects read as an
|
|
13
|
+
// identifier, so each letter lists its two ASCII cases instead.
|
|
14
|
+
/** @param {string} word */
|
|
15
|
+
export const keywordPattern = (word) =>
|
|
16
|
+
new RegExp(word.replaceAll(/[A-Za-z]/g, (letter) => `[${letter.toUpperCase()}${letter.toLowerCase()}]`));
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A keyword token, which appears in the tree as an anonymous node named after the keyword in upper case.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} word
|
|
22
|
+
*/
|
|
23
|
+
export const keyword = (word) => alias(keywordPattern(word), word.toUpperCase());
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A sequence of keywords separated by spaces in `phrase`, such as `'PRIMARY KEY'`.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} phrase
|
|
29
|
+
*/
|
|
30
|
+
export const keywords = (phrase) => seq(...phrase.split(' ').map(keyword));
|
|
31
|
+
|
|
32
|
+
/** @param {RuleOrLiteral} rule */
|
|
33
|
+
export const commaSep1 = (rule) => seq(rule, repeat(seq(',', rule)));
|
|
34
|
+
|
|
35
|
+
/** @param {RuleOrLiteral} rule */
|
|
36
|
+
export const commaSep = (rule) => optional(commaSep1(rule));
|
|
37
|
+
|
|
38
|
+
// The tokens below contain no NUL character, which ends the input of SQL parsers written in C, such as SQLite's, and
|
|
39
|
+
// which is therefore an error wherever it appears.
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A literal enclosed in `quote`, in which two consecutive quotes stand for one.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} quote
|
|
45
|
+
*/
|
|
46
|
+
export const quoted = (quote) => new RegExp(`${quote}([^${quote}\\x00]|${quote}${quote})*${quote}`);
|
|
47
|
+
|
|
48
|
+
/** A line comment from `--` to the end of the line. */
|
|
49
|
+
export const lineComment = seq('--', /[^\n\u0000]*/);
|
|
50
|
+
|
|
51
|
+
/** A block comment closed by the first `*\/`. */
|
|
52
|
+
export const blockComment = /\/\*[^*\u0000]*\*+([^/*\u0000][^*\u0000]*\*+)*\//;
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-sql",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "SQL grammars (SQLite) for tree-sitter",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"incremental",
|
|
7
|
+
"parsing",
|
|
8
|
+
"sql",
|
|
9
|
+
"sqlite",
|
|
10
|
+
"tree-sitter"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "WillBooster Inc.",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/WillBooster/tree-sitter-sql.git"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"common/common.mjs",
|
|
20
|
+
"sqlite/grammar.js",
|
|
21
|
+
"sqlite/package.json",
|
|
22
|
+
"sqlite/src/node-types.json",
|
|
23
|
+
"tree-sitter.json",
|
|
24
|
+
"*.wasm"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"registry": "https://registry.npmjs.org/"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build-wasm": "script/generate --build-wasm",
|
|
32
|
+
"build/ci": "bun run build-wasm",
|
|
33
|
+
"cleanup": "bun wb lint --fix --format",
|
|
34
|
+
"format": "bun wb lint --format",
|
|
35
|
+
"generate": "script/generate",
|
|
36
|
+
"lint": "bun wb lint",
|
|
37
|
+
"lint-fix": "bun wb lint --fix",
|
|
38
|
+
"prepare": "(bun lefthook install || true) && (test ! -f .lefthook/normalize-bun-lockfile.sh || bash .lefthook/normalize-bun-lockfile.sh)",
|
|
39
|
+
"release": "wb release",
|
|
40
|
+
"prestart": "bun run build-wasm",
|
|
41
|
+
"start": "script/tree-sitter playground",
|
|
42
|
+
"test": "bun wb test",
|
|
43
|
+
"test/ci": "bun wb test-on-ci",
|
|
44
|
+
"test/ci-setup": "playwright install --with-deps chromium",
|
|
45
|
+
"typecheck": "bun wb typecheck",
|
|
46
|
+
"verify": "bun wb verify",
|
|
47
|
+
"verify-full": "bun wb verify --full"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@tsconfig/bun": "1.0.11",
|
|
51
|
+
"@types/bun": "1.4.4",
|
|
52
|
+
"@vitest/browser-playwright": "5.0.5",
|
|
53
|
+
"@willbooster/oxlint-config": "1.4.9",
|
|
54
|
+
"@willbooster/wb": "23.3.0",
|
|
55
|
+
"@willbooster/web-tree-sitter": "17.0.1",
|
|
56
|
+
"conventional-changelog-conventionalcommits": "10.4.1",
|
|
57
|
+
"lefthook": "2.2.2",
|
|
58
|
+
"oxlint": "1.90.0",
|
|
59
|
+
"oxlint-tsgolint": "7.0.2005",
|
|
60
|
+
"playwright": "1.64.1",
|
|
61
|
+
"semantic-release": "25.0.9",
|
|
62
|
+
"vitest": "5.0.5",
|
|
63
|
+
"wrangler": "4.156.0"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"@willbooster/web-tree-sitter": "^17.0.1"
|
|
67
|
+
},
|
|
68
|
+
"peerDependenciesMeta": {
|
|
69
|
+
"@willbooster/web-tree-sitter": {
|
|
70
|
+
"optional": true
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|