Skip to content

feat: implement scanSync#6

Merged
aleclarson merged 2 commits into
pg-nano:16-latestfrom
markandrus:scanSync
Nov 7, 2024
Merged

feat: implement scanSync#6
aleclarson merged 2 commits into
pg-nano:16-latestfrom
markandrus:scanSync

Conversation

@markandrus
Copy link
Copy Markdown

@markandrus markandrus commented Oct 5, 2024

This PR exposes pg_query_scan as scanSync, copying the examples from https://github.com/pganalyze/libpg_query/blob/16-latest/examples/scan.c and the existing splitWithScannerSync function. I also added Token and KeywordKind types.

import { scanSync } from '@pg-nano/pg-parser'

const input = 'SELECT xyz WHERE xyz = 123'

const tokens = scanSync(inputs)

console.log(tokens)

/*

[
  { kind: 'SELECT', start: 0, end: 6, keyword: 'RESERVED_KEYWORD' },
  { kind: 'IDENT', start: 7, end: 10, keyword: 'NO_KEYWORD' },
  { kind: 'WHERE', start: 11, end: 16, keyword: 'RESERVED_KEYWORD' },
  { kind: 'IDENT', start: 17, end: 20, keyword: 'NO_KEYWORD' },
  { kind: 'ASCII_61', start: 21, end: 22, keyword: 'NO_KEYWORD' },
  { kind: 'ICONST', start: 23, end: 26, keyword: 'NO_KEYWORD' }
]

*/

for (const token of tokens) {
  console.log(input.slice(token.start, token.end))
}

/*

SELECT
xyz
WHERE
xyz
=
123

*/

My hope is to use the tokens' start and end values in my application to annotate the parse tree from parseQuerySync with more complete location information.

EDIT: This function is working well for me. I blogged about @pg-nano/pg-parser and using it here!

@aleclarson
Copy link
Copy Markdown

Ooh that's really sleek! Nice find. I'll give this a review after I'm done fixing #3. 👍

@aleclarson aleclarson changed the title feat: expose scanSync feat: implement scanSync Nov 7, 2024
@aleclarson aleclarson merged commit adc8277 into pg-nano:16-latest Nov 7, 2024
@markandrus markandrus deleted the scanSync branch November 11, 2024 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants