Skip to content

Commit

Permalink
Support tags in dollar-quoted Postgres strings
Browse files Browse the repository at this point in the history
FIX: Properly support tags in PostgreSQL `4073` quoted strings.
  • Loading branch information
marijnh committed Mar 19, 2024
1 parent a615a63 commit 7ca27f7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,22 @@ function readLiteral(input: InputStream, endQuote: number, backslashEscapes: boo
}
}

function readDoubleDollarLiteral(input: InputStream) {
for (;;) {
if (input.next < 0 || input.peek(1) < 0) return
if (input.next == Ch.Dollar && input.peek(1) == Ch.Dollar ) { input.advance(2); return }
input.advance()
function readDoubleDollarLiteral(input: InputStream, tag: string) {
scan: for (;;) {
if (input.next < 0) return console.log("exit at end", input.pos)
if (input.next == Ch.Dollar) {
input.advance()
for (let i = 0; i < tag.length; i++) {
if (input.next != tag.charCodeAt(i)) continue scan
input.advance()
}
if (input.next == Ch.Dollar) {
input.advance()
return
}
} else {
input.advance()
}
}
}

Expand Down Expand Up @@ -195,9 +206,13 @@ export function tokensFor(d: Dialect) {
if (inString(next, Space)) {
while (inString(input.next, Space)) input.advance()
input.acceptToken(whitespace)
} else if (next == Ch.Dollar && input.next == Ch.Dollar && d.doubleDollarQuotedStrings) {
readDoubleDollarLiteral(input)
input.acceptToken(StringToken)
} else if (next == Ch.Dollar) {
let tag = readWord(input, "")
if (input.next == Ch.Dollar) {
input.advance()
readDoubleDollarLiteral(input, tag)
input.acceptToken(StringToken)
}
} else if (next == Ch.SingleQuote || next == Ch.DoubleQuote && d.doubleQuotedStrings) {
readLiteral(input, next, d.backslashEscapes)
input.acceptToken(StringToken)
Expand Down
6 changes: 5 additions & 1 deletion test/test-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe("Parse PostgreSQL tokens", () => {
it("parses double dollar quoted string literals", () => {
ist(parser.parse("SELECT $$hello$$"), 'Script(Statement(Keyword,String))')
})

it("parses tagged double dollar quoted string literals", () => {
ist(parser.parse("SELECT $body$\nhello\n$body$"), 'Script(Statement(Keyword,String))')
})
})

describe("Parse BigQuery tokens", () => {
Expand Down Expand Up @@ -86,4 +90,4 @@ describe("Parse PL/SQL tokens", () => {
it("parses alternative quoting mechanism - unclosed", () => {
ist(parser.parse("SELECT q'~foo'bar' FROM DUAL"), 'Script(Statement(Keyword,String))')
})
})
})

0 comments on commit 7ca27f7

Please sign in to comment.