Age Owner Branch data TLA Line data Source code
1 : : %top{
2 : : /*-------------------------------------------------------------------------
3 : : *
4 : : * psqlscan.l
5 : : * lexical scanner for SQL commands
6 : : *
7 : : * This lexer used to be part of psql, and that heritage is reflected in
8 : : * the file name as well as function and typedef names, though it can now
9 : : * be used by other frontend programs as well. It's also possible to extend
10 : : * this lexer with a compatible add-on lexer to handle program-specific
11 : : * backslash commands.
12 : : *
13 : : * This code is mainly concerned with determining where the end of a SQL
14 : : * statement is: we are looking for semicolons that are not within quotes,
15 : : * comments, or parentheses. The most reliable way to handle this is to
16 : : * borrow the backend's flex lexer rules, lock, stock, and barrel. The rules
17 : : * below are (except for a few) the same as the backend's, but their actions
18 : : * are just ECHO whereas the backend's actions generally do other things.
19 : : *
20 : : * XXX The rules in this file must be kept in sync with the backend lexer!!!
21 : : *
22 : : * XXX Avoid creating backtracking cases --- see the backend lexer for info.
23 : : *
24 : : * See psqlscan_int.h for additional commentary.
25 : : *
26 : : *
27 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
28 : : * Portions Copyright (c) 1994, Regents of the University of California
29 : : *
30 : : * IDENTIFICATION
31 : : * src/fe_utils/psqlscan.l
32 : : *
33 : : *-------------------------------------------------------------------------
34 : : */
35 : : #include "postgres_fe.h"
36 : :
37 : : #include "common/logging.h"
38 : : #include "fe_utils/psqlscan.h"
39 : :
40 : : #include "libpq-fe.h"
41 : : }
42 : :
43 : : %{
44 : :
45 : : /* LCOV_EXCL_START */
46 : :
47 : : #include "fe_utils/psqlscan_int.h"
48 : :
49 : : /*
50 : : * We must have a typedef YYSTYPE for yylex's first argument, but this lexer
51 : : * doesn't presently make use of that argument, so just declare it as int.
52 : : */
53 : : typedef int YYSTYPE;
54 : :
55 : :
56 : : /* Return values from yylex() */
57 : : #define LEXRES_EOL 0 /* end of input */
58 : : #define LEXRES_SEMI 1 /* command-terminating semicolon found */
59 : : #define LEXRES_BACKSLASH 2 /* backslash command start */
60 : :
61 : :
62 : : #define ECHO psqlscan_emit(cur_state, yytext, yyleng)
63 : :
64 : : %}
65 : :
66 : : %option reentrant
67 : : %option bison-bridge
68 : : %option 8bit
69 : : %option never-interactive
70 : : %option nodefault
71 : : %option noinput
72 : : %option nounput
73 : : %option noyywrap
74 : : %option warn
75 : : %option prefix="psql_yy"
76 : :
77 : : /*
78 : : * Set the type of yyextra; we use it as a pointer back to the containing
79 : : * PsqlScanState.
80 : : */
81 : : %option extra-type="PsqlScanState"
82 : :
83 : : /*
84 : : * All of the following definitions and rules should exactly match
85 : : * src/backend/parser/scan.l so far as the flex patterns are concerned.
86 : : * The rule bodies are just ECHO as opposed to what the backend does,
87 : : * however. (But be sure to duplicate code that affects the lexing process,
88 : : * such as BEGIN() and yyless().) Also, psqlscan uses a single <<EOF>> rule
89 : : * whereas scan.l has a separate one for each exclusive state.
90 : : */
91 : :
92 : : /*
93 : : * OK, here is a short description of lex/flex rules behavior.
94 : : * The longest pattern which matches an input string is always chosen.
95 : : * For equal-length patterns, the first occurring in the rules list is chosen.
96 : : * INITIAL is the starting state, to which all non-conditional rules apply.
97 : : * Exclusive states change parsing rules while the state is active. When in
98 : : * an exclusive state, only those rules defined for that state apply.
99 : : *
100 : : * We use exclusive states for quoted strings, extended comments,
101 : : * and to eliminate parsing troubles for numeric strings.
102 : : * Exclusive states:
103 : : * <xb> bit string literal
104 : : * <xc> extended C-style comments
105 : : * <xd> delimited identifiers (double-quoted identifiers)
106 : : * <xh> hexadecimal byte string
107 : : * <xq> standard quoted strings
108 : : * <xqs> quote stop (detect continued strings)
109 : : * <xe> extended quoted strings (support backslash escape sequences)
110 : : * <xdolq> $foo$ quoted strings
111 : : * <xui> quoted identifier with Unicode escapes
112 : : * <xus> quoted string with Unicode escapes
113 : : *
114 : : * Note: we intentionally don't mimic the backend's <xeu> state; we have
115 : : * no need to distinguish it from <xe> state, and no good way to get out
116 : : * of it in error cases. The backend just throws yyerror() in those
117 : : * cases, but that's not an option here.
118 : : */
119 : :
120 : : %x xb
121 : : %x xc
122 : : %x xd
123 : : %x xh
124 : : %x xq
125 : : %x xqs
126 : : %x xe
127 : : %x xdolq
128 : : %x xui
129 : : %x xus
130 : :
131 : : /*
132 : : * In order to make the world safe for Windows and Mac clients as well as
133 : : * Unix ones, we accept either \n or \r as a newline. A DOS-style \r\n
134 : : * sequence will be seen as two successive newlines, but that doesn't cause
135 : : * any problems. Comments that start with -- and extend to the next
136 : : * newline are treated as equivalent to a single whitespace character.
137 : : *
138 : : * NOTE a fine point: if there is no newline following --, we will absorb
139 : : * everything to the end of the input as a comment. This is correct. Older
140 : : * versions of Postgres failed to recognize -- as a comment if the input
141 : : * did not end with a newline.
142 : : *
143 : : * non_newline_space tracks all space characters except newlines.
144 : : *
145 : : * XXX if you change the set of whitespace characters, fix scanner_isspace()
146 : : * to agree.
147 : : */
148 : :
149 : : space [ \t\n\r\f\v]
150 : : non_newline_space [ \t\f\v]
151 : : newline [\n\r]
152 : : non_newline [^\n\r]
153 : :
154 : : comment ("--"{non_newline}*)
155 : :
156 : : whitespace ({space}+|{comment})
157 : :
158 : : /*
159 : : * SQL requires at least one newline in the whitespace separating
160 : : * string literals that are to be concatenated. Silly, but who are we
161 : : * to argue? Note that {whitespace_with_newline} should not have * after
162 : : * it, whereas {whitespace} should generally have a * after it...
163 : : */
164 : :
165 : : special_whitespace ({space}+|{comment}{newline})
166 : : non_newline_whitespace ({non_newline_space}|{comment})
167 : : whitespace_with_newline ({non_newline_whitespace}*{newline}{special_whitespace}*)
168 : :
169 : : quote '
170 : : /* If we see {quote} then {quotecontinue}, the quoted string continues */
171 : : quotecontinue {whitespace_with_newline}{quote}
172 : :
173 : : /*
174 : : * {quotecontinuefail} is needed to avoid lexer backup when we fail to match
175 : : * {quotecontinue}. It might seem that this could just be {whitespace}*,
176 : : * but if there's a dash after {whitespace_with_newline}, it must be consumed
177 : : * to see if there's another dash --- which would start a {comment} and thus
178 : : * allow continuation of the {quotecontinue} token.
179 : : */
180 : : quotecontinuefail {whitespace}*"-"?
181 : :
182 : : /* Bit string
183 : : * It is tempting to scan the string for only those characters
184 : : * which are allowed. However, this leads to silently swallowed
185 : : * characters if illegal characters are included in the string.
186 : : * For example, if xbinside is [01] then B'ABCD' is interpreted
187 : : * as a zero-length string, and the ABCD' is lost!
188 : : * Better to pass the string forward and let the input routines
189 : : * validate the contents.
190 : : */
191 : : xbstart [bB]{quote}
192 : : xbinside [^']*
193 : :
194 : : /* Hexadecimal byte string */
195 : : xhstart [xX]{quote}
196 : : xhinside [^']*
197 : :
198 : : /* National character */
199 : : xnstart [nN]{quote}
200 : :
201 : : /* Quoted string that allows backslash escapes */
202 : : xestart [eE]{quote}
203 : : xeinside [^\\']+
204 : : xeescape [\\][^0-7]
205 : : xeoctesc [\\][0-7]{1,3}
206 : : xehexesc [\\]x[0-9A-Fa-f]{1,2}
207 : : xeunicode [\\](u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})
208 : : xeunicodefail [\\](u[0-9A-Fa-f]{0,3}|U[0-9A-Fa-f]{0,7})
209 : :
210 : : /* Extended quote
211 : : * xqdouble implements embedded quote, ''''
212 : : */
213 : : xqstart {quote}
214 : : xqdouble {quote}{quote}
215 : : xqinside [^']+
216 : :
217 : : /* $foo$ style quotes ("dollar quoting")
218 : : * The quoted string starts with $foo$ where "foo" is an optional string
219 : : * in the form of an identifier, except that it may not contain "$",
220 : : * and extends to the first occurrence of an identical string.
221 : : * There is *no* processing of the quoted text.
222 : : *
223 : : * {dolqfailed} is an error rule to avoid scanner backup when {dolqdelim}
224 : : * fails to match its trailing "$".
225 : : */
226 : : dolq_start [A-Za-z\200-\377_]
227 : : dolq_cont [A-Za-z\200-\377_0-9]
228 : : dolqdelim \$({dolq_start}{dolq_cont}*)?\$
229 : : dolqfailed \${dolq_start}{dolq_cont}*
230 : : dolqinside [^$]+
231 : :
232 : : /* Double quote
233 : : * Allows embedded spaces and other special characters into identifiers.
234 : : */
235 : : dquote \"
236 : : xdstart {dquote}
237 : : xdstop {dquote}
238 : : xddouble {dquote}{dquote}
239 : : xdinside [^"]+
240 : :
241 : : /* Quoted identifier with Unicode escapes */
242 : : xuistart [uU]&{dquote}
243 : :
244 : : /* Quoted string with Unicode escapes */
245 : : xusstart [uU]&{quote}
246 : :
247 : : /* error rule to avoid backup */
248 : : xufailed [uU]&
249 : :
250 : :
251 : : /* C-style comments
252 : : *
253 : : * The "extended comment" syntax closely resembles allowable operator syntax.
254 : : * The tricky part here is to get lex to recognize a string starting with
255 : : * slash-star as a comment, when interpreting it as an operator would produce
256 : : * a longer match --- remember lex will prefer a longer match! Also, if we
257 : : * have something like plus-slash-star, lex will think this is a 3-character
258 : : * operator whereas we want to see it as a + operator and a comment start.
259 : : * The solution is two-fold:
260 : : * 1. append {op_chars}* to xcstart so that it matches as much text as
261 : : * {operator} would. Then the tie-breaker (first matching rule of same
262 : : * length) ensures xcstart wins. We put back the extra stuff with yyless()
263 : : * in case it contains a star-slash that should terminate the comment.
264 : : * 2. In the operator rule, check for slash-star within the operator, and
265 : : * if found throw it back with yyless(). This handles the plus-slash-star
266 : : * problem.
267 : : * Dash-dash comments have similar interactions with the operator rule.
268 : : */
269 : : xcstart \/\*{op_chars}*
270 : : xcstop \*+\/
271 : : xcinside [^*/]+
272 : :
273 : : ident_start [A-Za-z\200-\377_]
274 : : ident_cont [A-Za-z\200-\377_0-9\$]
275 : :
276 : : identifier {ident_start}{ident_cont}*
277 : :
278 : : /* Assorted special-case operators and operator-like tokens */
279 : : typecast "::"
280 : : dot_dot \.\.
281 : : colon_equals ":="
282 : :
283 : : /*
284 : : * These operator-like tokens (unlike the above ones) also match the {operator}
285 : : * rule, which means that they might be overridden by a longer match if they
286 : : * are followed by a comment start or a + or - character. Accordingly, if you
287 : : * add to this list, you must also add corresponding code to the {operator}
288 : : * block to return the correct token in such cases. (This is not needed in
289 : : * psqlscan.l since the token value is ignored there.)
290 : : */
291 : : equals_greater "=>"
292 : : less_equals "<="
293 : : greater_equals ">="
294 : : less_greater "<>"
295 : : not_equals "!="
296 : : /* Note there is no need for left_arrow, since "<-" is not a single operator. */
297 : : right_arrow "->"
298 : :
299 : : /*
300 : : * "self" is the set of chars that should be returned as single-character
301 : : * tokens. "op_chars" is the set of chars that can make up "Op" tokens,
302 : : * which can be one or more characters long (but if a single-char token
303 : : * appears in the "self" set, it is not to be returned as an Op). Note
304 : : * that the sets overlap, but each has some chars that are not in the other.
305 : : *
306 : : * If you change either set, adjust the character lists appearing in the
307 : : * rule for "operator"!
308 : : */
309 : : self [,()\[\].;\:\|\+\-\*\/\%\^\<\>\=]
310 : : op_chars [\~\!\@\#\^\&\|\`\?\+\-\*\/\%\<\>\=]
311 : : operator {op_chars}+
312 : :
313 : : /*
314 : : * Numbers
315 : : *
316 : : * Unary minus is not part of a number here. Instead we pass it separately to
317 : : * the parser, and there it gets coerced via doNegate().
318 : : *
319 : : * {numericfail} is used because we would like "1..10" to lex as 1, dot_dot, 10.
320 : : *
321 : : * {realfail} is added to prevent the need for scanner
322 : : * backup when the {real} rule fails to match completely.
323 : : */
324 : : decdigit [0-9]
325 : : hexdigit [0-9A-Fa-f]
326 : : octdigit [0-7]
327 : : bindigit [0-1]
328 : :
329 : : decinteger {decdigit}(_?{decdigit})*
330 : : hexinteger 0[xX](_?{hexdigit})+
331 : : octinteger 0[oO](_?{octdigit})+
332 : : bininteger 0[bB](_?{bindigit})+
333 : :
334 : : hexfail 0[xX]_?
335 : : octfail 0[oO]_?
336 : : binfail 0[bB]_?
337 : :
338 : : numeric (({decinteger}\.{decinteger}?)|(\.{decinteger}))
339 : : numericfail {decinteger}\.\.
340 : :
341 : : real ({decinteger}|{numeric})[Ee][-+]?{decinteger}
342 : : realfail ({decinteger}|{numeric})[Ee][-+]
343 : :
344 : : /* Positional parameters don't accept underscores. */
345 : : param \${decdigit}+
346 : :
347 : : /*
348 : : * An identifier immediately following an integer literal is disallowed because
349 : : * in some cases it's ambiguous what is meant: for example, 0x1234 could be
350 : : * either a hexinteger or a decinteger "0" and an identifier "x1234". We can
351 : : * detect such problems by seeing if integer_junk matches a longer substring
352 : : * than any of the XXXinteger patterns (decinteger, hexinteger, octinteger,
353 : : * bininteger). One "junk" pattern is sufficient because
354 : : * {decinteger}{identifier} will match all the same strings we'd match with
355 : : * {hexinteger}{identifier} etc.
356 : : *
357 : : * Note that the rule for integer_junk must appear after the ones for
358 : : * XXXinteger to make this work correctly: 0x1234 will match both hexinteger
359 : : * and integer_junk, and we need hexinteger to be chosen in that case.
360 : : *
361 : : * Also disallow strings matched by numeric_junk, real_junk and param_junk
362 : : * for consistency.
363 : : */
364 : : integer_junk {decinteger}{identifier}
365 : : numeric_junk {numeric}{identifier}
366 : : real_junk {real}{identifier}
367 : : param_junk \${decdigit}+{identifier}
368 : :
369 : : /* psql-specific: characters allowed in variable names */
370 : : variable_char [A-Za-z\200-\377_0-9]
371 : :
372 : : other .
373 : :
374 : : /*
375 : : * Dollar quoted strings are totally opaque, and no escaping is done on them.
376 : : * Other quoted strings must allow some special characters such as single-quote
377 : : * and newline.
378 : : * Embedded single-quotes are implemented both in the SQL standard
379 : : * style of two adjacent single quotes "''" and in the Postgres/Java style
380 : : * of escaped-quote "\'".
381 : : * Other embedded escaped characters are matched explicitly and the leading
382 : : * backslash is dropped from the string.
383 : : * Note that xcstart must appear before operator, as explained above!
384 : : * Also whitespace (comment) must appear before operator.
385 : : */
386 : :
387 : : %%
388 : :
389 : : %{
390 : : /* Declare some local variables inside yylex(), for convenience */
391 : : PsqlScanState cur_state = yyextra;
3700 tgl@sss.pgh.pa.us 392 :CBC 796806 : PQExpBuffer output_buf = cur_state->output_buf;
393 : 796806 :
394 : : /*
395 : : * Force flex into the state indicated by start_state. This has a
396 : : * couple of purposes: it lets some of the functions below set a new
397 : : * starting state without ugly direct access to flex variables, and it
398 : : * allows us to transition from one flex lexer to another so that we
399 : : * can lex different parts of the source string using separate lexers.
400 : : */
401 : : BEGIN(cur_state->start_state);
402 : 796806 : %}
403 : :
404 : : {whitespace} {
405 : : /*
406 : : * Note that the whitespace rule includes both true
407 : : * whitespace and single-line ("--" style) comments.
408 : : * We suppress whitespace until we have collected some
409 : : * non-whitespace data. (This interacts with some
410 : : * decisions in MainLoop(); see there for details.)
411 : : */
412 : : if (output_buf->len > 0)
8111 413 [ + + ]: 1891267 : ECHO;
414 : 1778967 : }
415 : :
416 : 1891267 : {xcstart} {
417 : 450 : cur_state->xcdepth = 0;
418 : 450 : BEGIN(xc);
419 : 450 : /* Put back any characters past slash-star; see above */
420 : : yyless(2);
421 : 450 : ECHO;
422 : 450 : }
423 : :
2730 424 : 450 : <xc>{
425 : : {xcstart} {
8111 426 : 12 : cur_state->xcdepth++;
427 : 12 : /* Put back any characters past slash-star; see above */
428 : : yyless(2);
429 : 12 : ECHO;
430 : 12 : }
431 : :
2730 432 : 12 : {xcstop} {
8111 433 : 462 : if (cur_state->xcdepth <= 0)
434 [ + + ]: 462 : BEGIN(INITIAL);
435 : 450 : else
436 : : cur_state->xcdepth--;
437 : 12 : ECHO;
438 : 462 : }
439 : :
2730 440 : 462 : {xcinside} {
8111 441 : 1031 : ECHO;
442 : 1031 : }
443 : :
2730 444 : 1031 : {op_chars} {
8111 445 : 277 : ECHO;
446 : 277 : }
447 : :
2730 448 : 277 : \*+ {
7649 tgl@sss.pgh.pa.us 449 :UBC 0 : ECHO;
450 : 0 : }
451 : : } /* <xc> */
452 : 0 :
453 : : {xbstart} {
8111 tgl@sss.pgh.pa.us 454 :CBC 500 : BEGIN(xb);
455 : 500 : ECHO;
456 : 500 : }
457 : : <xh>{xhinside} |
458 : 500 : <xb>{xbinside} {
459 : 2695 : ECHO;
460 : 2695 : }
461 : :
462 : 2695 : {xhstart} {
463 : 2215 : /* Hexadecimal bit type.
464 : : * At some point we should simply pass the string
465 : : * forward to the parser and label it there.
466 : : * In the meantime, place a leading "x" on the string
467 : : * to mark it for the input routine as a hex string.
468 : : */
469 : : BEGIN(xh);
470 : 2215 : ECHO;
471 : 2215 : }
472 : :
473 : 2215 : {xnstart} {
3699 tgl@sss.pgh.pa.us 474 :UBC 0 : yyless(1); /* eat only 'n' this time */
8111 475 : 0 : ECHO;
476 : 0 : }
477 : :
478 : 0 : {xqstart} {
3700 tgl@sss.pgh.pa.us 479 :CBC 158422 : if (cur_state->std_strings)
7365 bruce@momjian.us 480 [ + - ]: 158422 : BEGIN(xq);
481 : 158422 : else
482 : : BEGIN(xe);
8111 tgl@sss.pgh.pa.us 483 :LBC (54) : ECHO;
8111 tgl@sss.pgh.pa.us 484 :CBC 158422 : }
485 : : {xestart} {
7365 bruce@momjian.us 486 : 158422 : BEGIN(xe);
7618 tgl@sss.pgh.pa.us 487 : 869 : ECHO;
488 : 869 : }
489 : : {xusstart} {
6397 peter_e@gmx.net 490 : 869 : BEGIN(xus);
491 : 368 : ECHO;
492 : 368 : }
493 : :
2304 tgl@sss.pgh.pa.us 494 : 368 : <xb,xh,xq,xe,xus>{quote} {
495 : 162374 : /*
496 : : * When we are scanning a quoted string and see an end
497 : : * quote, we must look ahead for a possible continuation.
498 : : * If we don't see one, we know the end quote was in fact
499 : : * the end of the string. To reduce the lexer table size,
500 : : * we use a single "xqs" state to do the lookahead for all
501 : : * types of strings.
502 : : */
503 : : cur_state->state_before_str_stop = YYSTATE;
504 : 162374 : BEGIN(xqs);
4800 heikki.linnakangas@i 505 : 162374 : ECHO;
506 : 162374 : }
507 : : <xqs>{quotecontinue} {
2304 tgl@sss.pgh.pa.us 508 : 162374 : /*
2304 tgl@sss.pgh.pa.us 509 :UBC 0 : * Found a quote continuation, so return to the in-quote
510 : : * state and continue scanning the literal. Nothing is
511 : : * added to the literal's contents.
512 : : */
513 : : BEGIN(cur_state->state_before_str_stop);
4800 heikki.linnakangas@i 514 : 0 : ECHO;
515 : 0 : }
516 : : <xqs>{quotecontinuefail} |
2304 tgl@sss.pgh.pa.us 517 : 0 : <xqs>{other} {
2304 tgl@sss.pgh.pa.us 518 :CBC 161559 : /*
519 : : * Failed to see a quote continuation. Throw back
520 : : * everything after the end quote, and handle the string
521 : : * according to the state we were in previously.
522 : : */
523 : : yyless(0);
6397 peter_e@gmx.net 524 : 161559 : BEGIN(INITIAL);
2304 tgl@sss.pgh.pa.us 525 : 161559 : /* There's nothing to echo ... */
526 : : }
527 : :
6397 peter_e@gmx.net 528 : 161559 : <xq,xe,xus>{xqdouble} {
8111 tgl@sss.pgh.pa.us 529 : 4127 : ECHO;
530 : 4127 : }
531 : : <xq,xus>{xqinside} {
532 : 4127 : ECHO;
533 : 166086 : }
534 : : <xe>{xeinside} {
7365 bruce@momjian.us 535 : 166086 : ECHO;
536 : 1663 : }
537 : : <xe>{xeunicode} {
6064 tgl@sss.pgh.pa.us 538 : 1663 : ECHO;
539 : 124 : }
540 : : <xe>{xeunicodefail} {
541 : 124 : ECHO;
542 : 8 : }
543 : : <xe>{xeescape} {
8111 544 : 8 : ECHO;
545 : 962 : }
546 : : <xe>{xeoctesc} {
547 : 962 : ECHO;
548 : 14 : }
549 : : <xe>{xehexesc} {
7642 bruce@momjian.us 550 : 14 : ECHO;
551 : 6 : }
552 : : <xe>. {
8106 tgl@sss.pgh.pa.us 553 : 6 : /* This is only needed for \ just before EOF */
8106 tgl@sss.pgh.pa.us 554 :UBC 0 : ECHO;
555 : 0 : }
556 : :
557 : 0 : {dolqdelim} {
8106 tgl@sss.pgh.pa.us 558 :CBC 4569 : cur_state->dolqstart = pg_strdup(yytext);
559 : 4569 : BEGIN(xdolq);
560 : 4569 : ECHO;
561 : 4569 : }
562 : : {dolqfailed} {
7649 563 : 4569 : /* throw back all but the initial "$" */
7649 tgl@sss.pgh.pa.us 564 :UBC 0 : yyless(1);
565 : 0 : ECHO;
566 : 0 : }
567 : : <xdolq>{dolqdelim} {
8106 568 : 0 : if (strcmp(yytext, cur_state->dolqstart) == 0)
8106 tgl@sss.pgh.pa.us 569 [ + + ]:CBC 4785 : {
570 : : free(cur_state->dolqstart);
571 : 4569 : cur_state->dolqstart = NULL;
572 : 4569 : BEGIN(INITIAL);
573 : 4569 : }
574 : : else
575 : : {
576 : : /*
577 : : * When we fail to match $...$ to dolqstart, transfer
578 : : * the $... part to the output, but put back the final
579 : : * $ for rescanning. Consider $delim$...$junk$delim$
580 : : */
581 : : yyless(yyleng - 1);
582 : 216 : }
583 : : ECHO;
584 : 4785 : }
585 : : <xdolq>{dolqinside} {
586 : 4785 : ECHO;
587 : 24144 : }
588 : : <xdolq>{dolqfailed} {
7649 589 : 24144 : ECHO;
590 : 574 : }
591 : : <xdolq>. {
8106 592 : 574 : /* This is only needed for $ inside the quoted text */
593 : 1624 : ECHO;
594 : 1624 : }
595 : :
8111 596 : 1624 : {xdstart} {
597 : 6546 : BEGIN(xd);
598 : 6546 : ECHO;
599 : 6546 : }
600 : : {xuistart} {
6397 peter_e@gmx.net 601 : 6546 : BEGIN(xui);
602 : 16 : ECHO;
603 : 16 : }
604 : : <xd>{xdstop} {
8111 tgl@sss.pgh.pa.us 605 : 16 : BEGIN(INITIAL);
606 : 6546 : ECHO;
607 : 6546 : }
608 : : <xui>{dquote} {
6397 peter_e@gmx.net 609 : 6546 : BEGIN(INITIAL);
8111 tgl@sss.pgh.pa.us 610 : 16 : ECHO;
611 : 16 : }
612 : : <xd,xui>{xddouble} {
6397 peter_e@gmx.net 613 : 16 : ECHO;
614 : 67 : }
615 : : <xd,xui>{xdinside} {
616 : 67 : ECHO;
617 : 6623 : }
618 : :
619 : 6623 : {xufailed} {
6397 peter_e@gmx.net 620 :UBC 0 : /* throw back all but the initial u/U */
621 : : yyless(1);
8111 tgl@sss.pgh.pa.us 622 : 0 : ECHO;
623 : 0 : }
624 : :
625 : 0 : {typecast} {
8111 tgl@sss.pgh.pa.us 626 :CBC 36964 : ECHO;
627 : 36964 : }
628 : :
6064 629 : 36964 : {dot_dot} {
6064 tgl@sss.pgh.pa.us 630 :UBC 0 : ECHO;
631 : 0 : }
632 : :
633 : 0 : {colon_equals} {
6064 tgl@sss.pgh.pa.us 634 :CBC 1673 : ECHO;
635 : 1673 : }
636 : :
4074 637 : 1673 : {equals_greater} {
638 : 1535 : ECHO;
639 : 1535 : }
640 : :
4073 641 : 1535 : {less_equals} {
642 : 1483 : ECHO;
643 : 1483 : }
644 : :
645 : 1483 : {greater_equals} {
646 : 4204 : ECHO;
647 : 4204 : }
648 : :
649 : 4204 : {less_greater} {
650 : 902 : ECHO;
651 : 902 : }
652 : :
653 : 902 : {not_equals} {
654 : 1534 : ECHO;
655 : 1534 : }
656 : :
50 peter@eisentraut.org 657 :GNC 1534 : {right_arrow} {
658 : 773 : ECHO;
659 : 773 : }
660 : :
8111 tgl@sss.pgh.pa.us 661 :CBC 773 : /*
662 : : * These rules are specific to psql --- they implement parenthesis
663 : : * counting and detection of command-ending semicolon. These must
664 : : * appear before the {self} rule so that they take precedence over it.
665 : : */
666 : :
667 : 256044 : "(" {
668 : : cur_state->paren_depth++;
669 : 256044 : ECHO;
670 : 256044 : }
671 : :
672 : 256044 : ")" {
673 : 256035 : if (cur_state->paren_depth > 0)
674 [ + - ]: 256035 : cur_state->paren_depth--;
675 : 256035 : ECHO;
676 : 256035 : }
677 : :
678 : 256035 : ";" {
679 : 244597 : ECHO;
1854 peter@eisentraut.org 680 : 244597 : if (cur_state->paren_depth == 0 && cur_state->begin_depth == 0)
8111 tgl@sss.pgh.pa.us 681 [ + + + + ]: 244597 : {
682 : : /* Terminate lexing temporarily */
683 : : cur_state->start_state = YY_START;
1854 peter@eisentraut.org 684 : 244434 : cur_state->identifier_count = 0;
8111 tgl@sss.pgh.pa.us 685 : 244434 : return LEXRES_SEMI;
686 : 244434 : }
687 : : }
688 : :
689 : 163 : /*
690 : : * psql-specific rules to handle backslash commands and variable
691 : : * substitution. We want these before {self}, also.
692 : : */
693 : :
2598 alvherre@alvh.no-ip. 694 : 512 : "\\"[;:] {
695 : : /* Force a semi-colon or colon into the query buffer */
696 : : psqlscan_emit(cur_state, yytext + 1, 1);
1854 peter@eisentraut.org 697 : 512 : if (yytext[1] == ';')
698 [ + - ]: 512 : cur_state->identifier_count = 0;
8111 tgl@sss.pgh.pa.us 699 : 512 : }
700 : :
701 : 512 : "\\" {
702 : 33087 : /* Terminate lexing temporarily */
703 : : cur_state->start_state = YY_START;
704 : 33087 : return LEXRES_BACKSLASH;
705 : 33087 : }
706 : :
707 : : :{variable_char}+ {
708 : 1791 : /* Possible psql variable substitution */
709 : : char *varname;
710 : : char *value;
711 : :
712 : : varname = psqlscan_extract_substring(cur_state,
3699 713 : 1791 : yytext + 1,
714 : 1791 : yyleng - 1);
3700 715 : 1791 : if (cur_state->callbacks->get_variable)
716 [ + + ]: 1791 : value = cur_state->callbacks->get_variable(varname,
3321 717 : 1195 : PQUOTE_PLAIN,
718 : : cur_state->cb_passthrough);
719 : : else
720 : : value = NULL;
8111 721 : 596 :
722 : : if (value)
723 [ + + ]: 1791 : {
724 : : /* It is a variable, check for recursion */
725 : : if (psqlscan_var_is_current_source(cur_state, varname))
5844 726 [ - + ]: 887 : {
727 : : /* Recursive expansion --- don't go there */
728 : : pg_log_warning("skipping recursive expansion of variable \"%s\"",
3700 tgl@sss.pgh.pa.us 729 :UBC 0 : varname);
730 : : /* Instead copy the string as is */
731 : : ECHO;
5844 732 : 0 : }
733 : : else
734 : : {
735 : : /* OK, perform substitution */
736 : : psqlscan_push_new_buffer(cur_state, value, varname);
5844 tgl@sss.pgh.pa.us 737 :CBC 887 : /* yy_scan_string already made buffer active */
738 : : }
739 : : free(value);
8111 740 : 887 : }
741 : : else
742 : : {
743 : : /*
744 : : * if the variable doesn't exist we'll copy the string
745 : : * as is
746 : : */
747 : : ECHO;
748 : 904 : }
749 : :
750 : : free(varname);
751 : 1791 : }
752 : :
5366 753 : 1791 : :'{variable_char}+' {
3321 754 : 632 : psqlscan_escape_variable(cur_state, yytext, yyleng,
755 : 632 : PQUOTE_SQL_LITERAL);
756 : : }
757 : :
5366 758 : 632 : :\"{variable_char}+\" {
3321 759 : 21 : psqlscan_escape_variable(cur_state, yytext, yyleng,
760 : 21 : PQUOTE_SQL_IDENT);
761 : : }
762 : :
3148 andrew@dunslane.net 763 : 21 : :\{\?{variable_char}+\} {
764 : 8 : psqlscan_test_variable(cur_state, yytext, yyleng);
765 : 8 : }
766 : :
5367 tgl@sss.pgh.pa.us 767 : 8 : /*
768 : : * These rules just avoid the need for scanner backup if one of the
769 : : * three rules above fails to match completely.
770 : : */
771 : :
5366 tgl@sss.pgh.pa.us 772 :UBC 0 : :'{variable_char}* {
773 : : /* Throw back everything but the colon */
774 : : yyless(1);
5367 775 : 0 : ECHO;
776 : 0 : }
777 : :
5366 778 : 0 : :\"{variable_char}* {
5367 779 : 0 : /* Throw back everything but the colon */
780 : : yyless(1);
781 : 0 : ECHO;
782 : 0 : }
783 : :
3148 andrew@dunslane.net 784 : 0 : :\{\?{variable_char}* {
785 : 0 : /* Throw back everything but the colon */
786 : : yyless(1);
787 : 0 : ECHO;
788 : 0 : }
789 : : :\{ {
790 : 0 : /* Throw back everything but the colon */
791 : 0 : yyless(1);
792 : 0 : ECHO;
793 : 0 : }
794 : :
8111 tgl@sss.pgh.pa.us 795 : 0 : /*
796 : : * Back to backend-compatible rules.
797 : : */
798 : :
8111 tgl@sss.pgh.pa.us 799 :CBC 446120 : {self} {
800 : : ECHO;
801 : 446120 : }
802 : :
803 : 446120 : {operator} {
804 : 12739 : /*
805 : : * Check for embedded slash-star or dash-dash; those
806 : : * are comment starts, so operator must stop there.
807 : : * Note that slash-star or dash-dash at the first
808 : : * character will match a prior rule, not this one.
809 : : */
810 : : int nchars = yyleng;
3699 811 : 12739 : char *slashstar = strstr(yytext, "/*");
812 : 12739 : char *dashdash = strstr(yytext, "--");
8111 813 : 12739 :
814 : : if (slashstar && dashdash)
815 [ + + - + ]: 12739 : {
816 : : /* if both appear, take the first one */
817 : : if (slashstar > dashdash)
8111 tgl@sss.pgh.pa.us 818 [ # # ]:UBC 0 : slashstar = dashdash;
819 : 0 : }
820 : : else if (!slashstar)
8111 tgl@sss.pgh.pa.us 821 [ + + ]:CBC 12739 : slashstar = dashdash;
822 : 12699 : if (slashstar)
823 [ + + ]: 12739 : nchars = slashstar - yytext;
824 : 48 :
825 : : /*
826 : : * For SQL compatibility, '+' and '-' cannot be the
827 : : * last char of a multi-char operator unless the operator
828 : : * contains chars that are not in SQL operators.
829 : : * The idea is to lex '=-' as two operators, but not
830 : : * to forbid operator names like '?-' that could not be
831 : : * sequences of SQL operators.
832 : : */
833 : : if (nchars > 1 &&
2812 rhodiumtoad@postgres 834 [ + + ]: 12739 : (yytext[nchars - 1] == '+' ||
835 [ + + ]: 11708 : yytext[nchars - 1] == '-'))
8111 tgl@sss.pgh.pa.us 836 [ + + ]: 11704 : {
837 : : int ic;
838 : :
839 : : for (ic = nchars - 2; ic >= 0; ic--)
840 [ + + ]: 385 : {
841 : : char c = yytext[ic];
2812 rhodiumtoad@postgres 842 : 326 : if (c == '~' || c == '!' || c == '@' ||
843 [ + - + + : 326 : c == '#' || c == '^' || c == '&' ||
+ - + + ]
844 [ + - + - : 270 : c == '|' || c == '`' || c == '?' ||
+ + ]
845 [ + - + + : 106 : c == '%')
+ - ]
846 : : break;
847 : : }
848 : : if (ic < 0)
849 [ + + ]: 291 : {
850 : : /*
851 : : * didn't find a qualifying character, so remove
852 : : * all trailing [+-]
853 : : */
854 : : do {
855 : : nchars--;
856 : 59 : } while (nchars > 1 &&
857 [ + + ]: 59 : (yytext[nchars - 1] == '+' ||
858 [ - + ]: 23 : yytext[nchars - 1] == '-'));
859 [ - + ]: 23 : }
860 : : }
861 : :
862 : : if (nchars < yyleng)
8111 tgl@sss.pgh.pa.us 863 [ + + ]: 12739 : {
864 : : /* Strip the unwanted chars from the token */
865 : : yyless(nchars);
866 : 107 : }
867 : : ECHO;
868 : 12739 : }
869 : :
870 : 12739 : {param} {
871 : 789 : ECHO;
872 : 789 : }
873 : : {param_junk} {
1539 peter@eisentraut.org 874 : 789 : ECHO;
875 : 8 : }
876 : :
1238 877 : 8 : {decinteger} {
878 : 142070 : ECHO;
879 : 142070 : }
880 : : {hexinteger} {
881 : 142070 : ECHO;
882 : 83 : }
883 : : {octinteger} {
884 : 83 : ECHO;
885 : 40 : }
886 : : {bininteger} {
887 : 40 : ECHO;
888 : 40 : }
889 : : {hexfail} {
8111 tgl@sss.pgh.pa.us 890 : 40 : ECHO;
891 : 4 : }
892 : : {octfail} {
893 : 4 : ECHO;
894 : 4 : }
895 : : {binfail} {
1238 peter@eisentraut.org 896 : 4 : ECHO;
897 : 4 : }
898 : : {numeric} {
899 : 4 : ECHO;
900 : 5280 : }
901 : : {numericfail} {
6018 tgl@sss.pgh.pa.us 902 : 5280 : /* throw back the .., and treat as integer */
3699 tgl@sss.pgh.pa.us 903 :UBC 0 : yyless(yyleng - 2);
6018 904 : 0 : ECHO;
905 : 0 : }
906 : : {real} {
8111 907 : 0 : ECHO;
8111 tgl@sss.pgh.pa.us 908 :CBC 418 : }
909 : : {realfail} {
7649 910 : 418 : ECHO;
911 : 4 : }
912 : : {integer_junk} {
1539 peter@eisentraut.org 913 : 4 : ECHO;
914 : 44 : }
915 : : {numeric_junk} {
916 : 44 : ECHO;
917 : 32 : }
918 : : {real_junk} {
7649 tgl@sss.pgh.pa.us 919 : 32 : ECHO;
7649 tgl@sss.pgh.pa.us 920 :UBC 0 : }
921 : :
8111 922 : 0 :
8111 tgl@sss.pgh.pa.us 923 :CBC 1839154 : {identifier} {
924 : : /*
925 : : * We need to track if we are inside a BEGIN .. END block
926 : : * in a function definition, so that semicolons contained
927 : : * therein don't terminate the whole statement. Short of
928 : : * writing a full parser here, the following heuristic
929 : : * should work. First, we track whether the beginning of
930 : : * the statement matches CREATE [OR REPLACE]
931 : : * {FUNCTION|PROCEDURE|SCHEMA}. (Allowing this in
932 : : * CREATE SCHEMA, without tracking whether we're within a
933 : : * CREATE FUNCTION/PROCEDURE subcommand, is a bit shaky
934 : : * but should be okay with the present set of valid
935 : : * subcommands.)
936 : : */
937 : :
938 : : if (cur_state->identifier_count == 0)
1845 peter@eisentraut.org 939 [ + + ]: 1839154 : memset(cur_state->identifiers, 0, sizeof(cur_state->identifiers));
940 : 250994 :
941 : : if (cur_state->identifier_count < sizeof(cur_state->identifiers))
1854 942 [ + + ]: 1839154 : {
943 : : if (pg_strcasecmp(yytext, "create") == 0 ||
29 tgl@sss.pgh.pa.us 944 [ + + + + ]:GNC 1677994 : pg_strcasecmp(yytext, "function") == 0 ||
945 [ + + ]: 1628653 : pg_strcasecmp(yytext, "procedure") == 0 ||
946 [ + + ]: 1621626 : pg_strcasecmp(yytext, "or") == 0 ||
947 [ + + ]: 1619759 : pg_strcasecmp(yytext, "replace") == 0 ||
948 [ + + ]: 1616864 : pg_strcasecmp(yytext, "schema") == 0)
1845 peter@eisentraut.org 949 [ + + ]:CBC 807702 : cur_state->identifiers[cur_state->identifier_count] = pg_tolower((unsigned char) yytext[0]);
1854 950 : 54004 : }
951 : :
952 : : cur_state->identifier_count++;
1845 953 : 1839154 :
954 : : if (cur_state->identifiers[0] == 'c' &&
955 [ + + ]: 1839154 : (cur_state->identifiers[1] == 'f' || cur_state->identifiers[1] == 'p' ||
956 [ + + + + ]: 440145 : (cur_state->identifiers[1] == 'o' && cur_state->identifiers[2] == 'r' &&
29 tgl@sss.pgh.pa.us 957 [ + + + + ]:GNC 401993 : (cur_state->identifiers[3] == 'f' || cur_state->identifiers[3] == 'p')) ||
958 [ + + + + ]: 14967 : cur_state->identifiers[1] == 's') &&
1845 peter@eisentraut.org 959 [ + + + + ]:CBC 392685 : cur_state->paren_depth == 0)
1854 960 [ + + ]: 54045 : {
961 : : if (pg_strcasecmp(yytext, "begin") == 0)
1845 962 [ + + ]: 42573 : cur_state->begin_depth++;
963 : 115 : else if (pg_strcasecmp(yytext, "case") == 0)
964 [ + + ]: 42458 : {
965 : : /*
966 : : * CASE also ends with END. We only need to track
967 : : * this if we are already inside a BEGIN.
968 : : */
969 : : if (cur_state->begin_depth >= 1)
970 [ + - ]: 4 : cur_state->begin_depth++;
971 : 4 : }
972 : : else if (pg_strcasecmp(yytext, "end") == 0)
973 [ + + ]: 42454 : {
974 : : if (cur_state->begin_depth > 0)
975 [ + - ]: 119 : cur_state->begin_depth--;
976 : 119 : }
977 : : }
978 : :
979 : : ECHO;
8111 tgl@sss.pgh.pa.us 980 : 1839154 : }
981 : :
982 : 1839154 : {other} {
8111 tgl@sss.pgh.pa.us 983 :GBC 8 : ECHO;
984 : 8 : }
985 : :
986 : 8 : <<EOF>> {
3699 tgl@sss.pgh.pa.us 987 :CBC 520172 : if (cur_state->buffer_stack == NULL)
3700 988 [ + + ]: 520172 : {
989 : : cur_state->start_state = YY_START;
3699 990 : 519285 : return LEXRES_EOL; /* end of input reached */
3700 991 : 519285 : }
992 : :
993 : : /*
994 : : * We were expanding a variable, so pop the inclusion
995 : : * stack and keep lexing
996 : : */
997 : : psqlscan_pop_buffer_stack(cur_state);
3699 998 : 887 : psqlscan_select_top_buffer(cur_state);
8111 999 : 887 : }
1000 : :
1001 : 887 : %%
8111 tgl@sss.pgh.pa.us 1002 :UBC 0 :
1003 : : /* LCOV_EXCL_STOP */
1004 : :
1005 : : /*
1006 : : * Create a lexer working state struct.
1007 : : *
1008 : : * callbacks is a struct of function pointers that encapsulate some
1009 : : * behavior we need from the surrounding program. This struct must
1010 : : * remain valid for the lifespan of the PsqlScanState.
1011 : : */
1012 : : PsqlScanState
1013 : : psql_scan_create(const PsqlScanCallbacks *callbacks)
8111 tgl@sss.pgh.pa.us 1014 :CBC 10702 : {
1015 : : PsqlScanState state;
1016 : :
1017 : : state = pg_malloc0_object(PsqlScanStateData);
1018 : 10702 :
1019 : : state->callbacks = callbacks;
3700 1020 : 10702 :
1021 : : yylex_init(&state->scanner);
3699 1022 : 10702 :
1023 : : yyset_extra(state, state->scanner);
3700 1024 : 10702 :
1025 : : psql_scan_reset(state);
8111 1026 : 10702 :
1027 : : return state;
1028 : 10702 : }
1029 : :
1030 : : /*
1031 : : * Destroy a lexer working state struct, releasing all resources.
1032 : : */
1033 : : void
1034 : : psql_scan_destroy(PsqlScanState state)
1035 : 10646 : {
1036 : : psql_scan_finish(state);
1037 : 10646 :
1038 : : psql_scan_reset(state);
8106 1039 : 10646 :
1040 : : yylex_destroy(state->scanner);
3700 1041 : 10646 :
1042 : : free(state);
8111 1043 : 10646 : }
1044 : 10646 :
1045 : : /*
1046 : : * Set the callback passthrough pointer for the lexer.
1047 : : *
1048 : : * This could have been integrated into psql_scan_create, but keeping it
1049 : : * separate allows the application to change the pointer later, which might
1050 : : * be useful.
1051 : : */
1052 : : void
1053 : : psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
3340 1054 : 10103 : {
1055 : : state->cb_passthrough = passthrough;
1056 : 10103 : }
1057 : 10103 :
1058 : : /*
1059 : : * Set up to perform lexing of the given input line.
1060 : : *
1061 : : * The text at *line, extending for line_len bytes, will be scanned by
1062 : : * subsequent calls to the psql_scan routines. psql_scan_finish should
1063 : : * be called when scanning is complete. Note that the lexer retains
1064 : : * a pointer to the storage at *line --- this string must not be altered
1065 : : * or freed until after psql_scan_finish is called.
1066 : : *
1067 : : * encoding is the libpq identifier for the character encoding in use,
1068 : : * and std_strings says whether standard_conforming_strings is on.
1069 : : */
1070 : : void
1071 : : psql_scan_setup(PsqlScanState state,
3700 1072 : 519640 : const char *line, int line_len,
1073 : : int encoding, bool std_strings)
1074 : : {
1075 : : /* Mustn't be scanning already */
1076 : : Assert(state->scanbufhandle == NULL);
4890 andrew@dunslane.net 1077 [ - + ]: 519640 : Assert(state->buffer_stack == NULL);
8111 tgl@sss.pgh.pa.us 1078 [ - + ]: 519640 :
1079 : : /* Do we need to hack the character set encoding? */
1080 : : state->encoding = encoding;
3700 1081 : 519640 : state->safe_encoding = pg_valid_server_encoding_id(encoding);
1082 : 519640 :
1083 : : /* Save standard-strings flag as well */
1084 : : state->std_strings = std_strings;
8111 1085 : 519640 :
1086 : : /* Set up flex input buffer with appropriate translation and padding */
1087 : : state->scanbufhandle = psqlscan_prepare_buffer(state, line, line_len,
3699 1088 : 519640 : &state->scanbuf);
1089 : : state->scanline = line;
8111 1090 : 519640 :
1091 : : /* Set lookaside data in case we have to map unsafe encoding */
1092 : : state->curline = state->scanbuf;
1093 : 519640 : state->refline = state->scanline;
432 1094 : 519640 :
1095 : : /* Initialize state for psql_scan_get_location() */
1096 : : state->cur_line_no = 0; /* yylex not called yet */
1097 : 519640 : state->cur_line_ptr = state->scanbuf;
8111 1098 : 519640 : }
1099 : 519640 :
1100 : : /*
1101 : : * Do lexical analysis of SQL command text.
1102 : : *
1103 : : * The text previously passed to psql_scan_setup is scanned, and appended
1104 : : * (possibly with transformation) to query_buf.
1105 : : *
1106 : : * The return value indicates the condition that stopped scanning:
1107 : : *
1108 : : * PSCAN_SEMICOLON: found a command-ending semicolon. (The semicolon is
1109 : : * transferred to query_buf.) The command accumulated in query_buf should
1110 : : * be executed, then clear query_buf and call again to scan the remainder
1111 : : * of the line.
1112 : : *
1113 : : * PSCAN_BACKSLASH: found a backslash that starts a special command.
1114 : : * Any previous data on the line has been transferred to query_buf.
1115 : : * The caller will typically next apply a separate flex lexer to scan
1116 : : * the special command.
1117 : : *
1118 : : * PSCAN_INCOMPLETE: the end of the line was reached, but we have an
1119 : : * incomplete SQL command. *prompt is set to the appropriate prompt type.
1120 : : *
1121 : : * PSCAN_EOL: the end of the line was reached, and there is no lexical
1122 : : * reason to consider the command incomplete. The caller may or may not
1123 : : * choose to send it. *prompt is set to the appropriate prompt type if
1124 : : * the caller chooses to collect more input.
1125 : : *
1126 : : * In the PSCAN_INCOMPLETE and PSCAN_EOL cases, psql_scan_finish() should
1127 : : * be called next, then the cycle may be repeated with a fresh input line.
1128 : : *
1129 : : * In all cases, *prompt is set to an appropriate prompt type code for the
1130 : : * next line-input operation.
1131 : : */
1132 : : PsqlScanResult
1133 : : psql_scan(PsqlScanState state,
1134 : 796806 : PQExpBuffer query_buf,
1135 : : promptStatus_t *prompt)
1136 : : {
1137 : : PsqlScanResult result;
1138 : : int lexresult;
1139 : :
1140 : : /* Must be scanning already */
1141 : : Assert(state->scanbufhandle != NULL);
1142 [ - + ]: 796806 :
1143 : : /* Set current output target */
1144 : : state->output_buf = query_buf;
1145 : 796806 :
1146 : : /* Set input source */
1147 : : if (state->buffer_stack != NULL)
3700 1148 [ + + ]: 796806 : yy_switch_to_buffer(state->buffer_stack->buf, state->scanner);
8111 1149 : 60 : else
1150 : : yy_switch_to_buffer(state->scanbufhandle, state->scanner);
1151 : 796746 :
1152 : : /* And lex. */
1153 : : lexresult = yylex(NULL, state->scanner);
1154 : 796806 :
1155 : : /* Notify psql_scan_get_location() that a yylex call has been made. */
1156 : : if (state->cur_line_no == 0)
432 1157 [ + + ]: 796806 : state->cur_line_no = 1;
1158 : 519638 :
1159 : : /*
1160 : : * Check termination state and return appropriate result info.
1161 : : */
1162 : : switch (lexresult)
8111 1163 [ + + + - ]: 796806 : {
1164 : : case LEXRES_EOL: /* end of input */
1165 : 519285 : switch (state->start_state)
1166 [ + - + + : 519285 : {
- + + + -
- - ]
1167 : : case INITIAL:
2304 1168 : 487707 : case xqs: /* we treat this like INITIAL */
1169 : : if (state->paren_depth > 0)
8111 1170 [ + + ]: 487707 : {
1171 : : result = PSCAN_INCOMPLETE;
1172 : 42568 : *prompt = PROMPT_PAREN;
1173 : 42568 : }
1174 : : else if (state->begin_depth > 0)
1854 peter@eisentraut.org 1175 [ + + ]: 445139 : {
1176 : : result = PSCAN_INCOMPLETE;
1177 : 665 : *prompt = PROMPT_CONTINUE;
1178 : 665 : }
1179 : : else if (query_buf->len > 0)
8111 tgl@sss.pgh.pa.us 1180 [ + + ]: 444474 : {
1181 : : result = PSCAN_EOL;
1182 : 94234 : *prompt = PROMPT_CONTINUE;
1183 : 94234 : }
1184 : : else
1185 : : {
1186 : : /* never bother to send an empty buffer */
1187 : : result = PSCAN_INCOMPLETE;
1188 : 350240 : *prompt = PROMPT_READY;
1189 : 350240 : }
1190 : : break;
1191 : 487707 : case xb:
8111 tgl@sss.pgh.pa.us 1192 :UBC 0 : result = PSCAN_INCOMPLETE;
1193 : 0 : *prompt = PROMPT_SINGLEQUOTE;
1194 : 0 : break;
1195 : 0 : case xc:
8111 tgl@sss.pgh.pa.us 1196 :CBC 513 : result = PSCAN_INCOMPLETE;
1197 : 513 : *prompt = PROMPT_COMMENT;
1198 : 513 : break;
1199 : 513 : case xd:
1200 : 23 : result = PSCAN_INCOMPLETE;
1201 : 23 : *prompt = PROMPT_DOUBLEQUOTE;
1202 : 23 : break;
1203 : 23 : case xh:
8111 tgl@sss.pgh.pa.us 1204 :UBC 0 : result = PSCAN_INCOMPLETE;
1205 : 0 : *prompt = PROMPT_SINGLEQUOTE;
1206 : 0 : break;
5670 1207 : 0 : case xe:
8111 tgl@sss.pgh.pa.us 1208 :CBC 301 : result = PSCAN_INCOMPLETE;
1209 : 301 : *prompt = PROMPT_SINGLEQUOTE;
1210 : 301 : break;
5670 1211 : 301 : case xq:
7365 bruce@momjian.us 1212 : 7022 : result = PSCAN_INCOMPLETE;
1213 : 7022 : *prompt = PROMPT_SINGLEQUOTE;
1214 : 7022 : break;
8106 tgl@sss.pgh.pa.us 1215 : 7022 : case xdolq:
1216 : 23719 : result = PSCAN_INCOMPLETE;
1217 : 23719 : *prompt = PROMPT_DOLLARQUOTE;
1218 : 23719 : break;
5670 1219 : 23719 : case xui:
5670 tgl@sss.pgh.pa.us 1220 :UBC 0 : result = PSCAN_INCOMPLETE;
1221 : 0 : *prompt = PROMPT_DOUBLEQUOTE;
1222 : 0 : break;
1223 : 0 : case xus:
1224 : 0 : result = PSCAN_INCOMPLETE;
1225 : 0 : *prompt = PROMPT_SINGLEQUOTE;
1226 : 0 : break;
8111 1227 : 0 : default:
1228 : 0 : /* can't get here */
1229 : : fprintf(stderr, "invalid YY_START\n");
1230 : 0 : exit(1);
1231 : 0 : }
1232 : : break;
8111 tgl@sss.pgh.pa.us 1233 :CBC 519285 : case LEXRES_SEMI: /* semicolon */
1234 : 244434 : result = PSCAN_SEMICOLON;
1235 : 244434 : *prompt = PROMPT_READY;
1236 : 244434 : break;
1237 : 244434 : case LEXRES_BACKSLASH: /* backslash */
1238 : 33087 : result = PSCAN_BACKSLASH;
1239 : 33087 : *prompt = PROMPT_READY;
1240 : 33087 : break;
1241 : 33087 : default:
8111 tgl@sss.pgh.pa.us 1242 :UBC 0 : /* can't get here */
1243 : : fprintf(stderr, "invalid yylex result\n");
1244 : 0 : exit(1);
1245 : 0 : }
1246 : :
1247 : : return result;
8111 tgl@sss.pgh.pa.us 1248 :CBC 796806 : }
1249 : :
1250 : : /*
1251 : : * Clean up after scanning a string. This flushes any unread input and
1252 : : * releases resources (but not the PsqlScanState itself). Note however
1253 : : * that this does not reset the lexer scan state; that can be done by
1254 : : * psql_scan_reset(), which is an orthogonal operation.
1255 : : *
1256 : : * It is legal to call this when not scanning anything (makes it easier
1257 : : * to deal with error recovery).
1258 : : */
1259 : : void
1260 : : psql_scan_finish(PsqlScanState state)
1261 : 529915 : {
1262 : : /* Drop any incomplete variable expansions. */
1263 : : while (state->buffer_stack != NULL)
3699 1264 [ - + ]: 529915 : psqlscan_pop_buffer_stack(state);
8111 tgl@sss.pgh.pa.us 1265 :UBC 0 :
1266 : : /* Done with the outer scan buffer, too */
1267 : : if (state->scanbufhandle)
3700 tgl@sss.pgh.pa.us 1268 [ + + ]:CBC 529915 : yy_delete_buffer(state->scanbufhandle, state->scanner);
8111 1269 : 519585 : state->scanbufhandle = NULL;
1270 : 529915 : if (state->scanbuf)
1271 [ + + ]: 529915 : free(state->scanbuf);
1272 : 519585 : state->scanbuf = NULL;
1273 : 529915 : }
1274 : 529915 :
1275 : : /*
1276 : : * Reset lexer scanning state to start conditions. This is appropriate
1277 : : * for executing \r psql commands (or any other time that we discard the
1278 : : * prior contents of query_buf). It is not, however, necessary to do this
1279 : : * when we execute and clear the buffer after getting a PSCAN_SEMICOLON or
1280 : : * PSCAN_EOL scan result, because the scan state must be INITIAL when those
1281 : : * conditions are returned.
1282 : : *
1283 : : * Note that this is unrelated to flushing unread input; that task is
1284 : : * done by psql_scan_finish().
1285 : : */
1286 : : void
1287 : : psql_scan_reset(PsqlScanState state)
1288 : 23462 : {
1289 : : state->start_state = INITIAL;
1290 : 23462 : state->paren_depth = 0;
1291 : 23462 : state->xcdepth = 0; /* not really necessary */
8106 1292 : 23462 : if (state->dolqstart)
1293 [ - + ]: 23462 : free(state->dolqstart);
8106 tgl@sss.pgh.pa.us 1294 :UBC 0 : state->dolqstart = NULL;
1854 peter@eisentraut.org 1295 :CBC 23462 : state->identifier_count = 0;
1296 : 23462 : state->begin_depth = 0;
8111 tgl@sss.pgh.pa.us 1297 : 23462 : }
1298 : 23462 :
1299 : : /*
1300 : : * Reselect this lexer (psqlscan.l) after using another one.
1301 : : *
1302 : : * Currently and for foreseeable uses, it's sufficient to reset to INITIAL
1303 : : * state, because we'd never switch to another lexer in a different state.
1304 : : * However, we don't want to reset e.g. paren_depth, so this can't be
1305 : : * the same as psql_scan_reset().
1306 : : *
1307 : : * Note: psql setjmp error recovery just calls psql_scan_reset(), so that
1308 : : * must be a superset of this.
1309 : : *
1310 : : * Note: it seems likely that other lexers could just assign INITIAL for
1311 : : * themselves, since that probably has the value zero in every flex-generated
1312 : : * lexer. But let's not assume that.
1313 : : */
1314 : : void
1315 : : psql_scan_reselect_sql_lexer(PsqlScanState state)
1316 : 157290 : {
1317 : : state->start_state = INITIAL;
1318 : 157290 : }
1319 : 157290 :
1320 : : /*
1321 : : * Return true if lexer is currently in an "inside quotes" state.
1322 : : *
1323 : : * This is pretty grotty but is needed to preserve the old behavior
1324 : : * that mainloop.c drops blank lines not inside quotes without even
1325 : : * echoing them.
1326 : : */
1327 : : bool
1328 : : psql_scan_in_quote(PsqlScanState state)
5366 1329 : 100070 : {
1330 : : return state->start_state != INITIAL &&
496 peter@eisentraut.org 1331 [ + + ]: 100688 : state->start_state != xqs;
5366 tgl@sss.pgh.pa.us 1332 [ + + ]: 618 : }
1333 : :
1334 : : /*
1335 : : * Return the current scanning location (end+1 of last scanned token),
1336 : : * as a line number counted from 1 and an offset from string start.
1337 : : *
1338 : : * This considers only the outermost input string, and therefore is of
1339 : : * limited use for programs that use psqlscan_push_new_buffer().
1340 : : *
1341 : : * It would be a bit easier probably to use "%option yylineno" to count
1342 : : * lines, but the flex manual says that has a performance cost, and only
1343 : : * a minority of programs using psqlscan have need for this functionality.
1344 : : * So we implement it ourselves without adding overhead to the lexer itself.
1345 : : */
1346 : : void
1347 : : psql_scan_get_location(PsqlScanState state,
432 1348 : 1737 : int *lineno, int *offset)
1349 : : {
1350 : : const char *line_end;
1351 : :
1352 : : /*
1353 : : * We rely on flex's having stored a NUL after the current token in
1354 : : * scanbuf. Therefore we must specially handle the state before yylex()
1355 : : * has been called, when obviously that won't have happened yet.
1356 : : */
1357 : : if (state->cur_line_no == 0)
1358 [ - + ]: 1737 : {
1359 : : *lineno = 1;
432 tgl@sss.pgh.pa.us 1360 :UBC 0 : *offset = 0;
1361 : 0 : return;
1362 : 0 : }
1363 : :
1364 : : /*
1365 : : * Advance cur_line_no/cur_line_ptr past whatever has been lexed so far.
1366 : : * Doing this prevents repeated calls from being O(N^2) for long inputs.
1367 : : */
1368 : : while ((line_end = strchr(state->cur_line_ptr, '\n')) != NULL)
432 tgl@sss.pgh.pa.us 1369 [ + + ]:CBC 2210 : {
1370 : : state->cur_line_no++;
1371 : 473 : state->cur_line_ptr = line_end + 1;
1372 : 473 : }
1373 : : state->cur_line_ptr += strlen(state->cur_line_ptr);
1374 : 1737 :
1375 : : /* Report current location. */
1376 : : *lineno = state->cur_line_no;
1377 : 1737 : *offset = state->cur_line_ptr - state->scanbuf;
1378 : 1737 : }
1379 : :
1380 : : /*
1381 : : * Push the given string onto the stack of stuff to scan.
1382 : : *
1383 : : * NOTE SIDE EFFECT: the new buffer is made the active flex input buffer.
1384 : : */
1385 : : void
1386 : : psqlscan_push_new_buffer(PsqlScanState state, const char *newstr,
3699 1387 : 887 : const char *varname)
1388 : : {
1389 : : StackElem *stackelem;
1390 : :
1391 : : stackelem = pg_malloc_object(StackElem);
5844 1392 : 887 :
1393 : : /*
1394 : : * In current usage, the passed varname points at the current flex input
1395 : : * buffer; we must copy it before calling psqlscan_prepare_buffer()
1396 : : * because that will change the buffer state.
1397 : : */
1398 : : stackelem->varname = varname ? pg_strdup(varname) : NULL;
1399 [ + - ]: 887 :
1400 : : stackelem->buf = psqlscan_prepare_buffer(state, newstr, strlen(newstr),
3699 1401 : 887 : &stackelem->bufstring);
1402 : : state->curline = stackelem->bufstring;
3700 1403 : 887 : if (state->safe_encoding)
8111 1404 [ + - ]: 887 : {
1405 : : stackelem->origstring = NULL;
3700 1406 : 887 : state->refline = stackelem->bufstring;
8111 1407 : 887 : }
1408 : : else
1409 : : {
1410 : : stackelem->origstring = pg_strdup(newstr);
3700 tgl@sss.pgh.pa.us 1411 :UBC 0 : state->refline = stackelem->origstring;
8111 1412 : 0 : }
1413 : : stackelem->next = state->buffer_stack;
3700 tgl@sss.pgh.pa.us 1414 :CBC 887 : state->buffer_stack = stackelem;
8111 1415 : 887 : }
1416 : 887 :
1417 : : /*
1418 : : * Pop the topmost buffer stack item (there must be one!)
1419 : : *
1420 : : * NB: after this, the flex input state is unspecified; caller must
1421 : : * switch to an appropriate buffer to continue lexing.
1422 : : * See psqlscan_select_top_buffer().
1423 : : */
1424 : : void
1425 : : psqlscan_pop_buffer_stack(PsqlScanState state)
5844 1426 : 887 : {
1427 : : StackElem *stackelem = state->buffer_stack;
1428 : 887 :
1429 : : state->buffer_stack = stackelem->next;
3700 1430 : 887 : yy_delete_buffer(stackelem->buf, state->scanner);
5844 1431 : 887 : free(stackelem->bufstring);
1432 : 887 : if (stackelem->origstring)
1433 [ - + ]: 887 : free(stackelem->origstring);
5844 tgl@sss.pgh.pa.us 1434 :UBC 0 : if (stackelem->varname)
5844 tgl@sss.pgh.pa.us 1435 [ + - ]:CBC 887 : free(stackelem->varname);
1436 : 887 : free(stackelem);
1437 : 887 : }
1438 : 887 :
1439 : : /*
1440 : : * Select the topmost surviving buffer as the active input.
1441 : : */
1442 : : void
1443 : : psqlscan_select_top_buffer(PsqlScanState state)
3699 1444 : 887 : {
1445 : : StackElem *stackelem = state->buffer_stack;
1446 : 887 :
1447 : : if (stackelem != NULL)
1448 [ - + ]: 887 : {
1449 : : yy_switch_to_buffer(stackelem->buf, state->scanner);
3699 tgl@sss.pgh.pa.us 1450 :UBC 0 : state->curline = stackelem->bufstring;
1451 : 0 : state->refline = stackelem->origstring ? stackelem->origstring : stackelem->bufstring;
1452 [ # # ]: 0 : }
1453 : : else
1454 : : {
1455 : : yy_switch_to_buffer(state->scanbufhandle, state->scanner);
3699 tgl@sss.pgh.pa.us 1456 :CBC 887 : state->curline = state->scanbuf;
1457 : 887 : state->refline = state->scanline;
1458 : 887 : }
1459 : : }
1460 : 887 :
1461 : : /*
1462 : : * Check if specified variable name is the source for any string
1463 : : * currently being scanned
1464 : : */
1465 : : bool
1466 : : psqlscan_var_is_current_source(PsqlScanState state, const char *varname)
5844 1467 : 887 : {
1468 : : StackElem *stackelem;
1469 : :
1470 : : for (stackelem = state->buffer_stack;
1471 : 887 : stackelem != NULL;
1472 [ - + ]: 887 : stackelem = stackelem->next)
5844 tgl@sss.pgh.pa.us 1473 :UBC 0 : {
1474 : : if (stackelem->varname && strcmp(stackelem->varname, varname) == 0)
1475 [ # # # # ]: 0 : return true;
1476 : 0 : }
1477 : : return false;
5844 tgl@sss.pgh.pa.us 1478 :CBC 887 : }
1479 : :
1480 : : /*
1481 : : * Set up a flex input buffer to scan the given data. We always make a
1482 : : * copy of the data. If working in an unsafe encoding, the copy has
1483 : : * multibyte sequences replaced by FFs to avoid fooling the lexer rules.
1484 : : *
1485 : : * NOTE SIDE EFFECT: the new buffer is made the active flex input buffer.
1486 : : */
1487 : : YY_BUFFER_STATE
1488 : : psqlscan_prepare_buffer(PsqlScanState state, const char *txt, int len,
3699 1489 : 520527 : char **txtcopy)
1490 : : {
1491 : : char *newtxt;
1492 : :
1493 : : /* Flex wants two \0 characters after the actual data */
1494 : : newtxt = pg_malloc_array(char, (len + 2));
8111 1495 : 520527 : *txtcopy = newtxt;
1496 : 520527 : newtxt[len] = newtxt[len + 1] = YY_END_OF_BUFFER_CHAR;
1497 : 520527 :
1498 : : if (state->safe_encoding)
1499 [ + + ]: 520527 : memcpy(newtxt, txt, len);
1500 : 520387 : else
1501 : : {
1502 : : /* Gotta do it the hard way */
1503 : : int i = 0;
1504 : 140 :
1505 : : while (i < len)
1506 [ + + ]: 808 : {
1507 : : int thislen = PQmblen(txt + i, state->encoding);
1508 : 668 :
1509 : : /* first byte should always be okay... */
1510 : : newtxt[i] = txt[i];
1511 : 668 : i++;
4902 ishii@postgresql.org 1512 : 668 : while (--thislen > 0 && i < len)
8111 tgl@sss.pgh.pa.us 1513 [ + + + - ]: 808 : newtxt[i++] = (char) 0xFF;
1514 : 140 : }
1515 : : }
1516 : :
1517 : : return yy_scan_buffer(newtxt, len + 2, state->scanner);
1518 : 520527 : }
1519 : :
1520 : : /*
1521 : : * psqlscan_emit() --- body for ECHO macro
1522 : : *
1523 : : * NB: this must be used for ALL and ONLY the text copied from the flex
1524 : : * input data. If you pass it something that is not part of the yytext
1525 : : * string, you are making a mistake. Internally generated text can be
1526 : : * appended directly to state->output_buf.
1527 : : */
1528 : : void
1529 : : psqlscan_emit(PsqlScanState state, const char *txt, int len)
1530 : 6596307 : {
1531 : : PQExpBuffer output_buf = state->output_buf;
3700 1532 : 6596307 :
1533 : : if (state->safe_encoding)
8111 1534 [ + + ]: 6596307 : appendBinaryPQExpBuffer(output_buf, txt, len);
1535 : 6595831 : else
1536 : : {
1537 : : /* Gotta do it the hard way */
1538 : : const char *reference = state->refline;
3699 1539 : 476 : int i;
1540 : :
1541 : : reference += (txt - state->curline);
8111 1542 : 476 :
1543 : : for (i = 0; i < len; i++)
1544 [ + + ]: 1277 : {
1545 : : char ch = txt[i];
1546 : 801 :
1547 : : if (ch == (char) 0xFF)
1548 [ + + ]: 801 : ch = reference[i];
1549 : 140 : appendPQExpBufferChar(output_buf, ch);
1550 : 801 : }
1551 : : }
1552 : : }
6064 1553 : 6596307 :
1554 : : /*
1555 : : * psqlscan_extract_substring --- fetch value of (part of) the current token
1556 : : *
1557 : : * This is like psqlscan_emit(), except that the data is returned as a
1558 : : * malloc'd string rather than being pushed directly to state->output_buf.
1559 : : */
1560 : : char *
1561 : : psqlscan_extract_substring(PsqlScanState state, const char *txt, int len)
5366 1562 : 3357 : {
1563 : : char *result = pg_malloc_array(char, (len + 1));
1564 : 3357 :
1565 : : if (state->safe_encoding)
1566 [ + - ]: 3357 : memcpy(result, txt, len);
1567 : 3357 : else
1568 : : {
1569 : : /* Gotta do it the hard way */
1570 : : const char *reference = state->refline;
3699 tgl@sss.pgh.pa.us 1571 :UBC 0 : int i;
1572 : :
1573 : : reference += (txt - state->curline);
5366 1574 : 0 :
1575 : : for (i = 0; i < len; i++)
1576 [ # # ]: 0 : {
1577 : : char ch = txt[i];
1578 : 0 :
1579 : : if (ch == (char) 0xFF)
1580 [ # # ]: 0 : ch = reference[i];
1581 : 0 : result[i] = ch;
1582 : 0 : }
1583 : : }
1584 : : result[len] = '\0';
5366 tgl@sss.pgh.pa.us 1585 :CBC 3357 : return result;
1586 : 3357 : }
1587 : :
1588 : : /*
1589 : : * psqlscan_escape_variable --- process :'VARIABLE' or :"VARIABLE"
1590 : : *
1591 : : * If the variable name is found, escape its value using the appropriate
1592 : : * quoting method and emit the value to output_buf. (Since the result is
1593 : : * surely quoted, there is never any reason to rescan it.) If we don't
1594 : : * find the variable or escaping fails, emit the token as-is.
1595 : : */
1596 : : void
1597 : : psqlscan_escape_variable(PsqlScanState state, const char *txt, int len,
3321 1598 : 697 : PsqlScanQuoteType quote)
1599 : : {
1600 : : char *varname;
1601 : : char *value;
1602 : :
1603 : : /* Variable lookup. */
1604 : : varname = psqlscan_extract_substring(state, txt + 2, len - 3);
3700 1605 : 697 : if (state->callbacks->get_variable)
3321 1606 [ + - ]: 697 : value = state->callbacks->get_variable(varname, quote,
3340 1607 : 697 : state->cb_passthrough);
1608 : : else
1609 : : value = NULL;
5366 tgl@sss.pgh.pa.us 1610 :UBC 0 : free(varname);
5940 rhaas@postgresql.org 1611 :CBC 697 :
1612 : : if (value)
1613 [ + + ]: 697 : {
1614 : : /* Emit the suitably-escaped value */
1615 : : appendPQExpBufferStr(state->output_buf, value);
3700 tgl@sss.pgh.pa.us 1616 : 660 : free(value);
1617 : 660 : }
1618 : : else
1619 : : {
1620 : : /* Emit original token as-is */
1621 : : psqlscan_emit(state, txt, len);
5940 rhaas@postgresql.org 1622 : 37 : }
1623 : : }
3148 andrew@dunslane.net 1624 : 697 :
1625 : : void
1626 : : psqlscan_test_variable(PsqlScanState state, const char *txt, int len)
1627 : 21 : {
1628 : : char *varname;
1629 : : char *value;
1630 : :
1631 : : varname = psqlscan_extract_substring(state, txt + 3, len - 4);
1632 : 21 : if (state->callbacks->get_variable)
1633 [ + - ]: 21 : value = state->callbacks->get_variable(varname, PQUOTE_PLAIN,
1634 : 21 : state->cb_passthrough);
1635 : : else
1636 : : value = NULL;
3148 andrew@dunslane.net 1637 :UBC 0 : free(varname);
3148 andrew@dunslane.net 1638 :CBC 21 :
1639 : : if (value != NULL)
1640 [ + + ]: 21 : {
1641 : : appendPQExpBufferStr(state->output_buf, "TRUE");
1642 : 9 : free(value);
1643 : 9 : }
1644 : : else
1645 : : {
1646 : : appendPQExpBufferStr(state->output_buf, "FALSE");
1647 : 12 : }
1648 : : }
0 s.n.thetic 1649 : 21 : /* END: function "psqlscan_test_variable" */
|