@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 CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Test](https://github.com/WillBooster/tree-sitter-sql/actions/workflows/test.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-sql/actions/workflows/test.yml)
6
6
  [![Test rust](https://github.com/WillBooster/tree-sitter-sql/actions/workflows/test-rust.yml/badge.svg)](https://github.com/WillBooster/tree-sitter-sql/actions/workflows/test-rust.yml)
7
7
  [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
8
- [![wbfy](https://img.shields.io/badge/wbfy-22.1.1-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
8
+ [![wbfy](https://img.shields.io/badge/wbfy-22.4.0-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
9
9
  [![crates.io](https://img.shields.io/crates/v/willbooster-tree-sitter-sql.svg)](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
- The `sqlite` grammar parses the SQL dialect of [SQLite](https://sqlite.org/lang.html) 3.54 in its default build. Its
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
- - `OVER` as an alias without `AS` before a WINDOW clause after a closing parenthesis other than a function call's, such
22
- as `SELECT (1) OVER WINDOW w AS ()`, which SQLite's tokenizer reads as a keyword by the token before it;
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
- SQLite's parser also reports errors of other kinds while it builds a statement, from comparing names (a trigger's
27
- schema with its table's), combining constraints (DEFAULT on a generated column, AUTOINCREMENT on a WITHOUT ROWID table),
28
- and looking up the schema or the limits of the connection. The grammar rejects some of them where they shape a single
29
- clause, such as an unknown join type, an ORDER BY clause before UNION, NULLS FIRST in an index, or a qualified table name
30
- in a trigger outside the temp schema, but it accepts the others.
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
- `common/common.mjs` holds the lexical rules that SQL dialects share, for the grammars of other dialects to build on.
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: valid input yields a correct tree, and invalid input yields `ERROR` or
37
- `MISSING` nodes without crashing, hanging, or taking more than linear time. Incremental parsing is not supported, and no
38
- queries (highlights, locals, injections, tags) are shipped. Node types, field names, and the trees that error recovery
39
- produces may change; such changes are released as new major versions.
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 `tree-sitter-sqlite.wasm` for
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. In Node.js and Bun, load the grammar from
46
- its path:
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 the grammar definitions (`common/common.mjs` and `sqlite/grammar.js`, with the `package.json`
86
- file that marks the latter as an ES module) and the node types in `sqlite/src/node-types.json`.
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 the [crate](https://crates.io/crates/willbooster-tree-sitter-sql) and 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::LANGUAGE_SQLITE.into())?;
108
+ parser.set_language(&tree_sitter_sql::LANGUAGE_POSTGRESQL.into())?;
100
109
  ```
101
110
 
102
- The crate also exports `SQLITE_NODE_TYPES`.
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
- The scripts and tests generate, build, test, and parse with `script/tree-sitter`, the tree-sitter CLI of the
117
- WillBooster/tree-sitter runtime version locked in `Cargo.lock`, since the generator and the runtime of upstream's CLI are
118
- not the ones this package ships with. Its first run downloads that CLI from the runtime's GitHub Release, or builds it
119
- with `cargo` (whose build runs the CMake that `mise.toml` pins) when the download fails or the release has no binary
120
- that runs here. Run other CLI commands through it as well (e.g. `script/tree-sitter parse --grammar-path sqlite
121
- file.sql`).
122
-
123
- `bun run generate` records a fresh generation profile from the `test/corpus` cases, then generates compact parser
124
- tables. It would also profile files tracked in `examples/`, but this repository tracks none. The parser also embeds
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]*\*+)*\//;