Skip to content

Commit

Permalink
fix: Fix parsing for single-quoted resolvables (#6)
Browse files Browse the repository at this point in the history
* fix: Fix parsing for quoted resolvables

* refactor: Grammar-only approach
  • Loading branch information
ivov authored Aug 18, 2023
1 parent 0cbe51a commit 56e27a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/sql.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ element {
Parens { ParenL element* ParenR } |
Braces { BraceL element* BraceR } |
Brackets { BracketL element* BracketR } |
orphanWrappedResolvable {
(OrphanSingleQuote Whitespace? Resolvable Whitespace? OrphanSingleQuote)
} |
Resolvable |
Whitespace
}

@tokens {
OrphanSingleQuote { "'" }

Resolvable { ResolvableStart resolvableContent* ResolvableEnd }

ResolvableStart[closedBy="ResolvableEnd"] { "{{" }
Expand Down
16 changes: 16 additions & 0 deletions test/test-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,20 @@ describe("Parse n8n resolvables", () => {

ist(parser.parse("SELECT my_column FROM {{ 'my_table' }};"), 'Script(Statement(Keyword,Whitespace,Identifier,Whitespace,Keyword,Whitespace,Resolvable,";"))')
})

it("parses single-quoted resolvable with no whitespace", () => {
ist(parser.parse("SELECT my_column FROM '{{ 'my_table' }}';"), 'Script(Statement(Keyword,Whitespace,Identifier,Whitespace,Keyword,Whitespace,OrphanSingleQuote,Resolvable,OrphanSingleQuote,";"))')
});

it("parses single-quoted resolvable with leading whitespace", () => {
ist(parser.parse("SELECT my_column FROM ' {{ 'my_table' }}';"), 'Script(Statement(Keyword,Whitespace,Identifier,Whitespace,Keyword,Whitespace,OrphanSingleQuote,Whitespace,Resolvable,OrphanSingleQuote,";"))')
});

it("parses single-quoted resolvable with trailing whitespace", () => {
ist(parser.parse("SELECT my_column FROM '{{ 'my_table' }} ';"), 'Script(Statement(Keyword,Whitespace,Identifier,Whitespace,Keyword,Whitespace,OrphanSingleQuote,Resolvable,Whitespace,OrphanSingleQuote,";"))')
});

it("parses single-quoted resolvable with surrounding whitespace", () => {
ist(parser.parse("SELECT my_column FROM ' {{ 'my_table' }} ';"), 'Script(Statement(Keyword,Whitespace,Identifier,Whitespace,Keyword,Whitespace,OrphanSingleQuote,Whitespace,Resolvable,Whitespace,OrphanSingleQuote,";"))')
});
})

0 comments on commit 56e27a3

Please sign in to comment.