forked from postgrespro/jsquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsquery_scan.l
509 lines (412 loc) · 11.4 KB
/
jsquery_scan.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*-------------------------------------------------------------------------
*
* jsquery_scan.l
* Lexical parser for jsquery datatype
*
* Copyright (c) 2014, PostgreSQL Global Development Group
* Portions Copyright (c) 2017-2021, Postgres Professional
* Author: Teodor Sigaev <[email protected]>
*
* IDENTIFICATION
* contrib/jsquery/jsquery_scan.l
*
*-------------------------------------------------------------------------
*/
%{
#include "mb/pg_wchar.h"
static string scanstring;
/* No reason to constrain amount of data slurped */
/* #define YY_READ_BUF_SIZE 16777216 */
/* Handles to the buffer that the lexer uses internally */
static YY_BUFFER_STATE scanbufhandle;
static char *scanbuf;
static int scanbuflen;
static void addstring(bool init, char *s, int l);
static void addchar(bool init, char s);
static int checkSpecialVal(void); /* examine scanstring for the special value */
static JsQueryHint checkHint(void);
static void parseUnicode(char *s, int l);
%}
%option 8bit
%option never-interactive
%option nodefault
%option noinput
%option nounput
%option noyywrap
%option warn
%option prefix="jsquery_yy"
%option bison-bridge
%x xQUOTED
%x xNONQUOTED
%x xCOMMENT
special [\?\%\$\.\[\]\(\)\|\&\!\=\<\>\@\#\,\*:]
any [^\?\%\$\.\[\]\(\)\|\&\!\=\<\>\@\#\,\* \t\n\r\f\\\"\/:]
blank [ \t\n\r\f]
unicode \\u[0-9A-Fa-f]{4}
%%
<INITIAL>{special} { return *yytext; }
<INITIAL>{blank}+ { /* ignore */ }
<INITIAL>\/\* {
addchar(true, '\0');
BEGIN xCOMMENT;
}
<INITIAL>[+-]?[0-9]+(\.[0-9]+)?[eE][+-]?[0-9]+ /* float */ {
addstring(true, yytext, yyleng);
addchar(false, '\0');
yylval->str = scanstring;
return NUMERIC_P;
}
<INITIAL>[+-]?\.[0-9]+[eE][+-]?[0-9]+ /* float */ {
addstring(true, yytext, yyleng);
addchar(false, '\0');
yylval->str = scanstring;
return NUMERIC_P;
}
<INITIAL>[+-]?([0-9]+)?\.[0-9]+ {
addstring(true, yytext, yyleng);
addchar(false, '\0');
yylval->str = scanstring;
return NUMERIC_P;
}
<INITIAL>[+-][0-9]+ {
addstring(true, yytext, yyleng);
addchar(false, '\0');
yylval->str = scanstring;
return NUMERIC_P;
}
<INITIAL>[0-9]+ {
addstring(true, yytext, yyleng);
addchar(false, '\0');
yylval->str = scanstring;
return INT_P;
}
<INITIAL>{any}+ {
addstring(true, yytext, yyleng);
BEGIN xNONQUOTED;
}
<INITIAL>\" {
addchar(true, '\0');
BEGIN xQUOTED;
}
<INITIAL>\\ {
yyless(0);
addchar(true, '\0');
BEGIN xNONQUOTED;
}
<xNONQUOTED>{any}+ {
addstring(false, yytext, yyleng);
}
<xNONQUOTED>{blank}+ {
yylval->str = scanstring;
BEGIN INITIAL;
return checkSpecialVal();
}
<xNONQUOTED>\/\* {
yylval->str = scanstring;
addchar(true, '\0');
BEGIN xCOMMENT;
return checkSpecialVal();
}
<xNONQUOTED,INITIAL>\/ { addchar(false, '/'); }
<xNONQUOTED>({special}|\") {
yylval->str = scanstring;
yyless(0);
BEGIN INITIAL;
return checkSpecialVal();
}
<xNONQUOTED><<EOF>> {
yylval->str = scanstring;
BEGIN INITIAL;
return checkSpecialVal();
}
<xNONQUOTED,xQUOTED>\\[\"\\] { addchar(false, yytext[1]); }
<xNONQUOTED,xQUOTED>\\b { addchar(false, '\b'); }
<xNONQUOTED,xQUOTED>\\f { addchar(false, '\f'); }
<xNONQUOTED,xQUOTED>\\n { addchar(false, '\n'); }
<xNONQUOTED,xQUOTED>\\r { addchar(false, '\r'); }
<xNONQUOTED,xQUOTED>\\t { addchar(false, '\t'); }
<xNONQUOTED,xQUOTED>{unicode}+ { parseUnicode(yytext, yyleng); }
<xNONQUOTED,xQUOTED>\\u { yyerror(NULL, "Unicode sequence is invalid"); }
<xNONQUOTED,xQUOTED>\\. { yyerror(NULL, "Escape sequence is invalid"); }
<xNONQUOTED,xQUOTED>\\ { yyerror(NULL, "Unexpected end after backslesh"); }
<xQUOTED><<EOF>> { yyerror(NULL, "Unexpected end of quoted string"); }
<xQUOTED>\" {
yylval->str = scanstring;
BEGIN INITIAL;
return STRING_P;
}
<xQUOTED>[^\\\"]+ { addstring(false, yytext, yyleng); }
<INITIAL><<EOF>> { yyterminate(); }
<xCOMMENT>\*\/ {
BEGIN INITIAL;
if ((yylval->hint = checkHint()) != jsqIndexDefault)
return HINT_P;
}
<xCOMMENT>[^\*]+ { addstring(false, yytext, yyleng); }
<xCOMMENT>\* { addchar(false, '*'); }
<xCOMMENT><<EOF>> { yyerror(NULL, "Unexpected end of comment"); }
%%
void
yyerror(JsQueryParseItem **result, const char *message)
{
if (*yytext == YY_END_OF_BUFFER_CHAR)
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("bad jsquery representation"),
/* translator: %s is typically "syntax error" */
errdetail("%s at the end of input", message)));
}
else
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("bad jsquery representation"),
/* translator: first %s is typically "syntax error" */
errdetail("%s at or near \"%s\"", message, yytext)));
}
}
typedef struct keyword
{
int16 len;
bool lowercase;
int val;
char *keyword;
} keyword;
/*
* Array of key words should be sorted by length and then
* alphabetical order
*/
static keyword keywords[] = {
{ 2, false, IN_P, "in"},
{ 2, false, IS_P, "is"},
{ 2, false, OR_P, "or"},
{ 3, false, AND_P, "and"},
{ 3, false, NOT_P, "not"},
{ 4, true, NULL_P, "null"},
{ 4, true, TRUE_P, "true"},
{ 5, false, ARRAY_T, "array"},
{ 5, true, FALSE_P, "false"},
{ 6, false, OBJECT_T, "object"},
{ 6, false, STRING_T, "string"},
{ 7, false, BOOLEAN_T, "boolean"},
{ 7, false, NUMERIC_T, "numeric"}
};
static int
checkSpecialVal()
{
int res = STRING_P;
int diff;
keyword *StopLow = keywords,
*StopHigh = keywords + lengthof(keywords),
*StopMiddle;
if (scanstring.len > keywords[lengthof(keywords) - 1].len)
return res;
while(StopLow < StopHigh)
{
StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
if (StopMiddle->len == scanstring.len)
diff = pg_strncasecmp(StopMiddle->keyword, scanstring.val, scanstring.len);
else
diff = StopMiddle->len - scanstring.len;
if (diff < 0)
StopLow = StopMiddle + 1;
else if (diff > 0)
StopHigh = StopMiddle;
else
{
if (StopMiddle->lowercase)
diff = strncmp(StopMiddle->keyword, scanstring.val, scanstring.len);
if (diff == 0)
res = StopMiddle->val;
break;
}
}
return res;
}
static JsQueryHint
checkHint()
{
if (scanstring.len <= 2 || strncmp(scanstring.val, "--", 2) != 0)
return jsqIndexDefault;
scanstring.val += 2;
scanstring.len -= 2;
while(scanstring.len > 0 && isspace(*scanstring.val))
{
scanstring.val++;
scanstring.len--;
}
if (scanstring.len >= 5 && pg_strncasecmp(scanstring.val, "index", 5) == 0)
return jsqForceIndex;
if (scanstring.len >= 7 && pg_strncasecmp(scanstring.val, "noindex", 7) == 0)
return jsqNoIndex;
return jsqIndexDefault;
}
/*
* Called before any actual parsing is done
*/
static void
jsquery_scanner_init(const char *str, int slen)
{
if (slen <= 0)
slen = strlen(str);
/*
* Might be left over after ereport()
*/
if (YY_CURRENT_BUFFER)
yy_delete_buffer(YY_CURRENT_BUFFER);
/*
* Make a scan buffer with special termination needed by flex.
*/
scanbuflen = slen;
scanbuf = palloc(slen + 2);
memcpy(scanbuf, str, slen);
scanbuf[slen] = scanbuf[slen + 1] = YY_END_OF_BUFFER_CHAR;
scanbufhandle = yy_scan_buffer(scanbuf, slen + 2);
BEGIN(INITIAL);
}
/*
* Called after parsing is done to clean up after jsquery_scanner_init()
*/
static void
jsquery_scanner_finish(void)
{
yy_delete_buffer(scanbufhandle);
pfree(scanbuf);
}
static void
addstring(bool init, char *s, int l) {
if (init) {
scanstring.total = 32;
scanstring.val = palloc(scanstring.total);
scanstring.len = 0;
}
if (s && l) {
while(scanstring.len + l + 1 >= scanstring.total) {
scanstring.total *= 2;
scanstring.val = repalloc(scanstring.val, scanstring.total);
}
memcpy(scanstring.val + scanstring.len, s, l);
scanstring.len += l;
}
}
static void
addchar(bool init, char s) {
if (init)
{
scanstring.total = 32;
scanstring.val = palloc(scanstring.total);
scanstring.len = 0;
}
else if(scanstring.len + 1 >= scanstring.total)
{
scanstring.total *= 2;
scanstring.val = repalloc(scanstring.val, scanstring.total);
}
scanstring.val[ scanstring.len ] = s;
if (s != '\0')
scanstring.len++;
}
JsQueryParseItem*
parsejsquery(const char *str, int len) {
JsQueryParseItem *parseresult;
jsquery_scanner_init(str, len);
if (jsquery_yyparse((void*)&parseresult) != 0)
jsquery_yyerror(NULL, "bugus input");
jsquery_scanner_finish();
return parseresult;
}
static int
hexval(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 0xA;
if (c >= 'A' && c <= 'F')
return c - 'A' + 0xA;
elog(ERROR, "invalid hexadecimal digit");
return 0; /* not reached */
}
/*
* parseUnicode was adopted from json_lex_string() in
* src/backend/utils/adt/json.c
*/
static void
parseUnicode(char *s, int l)
{
int i, j;
int ch = 0;
int hi_surrogate = -1;
Assert(l % 6 /* \uXXXX */ == 0);
for(i = 0; i < l / 6; i++)
{
ch = 0;
for(j=0; j<4; j++)
ch = (ch << 4) | hexval(s[ i*6 + 2 + j]);
if (ch >= 0xd800 && ch <= 0xdbff)
{
if (hi_surrogate != -1)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type jsquery"),
errdetail("Unicode high surrogate must not follow a high surrogate.")));
hi_surrogate = (ch & 0x3ff) << 10;
continue;
}
else if (ch >= 0xdc00 && ch <= 0xdfff)
{
if (hi_surrogate == -1)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type jsquery"),
errdetail("Unicode low surrogate must follow a high surrogate.")));
ch = 0x10000 + hi_surrogate + (ch & 0x3ff);
hi_surrogate = -1;
}
if (hi_surrogate != -1)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type jsquery"),
errdetail("Unicode low surrogate must follow a high surrogate.")));
/*
* For UTF8, replace the escape sequence by the actual
* utf8 character in lex->strval. Do this also for other
* encodings if the escape designates an ASCII character,
* otherwise raise an error.
*/
if (ch == 0)
{
/* We can't allow this, since our TEXT type doesn't */
ereport(ERROR,
(errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
errmsg("unsupported Unicode escape sequence"),
errdetail("\\u0000 cannot be converted to text.")));
}
else if (GetDatabaseEncoding() == PG_UTF8)
{
char utf8str[5];
int utf8len;
unicode_to_utf8(ch, (unsigned char *) utf8str);
utf8len = pg_utf_mblen((unsigned char *) utf8str);
addstring(false, utf8str, utf8len);
}
else if (ch <= 0x007f)
{
/*
* This is the only way to designate things like a
* form feed character in JSON, so it's useful in all
* encodings.
*/
addchar(false, (char) ch);
}
else
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type jsquery"),
errdetail("Unicode escape values cannot be used for code point values above 007F when the server encoding is not UTF8.")));
}
hi_surrogate = -1;
}
}