Skip to content
This repository was archived by the owner on Jul 15, 2021. It is now read-only.

Commit 41a7be8

Browse files
committed
Update README.md for latest version.
1 parent bfda806 commit 41a7be8

File tree

1 file changed

+62
-35
lines changed

1 file changed

+62
-35
lines changed

README.md

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ syntax error is produced if an AST cannot be generated.
1313

1414
## Install
1515

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-
1816
```
1917
npm install sqlite-parser
2018
```
@@ -30,8 +28,10 @@ can serve up a LiveReload version of the demo on your local machine by running
3028
## Usage
3129

3230
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.
3535

3636
``` javascript
3737
var sqliteParser = require('sqlite-parser');
@@ -52,10 +52,10 @@ sqliteParser(query, function (err, ast) {
5252

5353
## Syntax Errors
5454

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)`).
5959

6060
## AST
6161

@@ -127,50 +127,77 @@ FROM
127127

128128
Once the dependencies are installed, start development with the following command:
129129

130-
`grunt test`
130+
```
131+
grunt test-watch
132+
```
131133

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.
133137

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.
135140

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.
138143

139144
### Writing tests
140145

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.
143148

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`.
147153

148154
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+
```
154159

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+
```
159164

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
171+
``` javascript
172+
tree.error({
173+
message: 'You forgot to add a boop to the beep.',
174+
location: {
175+
start: { offset: 0, line: 1, column: 1 },
176+
end: { offset: 0, line: 1, column: 1 }
177+
}
178+
}, this, done)
179+
```
162180

163-
describe('sqlite-parser', function() {
164-
// Test SQL: test/sql/select/basic-select.sql
165-
// Expected JSON: test/json/select/basic-select.json
166-
it('basic select', function(done) {
181+
``` javascript
182+
/* All the helper functions in `/test/helpers.js` are already available
183+
* and do not need to be explicitly imported.
184+
*/
185+
describe('select', function () {
186+
/* Test SQL: test/sql/select/basic-select.sql
187+
* Expected JSON AST: test/json/select/basic-select.json
188+
*/
189+
it('basic select', function (done) {
167190
tree.equals(this, done);
168191
});
192+
});
169193
170-
// uses: test/sql/parse-error-1.sql
194+
describe('parse errors', function (done) {
195+
/* Test SQL: test/sql/parse-errors/parse-error-1.sql
196+
* Expected JSON AST: test/json/parse-errors/parse-error-1.json
197+
*/
171198
it('parse error 1', function(done) {
172199
tree.error({
173-
'message': 'There is a syntax error near FROM Clause [Table Identifier]'
200+
'message': 'Syntax error found near Column Identifier (WHERE Clause)'
174201
}, this, done);
175202
});
176203
});

0 commit comments

Comments
 (0)