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

Commit ee529be

Browse files
committed
getting closer to displaying correct error location when there are multiple statements in the input SQL. refs #2
1 parent 8eb07a7 commit ee529be

File tree

9 files changed

+3509
-3497
lines changed

9 files changed

+3509
-3497
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
All notable changes to this project will be documented in this file.
33

44
## [Unreleased][unreleased]
5-
6-
## [v0.8.1] - 2015-07-04
75
### Changed
86
- forked `pegjs` repository as `nwronski/pegjs` to get the changes into `pegjs` core into version control so they are not accidentally overwritten
7+
- getting closer to displaying correct error location when there are multiple statements in the input SQL
98

109
## [v0.8.0] - 2015-07-04
1110
### Added

demo/sqlite-parser-demo.js

Lines changed: 1163 additions & 1158 deletions
Large diffs are not rendered by default.

dist/sqlite-parser.js

Lines changed: 1163 additions & 1158 deletions
Large diffs are not rendered by default.

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
});
2121
}
2222
sqliteParser['NAME'] = 'sqlite-parser';
23-
sqliteParser['VERSION'] = '0.8.1';
23+
sqliteParser['VERSION'] = '0.8.2';
2424

2525
module.exports = root.sqliteParser = sqliteParser;
2626
})(typeof self === 'object' ? self : global);

lib/parser.js

Lines changed: 1152 additions & 1145 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tracer.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = (function (util) {
3030
* statement is reported when there is an error within a
3131
* statement that follows it
3232
*/
33-
this.indentation -= 1;
33+
this.indentation -= 1;
3434
break;
3535
case 'rule.fail':
3636
// remove failed leaf
@@ -43,22 +43,20 @@ module.exports = (function (util) {
4343
Tracer.prototype.smartError = function smartError(err) {
4444
var message, location, chain, chainDetail,
4545
lastIndent = 10000,
46-
bestDescriptor = false;
46+
bestDescriptor = false,
47+
multiStatement = false;
4748

48-
chain = this.events.filter(function (e) {
49-
// Only use nodes with a set description
50-
return e.description != null && !/whitespace|(semi$)|(^[oe]$)/i.test(e.rule);
51-
})
49+
chain = this.events
5250
.reverse()
5351
.filter(function (e) {
54-
if (e.indentation < lastIndent) {
55-
// Keep this node and update last indentation
56-
lastIndent = e.indentation;
57-
return true;
58-
} else {
59-
// Prune this node from a previous match sequence
52+
// Only use nodes with a set description
53+
if (multiStatement) {
6054
return false;
55+
} else if (/Statement$/i.test(e.description)) {
56+
multiStatement = true;
57+
return true;
6158
}
59+
return e.description !== null && !/whitespace|(semi$)|(^[oe]$)/i.test(e.rule);
6260
});
6361

6462
if (chain.length) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "sqlite-parser",
33
"description": "JavaScript implentation of SQLite 3 query parser",
44
"author": "Code School (http://codeschool.com)",
5-
"version": "0.8.1",
5+
"version": "0.8.2",
66
"contributors": [
77
"Nick Wronski <nick@javascript.com>"
88
],

src/grammar.pegjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ stmt_fallback_types
11021102
{ return k; }
11031103

11041104
/** {@link https://www.sqlite.org/lang_insert.html} */
1105-
stmt_insert "INSERT Statement"
1105+
stmt_insert
11061106
= k:( insert_keyword ) o t:( insert_target ) o p:( insert_parts )
11071107
{
11081108
// TODO: Not final syntax!
@@ -1120,15 +1120,15 @@ insert_keyword
11201120
= insert_keyword_ins
11211121
/ insert_keyword_repl
11221122

1123-
insert_keyword_ins
1123+
insert_keyword_ins "INSERT Statement"
11241124
= a:( INSERT ) e m:( insert_keyword_mod )?
11251125
{
11261126
return util.extend({
11271127
'action': util.key(a)
11281128
}, m);
11291129
}
11301130

1131-
insert_keyword_repl
1131+
insert_keyword_repl "REPLACE Statement"
11321132
= a:( REPLACE ) e
11331133
{
11341134
return {
@@ -1560,7 +1560,7 @@ stmt_create "CREATE Statement"
15601560
/ create_view
15611561
/ create_virtual
15621562

1563-
create_table "CREATE Table"
1563+
create_table "CREATE TABLE Statement"
15641564
= s:( CREATE ) e tmp:( create_core_tmp )? t:( TABLE ) e ne:( create_core_ine )?
15651565
id:( id_table ) o r:( create_table_source )
15661566
{
@@ -1975,7 +1975,7 @@ table_source_select
19751975
}
19761976

19771977
/* TODO: Left off here */
1978-
create_index "CREATE Index"
1978+
create_index "CREATE INDEX Statement"
19791979
= s:( CREATE ) e u:( index_unique )? i:( INDEX ) e ne:( create_core_ine )?
19801980
n:( id_index ) o o:( index_on ) w:( stmt_core_where )?
19811981
{
@@ -2014,7 +2014,7 @@ index_on
20142014
* enforced on UPDATE, DELETE, and INSERT statements in the trigger_action.
20152015
* See {@link https://www.sqlite.org/lang_createtrigger.html}.
20162016
*/
2017-
create_trigger "CREATE Trigger"
2017+
create_trigger "CREATE TRIGGER Statement"
20182018
= s:( CREATE ) e p:( create_core_tmp )? t:( TRIGGER ) e ne:( create_core_ine )?
20192019
n:( id_trigger ) o cd:( trigger_conditions ) ( ON ) e o:( name_table ) o
20202020
me:( trigger_foreach )? wh:( trigger_when )? a:( trigger_action )
@@ -2109,7 +2109,7 @@ action_loop_stmt
21092109
= s:( stmt ) o c:( sym_semi )
21102110
{ return s; }
21112111

2112-
create_view "CREATE View"
2112+
create_view "CREATE VIEW Statement"
21132113
= s:( CREATE ) e p:( create_core_tmp )? v:( VIEW ) e ne:( create_core_ine )?
21142114
n:( id_view ) o r:( create_as_select )
21152115
{
@@ -2135,7 +2135,7 @@ create_as_select
21352135
* as is allowed in the SQLite spec for virtual table module arguments.
21362136
* See {@link https://www.sqlite.org/lang_createvtab.html}.
21372137
*/
2138-
create_virtual "CREATE Virtual Table"
2138+
create_virtual "CREATE VIRTUAL TABLE Statement"
21392139
= s:( CREATE ) e v:( VIRTUAL ) e t:( TABLE ) e ne:( create_core_ine )?
21402140
n:( id_table ) e ( USING ) e m:( name_module ) o a:( virtual_args )?
21412141
{

src/tracer.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = (function (util) {
3030
* statement is reported when there is an error within a
3131
* statement that follows it
3232
*/
33-
this.indentation -= 1;
33+
this.indentation -= 1;
3434
break;
3535
case 'rule.fail':
3636
// remove failed leaf
@@ -43,22 +43,20 @@ module.exports = (function (util) {
4343
Tracer.prototype.smartError = function smartError(err) {
4444
var message, location, chain, chainDetail,
4545
lastIndent = 10000,
46-
bestDescriptor = false;
46+
bestDescriptor = false,
47+
multiStatement = false;
4748

48-
chain = this.events.filter(function (e) {
49-
// Only use nodes with a set description
50-
return e.description != null && !/whitespace|(semi$)|(^[oe]$)/i.test(e.rule);
51-
})
49+
chain = this.events
5250
.reverse()
5351
.filter(function (e) {
54-
if (e.indentation < lastIndent) {
55-
// Keep this node and update last indentation
56-
lastIndent = e.indentation;
57-
return true;
58-
} else {
59-
// Prune this node from a previous match sequence
52+
// Only use nodes with a set description
53+
if (multiStatement) {
6054
return false;
55+
} else if (/Statement$/i.test(e.description)) {
56+
multiStatement = true;
57+
return true;
6158
}
59+
return e.description !== null && !/whitespace|(semi$)|(^[oe]$)/i.test(e.rule);
6260
});
6361

6462
if (chain.length) {

0 commit comments

Comments
 (0)