Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement scanSync #6

Merged
merged 2 commits into from
Nov 7, 2024
Merged

Conversation

markandrus
Copy link

@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

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