Age Owner Branch data TLA Line data Source code
1 : : %top{
2 : : /*-------------------------------------------------------------------------
3 : : *
4 : : * scan.l
5 : : * lexical scanner for PostgreSQL
6 : : *
7 : : * NOTE NOTE NOTE:
8 : : *
9 : : * The rules in this file must be kept in sync with src/fe_utils/psqlscan.l
10 : : * and src/interfaces/ecpg/preproc/pgc.l!
11 : : *
12 : : * The rules are designed so that the scanner never has to backtrack,
13 : : * in the sense that there is always a rule that can match the input
14 : : * consumed so far (the rule action may internally throw back some input
15 : : * with yyless(), however). As explained in the flex manual, this makes
16 : : * for a useful speed increase --- several percent faster when measuring
17 : : * raw parsing (Flex + Bison). The extra complexity is mostly in the rules
18 : : * for handling float numbers and continued string literals. If you change
19 : : * the lexical rules, verify that you haven't broken the no-backtrack
20 : : * property by running flex with the "-b" option and checking that the
21 : : * resulting "lex.backup" file says that no backing up is needed. (As of
22 : : * Postgres 9.2, this check is made automatically by the Makefile.)
23 : : *
24 : : *
25 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
26 : : * Portions Copyright (c) 1994, Regents of the University of California
27 : : *
28 : : * IDENTIFICATION
29 : : * src/backend/parser/scan.l
30 : : *
31 : : *-------------------------------------------------------------------------
32 : : */
33 : : #include "postgres.h"
34 : :
35 : : #include <ctype.h>
36 : : #include <unistd.h>
37 : :
38 : : #include "common/string.h"
39 : : #include "gramparse.h"
40 : : #include "nodes/miscnodes.h"
41 : : #include "parser/parser.h" /* only needed for GUC variables */
42 : : #include "parser/scansup.h"
43 : : #include "port/pg_bitutils.h"
44 : : #include "mb/pg_wchar.h"
45 : : #include "utils/builtins.h"
46 : : }
47 : :
48 : : %{
49 : :
50 : : /* LCOV_EXCL_START */
51 : :
52 : : /* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
53 : : #undef fprintf
54 : : #define fprintf(file, fmt, msg) fprintf_to_ereport(fmt, msg)
55 : :
56 : : static void
4860 tgl@sss.pgh.pa.us 57 :UBC 0 : fprintf_to_ereport(const char *fmt, const char *msg)
58 : : {
59 [ # # ]: 0 : ereport(ERROR, (errmsg_internal("%s", msg)));
60 : : }
61 : :
62 : : /*
63 : : * GUC variable. This is a DIRECT violation of the warning given at the
64 : : * head of gram.y, ie flex/bison code must not depend on any GUC variables;
65 : : * as such, changing its value can induce very unintuitive behavior.
66 : : * In practice, backslash_quote is not too awful since it only controls
67 : : * whether to throw an error: it cannot change non-error results.
68 : : */
69 : : int backslash_quote = BACKSLASH_QUOTE_SAFE_ENCODING;
70 : :
71 : : /*
72 : : * Constant data exported from this file. This array maps from the
73 : : * zero-based keyword numbers returned by ScanKeywordLookup to the
74 : : * Bison token numbers needed by gram.y. This is exported because
75 : : * callers need to pass it to scanner_init, if they are using the
76 : : * standard keyword list ScanKeywords.
77 : : */
78 : : #define PG_KEYWORD(kwname, value, category, collabel) value,
79 : :
80 : : const uint16 ScanKeywordTokens[] = {
81 : : #include "parser/kwlist.h"
82 : : };
83 : :
84 : : #undef PG_KEYWORD
85 : :
86 : : /*
87 : : * Set the type of YYSTYPE.
88 : : */
89 : : #define YYSTYPE core_YYSTYPE
90 : :
91 : : /*
92 : : * Each call to yylex must set yylloc to the location of the found token
93 : : * (expressed as a byte offset from the start of the input text).
94 : : * When we parse a token that requires multiple lexer rules to process,
95 : : * this should be done in the first such rule, else yylloc will point
96 : : * into the middle of the token.
97 : : */
98 : : #define SET_YYLLOC() (*(yylloc) = yytext - yyextra->scanbuf)
99 : :
100 : : /*
101 : : * Advance yylloc by the given number of bytes.
102 : : */
103 : : #define ADVANCE_YYLLOC(delta) ( *(yylloc) += (delta) )
104 : :
105 : : /*
106 : : * Sometimes, we do want yylloc to point into the middle of a token; this is
107 : : * useful for instance to throw an error about an escape sequence within a
108 : : * string literal. But if we find no error there, we want to revert yylloc
109 : : * to the token start, so that that's the location reported to the parser.
110 : : * Use PUSH_YYLLOC/POP_YYLLOC to save/restore yylloc around such code.
111 : : * (Currently the implied "stack" is just one location, but someday we might
112 : : * need to nest these.)
113 : : */
114 : : #define PUSH_YYLLOC() (yyextra->save_yylloc = *(yylloc))
115 : : #define POP_YYLLOC() (*(yylloc) = yyextra->save_yylloc)
116 : :
117 : : #define startlit() ( yyextra->literallen = 0 )
118 : : static void addlit(char *ytext, int yleng, core_yyscan_t yyscanner);
119 : : static void addlitchar(unsigned char ychar, core_yyscan_t yyscanner);
120 : : static char *litbufdup(core_yyscan_t yyscanner);
121 : : static unsigned char unescape_single_char(unsigned char c, core_yyscan_t yyscanner);
122 : : static int process_integer_literal(const char *token, YYSTYPE *lval, int base);
123 : : static void addunicode(char32_t c, yyscan_t yyscanner);
124 : :
125 : : #define yyerror(msg) scanner_yyerror(msg, yyscanner)
126 : :
127 : : #define lexer_errposition() scanner_errposition(*(yylloc), yyscanner)
128 : :
129 : : %}
130 : :
131 : : %option reentrant
132 : : %option bison-bridge
133 : : %option bison-locations
134 : : %option 8bit
135 : : %option never-interactive
136 : : %option nodefault
137 : : %option noinput
138 : : %option nounput
139 : : %option noyywrap
140 : : %option noyyalloc
141 : : %option noyyrealloc
142 : : %option noyyfree
143 : : %option warn
144 : : %option prefix="core_yy"
145 : : %option extra-type="core_yy_extra_type *"
146 : :
147 : : /*
148 : : * OK, here is a short description of lex/flex rules behavior.
149 : : * The longest pattern which matches an input string is always chosen.
150 : : * For equal-length patterns, the first occurring in the rules list is chosen.
151 : : * INITIAL is the starting state, to which all non-conditional rules apply.
152 : : * Exclusive states change parsing rules while the state is active. When in
153 : : * an exclusive state, only those rules defined for that state apply.
154 : : *
155 : : * We use exclusive states for quoted strings, extended comments,
156 : : * and to eliminate parsing troubles for numeric strings.
157 : : * Exclusive states:
158 : : * <xb> bit string literal
159 : : * <xc> extended C-style comments
160 : : * <xd> delimited identifiers (double-quoted identifiers)
161 : : * <xh> hexadecimal byte string
162 : : * <xq> standard quoted strings
163 : : * <xqs> quote stop (detect continued strings)
164 : : * <xe> extended quoted strings (support backslash escape sequences)
165 : : * <xdolq> $foo$ quoted strings
166 : : * <xui> quoted identifier with Unicode escapes
167 : : * <xus> quoted string with Unicode escapes
168 : : * <xeu> Unicode surrogate pair in extended quoted string
169 : : *
170 : : * Remember to add an <<EOF>> case whenever you add a new exclusive state!
171 : : * The default one is probably not the right thing.
172 : : */
173 : :
174 : : %x xb
175 : : %x xc
176 : : %x xd
177 : : %x xh
178 : : %x xq
179 : : %x xqs
180 : : %x xe
181 : : %x xdolq
182 : : %x xui
183 : : %x xus
184 : : %x xeu
185 : :
186 : : /*
187 : : * In order to make the world safe for Windows and Mac clients as well as
188 : : * Unix ones, we accept either \n or \r as a newline. A DOS-style \r\n
189 : : * sequence will be seen as two successive newlines, but that doesn't cause
190 : : * any problems. Comments that start with -- and extend to the next
191 : : * newline are treated as equivalent to a single whitespace character.
192 : : *
193 : : * NOTE a fine point: if there is no newline following --, we will absorb
194 : : * everything to the end of the input as a comment. This is correct. Older
195 : : * versions of Postgres failed to recognize -- as a comment if the input
196 : : * did not end with a newline.
197 : : *
198 : : * non_newline_space tracks all the other space characters except newlines.
199 : : *
200 : : * XXX if you change the set of whitespace characters, fix scanner_isspace()
201 : : * to agree.
202 : : */
203 : :
204 : : space [ \t\n\r\f\v]
205 : : non_newline_space [ \t\f\v]
206 : : newline [\n\r]
207 : : non_newline [^\n\r]
208 : :
209 : : comment ("--"{non_newline}*)
210 : :
211 : : whitespace ({space}+|{comment})
212 : :
213 : : /*
214 : : * SQL requires at least one newline in the whitespace separating
215 : : * string literals that are to be concatenated. Silly, but who are we
216 : : * to argue? Note that {whitespace_with_newline} should not have * after
217 : : * it, whereas {whitespace} should generally have a * after it...
218 : : */
219 : :
220 : : special_whitespace ({space}+|{comment}{newline})
221 : : non_newline_whitespace ({non_newline_space}|{comment})
222 : : whitespace_with_newline ({non_newline_whitespace}*{newline}{special_whitespace}*)
223 : :
224 : : quote '
225 : : /* If we see {quote} then {quotecontinue}, the quoted string continues */
226 : : quotecontinue {whitespace_with_newline}{quote}
227 : :
228 : : /*
229 : : * {quotecontinuefail} is needed to avoid lexer backup when we fail to match
230 : : * {quotecontinue}. It might seem that this could just be {whitespace}*,
231 : : * but if there's a dash after {whitespace_with_newline}, it must be consumed
232 : : * to see if there's another dash --- which would start a {comment} and thus
233 : : * allow continuation of the {quotecontinue} token.
234 : : */
235 : : quotecontinuefail {whitespace}*"-"?
236 : :
237 : : /* Bit string
238 : : * It is tempting to scan the string for only those characters
239 : : * which are allowed. However, this leads to silently swallowed
240 : : * characters if illegal characters are included in the string.
241 : : * For example, if xbinside is [01] then B'ABCD' is interpreted
242 : : * as a zero-length string, and the ABCD' is lost!
243 : : * Better to pass the string forward and let the input routines
244 : : * validate the contents.
245 : : */
246 : : xbstart [bB]{quote}
247 : : xbinside [^']*
248 : :
249 : : /* Hexadecimal byte string */
250 : : xhstart [xX]{quote}
251 : : xhinside [^']*
252 : :
253 : : /* National character */
254 : : xnstart [nN]{quote}
255 : :
256 : : /* Quoted string that allows backslash escapes */
257 : : xestart [eE]{quote}
258 : : xeinside [^\\']+
259 : : xeescape [\\][^0-7]
260 : : xeoctesc [\\][0-7]{1,3}
261 : : xehexesc [\\]x[0-9A-Fa-f]{1,2}
262 : : xeunicode [\\](u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})
263 : : xeunicodefail [\\](u[0-9A-Fa-f]{0,3}|U[0-9A-Fa-f]{0,7})
264 : :
265 : : /* Extended quote
266 : : * xqdouble implements embedded quote, ''''
267 : : */
268 : : xqstart {quote}
269 : : xqdouble {quote}{quote}
270 : : xqinside [^']+
271 : :
272 : : /* $foo$ style quotes ("dollar quoting")
273 : : * The quoted string starts with $foo$ where "foo" is an optional string
274 : : * in the form of an identifier, except that it may not contain "$",
275 : : * and extends to the first occurrence of an identical string.
276 : : * There is *no* processing of the quoted text.
277 : : *
278 : : * {dolqfailed} is an error rule to avoid scanner backup when {dolqdelim}
279 : : * fails to match its trailing "$".
280 : : */
281 : : dolq_start [A-Za-z\200-\377_]
282 : : dolq_cont [A-Za-z\200-\377_0-9]
283 : : dolqdelim \$({dolq_start}{dolq_cont}*)?\$
284 : : dolqfailed \${dolq_start}{dolq_cont}*
285 : : dolqinside [^$]+
286 : :
287 : : /* Double quote
288 : : * Allows embedded spaces and other special characters into identifiers.
289 : : */
290 : : dquote \"
291 : : xdstart {dquote}
292 : : xdstop {dquote}
293 : : xddouble {dquote}{dquote}
294 : : xdinside [^"]+
295 : :
296 : : /* Quoted identifier with Unicode escapes */
297 : : xuistart [uU]&{dquote}
298 : :
299 : : /* Quoted string with Unicode escapes */
300 : : xusstart [uU]&{quote}
301 : :
302 : : /* error rule to avoid backup */
303 : : xufailed [uU]&
304 : :
305 : :
306 : : /* C-style comments
307 : : *
308 : : * The "extended comment" syntax closely resembles allowable operator syntax.
309 : : * The tricky part here is to get lex to recognize a string starting with
310 : : * slash-star as a comment, when interpreting it as an operator would produce
311 : : * a longer match --- remember lex will prefer a longer match! Also, if we
312 : : * have something like plus-slash-star, lex will think this is a 3-character
313 : : * operator whereas we want to see it as a + operator and a comment start.
314 : : * The solution is two-fold:
315 : : * 1. append {op_chars}* to xcstart so that it matches as much text as
316 : : * {operator} would. Then the tie-breaker (first matching rule of same
317 : : * length) ensures xcstart wins. We put back the extra stuff with yyless()
318 : : * in case it contains a star-slash that should terminate the comment.
319 : : * 2. In the operator rule, check for slash-star within the operator, and
320 : : * if found throw it back with yyless(). This handles the plus-slash-star
321 : : * problem.
322 : : * Dash-dash comments have similar interactions with the operator rule.
323 : : */
324 : : xcstart \/\*{op_chars}*
325 : : xcstop \*+\/
326 : : xcinside [^*/]+
327 : :
328 : : ident_start [A-Za-z\200-\377_]
329 : : ident_cont [A-Za-z\200-\377_0-9\$]
330 : :
331 : : identifier {ident_start}{ident_cont}*
332 : :
333 : : /* Assorted special-case operators and operator-like tokens */
334 : : typecast "::"
335 : : dot_dot \.\.
336 : : colon_equals ":="
337 : :
338 : : /*
339 : : * These operator-like tokens (unlike the above ones) also match the {operator}
340 : : * rule, which means that they might be overridden by a longer match if they
341 : : * are followed by a comment start or a + or - character. Accordingly, if you
342 : : * add to this list, you must also add corresponding code to the {operator}
343 : : * block to return the correct token in such cases. (This is not needed in
344 : : * psqlscan.l since the token value is ignored there.)
345 : : */
346 : : equals_greater "=>"
347 : : less_equals "<="
348 : : greater_equals ">="
349 : : less_greater "<>"
350 : : not_equals "!="
351 : : /* Note there is no need for left_arrow, since "<-" is not a single operator. */
352 : : right_arrow "->"
353 : :
354 : : /*
355 : : * "self" is the set of chars that should be returned as single-character
356 : : * tokens. "op_chars" is the set of chars that can make up "Op" tokens,
357 : : * which can be one or more characters long (but if a single-char token
358 : : * appears in the "self" set, it is not to be returned as an Op). Note
359 : : * that the sets overlap, but each has some chars that are not in the other.
360 : : *
361 : : * If you change either set, adjust the character lists appearing in the
362 : : * rule for "operator"!
363 : : */
364 : : self [,()\[\].;\:\|\+\-\*\/\%\^\<\>\=]
365 : : op_chars [\~\!\@\#\^\&\|\`\?\+\-\*\/\%\<\>\=]
366 : : operator {op_chars}+
367 : :
368 : : /*
369 : : * Numbers
370 : : *
371 : : * Unary minus is not part of a number here. Instead we pass it separately to
372 : : * the parser, and there it gets coerced via doNegate().
373 : : *
374 : : * {numericfail} is used because we would like "1..10" to lex as 1, dot_dot, 10.
375 : : *
376 : : * {realfail} is added to prevent the need for scanner
377 : : * backup when the {real} rule fails to match completely.
378 : : */
379 : : decdigit [0-9]
380 : : hexdigit [0-9A-Fa-f]
381 : : octdigit [0-7]
382 : : bindigit [0-1]
383 : :
384 : : decinteger {decdigit}(_?{decdigit})*
385 : : hexinteger 0[xX](_?{hexdigit})+
386 : : octinteger 0[oO](_?{octdigit})+
387 : : bininteger 0[bB](_?{bindigit})+
388 : :
389 : : hexfail 0[xX]_?
390 : : octfail 0[oO]_?
391 : : binfail 0[bB]_?
392 : :
393 : : numeric (({decinteger}\.{decinteger}?)|(\.{decinteger}))
394 : : numericfail {decinteger}\.\.
395 : :
396 : : real ({decinteger}|{numeric})[Ee][-+]?{decinteger}
397 : : realfail ({decinteger}|{numeric})[Ee][-+]
398 : :
399 : : /* Positional parameters don't accept underscores. */
400 : : param \${decdigit}+
401 : :
402 : : /*
403 : : * An identifier immediately following an integer literal is disallowed because
404 : : * in some cases it's ambiguous what is meant: for example, 0x1234 could be
405 : : * either a hexinteger or a decinteger "0" and an identifier "x1234". We can
406 : : * detect such problems by seeing if integer_junk matches a longer substring
407 : : * than any of the XXXinteger patterns (decinteger, hexinteger, octinteger,
408 : : * bininteger). One "junk" pattern is sufficient because
409 : : * {decinteger}{identifier} will match all the same strings we'd match with
410 : : * {hexinteger}{identifier} etc.
411 : : *
412 : : * Note that the rule for integer_junk must appear after the ones for
413 : : * XXXinteger to make this work correctly: 0x1234 will match both hexinteger
414 : : * and integer_junk, and we need hexinteger to be chosen in that case.
415 : : *
416 : : * Also disallow strings matched by numeric_junk, real_junk and param_junk
417 : : * for consistency.
418 : : */
419 : : integer_junk {decinteger}{identifier}
420 : : numeric_junk {numeric}{identifier}
421 : : real_junk {real}{identifier}
422 : : param_junk \${decdigit}+{identifier}
423 : :
424 : : other .
425 : :
426 : : /*
427 : : * Dollar quoted strings are totally opaque, and no escaping is done on them.
428 : : * Other quoted strings must allow some special characters such as single-quote
429 : : * and newline.
430 : : * Embedded single-quotes are implemented both in the SQL standard
431 : : * style of two adjacent single quotes "''" and in the Postgres/Java style
432 : : * of escaped-quote "\'".
433 : : * Other embedded escaped characters are matched explicitly and the leading
434 : : * backslash is dropped from the string.
435 : : * Note that xcstart must appear before operator, as explained above!
436 : : * Also whitespace (comment) must appear before operator.
437 : : */
438 : :
439 : : %%
440 : :
441 : : {whitespace} {
442 : : /* ignore */
443 : : }
10892 scrappy@hub.org 444 :CBC 6810217 :
9551 tgl@sss.pgh.pa.us 445 : 42691 : {xcstart} {
446 : : /* Set location in case of syntax error in comment */
7357 447 : 42691 : SET_YYLLOC();
6140 448 : 42691 : yyextra->xcdepth = 0;
9551 449 : 42691 : BEGIN(xc);
450 : : /* Put back any characters past slash-star; see above */
451 : 42691 : yyless(2);
452 : : }
10892 scrappy@hub.org 453 : 42691 :
454 : : <xc>{
2730 tgl@sss.pgh.pa.us 455 : 12 : {xcstart} {
6140 456 : 12 : (yyextra->xcdepth)++;
457 : : /* Put back any characters past slash-star; see above */
9426 lockhart@fourpalms.o 458 : 12 : yyless(2);
459 : : }
460 : 12 :
2730 tgl@sss.pgh.pa.us 461 : 42703 : {xcstop} {
6140 462 [ + + ]: 42703 : if (yyextra->xcdepth <= 0)
9426 lockhart@fourpalms.o 463 : 42691 : BEGIN(INITIAL);
464 : : else
6140 tgl@sss.pgh.pa.us 465 : 12 : (yyextra->xcdepth)--;
466 : : }
10892 scrappy@hub.org 467 : 42703 :
2730 tgl@sss.pgh.pa.us 468 : 94026 : {xcinside} {
469 : : /* ignore */
470 : : }
10473 lockhart@fourpalms.o 471 : 94026 :
2730 tgl@sss.pgh.pa.us 472 : 51356 : {op_chars} {
473 : : /* ignore */
474 : : }
9426 lockhart@fourpalms.o 475 : 51356 :
2730 tgl@sss.pgh.pa.us 476 :UBC 0 : \*+ {
477 : : /* ignore */
478 : : }
7649 479 : 0 :
2730 480 : 0 : <<EOF>> {
481 : 0 : yyerror("unterminated /* comment");
482 : : }
483 : : } /* <xc> */
484 : :
8729 lockhart@fourpalms.o 485 :CBC 533 : {xbstart} {
486 : : /* Binary bit type.
487 : : * At some point we should simply pass the string
488 : : * forward to the parser and label it there.
489 : : * In the meantime, place a leading "b" on the string
490 : : * to mark it for the input routine as a binary string.
491 : : */
7357 tgl@sss.pgh.pa.us 492 : 533 : SET_YYLLOC();
8729 lockhart@fourpalms.o 493 : 533 : BEGIN(xb);
9696 tgl@sss.pgh.pa.us 494 : 533 : startlit();
6140 495 : 533 : addlitchar('b', yyscanner);
496 : : }
10396 lockhart@fourpalms.o 497 : 533 : <xh>{xhinside} |
8729 498 : 3253 : <xb>{xbinside} {
6140 tgl@sss.pgh.pa.us 499 : 3253 : addlit(yytext, yyleng, yyscanner);
500 : : }
8111 501 : 3253 : <xb><<EOF>> { yyerror("unterminated bit string literal"); }
8111 tgl@sss.pgh.pa.us 502 :UBC 0 :
10396 lockhart@fourpalms.o 503 :CBC 2741 : {xhstart} {
504 : : /* Hexadecimal bit type.
505 : : * At some point we should simply pass the string
506 : : * forward to the parser and label it there.
507 : : * In the meantime, place a leading "x" on the string
508 : : * to mark it for the input routine as a hex string.
509 : : */
7357 tgl@sss.pgh.pa.us 510 : 2741 : SET_YYLLOC();
10396 lockhart@fourpalms.o 511 : 2741 : BEGIN(xh);
9696 tgl@sss.pgh.pa.us 512 : 2741 : startlit();
6140 513 : 2741 : addlitchar('x', yyscanner);
514 : : }
8675 lockhart@fourpalms.o 515 : 2741 : <xh><<EOF>> { yyerror("unterminated hexadecimal string literal"); }
10396 lockhart@fourpalms.o 516 :UBC 0 :
8718 lockhart@fourpalms.o 517 :CBC 1 : {xnstart} {
518 : : /* National character.
519 : : * We will pass this along as a normal character string,
520 : : * but preceded with an internally-generated "NCHAR".
521 : : */
522 : : int kwnum;
523 : :
7357 tgl@sss.pgh.pa.us 524 : 1 : SET_YYLLOC();
3699 525 : 1 : yyless(1); /* eat only 'n' this time */
526 : :
2676 527 : 1 : kwnum = ScanKeywordLookup("nchar",
528 : 1 : yyextra->keywordlist);
529 [ + - ]: 1 : if (kwnum >= 0)
530 : : {
531 : 2 : yylval->keyword = GetScanKeyword(kwnum,
532 : 1 : yyextra->keywordlist);
533 : 1 : return yyextra->keyword_tokens[kwnum];
534 : : }
535 : : else
536 : : {
537 : : /* If NCHAR isn't a keyword, just return "n" */
6139 tgl@sss.pgh.pa.us 538 :UBC 0 : yylval->str = pstrdup("n");
539 : 0 : return IDENT;
540 : : }
541 : : }
542 : :
10473 lockhart@fourpalms.o 543 :CBC 529273 : {xqstart} {
6140 tgl@sss.pgh.pa.us 544 : 529273 : yyextra->saw_non_ascii = false;
7357 545 : 529273 : SET_YYLLOC();
104 tgl@sss.pgh.pa.us 546 :GNC 529273 : BEGIN(xq);
7618 bruce@momjian.us 547 :CBC 529273 : startlit();
548 : : }
549 : 529273 : {xestart} {
6140 tgl@sss.pgh.pa.us 550 : 3110 : yyextra->saw_non_ascii = false;
7357 551 : 3110 : SET_YYLLOC();
7365 bruce@momjian.us 552 : 3110 : BEGIN(xe);
9696 tgl@sss.pgh.pa.us 553 : 3110 : startlit();
554 : : }
6397 peter_e@gmx.net 555 : 3110 : {xusstart} {
6209 tgl@sss.pgh.pa.us 556 : 404 : SET_YYLLOC();
6397 peter_e@gmx.net 557 : 404 : BEGIN(xus);
558 : 404 : startlit();
559 : : }
2304 tgl@sss.pgh.pa.us 560 : 404 :
561 : 536052 : <xb,xh,xq,xe,xus>{quote} {
562 : : /*
563 : : * When we are scanning a quoted string and see an end
564 : : * quote, we must look ahead for a possible continuation.
565 : : * If we don't see one, we know the end quote was in fact
566 : : * the end of the string. To reduce the lexer table size,
567 : : * we use a single "xqs" state to do the lookahead for all
568 : : * types of strings.
569 : : */
570 : 536052 : yyextra->state_before_str_stop = YYSTATE;
571 : 536052 : BEGIN(xqs);
572 : : }
573 : 536052 : <xqs>{quotecontinue} {
574 : 23 : /*
575 : : * Found a quote continuation, so return to the in-quote
576 : : * state and continue scanning the literal. Nothing is
577 : : * added to the literal's contents.
578 : : */
579 : 23 : BEGIN(yyextra->state_before_str_stop);
580 : : }
581 : 23 : <xqs>{quotecontinuefail} |
582 : 536029 : <xqs>{other} |
583 : : <xqs><<EOF>> {
584 : : /*
585 : : * Failed to see a quote continuation. Throw back
586 : : * everything after the end quote, and handle the string
587 : : * according to the state we were in previously.
588 : : */
4800 heikki.linnakangas@i 589 : 536029 : yyless(0);
6397 peter_e@gmx.net 590 : 536029 : BEGIN(INITIAL);
591 : :
2304 tgl@sss.pgh.pa.us 592 [ + + + + : 536029 : switch (yyextra->state_before_str_stop)
- ]
593 : : {
594 : 533 : case xb:
595 : 533 : yylval->str = litbufdup(yyscanner);
596 : 533 : return BCONST;
597 : 2741 : case xh:
598 : 2741 : yylval->str = litbufdup(yyscanner);
599 : 2741 : return XCONST;
600 : 532351 : case xq:
601 : : case xe:
602 : : /*
603 : : * Check that the data remains valid, if it might
604 : : * have been made invalid by unescaping any chars.
605 : : */
606 [ + + ]: 532351 : if (yyextra->saw_non_ascii)
607 : 3 : pg_verifymbstr(yyextra->literalbuf,
608 : 3 : yyextra->literallen,
609 : : false);
610 : 532351 : yylval->str = litbufdup(yyscanner);
611 : 532351 : return SCONST;
612 : 404 : case xus:
613 : 404 : yylval->str = litbufdup(yyscanner);
614 : 404 : return USCONST;
2304 tgl@sss.pgh.pa.us 615 :UBC 0 : default:
616 : 0 : yyerror("unhandled previous state in xqs");
617 : : }
618 : : }
619 : :
6397 peter_e@gmx.net 620 :CBC 4426 : <xq,xe,xus>{xqdouble} {
6140 tgl@sss.pgh.pa.us 621 : 4426 : addlitchar('\'', yyscanner);
622 : : }
6397 peter_e@gmx.net 623 : 4426 : <xq,xus>{xqinside} {
6140 tgl@sss.pgh.pa.us 624 : 520214 : addlit(yytext, yyleng, yyscanner);
625 : : }
7365 bruce@momjian.us 626 : 520214 : <xe>{xeinside} {
6140 tgl@sss.pgh.pa.us 627 : 3440 : addlit(yytext, yyleng, yyscanner);
628 : : }
6069 peter_e@gmx.net 629 : 3440 : <xe>{xeunicode} {
188 jdavis@postgresql.or 630 :GNC 116 : char32_t c = strtoul(yytext + 2, NULL, 16);
631 : :
632 : : /* Remember start of overall string token ... */
2251 tgl@sss.pgh.pa.us 633 :CBC 116 : PUSH_YYLLOC();
634 : : /* ... and set the error cursor to point at this esc seq */
635 : 116 : SET_YYLLOC();
636 : :
6069 peter_e@gmx.net 637 [ + + ]: 116 : if (is_utf16_surrogate_first(c))
638 : : {
639 : 20 : yyextra->utf16_first_part = c;
640 : 20 : BEGIN(xeu);
641 : : }
642 [ - + ]: 96 : else if (is_utf16_surrogate_second(c))
6069 peter_e@gmx.net 643 :UBC 0 : yyerror("invalid Unicode surrogate pair");
644 : : else
6069 peter_e@gmx.net 645 :CBC 96 : addunicode(c, yyscanner);
646 : :
647 : : /* Restore yylloc to be start of string token */
2251 tgl@sss.pgh.pa.us 648 : 112 : POP_YYLLOC();
649 : : }
6069 peter_e@gmx.net 650 : 112 : <xeu>{xeunicode} {
188 jdavis@postgresql.or 651 :GNC 8 : char32_t c = strtoul(yytext + 2, NULL, 16);
652 : :
653 : : /* Remember start of overall string token ... */
2251 tgl@sss.pgh.pa.us 654 :CBC 8 : PUSH_YYLLOC();
655 : : /* ... and set the error cursor to point at this esc seq */
656 : 8 : SET_YYLLOC();
657 : :
6069 peter_e@gmx.net 658 [ + - ]: 8 : if (!is_utf16_surrogate_second(c))
659 : 8 : yyerror("invalid Unicode surrogate pair");
660 : :
6069 peter_e@gmx.net 661 :UBC 0 : c = surrogate_pair_to_codepoint(yyextra->utf16_first_part, c);
662 : :
663 : 0 : addunicode(c, yyscanner);
664 : :
665 : : /* Restore yylloc to be start of string token */
2251 tgl@sss.pgh.pa.us 666 : 0 : POP_YYLLOC();
667 : :
6069 peter_e@gmx.net 668 : 0 : BEGIN(xe);
669 : : }
2251 tgl@sss.pgh.pa.us 670 : 0 : <xeu>. |
2251 tgl@sss.pgh.pa.us 671 :CBC 12 : <xeu>\n |
672 : : <xeu><<EOF>> {
673 : : /* Set the error cursor to point at missing esc seq */
674 : 12 : SET_YYLLOC();
675 : 12 : yyerror("invalid Unicode surrogate pair");
676 : : }
677 : : <xe,xeu>{xeunicodefail} {
678 : 8 : /* Set the error cursor to point at malformed esc seq */
679 : 8 : SET_YYLLOC();
3699 680 [ + - ]: 8 : ereport(ERROR,
681 : : (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE),
682 : : errmsg("invalid Unicode escape"),
683 : : errhint("Unicode escapes must be \\uXXXX or \\UXXXXXXXX."),
684 : : lexer_errposition()));
685 : : }
686 : : <xe>{xeescape} {
7289 687 [ - + ]: 1897 : if (yytext[1] == '\'')
688 : : {
4121 tgl@sss.pgh.pa.us 689 [ # # ]:LBC (18) : if (yyextra->backslash_quote == BACKSLASH_QUOTE_OFF ||
690 [ # # # # ]: (36) : (yyextra->backslash_quote == BACKSLASH_QUOTE_SAFE_ENCODING &&
7289 691 [ # # ]: (18) : PG_ENCODING_IS_CLIENT_ONLY(pg_get_client_encoding())))
7289 tgl@sss.pgh.pa.us 692 [ # # ]:UBC 0 : ereport(ERROR,
693 : : (errcode(ERRCODE_NONSTANDARD_USE_OF_ESCAPE_CHARACTER),
694 : : errmsg("unsafe use of \\' in a string literal"),
695 : : errhint("Use '' to write quotes in strings. \\' is insecure in client-only encodings."),
696 : : lexer_errposition()));
697 : : }
6140 tgl@sss.pgh.pa.us 698 :CBC 1897 : addlitchar(unescape_single_char(yytext[1], yyscanner),
699 : : yyscanner);
700 : : }
7365 bruce@momjian.us 701 : 1897 : <xe>{xeoctesc} {
3699 tgl@sss.pgh.pa.us 702 : 30 : unsigned char c = strtoul(yytext + 1, NULL, 8);
703 : :
6140 704 : 30 : addlitchar(c, yyscanner);
6225 705 [ + - - + ]: 30 : if (c == '\0' || IS_HIGHBIT_SET(c))
6140 tgl@sss.pgh.pa.us 706 :UBC 0 : yyextra->saw_non_ascii = true;
707 : : }
7365 bruce@momjian.us 708 :CBC 30 : <xe>{xehexesc} {
3699 tgl@sss.pgh.pa.us 709 : 6 : unsigned char c = strtoul(yytext + 2, NULL, 16);
710 : :
6140 711 : 6 : addlitchar(c, yyscanner);
6225 712 [ + - + + ]: 6 : if (c == '\0' || IS_HIGHBIT_SET(c))
6140 713 : 5 : yyextra->saw_non_ascii = true;
714 : : }
7365 bruce@momjian.us 715 : 6 : <xe>. {
8106 tgl@sss.pgh.pa.us 716 :UBC 0 : /* This is only needed for \ just before EOF */
6140 717 : 0 : addlitchar(yytext[0], yyscanner);
718 : : }
6397 peter_e@gmx.net 719 : 0 : <xq,xe,xus><<EOF>> { yyerror("unterminated quoted string"); }
10466 bruce@momjian.us 720 : 0 :
8106 tgl@sss.pgh.pa.us 721 :CBC 5951 : {dolqdelim} {
7357 722 : 5951 : SET_YYLLOC();
6140 723 : 5951 : yyextra->dolqstart = pstrdup(yytext);
8106 724 : 5951 : BEGIN(xdolq);
725 : 5951 : startlit();
726 : : }
7649 727 : 5951 : {dolqfailed} {
6230 tgl@sss.pgh.pa.us 728 :UBC 0 : SET_YYLLOC();
729 : : /* throw back all but the initial "$" */
7649 730 : 0 : yyless(1);
731 : : /* and treat it as {other} */
732 : 0 : return yytext[0];
733 : : }
734 : : <xdolq>{dolqdelim} {
6140 tgl@sss.pgh.pa.us 735 [ + + ]:CBC 6230 : if (strcmp(yytext, yyextra->dolqstart) == 0)
736 : : {
737 : 5951 : pfree(yyextra->dolqstart);
738 : 5951 : yyextra->dolqstart = NULL;
8106 739 : 5951 : BEGIN(INITIAL);
6140 740 : 5951 : yylval->str = litbufdup(yyscanner);
8106 741 : 5951 : return SCONST;
742 : : }
743 : : else
744 : : {
745 : : /*
746 : : * When we fail to match $...$ to dolqstart, transfer
747 : : * the $... part to the output, but put back the final
748 : : * $ for rescanning. Consider $delim$...$junk$delim$
749 : : */
3699 750 : 279 : addlit(yytext, yyleng - 1, yyscanner);
751 : 279 : yyless(yyleng - 1);
752 : : }
753 : : }
8106 754 : 279 : <xdolq>{dolqinside} {
6140 755 : 8825 : addlit(yytext, yyleng, yyscanner);
756 : : }
7649 757 : 8825 : <xdolq>{dolqfailed} {
6140 758 : 580 : addlit(yytext, yyleng, yyscanner);
759 : : }
8106 760 : 580 : <xdolq>. {
761 : 2294 : /* This is only needed for $ inside the quoted text */
6140 762 : 2294 : addlitchar(yytext[0], yyscanner);
763 : : }
8106 764 : 2294 : <xdolq><<EOF>> { yyerror("unterminated dollar-quoted string"); }
8106 tgl@sss.pgh.pa.us 765 :UBC 0 :
10414 lockhart@fourpalms.o 766 :CBC 69082 : {xdstart} {
7357 tgl@sss.pgh.pa.us 767 : 69082 : SET_YYLLOC();
10414 lockhart@fourpalms.o 768 : 69082 : BEGIN(xd);
9696 tgl@sss.pgh.pa.us 769 : 69082 : startlit();
770 : : }
6397 peter_e@gmx.net 771 : 69082 : {xuistart} {
772 : 17 : SET_YYLLOC();
773 : 17 : BEGIN(xui);
774 : 17 : startlit();
775 : : }
10414 lockhart@fourpalms.o 776 : 17 : <xd>{xdstop} {
3699 tgl@sss.pgh.pa.us 777 : 69082 : char *ident;
778 : :
10414 lockhart@fourpalms.o 779 : 69082 : BEGIN(INITIAL);
6140 tgl@sss.pgh.pa.us 780 [ + + ]: 69082 : if (yyextra->literallen == 0)
8770 781 : 4 : yyerror("zero-length delimited identifier");
6140 782 : 69078 : ident = litbufdup(yyscanner);
783 [ - + ]: 69078 : if (yyextra->literallen >= NAMEDATALEN)
6140 tgl@sss.pgh.pa.us 784 :UBC 0 : truncate_identifier(ident, yyextra->literallen, true);
6140 tgl@sss.pgh.pa.us 785 :CBC 69078 : yylval->str = ident;
10108 bruce@momjian.us 786 : 69078 : return IDENT;
787 : : }
788 : : <xui>{dquote} {
6397 peter_e@gmx.net 789 : 17 : BEGIN(INITIAL);
6140 tgl@sss.pgh.pa.us 790 [ - + ]: 17 : if (yyextra->literallen == 0)
6397 peter_e@gmx.net 791 :UBC 0 : yyerror("zero-length delimited identifier");
792 : : /* can't truncate till after we de-escape the ident */
2304 tgl@sss.pgh.pa.us 793 :CBC 17 : yylval->str = litbufdup(yyscanner);
794 : 17 : return UIDENT;
795 : : }
796 : : <xd,xui>{xddouble} {
6140 797 : 88 : addlitchar('"', yyscanner);
798 : : }
6397 peter_e@gmx.net 799 : 88 : <xd,xui>{xdinside} {
6140 tgl@sss.pgh.pa.us 800 : 69177 : addlit(yytext, yyleng, yyscanner);
801 : : }
6397 peter_e@gmx.net 802 : 69177 : <xd,xui><<EOF>> { yyerror("unterminated quoted identifier"); }
6397 peter_e@gmx.net 803 :UBC 0 :
804 : 0 : {xufailed} {
805 : : char *ident;
806 : :
6230 tgl@sss.pgh.pa.us 807 : 0 : SET_YYLLOC();
808 : : /* throw back all but the initial u/U */
6397 peter_e@gmx.net 809 : 0 : yyless(1);
810 : : /* and treat it as {identifier} */
6230 tgl@sss.pgh.pa.us 811 : 0 : ident = downcase_truncate_identifier(yytext, yyleng, true);
6140 812 : 0 : yylval->str = ident;
6230 813 : 0 : return IDENT;
814 : : }
815 : :
8111 tgl@sss.pgh.pa.us 816 :CBC 156252 : {typecast} {
7357 817 : 156252 : SET_YYLLOC();
8111 818 : 156252 : return TYPECAST;
819 : : }
820 : :
6139 821 : 454 : {dot_dot} {
822 : 454 : SET_YYLLOC();
823 : 454 : return DOT_DOT;
824 : : }
825 : :
826 : 32077 : {colon_equals} {
827 : 32077 : SET_YYLLOC();
828 : 32077 : return COLON_EQUALS;
829 : : }
830 : :
3699 831 : 1674 : {equals_greater} {
4074 rhaas@postgresql.org 832 : 1674 : SET_YYLLOC();
833 : 1674 : return EQUALS_GREATER;
834 : : }
835 : :
4073 tgl@sss.pgh.pa.us 836 : 3560 : {less_equals} {
837 : 3560 : SET_YYLLOC();
838 : 3560 : return LESS_EQUALS;
839 : : }
840 : :
841 : 8129 : {greater_equals} {
842 : 8129 : SET_YYLLOC();
843 : 8129 : return GREATER_EQUALS;
844 : : }
845 : :
846 : 8352 : {less_greater} {
847 : : /* We accept both "<>" and "!=" as meaning NOT_EQUALS */
848 : 8352 : SET_YYLLOC();
849 : 8352 : return NOT_EQUALS;
850 : : }
851 : :
852 : 18835 : {not_equals} {
853 : : /* We accept both "<>" and "!=" as meaning NOT_EQUALS */
854 : 18835 : SET_YYLLOC();
855 : 18835 : return NOT_EQUALS;
856 : : }
857 : :
50 peter@eisentraut.org 858 :GNC 1071 : {right_arrow} {
859 : 1071 : SET_YYLLOC();
860 : 1071 : return RIGHT_ARROW;
861 : : }
862 : :
8111 tgl@sss.pgh.pa.us 863 :CBC 4432109 : {self} {
7357 864 : 4432109 : SET_YYLLOC();
8111 865 : 4432109 : return yytext[0];
866 : : }
867 : :
10466 bruce@momjian.us 868 : 39424 : {operator} {
869 : : /*
870 : : * Check for embedded slash-star or dash-dash; those
871 : : * are comment starts, so operator must stop there.
872 : : * Note that slash-star or dash-dash at the first
873 : : * character will match a prior rule, not this one.
874 : : */
3699 tgl@sss.pgh.pa.us 875 : 39424 : int nchars = yyleng;
876 : 39424 : char *slashstar = strstr(yytext, "/*");
877 : 39424 : char *dashdash = strstr(yytext, "--");
878 : :
9551 879 [ + + - + ]: 39424 : if (slashstar && dashdash)
880 : : {
881 : : /* if both appear, take the first one */
9551 tgl@sss.pgh.pa.us 882 [ # # ]:UBC 0 : if (slashstar > dashdash)
883 : 0 : slashstar = dashdash;
884 : : }
9551 tgl@sss.pgh.pa.us 885 [ + + ]:CBC 39424 : else if (!slashstar)
886 : 39379 : slashstar = dashdash;
887 [ + + ]: 39424 : if (slashstar)
8770 888 : 70 : nchars = slashstar - yytext;
889 : :
890 : : /*
891 : : * For SQL compatibility, '+' and '-' cannot be the
892 : : * last char of a multi-char operator unless the operator
893 : : * contains chars that are not in SQL operators.
894 : : * The idea is to lex '=-' as two operators, but not
895 : : * to forbid operator names like '?-' that could not be
896 : : * sequences of SQL operators.
897 : : */
2812 rhodiumtoad@postgres 898 [ + + ]: 39424 : if (nchars > 1 &&
899 [ + + ]: 31654 : (yytext[nchars - 1] == '+' ||
900 [ + + ]: 31649 : yytext[nchars - 1] == '-'))
901 : : {
902 : : int ic;
903 : :
3699 tgl@sss.pgh.pa.us 904 [ + + ]: 2391 : for (ic = nchars - 2; ic >= 0; ic--)
905 : : {
2812 rhodiumtoad@postgres 906 : 1348 : char c = yytext[ic];
907 [ + - + + : 1348 : if (c == '~' || c == '!' || c == '@' ||
+ - + + ]
908 [ + - + - : 1284 : c == '#' || c == '^' || c == '&' ||
+ + ]
909 [ + - + + : 1099 : c == '|' || c == '`' || c == '?' ||
+ - ]
910 : : c == '%')
911 : : break;
912 : : }
913 [ + + ]: 1305 : if (ic < 0)
914 : : {
915 : : /*
916 : : * didn't find a qualifying character, so remove
917 : : * all trailing [+-]
918 : : */
919 : : do {
920 : 1043 : nchars--;
921 [ + + ]: 1043 : } while (nchars > 1 &&
922 [ - + ]: 28 : (yytext[nchars - 1] == '+' ||
923 [ - + ]: 28 : yytext[nchars - 1] == '-'));
924 : : }
925 : : }
926 : :
7357 tgl@sss.pgh.pa.us 927 : 39424 : SET_YYLLOC();
928 : :
9544 929 [ + + ]: 39424 : if (nchars < yyleng)
930 : : {
931 : : /* Strip the unwanted chars from the token */
9551 932 : 1113 : yyless(nchars);
933 : : /*
934 : : * If what we have left is only one char, and it's
935 : : * one of the characters matching "self", then
936 : : * return it as a character token the same way
937 : : * that the "self" rule would have.
938 : : */
939 [ + + ]: 1113 : if (nchars == 1 &&
50 peter@eisentraut.org 940 [ + - ]:GNC 1015 : strchr(",()[].;:|+-*/%^<>=", yytext[0]))
9551 tgl@sss.pgh.pa.us 941 :CBC 1015 : return yytext[0];
942 : : /*
943 : : * Likewise, if what we have left is two chars, and
944 : : * those match the tokens ">=", "<=", "=>", "<>" or
945 : : * "!=", then we must return the appropriate token
946 : : * rather than the generic Op.
947 : : */
2812 rhodiumtoad@postgres 948 [ + - ]: 98 : if (nchars == 2)
949 : : {
950 [ + + + - ]: 98 : if (yytext[0] == '=' && yytext[1] == '>')
951 : 30 : return EQUALS_GREATER;
952 [ + + + - ]: 68 : if (yytext[0] == '>' && yytext[1] == '=')
953 : 14 : return GREATER_EQUALS;
954 [ + + + + ]: 54 : if (yytext[0] == '<' && yytext[1] == '=')
955 : 14 : return LESS_EQUALS;
956 [ + + + - ]: 40 : if (yytext[0] == '<' && yytext[1] == '>')
957 : 18 : return NOT_EQUALS;
958 [ + + + - ]: 22 : if (yytext[0] == '!' && yytext[1] == '=')
959 : 19 : return NOT_EQUALS;
50 peter@eisentraut.org 960 [ + - + - ]:GNC 3 : if (yytext[0] == '-' && yytext[1] == '>')
961 : 3 : return RIGHT_ARROW;
962 : : }
963 : : }
964 : :
965 : : /*
966 : : * Complain if operator is too long. Unlike the case
967 : : * for identifiers, we make this an error not a notice-
968 : : * and-truncate, because the odds are we are looking at
969 : : * a syntactic mistake anyway.
970 : : */
7567 tgl@sss.pgh.pa.us 971 [ - + ]:CBC 38311 : if (nchars >= NAMEDATALEN)
7567 tgl@sss.pgh.pa.us 972 :UBC 0 : yyerror("operator too long");
973 : :
4073 tgl@sss.pgh.pa.us 974 :CBC 38311 : yylval->str = pstrdup(yytext);
10108 bruce@momjian.us 975 : 38311 : return Op;
976 : : }
977 : :
10466 978 : 25926 : {param} {
672 peter@eisentraut.org 979 : 25926 : ErrorSaveContext escontext = {T_ErrorSaveContext};
980 : : int32 val;
981 : :
7357 tgl@sss.pgh.pa.us 982 : 25926 : SET_YYLLOC();
672 peter@eisentraut.org 983 : 25926 : val = pg_strtoint32_safe(yytext + 1, (Node *) &escontext);
984 [ + + ]: 25926 : if (escontext.error_occurred)
985 : 4 : yyerror("parameter number too large");
986 : 25922 : yylval->ival = val;
10108 bruce@momjian.us 987 : 25922 : return PARAM;
988 : : }
989 : : {param_junk} {
1539 peter@eisentraut.org 990 : 8 : SET_YYLLOC();
991 : 8 : yyerror("trailing junk after parameter");
992 : : }
993 : :
1238 994 : 302344 : {decinteger} {
995 : 302344 : SET_YYLLOC();
996 : 302344 : return process_integer_literal(yytext, yylval, 10);
997 : : }
998 : : {hexinteger} {
999 : 524 : SET_YYLLOC();
1000 : 524 : return process_integer_literal(yytext, yylval, 16);
1001 : : }
1002 : : {octinteger} {
1003 : 40 : SET_YYLLOC();
1004 : 40 : return process_integer_literal(yytext, yylval, 8);
1005 : : }
1006 : : {bininteger} {
1007 : 41 : SET_YYLLOC();
1008 : 41 : return process_integer_literal(yytext, yylval, 2);
1009 : : }
1010 : : {hexfail} {
1011 : 4 : SET_YYLLOC();
1012 : 4 : yyerror("invalid hexadecimal integer");
1013 : : }
1014 : : {octfail} {
7357 tgl@sss.pgh.pa.us 1015 : 4 : SET_YYLLOC();
1238 peter@eisentraut.org 1016 : 4 : yyerror("invalid octal integer");
1017 : : }
1018 : : {binfail} {
1019 : 4 : SET_YYLLOC();
1020 : 4 : yyerror("invalid binary integer");
1021 : : }
1022 : : {numeric} {
7357 tgl@sss.pgh.pa.us 1023 : 7420 : SET_YYLLOC();
6140 1024 : 7420 : yylval->str = pstrdup(yytext);
9570 1025 : 7420 : return FCONST;
1026 : : }
1027 : : {numericfail} {
6018 1028 : 68 : /* throw back the .., and treat as integer */
3699 1029 : 68 : yyless(yyleng - 2);
6018 1030 : 68 : SET_YYLLOC();
1238 peter@eisentraut.org 1031 : 68 : return process_integer_literal(yytext, yylval, 10);
1032 : : }
1033 : : {real} {
7357 tgl@sss.pgh.pa.us 1034 : 783 : SET_YYLLOC();
6140 1035 : 783 : yylval->str = pstrdup(yytext);
10108 bruce@momjian.us 1036 : 783 : return FCONST;
1037 : : }
1038 : : {realfail} {
7357 tgl@sss.pgh.pa.us 1039 : 4 : SET_YYLLOC();
1539 peter@eisentraut.org 1040 : 4 : yyerror("trailing junk after numeric literal");
1041 : : }
1042 : : {integer_junk} {
7357 tgl@sss.pgh.pa.us 1043 : 44 : SET_YYLLOC();
1539 peter@eisentraut.org 1044 : 44 : yyerror("trailing junk after numeric literal");
1045 : : }
1046 : : {numeric_junk} {
1047 : 32 : SET_YYLLOC();
1048 : 32 : yyerror("trailing junk after numeric literal");
1049 : : }
1050 : : {real_junk} {
1539 peter@eisentraut.org 1051 :UBC 0 : SET_YYLLOC();
1052 : 0 : yyerror("trailing junk after numeric literal");
1053 : : }
1054 : :
10223 lockhart@fourpalms.o 1055 :CBC 7282179 :
1056 : : {identifier} {
1057 : : int kwnum;
1058 : : char *ident;
1059 : :
7357 tgl@sss.pgh.pa.us 1060 : 7282179 : SET_YYLLOC();
1061 : :
1062 : : /* Is it a keyword? */
2676 1063 : 7282179 : kwnum = ScanKeywordLookup(yytext,
1064 : 7282179 : yyextra->keywordlist);
1065 [ + + ]: 7282179 : if (kwnum >= 0)
1066 : : {
1067 : 6475378 : yylval->keyword = GetScanKeyword(kwnum,
1068 : 3237689 : yyextra->keywordlist);
1069 : 3237689 : return yyextra->keyword_tokens[kwnum];
1070 : : }
1071 : :
1072 : : /*
1073 : : * No. Convert the identifier to lower case, and truncate
1074 : : * if necessary.
1075 : : */
8109 1076 : 4044490 : ident = downcase_truncate_identifier(yytext, yyleng, true);
6140 1077 : 4044490 : yylval->str = ident;
9204 1078 : 4044490 : return IDENT;
1079 : : }
1080 : :
8111 tgl@sss.pgh.pa.us 1081 :GBC 8 : {other} {
7357 1082 : 8 : SET_YYLLOC();
8111 1083 : 8 : return yytext[0];
1084 : : }
1085 : :
7357 tgl@sss.pgh.pa.us 1086 :CBC 507569 : <<EOF>> {
1087 : 507569 : SET_YYLLOC();
1088 : 507569 : yyterminate();
1089 : : }
1090 : :
10892 scrappy@hub.org 1091 :UBC 0 : %%
1092 : :
1093 : : /* LCOV_EXCL_STOP */
1094 : :
1095 : : /*
1096 : : * Arrange access to yyextra for subroutines of the main yylex() function.
1097 : : * We expect each subroutine to have a yyscanner parameter. Rather than
1098 : : * use the yyget_xxx functions, which might or might not get inlined by the
1099 : : * compiler, we cheat just a bit and cast yyscanner to the right type.
1100 : : */
1101 : : #undef yyextra
1102 : : #define yyextra (((struct yyguts_t *) yyscanner)->yyextra_r)
1103 : :
1104 : : /* Likewise for a couple of other things we need. */
1105 : : #undef yylloc
1106 : : #define yylloc (((struct yyguts_t *) yyscanner)->yylloc_r)
1107 : : #undef yyleng
1108 : : #define yyleng (((struct yyguts_t *) yyscanner)->yyleng_r)
1109 : :
1110 : :
1111 : : /*
1112 : : * scanner_errposition
1113 : : * Report a lexer or grammar error cursor position, if possible.
1114 : : *
1115 : : * This is expected to be used within an ereport() call, or via an error
1116 : : * callback such as setup_scanner_errposition_callback(). The return value
1117 : : * is a dummy (always 0, in fact).
1118 : : *
1119 : : * Note that this can only be used for messages emitted during raw parsing
1120 : : * (essentially, scan.l, parser.c, and gram.y), since it requires the
1121 : : * yyscanner struct to still be available.
1122 : : */
1123 : : int
6021 tgl@sss.pgh.pa.us 1124 :CBC 825 : scanner_errposition(int location, core_yyscan_t yyscanner)
1125 : : {
1126 : : int pos;
1127 : :
6455 1128 [ - + ]: 825 : if (location < 0)
2232 tgl@sss.pgh.pa.us 1129 :UBC 0 : return 0; /* no-op if location is unknown */
1130 : :
1131 : : /* Convert byte offset to character number */
6140 tgl@sss.pgh.pa.us 1132 :CBC 825 : pos = pg_mbstrlen_with_len(yyextra->scanbuf, location) + 1;
1133 : : /* And pass it to the ereport mechanism */
2232 1134 : 825 : return errposition(pos);
1135 : : }
1136 : :
1137 : : /*
1138 : : * Error context callback for inserting scanner error location.
1139 : : *
1140 : : * Note that this will be called for *any* error occurring while the
1141 : : * callback is installed. We avoid inserting an irrelevant error location
1142 : : * if the error is a query cancel --- are there any other important cases?
1143 : : */
1144 : : static void
2251 1145 : 24 : scb_error_callback(void *arg)
1146 : : {
1147 : 24 : ScannerCallbackState *scbstate = (ScannerCallbackState *) arg;
1148 : :
1149 [ + - ]: 24 : if (geterrcode() != ERRCODE_QUERY_CANCELED)
1150 : 24 : (void) scanner_errposition(scbstate->location, scbstate->yyscanner);
1151 : 24 : }
1152 : :
1153 : : /*
1154 : : * setup_scanner_errposition_callback
1155 : : * Arrange for non-scanner errors to report an error position
1156 : : *
1157 : : * Sometimes the scanner calls functions that aren't part of the scanner
1158 : : * subsystem and can't reasonably be passed the yyscanner pointer; yet
1159 : : * we would like any errors thrown in those functions to be tagged with an
1160 : : * error location. Use this function to set up an error context stack
1161 : : * entry that will accomplish that. Usage pattern:
1162 : : *
1163 : : * declare a local variable "ScannerCallbackState scbstate"
1164 : : * ...
1165 : : * setup_scanner_errposition_callback(&scbstate, yyscanner, location);
1166 : : * call function that might throw error;
1167 : : * cancel_scanner_errposition_callback(&scbstate);
1168 : : */
1169 : : void
1170 : 618 : setup_scanner_errposition_callback(ScannerCallbackState *scbstate,
1171 : : core_yyscan_t yyscanner,
1172 : : int location)
1173 : : {
1174 : : /* Setup error traceback support for ereport() */
1175 : 618 : scbstate->yyscanner = yyscanner;
1176 : 618 : scbstate->location = location;
1177 : 618 : scbstate->errcallback.callback = scb_error_callback;
523 peter@eisentraut.org 1178 : 618 : scbstate->errcallback.arg = scbstate;
2251 tgl@sss.pgh.pa.us 1179 : 618 : scbstate->errcallback.previous = error_context_stack;
1180 : 618 : error_context_stack = &scbstate->errcallback;
1181 : 618 : }
1182 : :
1183 : : /*
1184 : : * Cancel a previously-set-up errposition callback.
1185 : : */
1186 : : void
1187 : 594 : cancel_scanner_errposition_callback(ScannerCallbackState *scbstate)
1188 : : {
1189 : : /* Pop the error context stack */
1190 : 594 : error_context_stack = scbstate->errcallback.previous;
1191 : 594 : }
1192 : :
1193 : : /*
1194 : : * scanner_yyerror
1195 : : * Report a lexer or grammar error.
1196 : : *
1197 : : * The message's cursor position is whatever YYLLOC was last set to,
1198 : : * ie, the start of the current token if called within yylex(), or the
1199 : : * most recently lexed token if called from the grammar.
1200 : : * This is OK for syntax error messages from the Bison parser, because Bison
1201 : : * parsers report error as soon as the first unparsable token is reached.
1202 : : * Beware of using yyerror for other purposes, as the cursor position might
1203 : : * be misleading!
1204 : : */
1205 : : void
6021 1206 : 619 : scanner_yyerror(const char *message, core_yyscan_t yyscanner)
1207 : : {
6140 1208 : 619 : const char *loc = yyextra->scanbuf + *yylloc;
1209 : :
8661 1210 [ + + ]: 619 : if (*loc == YY_END_OF_BUFFER_CHAR)
1211 : : {
8412 1212 [ + - ]: 12 : ereport(ERROR,
1213 : : (errcode(ERRCODE_SYNTAX_ERROR),
1214 : : /* translator: %s is typically the translation of "syntax error" */
1215 : : errmsg("%s at end of input", _(message)),
1216 : : lexer_errposition()));
1217 : : }
1218 : : else
1219 : : {
1220 [ + - ]: 607 : ereport(ERROR,
1221 : : (errcode(ERRCODE_SYNTAX_ERROR),
1222 : : /* translator: first %s is typically the translation of "syntax error" */
1223 : : errmsg("%s at or near \"%s\"", _(message), loc),
1224 : : lexer_errposition()));
1225 : : }
1226 : : }
1227 : :
1228 : :
1229 : : /*
1230 : : * Called before any actual parsing is done
1231 : : */
1232 : : core_yyscan_t
6139 1233 : 519918 : scanner_init(const char *str,
1234 : : core_yy_extra_type *yyext,
1235 : : const ScanKeywordList *keywordlist,
1236 : : const uint16 *keyword_tokens)
1237 : : {
6140 1238 : 519918 : Size slen = strlen(str);
1239 : : yyscan_t scanner;
1240 : :
1241 [ - + ]: 519918 : if (yylex_init(&scanner) != 0)
6140 tgl@sss.pgh.pa.us 1242 [ # # ]:UBC 0 : elog(ERROR, "yylex_init() failed: %m");
1243 : :
6021 tgl@sss.pgh.pa.us 1244 :CBC 519918 : core_yyset_extra(yyext, scanner);
1245 : :
2676 1246 : 519918 : yyext->keywordlist = keywordlist;
1247 : 519918 : yyext->keyword_tokens = keyword_tokens;
1248 : :
4121 1249 : 519918 : yyext->backslash_quote = backslash_quote;
1250 : :
1251 : : /*
1252 : : * Make a scan buffer with special termination needed by flex.
1253 : : */
6140 1254 : 519918 : yyext->scanbuf = (char *) palloc(slen + 2);
1255 : 519918 : yyext->scanbuflen = slen;
1256 : 519918 : memcpy(yyext->scanbuf, str, slen);
1257 : 519918 : yyext->scanbuf[slen] = yyext->scanbuf[slen + 1] = YY_END_OF_BUFFER_CHAR;
1258 : 519918 : yy_scan_buffer(yyext->scanbuf, slen + 2, scanner);
1259 : :
1260 : : /* initialize literal buffer to a reasonable but expansible size */
1261 : 519918 : yyext->literalalloc = 1024;
1262 : 519918 : yyext->literalbuf = (char *) palloc(yyext->literalalloc);
1263 : 519918 : yyext->literallen = 0;
1264 : :
1265 : 519918 : return scanner;
1266 : : }
1267 : :
1268 : :
1269 : : /*
1270 : : * Called after parsing is done to clean up after scanner_init()
1271 : : */
1272 : : void
6021 1273 : 518998 : scanner_finish(core_yyscan_t yyscanner)
1274 : : {
1275 : : /*
1276 : : * We don't bother to call yylex_destroy(), because all it would do is
1277 : : * pfree a small amount of control storage. It's cheaper to leak the
1278 : : * storage until the parsing context is destroyed. The amount of space
1279 : : * involved is usually negligible compared to the output parse tree
1280 : : * anyway.
1281 : : *
1282 : : * We do bother to pfree the scanbuf and literal buffer, but only if they
1283 : : * represent a nontrivial amount of space. The 8K cutoff is arbitrary.
1284 : : */
6140 1285 [ + + ]: 518998 : if (yyextra->scanbuflen >= 8192)
1286 : 66 : pfree(yyextra->scanbuf);
1287 [ + + ]: 518998 : if (yyextra->literalalloc >= 8192)
1288 : 46 : pfree(yyextra->literalbuf);
8781 peter_e@gmx.net 1289 : 518998 : }
1290 : :
1291 : :
1292 : : static void
6021 tgl@sss.pgh.pa.us 1293 : 605860 : addlit(char *ytext, int yleng, core_yyscan_t yyscanner)
1294 : : {
1295 : : /* enlarge buffer if needed */
6140 1296 [ + + ]: 605860 : if ((yyextra->literallen + yleng) >= yyextra->literalalloc)
1297 : : {
1769 drowley@postgresql.o 1298 : 181 : yyextra->literalalloc = pg_nextpower2_32(yyextra->literallen + yleng + 1);
6140 tgl@sss.pgh.pa.us 1299 : 181 : yyextra->literalbuf = (char *) repalloc(yyextra->literalbuf,
1300 : 181 : yyextra->literalalloc);
1301 : : }
1302 : : /* append new data */
1303 : 605860 : memcpy(yyextra->literalbuf + yyextra->literallen, ytext, yleng);
1304 : 605860 : yyextra->literallen += yleng;
9696 1305 : 605860 : }
1306 : :
1307 : :
1308 : : static void
6021 1309 : 12015 : addlitchar(unsigned char ychar, core_yyscan_t yyscanner)
1310 : : {
1311 : : /* enlarge buffer if needed */
6140 1312 [ - + ]: 12015 : if ((yyextra->literallen + 1) >= yyextra->literalalloc)
1313 : : {
6140 tgl@sss.pgh.pa.us 1314 :UBC 0 : yyextra->literalalloc *= 2;
1315 : 0 : yyextra->literalbuf = (char *) repalloc(yyextra->literalbuf,
1316 : 0 : yyextra->literalalloc);
1317 : : }
1318 : : /* append new data */
6140 tgl@sss.pgh.pa.us 1319 :CBC 12015 : yyextra->literalbuf[yyextra->literallen] = ychar;
1320 : 12015 : yyextra->literallen += 1;
10892 scrappy@hub.org 1321 : 12015 : }
1322 : :
1323 : :
1324 : : /*
1325 : : * Create a palloc'd copy of literalbuf, adding a trailing null.
1326 : : */
1327 : : static char *
6021 tgl@sss.pgh.pa.us 1328 : 611075 : litbufdup(core_yyscan_t yyscanner)
1329 : : {
6140 1330 : 611075 : int llen = yyextra->literallen;
1331 : : char *new;
1332 : :
1333 : 611075 : new = palloc(llen + 1);
1334 : 611075 : memcpy(new, yyextra->literalbuf, llen);
1335 : 611075 : new[llen] = '\0';
8781 peter_e@gmx.net 1336 : 611075 : return new;
1337 : : }
1338 : :
1339 : : /*
1340 : : * Process {decinteger}, {hexinteger}, etc. Note this will also do the right
1341 : : * thing with {numeric}, ie digits and a decimal point.
1342 : : */
1343 : : static int
1238 peter@eisentraut.org 1344 : 303017 : process_integer_literal(const char *token, YYSTYPE *lval, int base)
1345 : : {
1186 dean.a.rasheed@gmail 1346 : 303017 : ErrorSaveContext escontext = {T_ErrorSaveContext};
1347 : : int32 val;
1348 : :
1349 : 303017 : val = pg_strtoint32_safe(token, (Node *) &escontext);
1350 [ + + ]: 303017 : if (escontext.error_occurred)
1351 : : {
1352 : : /* integer too large (or contains decimal pt), treat it as a float */
6018 tgl@sss.pgh.pa.us 1353 : 1084 : lval->str = pstrdup(token);
1354 : 1084 : return FCONST;
1355 : : }
1356 : 301933 : lval->ival = val;
1357 : 301933 : return ICONST;
1358 : : }
1359 : :
1360 : : static void
188 jdavis@postgresql.or 1361 :GNC 96 : addunicode(char32_t c, core_yyscan_t yyscanner)
1362 : : {
1363 : : ScannerCallbackState scbstate;
1364 : : char buf[MAX_UNICODE_EQUIVALENT_STRING + 1];
1365 : :
2251 tgl@sss.pgh.pa.us 1366 [ + + ]:CBC 96 : if (!is_valid_unicode_codepoint(c))
6021 1367 : 4 : yyerror("invalid Unicode escape value");
1368 : :
1369 : : /*
1370 : : * We expect that pg_unicode_to_server() will complain about any
1371 : : * unconvertible code point, so we don't have to set saw_non_ascii.
1372 : : */
2251 1373 : 92 : setup_scanner_errposition_callback(&scbstate, yyscanner, *(yylloc));
1374 : 92 : pg_unicode_to_server(c, (unsigned char *) buf);
1375 : 92 : cancel_scanner_errposition_callback(&scbstate);
1376 : 92 : addlit(buf, strlen(buf), yyscanner);
6021 1377 : 92 : }
1378 : :
1379 : : static unsigned char
1380 : 1897 : unescape_single_char(unsigned char c, core_yyscan_t yyscanner)
1381 : : {
8781 peter_e@gmx.net 1382 [ + + + + : 1897 : switch (c)
+ - + ]
1383 : : {
1384 : 18 : case 'b':
1385 : 18 : return '\b';
1386 : 6 : case 'f':
1387 : 6 : return '\f';
1388 : 900 : case 'n':
1389 : 900 : return '\n';
1390 : 46 : case 'r':
1391 : 46 : return '\r';
1392 : 22 : case 't':
1393 : 22 : return '\t';
1034 michael@paquier.xyz 1394 :UBC 0 : case 'v':
1395 : 0 : return '\v';
8781 peter_e@gmx.net 1396 :CBC 905 : default:
1397 : : /* check for backslash followed by non-7-bit-ASCII */
6225 tgl@sss.pgh.pa.us 1398 [ + - - + ]: 905 : if (c == '\0' || IS_HIGHBIT_SET(c))
6140 tgl@sss.pgh.pa.us 1399 :UBC 0 : yyextra->saw_non_ascii = true;
1400 : :
8781 peter_e@gmx.net 1401 :CBC 905 : return c;
1402 : : }
1403 : : }
1404 : :
1405 : : /*
1406 : : * Interface functions to make flex use palloc() instead of malloc().
1407 : : * It'd be better to make these static, but flex insists otherwise.
1408 : : */
1409 : :
1410 : : void *
6021 tgl@sss.pgh.pa.us 1411 : 1559754 : core_yyalloc(yy_size_t bytes, core_yyscan_t yyscanner)
1412 : : {
6140 1413 : 1559754 : return palloc(bytes);
1414 : : }
1415 : :
1416 : : void *
6021 tgl@sss.pgh.pa.us 1417 :UBC 0 : core_yyrealloc(void *ptr, yy_size_t bytes, core_yyscan_t yyscanner)
1418 : : {
6140 1419 [ # # ]: 0 : if (ptr)
1420 : 0 : return repalloc(ptr, bytes);
1421 : : else
1422 : 0 : return palloc(bytes);
1423 : : }
1424 : :
1425 : : void
6021 1426 : 0 : core_yyfree(void *ptr, core_yyscan_t yyscanner)
1427 : : {
6140 1428 [ # # ]: 0 : if (ptr)
1429 : 0 : pfree(ptr);
7618 bruce@momjian.us 1430 : 0 : }
|