Skip to content

Commit

Permalink
feat: Support for double-quoted identifiers in queries (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvogel authored Mar 31, 2023
1 parent 9fb56c7 commit c69ef50
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/select-query-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,39 @@ type ReadLettersHelper<Input extends string, Acc extends string> = string extend
: [Acc, '']

/**
* Parses an identifier.
* Reads a consecutive sequence of more than 1 double-quoted letters,
* where letters are `[^"]`.
*/
type ReadQuotedLetters<Input extends string> = string extends Input
? GenericStringError
: Input extends `"${infer Remainder}`
? ReadQuotedLettersHelper<Remainder, ''> extends [`${infer Letters}`, `${infer Remainder}`]
? Letters extends ''
? ParserError<`Expected string at \`${Remainder}\``>
: [Letters, Remainder]
: ReadQuotedLettersHelper<Remainder, ''>
: ParserError<`Not a double-quoted string at \`${Input}\``>

type ReadQuotedLettersHelper<Input extends string, Acc extends string> = string extends Input
? GenericStringError
: Input extends `${infer L}${infer Remainder}`
? L extends '"'
? [Acc, Remainder]
: ReadQuotedLettersHelper<Remainder, `${Acc}${L}`>
: ParserError<`Missing closing double-quote in \`"${Acc}${Input}\``>

/**
* Parses a (possibly double-quoted) identifier.
* For now, identifiers are just sequences of more than 1 letter.
*
* TODO: allow for double quoted strings.
*/
type ParseIdentifier<Input extends string> = ReadLetters<Input>
type ParseIdentifier<Input extends string> = ReadLetters<Input> extends [
infer Name,
`${infer Remainder}`
]
? [Name, `${Remainder}`]
: ReadQuotedLetters<Input> extends [infer Name, `${infer Remainder}`]
? [Name, `${Remainder}`]
: ParserError<`No (possibly double-quoted) identifier at \`${Input}\``>

/**
* Parses a node.
Expand Down

0 comments on commit c69ef50

Please sign in to comment.