You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 15, 2021. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+62-35Lines changed: 62 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,8 +13,6 @@ syntax error is produced if an AST cannot be generated.
13
13
14
14
## Install
15
15
16
-
**IMPORTANT: If you want intelligent error messages for syntax errors, then use the `v0.11.3` release. If you want the fastest possible version of the parser, with the tradeoff of poor syntax error feedback, use the `v0.12.3` release.**
17
-
18
16
```
19
17
npm install sqlite-parser
20
18
```
@@ -30,8 +28,10 @@ can serve up a LiveReload version of the demo on your local machine by running
30
28
## Usage
31
29
32
30
The library exposes a function that accepts two arguments: a string
33
-
containing SQL to parse and a callback function. The function will invoke
34
-
the callback function with the AST object generated from the source string.
31
+
containing SQL to parse and a callback function.
32
+
33
+
If invoked without a callback function the parser will runs synchronously and
34
+
return the resulting AST or throw an error if one occurs.
35
35
36
36
```javascript
37
37
var sqliteParser =require('sqlite-parser');
@@ -52,10 +52,10 @@ sqliteParser(query, function (err, ast) {
52
52
53
53
## Syntax Errors
54
54
55
-
This parser uses the `--trace` flag exposed in `pegjs` to create "smart" error
56
-
messages. The parser includes a `Trace` class that keeps track of which grammar
57
-
rules were being traversed just prior to the error and uses that information
58
-
to improve the error message and location information.
55
+
This parser will try to create _smart_ error messages when it cannot parse
56
+
some input SQL. In addition to an approximate location for the syntax error,
57
+
the parser will attempt to describe the area of concern
58
+
(e.g.: `Syntax error found near Column Identifier (WHERE Clause)`).
59
59
60
60
## AST
61
61
@@ -127,50 +127,77 @@ FROM
127
127
128
128
Once the dependencies are installed, start development with the following command:
129
129
130
-
`grunt test`
130
+
```
131
+
grunt test-watch
132
+
```
131
133
132
-
which will automatically compile the parser and run the tests in `test/core/**/*-spec.js`.
134
+
which will automatically compile the parser and run the tests in
135
+
`test/core/**/*-spec.js` each time a change is made to the tests and/or
136
+
the source code.
133
137
134
-
Optionally, run `grunt debug` to get extended output and start a file watcher.
138
+
Optionally, run `grunt debug` to get AST output from each test in addition to
139
+
live reloading.
135
140
136
-
Finally, you should run `grunt release`, before creating any PR, to run all tests
137
-
and rebuild the `dist/` and `demo/` folders.
141
+
Finally, you should run `grunt release` before creating any PR. **Do not** change
142
+
the version number in `package.json` inside of the pull request.
138
143
139
144
### Writing tests
140
145
141
-
Tests refer to a SQL test file in `test/sql/` and the test name is a
142
-
reference to the filename of the test file. For example `super test 2` as a test name in an `it()` block within a `describe()` block with title `parent block` points to the file `test/sql/parent-block/super-test-2.sql`.
146
+
Each test refers to a SQL input file in `test/sql/` and an expected output
147
+
JSON AST file.
143
148
144
-
The expected AST that should be generated from `super-test-2.sql` should
145
-
be located in a JSON file in the following location:
146
-
`test/json/super-test-2.json`.
149
+
For example a `describe()` block with the title `parent block` that contains an
150
+
`it()` block named `super test 2` will look for the SQL input at
151
+
`test/sql/parent-block/super-test-2.sql` and the JSON AST at
152
+
`test/json/parent-block/super-test-2.json`.
147
153
148
154
There are three options for the test helpers exposed by `tree`:
149
-
-`tree.ok(this, done)` to assert that the test file successfully generates an AST
150
-
-`tree.equals(this, done)` to assert that the test file generates an AST that exactly matches the expected output JSON file
151
-
-`tree.error()` to assert that a test throws an error
152
-
-`tree.error("This is the error message", this, done)` assert an error `message`
153
-
-`tree.error({'line': 2}, this, done)` assert an object of properties that each exist in the error
155
+
- Assert that the test file successfully generates _any_ valid AST
156
+
```javascript
157
+
tree.ok(this, done);
158
+
```
154
159
155
-
Use `grunt rewrite-json` generate new JSON files for each of the specs in
156
-
`test/core/**/*-spec.js` and save them in `test/json/`. Example:
157
-
the AST for `test/sql/parent-block/it-block.sql` will be re-parsed and the
158
-
results will overwrite the existing `json/parent block/it-block.json` file.
160
+
- Assert that the test file generates an AST that exactly matches the expected output JSON file
161
+
```javascript
162
+
tree.equals(this, done);
163
+
```
159
164
160
-
```javascript
161
-
var tree =require('./helpers');
165
+
-`tree.error()` to assert that a test throws an error
166
+
- Assert a specific error `message` for the thrown error
167
+
```javascript
168
+
tree.error('My error message.', this, done);
169
+
```
170
+
- Assert an object of properties that all exist in the thrown error object
0 commit comments