@willbooster/tree-sitter-sql 3.0.5 → 5.0.0
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/README.md +53 -77
- package/common/common.mjs +0 -17
- package/mysql/src/node-types.json +8500 -0
- package/oracle/src/node-types.json +11702 -0
- package/package.json +11 -6
- package/postgresql/src/node-types.json +10087 -0
- package/sqlite/grammar.js +14 -10
- package/tree-sitter-mysql.wasm +0 -0
- package/tree-sitter-oracle.wasm +0 -0
- package/tree-sitter-postgresql.wasm +0 -0
- package/tree-sitter-sqlite.wasm +0 -0
- package/tree-sitter.json +23 -2
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://github.com/WillBooster/tree-sitter-sql/actions/workflows/test.yml)
|
|
6
6
|
[](https://github.com/WillBooster/tree-sitter-sql/actions/workflows/test-rust.yml)
|
|
7
7
|
[](https://github.com/semantic-release/semantic-release)
|
|
8
|
-
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
9
9
|
[](https://crates.io/crates/willbooster-tree-sitter-sql)
|
|
10
10
|
|
|
11
11
|
SQL grammars for [tree-sitter](https://github.com/tree-sitter/tree-sitter), generated with the
|
|
@@ -13,37 +13,47 @@ SQL grammars for [tree-sitter](https://github.com/tree-sitter/tree-sitter), gene
|
|
|
13
13
|
the WillBooster ABI (`0x5742000d`), which upstream `tree-sitter` runtimes and WillBooster runtimes prior to 22.0.0
|
|
14
14
|
reject. The supported WillBooster runtime is version 23.0.0 or later in the 23.x series.
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
rules follow SQLite's own grammar (`src/parse.y`) and tokenizer (`src/tokenize.c`), which are in the public domain:
|
|
18
|
-
input that they reject, which SQLite reports as a syntax error, an unrecognized token, or incomplete input, yields
|
|
19
|
-
`ERROR` or `MISSING` nodes, except for
|
|
16
|
+
## Dialects
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
- a NUL character, which ends the input for SQLite but is an error here;
|
|
24
|
-
- input that is not valid UTF-8 or UTF-16, which SQLite reads byte by byte.
|
|
18
|
+
Each dialect has its own grammar and, except MySQL, an external scanner in `<dialect>/`, built independently on top of
|
|
19
|
+
the small DSL helpers in `common/common.mjs`:
|
|
25
20
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
21
|
+
| Dialect | Directory | Wasm file | Rust constants | Cargo feature |
|
|
22
|
+
| -------------------------------------------------------------------------------------------- | ------------- | ----------------------------- | ---------------------------------------------- | ------------- |
|
|
23
|
+
| [SQLite](https://sqlite.org/lang.html) 3.54 | `sqlite/` | `tree-sitter-sqlite.wasm` | `LANGUAGE_SQLITE`, `SQLITE_NODE_TYPES` | `sqlite` |
|
|
24
|
+
| [PostgreSQL](https://www.postgresql.org/docs/18/sql-syntax.html) 18 | `postgresql/` | `tree-sitter-postgresql.wasm` | `LANGUAGE_POSTGRESQL`, `POSTGRESQL_NODE_TYPES` | `postgresql` |
|
|
25
|
+
| [MySQL](https://dev.mysql.com/doc/refman/8.4/en/sql-statements.html) 8.4 | `mysql/` | `tree-sitter-mysql.wasm` | `LANGUAGE_MYSQL`, `MYSQL_NODE_TYPES` | `mysql` |
|
|
26
|
+
| [Oracle Database](https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/) 23ai | `oracle/` | `tree-sitter-oracle.wasm` | `LANGUAGE_ORACLE`, `ORACLE_NODE_TYPES` | `oracle` |
|
|
31
27
|
|
|
32
|
-
`
|
|
28
|
+
`sqlite` follows SQLite's parser (`src/parse.y`) and tokenizer (`src/tokenize.c`) in its default build, except for
|
|
29
|
+
`OVER` as an alias without `AS` before `WINDOW` after a non-call closing parenthesis (such as
|
|
30
|
+
`SELECT (1) OVER WINDOW w AS ()`), a NUL character (which ends input for SQLite's C parser), and invalid UTF-8/UTF-16.
|
|
31
|
+
It rejects what SQLite's parser grammar and tokenizer reject, plus single-clause shape checks; semantic, compile-time,
|
|
32
|
+
scope, label, numeric-range, limit, catalog, and cross-clause errors are accepted without `ERROR` nodes.
|
|
33
|
+
|
|
34
|
+
The other three grammars favor size and speed over completeness: they target the SQL found in application code and
|
|
35
|
+
schema scripts, give structured trees to its common statements, and parse most other statements as a
|
|
36
|
+
`generic_statement` of words, literals, operators, and parenthesized groups up to the next statement terminator.
|
|
37
|
+
Rarely written clauses inside structured statements yield `ERROR` nodes, and some invalid input parses without them.
|
|
33
38
|
|
|
34
39
|
## Support Scope
|
|
35
40
|
|
|
36
|
-
This repository maintains only full parsing
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
This repository maintains only full parsing, optimized for binary size and parse speed: valid code as found in
|
|
42
|
+
real-world projects yields a correct tree, other valid code yields a correct tree or one with `ERROR` or `MISSING`
|
|
43
|
+
nodes, and any input parses without crashing, hanging, or taking more than linear time. Invalid input may parse without
|
|
44
|
+
`ERROR` or `MISSING` nodes. Incremental parsing is not supported, and no queries (highlights, locals, injections, tags)
|
|
45
|
+
are shipped. Node types, field names, and the trees that error recovery produces may change; such changes are released
|
|
46
|
+
as new major versions.
|
|
40
47
|
|
|
41
48
|
## Usage
|
|
42
49
|
|
|
43
|
-
The npm package ships
|
|
50
|
+
The npm package ships one `.wasm` file per dialect for
|
|
44
51
|
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 23.0.0 or later in
|
|
45
|
-
the 23.x series, which runs in Node.js, Bun, browsers, and Cloudflare Workers.
|
|
46
|
-
|
|
52
|
+
the 23.x series, which runs in Node.js, Bun, browsers, and Cloudflare Workers. Replace `tree-sitter-sqlite.wasm` in the
|
|
53
|
+
examples below with `tree-sitter-postgresql.wasm`, `tree-sitter-mysql.wasm`, or `tree-sitter-oracle.wasm` for another
|
|
54
|
+
dialect.
|
|
55
|
+
|
|
56
|
+
In Node.js and Bun, load the grammar from its path:
|
|
47
57
|
|
|
48
58
|
```js
|
|
49
59
|
import { fileURLToPath } from 'node:url';
|
|
@@ -82,12 +92,11 @@ const parser = new Parser();
|
|
|
82
92
|
parser.setLanguage(await Language.load(sqlite));
|
|
83
93
|
```
|
|
84
94
|
|
|
85
|
-
The package also ships
|
|
86
|
-
|
|
95
|
+
The package also ships each dialect's `src/node-types.json`, `sqlite/grammar.js` and `sqlite/package.json`,
|
|
96
|
+
`common/common.mjs`, and `tree-sitter.json`.
|
|
87
97
|
|
|
88
|
-
In Rust, depend on
|
|
89
|
-
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter) version 23.0.0 or later in the 23.x series
|
|
90
|
-
`Cargo.lock` records the version this package is tested and fuzzed with:
|
|
98
|
+
In Rust, depend on [willbooster-tree-sitter-sql](https://crates.io/crates/willbooster-tree-sitter-sql) and on
|
|
99
|
+
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter) version 23.0.0 or later in the 23.x series:
|
|
91
100
|
|
|
92
101
|
```sh
|
|
93
102
|
cargo add willbooster-tree-sitter@23 --rename tree-sitter
|
|
@@ -96,10 +105,15 @@ cargo add willbooster-tree-sitter-sql --rename tree-sitter-sql
|
|
|
96
105
|
|
|
97
106
|
```rust
|
|
98
107
|
let mut parser = tree_sitter::Parser::new();
|
|
99
|
-
parser.set_language(&tree_sitter_sql::
|
|
108
|
+
parser.set_language(&tree_sitter_sql::LANGUAGE_POSTGRESQL.into())?;
|
|
100
109
|
```
|
|
101
110
|
|
|
102
|
-
|
|
111
|
+
All four dialect features (`sqlite`, `postgresql`, `mysql`, `oracle`) are enabled by default. Disable default features
|
|
112
|
+
to compile only selected dialects:
|
|
113
|
+
|
|
114
|
+
```sh
|
|
115
|
+
cargo add willbooster-tree-sitter-sql --no-default-features --features postgresql --rename tree-sitter-sql
|
|
116
|
+
```
|
|
103
117
|
|
|
104
118
|
## Development
|
|
105
119
|
|
|
@@ -108,55 +122,17 @@ mise install
|
|
|
108
122
|
bun install --frozen-lockfile
|
|
109
123
|
bun run test/ci-setup # installs Chromium for the browser test
|
|
110
124
|
bun run build/ci
|
|
125
|
+
bun run verify
|
|
111
126
|
bun run test
|
|
112
|
-
script/parse-examples
|
|
113
127
|
cargo test
|
|
114
128
|
```
|
|
115
129
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
metadata from `tree-sitter.json`. After changing a grammar, the scanner, `tree-sitter.json`, or a corpus case,
|
|
126
|
-
regenerate and commit `sqlite/src/`. Profiles in `.tmp/generation-profiles/` are temporary and must not be committed.
|
|
127
|
-
`bun run build-wasm`, `bun run build/ci`, and the release build regenerate the parser before compiling it.
|
|
128
|
-
|
|
129
|
-
`bun run test` runs:
|
|
130
|
-
|
|
131
|
-
- the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK);
|
|
132
|
-
- a check that the real-world files in `examples/` fail to parse exactly as listed in `script/known-failures.txt`. The
|
|
133
|
-
first run clones the repositories. The example repositories are pinned to commits in `script/parse-examples`. After a
|
|
134
|
-
grammar change or a moved pin alters that list, `script/parse-examples` rewrites it; review its diff before
|
|
135
|
-
committing. Each listed file is also rejected by SQLite;
|
|
136
|
-
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each line takes linear time,
|
|
137
|
-
since consumers parse files while they are being edited, and that the scanner reads the input a bounded number of
|
|
138
|
-
times. It loads the Wasm build through @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after
|
|
139
|
-
regenerating the parser;
|
|
140
|
-
- a check (`test/unit/runtimeVersion.test.ts`) that `@willbooster/web-tree-sitter` in `package.json` and
|
|
141
|
-
`willbooster-tree-sitter` in `Cargo.lock` are the same version, since the Wasm tests run on the former and the Rust
|
|
142
|
-
tests, the fuzzer, and the CLI on the latter;
|
|
143
|
-
- tests that load the Wasm build with @willbooster/web-tree-sitter in Chromium (`test/e2e/browser.test.ts`) and in
|
|
144
|
-
Cloudflare Workers with and without Node.js compatibility (`test/e2e/workers.test.ts`).
|
|
145
|
-
|
|
146
|
-
The tests compile the parser into `.tmp/tree-sitter-lib` rather than the CLI's cache shared by every checkout;
|
|
147
|
-
`script/parse-examples` builds a parser of its own in `.tmp/` for each run and deletes it afterwards.
|
|
148
|
-
|
|
149
|
-
CI also runs these tests on Linux arm64 and macOS, where the CLI compiles the parser and the scanner natively, and
|
|
150
|
-
fuzzes the parser with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
|
|
151
|
-
|
|
152
|
-
When changing the grammar, compare what it accepts with SQLite itself: a valid statement must parse without errors, and
|
|
153
|
-
a statement that SQLite rejects with a syntax error must yield `ERROR` or `MISSING` nodes. SQL from SQLite's test suite
|
|
154
|
-
(`test/*.test`), and that SQL with tokens deleted, inserted, replaced, or swapped, make good inputs for the comparison.
|
|
155
|
-
To check the grouping of operators, evaluate random constant expressions in SQLite both as written and with every node of
|
|
156
|
-
their tree enclosed in parentheses.
|
|
157
|
-
|
|
158
|
-
### References
|
|
159
|
-
|
|
160
|
-
- [SQL As Understood By SQLite](https://sqlite.org/lang.html)
|
|
161
|
-
- [SQLite's grammar (`src/parse.y`)](https://sqlite.org/src/file/src/parse.y) and
|
|
162
|
-
[tokenizer (`src/tokenize.c`)](https://sqlite.org/src/file/src/tokenize.c)
|
|
130
|
+
- Run tree-sitter CLI subcommands through `script/tree-sitter`. Upstream's `tree-sitter` CLI uses a generator and
|
|
131
|
+
runtime ABI incompatible with `Cargo.lock`.
|
|
132
|
+
- After changing `<dialect>/grammar.js`, `<dialect>/src/scanner.c`, `common/common.mjs`, `tree-sitter.json`, or
|
|
133
|
+
`test/corpus/<dialect>/`, run `bun run build/ci` (`script/generate --build-wasm`) and commit `<dialect>/src/`. Do not
|
|
134
|
+
commit `.tmp/`.
|
|
135
|
+
- Every corpus case in `test/corpus/<dialect>/*.txt` requires `:language(<dialect>)` in its header. Each dialect keeps
|
|
136
|
+
raw NUL byte cases in `test/corpus/<dialect>/null-characters.txt` for `.github/workflows/robustness.yml`.
|
|
137
|
+
- Run `script/parse-examples` without `--check` to refresh `script/known-failures/<dialect>.txt` after a grammar change
|
|
138
|
+
or moved commit pin alters a list. Confirm every listed file is also rejected by that dialect's SQL parser.
|
package/common/common.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// @ts-check
|
|
2
|
-
/* oxlint-disable no-control-regex -- Tokens exclude NUL characters, which end the input of C parsers. */
|
|
3
2
|
|
|
4
3
|
/** @import {RuleOrLiteral} from '@willbooster/web-tree-sitter/dsl' */
|
|
5
4
|
|
|
@@ -34,19 +33,3 @@ export const commaSep1 = (rule) => seq(rule, repeat(seq(',', rule)));
|
|
|
34
33
|
|
|
35
34
|
/** @param {RuleOrLiteral} rule */
|
|
36
35
|
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]*\*+)*\//;
|