Skip to content

Commit c69ef50

Browse files
authored
feat: Support for double-quoted identifiers in queries (#415)
1 parent 9fb56c7 commit c69ef50

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/select-query-parser.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,39 @@ type ReadLettersHelper<Input extends string, Acc extends string> = string extend
124124
: [Acc, '']
125125

126126
/**
127-
* Parses an identifier.
127+
* Reads a consecutive sequence of more than 1 double-quoted letters,
128+
* where letters are `[^"]`.
129+
*/
130+
type ReadQuotedLetters<Input extends string> = string extends Input
131+
? GenericStringError
132+
: Input extends `"${infer Remainder}`
133+
? ReadQuotedLettersHelper<Remainder, ''> extends [`${infer Letters}`, `${infer Remainder}`]
134+
? Letters extends ''
135+
? ParserError<`Expected string at \`${Remainder}\``>
136+
: [Letters, Remainder]
137+
: ReadQuotedLettersHelper<Remainder, ''>
138+
: ParserError<`Not a double-quoted string at \`${Input}\``>
139+
140+
type ReadQuotedLettersHelper<Input extends string, Acc extends string> = string extends Input
141+
? GenericStringError
142+
: Input extends `${infer L}${infer Remainder}`
143+
? L extends '"'
144+
? [Acc, Remainder]
145+
: ReadQuotedLettersHelper<Remainder, `${Acc}${L}`>
146+
: ParserError<`Missing closing double-quote in \`"${Acc}${Input}\``>
147+
148+
/**
149+
* Parses a (possibly double-quoted) identifier.
128150
* For now, identifiers are just sequences of more than 1 letter.
129-
*
130-
* TODO: allow for double quoted strings.
131151
*/
132-
type ParseIdentifier<Input extends string> = ReadLetters<Input>
152+
type ParseIdentifier<Input extends string> = ReadLetters<Input> extends [
153+
infer Name,
154+
`${infer Remainder}`
155+
]
156+
? [Name, `${Remainder}`]
157+
: ReadQuotedLetters<Input> extends [infer Name, `${infer Remainder}`]
158+
? [Name, `${Remainder}`]
159+
: ParserError<`No (possibly double-quoted) identifier at \`${Input}\``>
133160

134161
/**
135162
* Parses a node.

0 commit comments

Comments
 (0)