@willbooster/tree-sitter-sql 3.0.5 → 4.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
@@ -13,23 +13,34 @@ 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
20
-
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.
25
-
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.
31
-
32
- `common/common.mjs` holds the lexical rules that SQL dialects share, for the grammars of other dialects to build on.
16
+ ## Dialects
17
+
18
+ Each dialect has its own grammar and external scanner in `<dialect>/`, built independently on top of the small DSL
19
+ helpers in `common/common.mjs`:
20
+
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` |
27
+
28
+ All four grammars reject what each server's parser grammar and lexer reject (unrecognized tokens, incomplete input,
29
+ syntax errors, raw NUL bytes, and client CLI directives) plus single-clause shape checks. Semantic, compile-time, scope,
30
+ label, numeric-range, limit, catalog, and cross-clause errors are accepted without `ERROR` nodes:
31
+
32
+ - `sqlite` follows SQLite's parser (`src/parse.y`) and tokenizer (`src/tokenize.c`) in its default build, except for
33
+ `OVER` as an alias without `AS` before `WINDOW` after a non-call closing parenthesis (such as
34
+ `SELECT (1) OVER WINDOW w AS ()`), a NUL character (which ends input for SQLite's C parser), and invalid UTF-8/UTF-16.
35
+ - `postgresql` follows PostgreSQL 18's parser (`gram.y`) and lexer (`scan.l`). Client `psql` meta-commands (`\c`, `\i`)
36
+ yield syntax errors.
37
+ - `mysql` follows MySQL 8.4's parser (`sql_yacc.yy`) and lexer (`sql_lex.cc`) in its default SQL mode, including stored
38
+ programs (`CREATE PROCEDURE`, `CREATE FUNCTION`, `CREATE TRIGGER`, `CREATE EVENT`) and executable versioned comments
39
+ (`/*! ... */` and `/*!50700 ... */`). Client `mysql` directives (`DELIMITER`, `SOURCE`) yield syntax errors.
40
+ - `oracle` follows Oracle Database 23ai's documented SQL and embedded PL/SQL syntax (`CREATE TRIGGER`, `CREATE FUNCTION`,
41
+ `CREATE PROCEDURE`, `CREATE PACKAGE`, `CREATE TYPE`, anonymous `DECLARE`/`BEGIN` blocks, and inline `WITH` functions
42
+ and procedures). Client SQL*Plus directives (`REM`, `SET`, `CONNECT`, `PROMPT`, `SPOOL`, `@`, standalone `/`) yield
43
+ syntax errors.
33
44
 
34
45
  ## Support Scope
35
46
 
@@ -40,10 +51,13 @@ produces may change; such changes are released as new major versions.
40
51
 
41
52
  ## Usage
42
53
 
43
- The npm package ships `tree-sitter-sqlite.wasm` for
54
+ The npm package ships one `.wasm` file per dialect for
44
55
  [@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:
56
+ the 23.x series, which runs in Node.js, Bun, browsers, and Cloudflare Workers. Replace `tree-sitter-sqlite.wasm` in the
57
+ examples below with `tree-sitter-postgresql.wasm`, `tree-sitter-mysql.wasm`, or `tree-sitter-oracle.wasm` for another
58
+ dialect.
59
+
60
+ In Node.js and Bun, load the grammar from its path:
47
61
 
48
62
  ```js
49
63
  import { fileURLToPath } from 'node:url';
@@ -82,12 +96,11 @@ const parser = new Parser();
82
96
  parser.setLanguage(await Language.load(sqlite));
83
97
  ```
84
98
 
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`.
99
+ The package also ships each dialect's `src/node-types.json`, `sqlite/grammar.js` and `sqlite/package.json`,
100
+ `common/common.mjs`, and `tree-sitter.json`.
87
101
 
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:
102
+ In Rust, depend on [willbooster-tree-sitter-sql](https://crates.io/crates/willbooster-tree-sitter-sql) and on
103
+ [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter) version 23.0.0 or later in the 23.x series:
91
104
 
92
105
  ```sh
93
106
  cargo add willbooster-tree-sitter@23 --rename tree-sitter
@@ -96,10 +109,15 @@ cargo add willbooster-tree-sitter-sql --rename tree-sitter-sql
96
109
 
97
110
  ```rust
98
111
  let mut parser = tree_sitter::Parser::new();
99
- parser.set_language(&tree_sitter_sql::LANGUAGE_SQLITE.into())?;
112
+ parser.set_language(&tree_sitter_sql::LANGUAGE_POSTGRESQL.into())?;
100
113
  ```
101
114
 
102
- The crate also exports `SQLITE_NODE_TYPES`.
115
+ All four dialect features (`sqlite`, `postgresql`, `mysql`, `oracle`) are enabled by default. Disable default features
116
+ to compile only selected dialects:
117
+
118
+ ```sh
119
+ cargo add willbooster-tree-sitter-sql --no-default-features --features postgresql --rename tree-sitter-sql
120
+ ```
103
121
 
104
122
  ## Development
105
123
 
@@ -108,55 +126,17 @@ mise install
108
126
  bun install --frozen-lockfile
109
127
  bun run test/ci-setup # installs Chromium for the browser test
110
128
  bun run build/ci
129
+ bun run verify
111
130
  bun run test
112
- script/parse-examples
113
131
  cargo test
114
132
  ```
115
133
 
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)
134
+ - Run tree-sitter CLI subcommands through `script/tree-sitter`. Upstream's `tree-sitter` CLI uses a generator and
135
+ runtime ABI incompatible with `Cargo.lock`.
136
+ - After changing `<dialect>/grammar.js`, `<dialect>/src/scanner.c`, `common/common.mjs`, `tree-sitter.json`, or
137
+ `test/corpus/<dialect>/`, run `bun run build/ci` (`script/generate --build-wasm`) and commit `<dialect>/src/`. Do not
138
+ commit `.tmp/`.
139
+ - Every corpus case in `test/corpus/<dialect>/*.txt` requires `:language(<dialect>)` in its header. Each dialect keeps
140
+ raw NUL byte cases in `test/corpus/<dialect>/null-characters.txt` for `.github/workflows/robustness.yml`.
141
+ - Run `script/parse-examples` without `--check` to refresh `script/known-failures/<dialect>.txt` after a grammar change
142
+ 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]*\*+)*\//;