Skip to content

Commit 27f8ed9

Browse files
committed
Support conditional comments to disable the parser
Fixes #125
1 parent 838e30d commit 27f8ed9

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ Parses SQL string and returns the CST tree. Takes the following options:
201201
After the semicolon, parsing will resume as normal. This option is primarily intended as a workaround
202202
for using the parser with an SQL dialect that's not yet 100% supported.
203203

204+
**Deprecated:** use conditional comments instead (see below).
205+
204206
When parsing fails with syntax error, it throws `FormattedSyntaxError` which contains a message like:
205207

206208
```
@@ -212,6 +214,18 @@ Was expecting to see: "!", "$", "(", "-", ":", "?", "@", "CASE", ...
212214
| ^
213215
```
214216

217+
Because not all SQL dialects are 100% supported and even the ones that are can have bugs,
218+
you might encounter SQL that the parser fails to parse. As a workaround you can use conditional
219+
comments to disable the parser for the problematic part of SQL:
220+
221+
```sql
222+
SELECT * FROM products;
223+
/* sql-parser-cst-disable */
224+
UNSUPPORTED SQL IN HERE;
225+
/* sql-parser-cst-enable */
226+
UPDATE products SET sold = true WHERE id = 2;
227+
```
228+
215229
### show(cst: Node): string
216230

217231
Converts CST back to string.

src/parser.pegjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9261,10 +9261,24 @@ __hspace__
92619261
// Comments
92629262
comment
92639263
= line_comment
9264+
/ conditional_comment
92649265
/ !postgres x:pound_sign_comment { return x; }
92659266
/ !postgres x:block_comment { return x; }
92669267
/ &postgres x:nested_block_comment { return x; }
92679268

9269+
conditional_comment
9270+
= conditional_comment_start (!conditional_comment_end .)* conditional_comment_end {
9271+
return loc({
9272+
type: "block_comment",
9273+
text: text(),
9274+
});
9275+
}
9276+
9277+
conditional_comment_start
9278+
= "/*" " "* "sql-parser-cst-disable" " "* "*/"
9279+
conditional_comment_end
9280+
= "/*" " "* "sql-parser-cst-enable" " "* "*/"
9281+
92689282
block_comment
92699283
= "/*" (!"*/" .)* "*/" {
92709284
return loc({

test/whitespace.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,44 @@ describe("whitespace", () => {
211211
).toBe("SELECT 1 + 2");
212212
});
213213
});
214+
215+
describe("conditional comments", () => {
216+
it("ignores code between sql-parser-cst comments as one single comment", () => {
217+
test(
218+
"SELECT 1; /* sql-parser-cst-disable */ RANDOM STUFF /* sql-parser-cst-enable */ SELECT 1;"
219+
);
220+
});
221+
222+
it("handles whitespace variation in sql-parser-cst comments", () => {
223+
test("/* sql-parser-cst-disable */ RANDOM STUFF /* sql-parser-cst-enable */");
224+
test("/*sql-parser-cst-disable*/ RANDOM STUFF /*sql-parser-cst-enable*/");
225+
});
226+
227+
it("handles comments inside in sql-parser-cst comments", () => {
228+
test("/* sql-parser-cst-disable */ RANDOM /* foo */ STUFF /* sql-parser-cst-enable */");
229+
});
230+
231+
it("parses everything between sql-parser-cst comments as one single comment", () => {
232+
expect(
233+
parse("/* sql-parser-cst-disable */ RANDOM STUFF /* sql-parser-cst-enable */", {
234+
includeComments: true,
235+
})
236+
).toMatchInlineSnapshot(`
237+
{
238+
"leading": [
239+
{
240+
"text": "/* sql-parser-cst-disable */ RANDOM STUFF /* sql-parser-cst-enable */",
241+
"type": "block_comment",
242+
},
243+
],
244+
"statements": [
245+
{
246+
"type": "empty",
247+
},
248+
],
249+
"type": "program",
250+
}
251+
`);
252+
});
253+
});
214254
});

0 commit comments

Comments
 (0)