2 /*-------------------------------------------------------------------------
5 * lexical scanner for SQL commands
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
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.
20 * XXX The rules in this file must be kept in sync with the backend lexer!!!
22 * XXX Avoid creating backtracking cases --- see the backend lexer for info.
24 * See psqlscan_int.h for additional commentary.
27 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
28 * Portions Copyright (c) 1994, Regents of the University of California
31 * src/fe_utils/psqlscan.l
33 *-------------------------------------------------------------------------
35 #include "postgres_fe.h"
37 #include "common/logging.h"
38 #include "fe_utils/psqlscan.h"
47 #include "fe_utils/psqlscan_int.h"
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.
56 * Set the type of yyextra; we use it as a pointer back to the containing
59 #define YY_EXTRA_TYPE PsqlScanState
62 /* Return values from yylex() */
63 #define LEXRES_EOL 0 /* end of input */
64 #define LEXRES_SEMI 1 /* command-terminating semicolon found */
65 #define LEXRES_BACKSLASH 2 /* backslash command start */
68 #define ECHO psqlscan_emit(cur_state, yytext, yyleng)
71 * Work around a bug in flex 2.5.35: it emits a couple of functions that
72 * it forgets to emit declarations for. Since we use -Wmissing-prototypes,
73 * this would cause warnings. Providing our own declarations should be
74 * harmless even when the bug gets fixed.
76 extern int psql_yyget_column(yyscan_t yyscanner);
77 extern void psql_yyset_column(int column_no, yyscan_t yyscanner);
84 %option never-interactive
90 %option prefix="psql_yy"
93 * All of the following definitions and rules should exactly match
94 * src/backend/parser/scan.l so far as the flex patterns are concerned.
95 * The rule bodies are just ECHO as opposed to what the backend does,
96 * however. (But be sure to duplicate code that affects the lexing process,
97 * such as BEGIN() and yyless().) Also, psqlscan uses a single <<EOF>> rule
98 * whereas scan.l has a separate one for each exclusive state.
102 * OK, here is a short description of lex/flex rules behavior.
103 * The longest pattern which matches an input string is always chosen.
104 * For equal-length patterns, the first occurring in the rules list is chosen.
105 * INITIAL is the starting state, to which all non-conditional rules apply.
106 * Exclusive states change parsing rules while the state is active. When in
107 * an exclusive state, only those rules defined for that state apply.
109 * We use exclusive states for quoted strings, extended comments,
110 * and to eliminate parsing troubles for numeric strings.
112 * <xb> bit string literal
113 * <xc> extended C-style comments
114 * <xd> delimited identifiers (double-quoted identifiers)
115 * <xh> hexadecimal byte string
116 * <xq> standard quoted strings
117 * <xqs> quote stop (detect continued strings)
118 * <xe> extended quoted strings (support backslash escape sequences)
119 * <xdolq> $foo$ quoted strings
120 * <xui> quoted identifier with Unicode escapes
121 * <xus> quoted string with Unicode escapes
123 * Note: we intentionally don't mimic the backend's <xeu> state; we have
124 * no need to distinguish it from <xe> state, and no good way to get out
125 * of it in error cases. The backend just throws yyerror() in those
126 * cases, but that's not an option here.
141 * In order to make the world safe for Windows and Mac clients as well as
142 * Unix ones, we accept either \n or \r as a newline. A DOS-style \r\n
143 * sequence will be seen as two successive newlines, but that doesn't cause
144 * any problems. Comments that start with -- and extend to the next
145 * newline are treated as equivalent to a single whitespace character.
147 * NOTE a fine point: if there is no newline following --, we will absorb
148 * everything to the end of the input as a comment. This is correct. Older
149 * versions of Postgres failed to recognize -- as a comment if the input
150 * did not end with a newline.
152 * XXX perhaps \f (formfeed) should be treated as a newline as well?
154 * XXX if you change the set of whitespace characters, fix scanner_isspace()
163 comment ("--"{non_newline}*)
165 whitespace ({space}+|{comment})
168 * SQL requires at least one newline in the whitespace separating
169 * string literals that are to be concatenated. Silly, but who are we
170 * to argue? Note that {whitespace_with_newline} should not have * after
171 * it, whereas {whitespace} should generally have a * after it...
174 special_whitespace ({space}+|{comment}{newline})
175 horiz_whitespace ({horiz_space}|{comment})
176 whitespace_with_newline ({horiz_whitespace}*{newline}{special_whitespace}*)
179 /* If we see {quote} then {quotecontinue}, the quoted string continues */
180 quotecontinue {whitespace_with_newline}{quote}
183 * {quotecontinuefail} is needed to avoid lexer backup when we fail to match
184 * {quotecontinue}. It might seem that this could just be {whitespace}*,
185 * but if there's a dash after {whitespace_with_newline}, it must be consumed
186 * to see if there's another dash --- which would start a {comment} and thus
187 * allow continuation of the {quotecontinue} token.
189 quotecontinuefail {whitespace}*"-"?
192 * It is tempting to scan the string for only those characters
193 * which are allowed. However, this leads to silently swallowed
194 * characters if illegal characters are included in the string.
195 * For example, if xbinside is [01] then B'ABCD' is interpreted
196 * as a zero-length string, and the ABCD' is lost!
197 * Better to pass the string forward and let the input routines
198 * validate the contents.
203 /* Hexadecimal byte string */
207 /* National character */
210 /* Quoted string that allows backslash escapes */
214 xeoctesc [\\][0-7]{1,3}
215 xehexesc [\\]x[0-9A-Fa-f]{1,2}
216 xeunicode [\\](u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})
217 xeunicodefail [\\](u[0-9A-Fa-f]{0,3}|U[0-9A-Fa-f]{0,7})
220 * xqdouble implements embedded quote, ''''
223 xqdouble {quote}{quote}
226 /* $foo$ style quotes ("dollar quoting")
227 * The quoted string starts with $foo$ where "foo" is an optional string
228 * in the form of an identifier, except that it may not contain "$",
229 * and extends to the first occurrence of an identical string.
230 * There is *no* processing of the quoted text.
232 * {dolqfailed} is an error rule to avoid scanner backup when {dolqdelim}
233 * fails to match its trailing "$".
235 dolq_start [A-Za-z\200-\377_]
236 dolq_cont [A-Za-z\200-\377_0-9]
237 dolqdelim \$({dolq_start}{dolq_cont}*)?\$
238 dolqfailed \${dolq_start}{dolq_cont}*
242 * Allows embedded spaces and other special characters into identifiers.
247 xddouble {dquote}{dquote}
250 /* Quoted identifier with Unicode escapes */
251 xuistart [uU]&{dquote}
253 /* Quoted string with Unicode escapes */
254 xusstart [uU]&{quote}
256 /* error rule to avoid backup */
262 * The "extended comment" syntax closely resembles allowable operator syntax.
263 * The tricky part here is to get lex to recognize a string starting with
264 * slash-star as a comment, when interpreting it as an operator would produce
265 * a longer match --- remember lex will prefer a longer match! Also, if we
266 * have something like plus-slash-star, lex will think this is a 3-character
267 * operator whereas we want to see it as a + operator and a comment start.
268 * The solution is two-fold:
269 * 1. append {op_chars}* to xcstart so that it matches as much text as
270 * {operator} would. Then the tie-breaker (first matching rule of same
271 * length) ensures xcstart wins. We put back the extra stuff with yyless()
272 * in case it contains a star-slash that should terminate the comment.
273 * 2. In the operator rule, check for slash-star within the operator, and
274 * if found throw it back with yyless(). This handles the plus-slash-star
276 * Dash-dash comments have similar interactions with the operator rule.
278 xcstart \/\*{op_chars}*
282 ident_start [A-Za-z\200-\377_]
283 ident_cont [A-Za-z\200-\377_0-9\$]
285 identifier {ident_start}{ident_cont}*
287 /* Assorted special-case operators and operator-like tokens */
293 * These operator-like tokens (unlike the above ones) also match the {operator}
294 * rule, which means that they might be overridden by a longer match if they
295 * are followed by a comment start or a + or - character. Accordingly, if you
296 * add to this list, you must also add corresponding code to the {operator}
297 * block to return the correct token in such cases. (This is not needed in
298 * psqlscan.l since the token value is ignored there.)
307 * "self" is the set of chars that should be returned as single-character
308 * tokens. "op_chars" is the set of chars that can make up "Op" tokens,
309 * which can be one or more characters long (but if a single-char token
310 * appears in the "self" set, it is not to be returned as an Op). Note
311 * that the sets overlap, but each has some chars that are not in the other.
313 * If you change either set, adjust the character lists appearing in the
314 * rule for "operator"!
316 self [,()\[\].;\:\+\-\*\/\%\^\<\>\=]
317 op_chars [\~\!\@\#\^\&\|\`\?\+\-\*\/\%\<\>\=]
323 * Unary minus is not part of a number here. Instead we pass it separately to
324 * the parser, and there it gets coerced via doNegate().
326 * {decimalfail} is used because we would like "1..10" to lex as 1, dot_dot, 10.
328 * {realfail1} and {realfail2} are added to prevent the need for scanner
329 * backup when the {real} rule fails to match completely.
334 decimal (({digit}*\.{digit}+)|({digit}+\.{digit}*))
335 decimalfail {digit}+\.\.
336 real ({integer}|{decimal})[Ee][-+]?{digit}+
337 realfail1 ({integer}|{decimal})[Ee]
338 realfail2 ({integer}|{decimal})[Ee][-+]
342 /* psql-specific: characters allowed in variable names */
343 variable_char [A-Za-z\200-\377_0-9]
348 * Dollar quoted strings are totally opaque, and no escaping is done on them.
349 * Other quoted strings must allow some special characters such as single-quote
351 * Embedded single-quotes are implemented both in the SQL standard
352 * style of two adjacent single quotes "''" and in the Postgres/Java style
353 * of escaped-quote "\'".
354 * Other embedded escaped characters are matched explicitly and the leading
355 * backslash is dropped from the string.
356 * Note that xcstart must appear before operator, as explained above!
357 * Also whitespace (comment) must appear before operator.
363 /* Declare some local variables inside yylex(), for convenience */
364 PsqlScanState cur_state = yyextra;
365 PQExpBuffer output_buf = cur_state->output_buf;
368 * Force flex into the state indicated by start_state. This has a
369 * couple of purposes: it lets some of the functions below set a new
370 * starting state without ugly direct access to flex variables, and it
371 * allows us to transition from one flex lexer to another so that we
372 * can lex different parts of the source string using separate lexers.
374 BEGIN(cur_state->start_state);
379 * Note that the whitespace rule includes both true
380 * whitespace and single-line ("--" style) comments.
381 * We suppress whitespace until we have collected some
382 * non-whitespace data. (This interacts with some
383 * decisions in MainLoop(); see there for details.)
385 if (output_buf->len > 0)
390 cur_state->xcdepth = 0;
392 /* Put back any characters past slash-star; see above */
399 cur_state->xcdepth++;
400 /* Put back any characters past slash-star; see above */
406 if (cur_state->xcdepth <= 0)
409 cur_state->xcdepth--;
436 /* Hexadecimal bit type.
437 * At some point we should simply pass the string
438 * forward to the parser and label it there.
439 * In the meantime, place a leading "x" on the string
440 * to mark it for the input routine as a hex string.
447 yyless(1); /* eat only 'n' this time */
452 if (cur_state->std_strings)
467 <xb,xh,xq,xe,xus>{quote} {
469 * When we are scanning a quoted string and see an end
470 * quote, we must look ahead for a possible continuation.
471 * If we don't see one, we know the end quote was in fact
472 * the end of the string. To reduce the lexer table size,
473 * we use a single "xqs" state to do the lookahead for all
476 cur_state->state_before_str_stop = YYSTATE;
480 <xqs>{quotecontinue} {
482 * Found a quote continuation, so return to the in-quote
483 * state and continue scanning the literal. Nothing is
484 * added to the literal's contents.
486 BEGIN(cur_state->state_before_str_stop);
489 <xqs>{quotecontinuefail} |
492 * Failed to see a quote continuation. Throw back
493 * everything after the end quote, and handle the string
494 * according to the state we were in previously.
498 /* There's nothing to echo ... */
501 <xq,xe,xus>{xqdouble} {
513 <xe>{xeunicodefail} {
526 /* This is only needed for \ just before EOF */
531 cur_state->dolqstart = pg_strdup(yytext);
536 /* throw back all but the initial "$" */
541 if (strcmp(yytext, cur_state->dolqstart) == 0)
543 free(cur_state->dolqstart);
544 cur_state->dolqstart = NULL;
550 * When we fail to match $...$ to dolqstart, transfer
551 * the $... part to the output, but put back the final
552 * $ for rescanning. Consider $delim$...$junk$delim$
558 <xdolq>{dolqinside} {
561 <xdolq>{dolqfailed} {
565 /* This is only needed for $ inside the quoted text */
593 /* throw back all but the initial u/U */
631 * These rules are specific to psql --- they implement parenthesis
632 * counting and detection of command-ending semicolon. These must
633 * appear before the {self} rule so that they take precedence over it.
637 cur_state->paren_depth++;
642 if (cur_state->paren_depth > 0)
643 cur_state->paren_depth--;
649 if (cur_state->paren_depth == 0 && cur_state->begin_depth == 0)
651 /* Terminate lexing temporarily */
652 cur_state->start_state = YY_START;
653 cur_state->identifier_count = 0;
659 * psql-specific rules to handle backslash commands and variable
660 * substitution. We want these before {self}, also.
664 /* Force a semi-colon or colon into the query buffer */
665 psqlscan_emit(cur_state, yytext + 1, 1);
666 if (yytext[1] == ';')
667 cur_state->identifier_count = 0;
671 /* Terminate lexing temporarily */
672 cur_state->start_state = YY_START;
673 return LEXRES_BACKSLASH;
677 /* Possible psql variable substitution */
681 varname = psqlscan_extract_substring(cur_state,
684 if (cur_state->callbacks->get_variable)
685 value = cur_state->callbacks->get_variable(varname,
687 cur_state->cb_passthrough);
693 /* It is a variable, check for recursion */
694 if (psqlscan_var_is_current_source(cur_state, varname))
696 /* Recursive expansion --- don't go there */
697 pg_log_warning("skipping recursive expansion of variable \"%s\"",
699 /* Instead copy the string as is */
704 /* OK, perform substitution */
705 psqlscan_push_new_buffer(cur_state, value, varname);
706 /* yy_scan_string already made buffer active */
713 * if the variable doesn't exist we'll copy the string
722 :'{variable_char}+' {
723 psqlscan_escape_variable(cur_state, yytext, yyleng,
727 :\"{variable_char}+\" {
728 psqlscan_escape_variable(cur_state, yytext, yyleng,
732 :\{\?{variable_char}+\} {
733 psqlscan_test_variable(cur_state, yytext, yyleng);
737 * These rules just avoid the need for scanner backup if one of the
738 * three rules above fails to match completely.
742 /* Throw back everything but the colon */
747 :\"{variable_char}* {
748 /* Throw back everything but the colon */
753 :\{\?{variable_char}* {
754 /* Throw back everything but the colon */
759 /* Throw back everything but the colon */
765 * Back to backend-compatible rules.
774 * Check for embedded slash-star or dash-dash; those
775 * are comment starts, so operator must stop there.
776 * Note that slash-star or dash-dash at the first
777 * character will match a prior rule, not this one.
780 char *slashstar = strstr(yytext, "/*");
781 char *dashdash = strstr(yytext, "--");
783 if (slashstar && dashdash)
785 /* if both appear, take the first one */
786 if (slashstar > dashdash)
787 slashstar = dashdash;
790 slashstar = dashdash;
792 nchars = slashstar - yytext;
795 * For SQL compatibility, '+' and '-' cannot be the
796 * last char of a multi-char operator unless the operator
797 * contains chars that are not in SQL operators.
798 * The idea is to lex '=-' as two operators, but not
799 * to forbid operator names like '?-' that could not be
800 * sequences of SQL operators.
803 (yytext[nchars - 1] == '+' ||
804 yytext[nchars - 1] == '-'))
808 for (ic = nchars - 2; ic >= 0; ic--)
811 if (c == '~' || c == '!' || c == '@' ||
812 c == '#' || c == '^' || c == '&' ||
813 c == '|' || c == '`' || c == '?' ||
820 * didn't find a qualifying character, so remove
825 } while (nchars > 1 &&
826 (yytext[nchars - 1] == '+' ||
827 yytext[nchars - 1] == '-'));
833 /* Strip the unwanted chars from the token */
850 /* throw back the .., and treat as integer */
859 * throw back the [Ee], and figure out whether what
860 * remains is an {integer} or {decimal}.
861 * (in psql, we don't actually care...)
867 /* throw back the [Ee][+-], and proceed as above */
875 * We need to track if we are inside a BEGIN .. END block
876 * in a function definition, so that semicolons contained
877 * therein don't terminate the whole statement. Short of
878 * writing a full parser here, the following heuristic
879 * should work. First, we track whether the beginning of
880 * the statement matches CREATE [OR REPLACE]
881 * {FUNCTION|PROCEDURE}
884 if (cur_state->identifier_count == 0)
885 memset(cur_state->identifiers, 0, sizeof(cur_state->identifiers));
887 if (pg_strcasecmp(yytext, "create") == 0 ||
888 pg_strcasecmp(yytext, "function") == 0 ||
889 pg_strcasecmp(yytext, "procedure") == 0 ||
890 pg_strcasecmp(yytext, "or") == 0 ||
891 pg_strcasecmp(yytext, "replace") == 0)
893 if (cur_state->identifier_count < sizeof(cur_state->identifiers))
894 cur_state->identifiers[cur_state->identifier_count] = pg_tolower((unsigned char) yytext[0]);
897 cur_state->identifier_count++;
899 if (cur_state->identifiers[0] == 'c' &&
900 (cur_state->identifiers[1] == 'f' || cur_state->identifiers[1] == 'p' ||
901 (cur_state->identifiers[1] == 'o' && cur_state->identifiers[2] == 'r' &&
902 (cur_state->identifiers[3] == 'f' || cur_state->identifiers[3] == 'p'))) &&
903 cur_state->paren_depth == 0)
905 if (pg_strcasecmp(yytext, "begin") == 0)
906 cur_state->begin_depth++;
907 else if (pg_strcasecmp(yytext, "case") == 0)
910 * CASE also ends with END. We only need to track
911 * this if we are already inside a BEGIN.
913 if (cur_state->begin_depth >= 1)
914 cur_state->begin_depth++;
916 else if (pg_strcasecmp(yytext, "end") == 0)
918 if (cur_state->begin_depth > 0)
919 cur_state->begin_depth--;
931 if (cur_state->buffer_stack == NULL)
933 cur_state->start_state = YY_START;
934 return LEXRES_EOL; /* end of input reached */
938 * We were expanding a variable, so pop the inclusion
939 * stack and keep lexing
941 psqlscan_pop_buffer_stack(cur_state);
942 psqlscan_select_top_buffer(cur_state);
950 * Create a lexer working state struct.
952 * callbacks is a struct of function pointers that encapsulate some
953 * behavior we need from the surrounding program. This struct must
954 * remain valid for the lifespan of the PsqlScanState.
957 psql_scan_create(const PsqlScanCallbacks *callbacks)
961 state = (PsqlScanStateData *) pg_malloc0(sizeof(PsqlScanStateData));
963 state->callbacks = callbacks;
965 yylex_init(&state->scanner);
967 yyset_extra(state, state->scanner);
969 psql_scan_reset(state);
975 * Destroy a lexer working state struct, releasing all resources.
978 psql_scan_destroy(PsqlScanState state)
980 psql_scan_finish(state);
982 psql_scan_reset(state);
984 yylex_destroy(state->scanner);
990 * Set the callback passthrough pointer for the lexer.
992 * This could have been integrated into psql_scan_create, but keeping it
993 * separate allows the application to change the pointer later, which might
997 psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
999 state->cb_passthrough = passthrough;
1003 * Set up to perform lexing of the given input line.
1005 * The text at *line, extending for line_len bytes, will be scanned by
1006 * subsequent calls to the psql_scan routines. psql_scan_finish should
1007 * be called when scanning is complete. Note that the lexer retains
1008 * a pointer to the storage at *line --- this string must not be altered
1009 * or freed until after psql_scan_finish is called.
1011 * encoding is the libpq identifier for the character encoding in use,
1012 * and std_strings says whether standard_conforming_strings is on.
1015 psql_scan_setup(PsqlScanState state,
1016 const char *line, int line_len,
1017 int encoding, bool std_strings)
1019 /* Mustn't be scanning already */
1020 Assert(state->scanbufhandle == NULL);
1021 Assert(state->buffer_stack == NULL);
1023 /* Do we need to hack the character set encoding? */
1024 state->encoding = encoding;
1025 state->safe_encoding = pg_valid_server_encoding_id(encoding);
1027 /* Save standard-strings flag as well */
1028 state->std_strings = std_strings;
1030 /* Set up flex input buffer with appropriate translation and padding */
1031 state->scanbufhandle = psqlscan_prepare_buffer(state, line, line_len,
1033 state->scanline = line;
1035 /* Set lookaside data in case we have to map unsafe encoding */
1036 state->curline = state->scanbuf;
1037 state->refline = state->scanline;
1041 * Do lexical analysis of SQL command text.
1043 * The text previously passed to psql_scan_setup is scanned, and appended
1044 * (possibly with transformation) to query_buf.
1046 * The return value indicates the condition that stopped scanning:
1048 * PSCAN_SEMICOLON: found a command-ending semicolon. (The semicolon is
1049 * transferred to query_buf.) The command accumulated in query_buf should
1050 * be executed, then clear query_buf and call again to scan the remainder
1053 * PSCAN_BACKSLASH: found a backslash that starts a special command.
1054 * Any previous data on the line has been transferred to query_buf.
1055 * The caller will typically next apply a separate flex lexer to scan
1056 * the special command.
1058 * PSCAN_INCOMPLETE: the end of the line was reached, but we have an
1059 * incomplete SQL command. *prompt is set to the appropriate prompt type.
1061 * PSCAN_EOL: the end of the line was reached, and there is no lexical
1062 * reason to consider the command incomplete. The caller may or may not
1063 * choose to send it. *prompt is set to the appropriate prompt type if
1064 * the caller chooses to collect more input.
1066 * In the PSCAN_INCOMPLETE and PSCAN_EOL cases, psql_scan_finish() should
1067 * be called next, then the cycle may be repeated with a fresh input line.
1069 * In all cases, *prompt is set to an appropriate prompt type code for the
1070 * next line-input operation.
1073 psql_scan(PsqlScanState state,
1074 PQExpBuffer query_buf,
1075 promptStatus_t *prompt)
1077 PsqlScanResult result;
1080 /* Must be scanning already */
1081 Assert(state->scanbufhandle != NULL);
1083 /* Set current output target */
1084 state->output_buf = query_buf;
1086 /* Set input source */
1087 if (state->buffer_stack != NULL)
1088 yy_switch_to_buffer(state->buffer_stack->buf, state->scanner);
1090 yy_switch_to_buffer(state->scanbufhandle, state->scanner);
1093 lexresult = yylex(NULL, state->scanner);
1096 * Check termination state and return appropriate result info.
1100 case LEXRES_EOL: /* end of input */
1101 switch (state->start_state)
1104 case xqs: /* we treat this like INITIAL */
1105 if (state->paren_depth > 0)
1107 result = PSCAN_INCOMPLETE;
1108 *prompt = PROMPT_PAREN;
1110 else if (state->begin_depth > 0)
1112 result = PSCAN_INCOMPLETE;
1113 *prompt = PROMPT_CONTINUE;
1115 else if (query_buf->len > 0)
1118 *prompt = PROMPT_CONTINUE;
1122 /* never bother to send an empty buffer */
1123 result = PSCAN_INCOMPLETE;
1124 *prompt = PROMPT_READY;
1128 result = PSCAN_INCOMPLETE;
1129 *prompt = PROMPT_SINGLEQUOTE;
1132 result = PSCAN_INCOMPLETE;
1133 *prompt = PROMPT_COMMENT;
1136 result = PSCAN_INCOMPLETE;
1137 *prompt = PROMPT_DOUBLEQUOTE;
1140 result = PSCAN_INCOMPLETE;
1141 *prompt = PROMPT_SINGLEQUOTE;
1144 result = PSCAN_INCOMPLETE;
1145 *prompt = PROMPT_SINGLEQUOTE;
1148 result = PSCAN_INCOMPLETE;
1149 *prompt = PROMPT_SINGLEQUOTE;
1152 result = PSCAN_INCOMPLETE;
1153 *prompt = PROMPT_DOLLARQUOTE;
1156 result = PSCAN_INCOMPLETE;
1157 *prompt = PROMPT_DOUBLEQUOTE;
1160 result = PSCAN_INCOMPLETE;
1161 *prompt = PROMPT_SINGLEQUOTE;
1164 /* can't get here */
1165 fprintf(stderr, "invalid YY_START\n");
1169 case LEXRES_SEMI: /* semicolon */
1170 result = PSCAN_SEMICOLON;
1171 *prompt = PROMPT_READY;
1173 case LEXRES_BACKSLASH: /* backslash */
1174 result = PSCAN_BACKSLASH;
1175 *prompt = PROMPT_READY;
1178 /* can't get here */
1179 fprintf(stderr, "invalid yylex result\n");
1187 * Clean up after scanning a string. This flushes any unread input and
1188 * releases resources (but not the PsqlScanState itself). Note however
1189 * that this does not reset the lexer scan state; that can be done by
1190 * psql_scan_reset(), which is an orthogonal operation.
1192 * It is legal to call this when not scanning anything (makes it easier
1193 * to deal with error recovery).
1196 psql_scan_finish(PsqlScanState state)
1198 /* Drop any incomplete variable expansions. */
1199 while (state->buffer_stack != NULL)
1200 psqlscan_pop_buffer_stack(state);
1202 /* Done with the outer scan buffer, too */
1203 if (state->scanbufhandle)
1204 yy_delete_buffer(state->scanbufhandle, state->scanner);
1205 state->scanbufhandle = NULL;
1207 free(state->scanbuf);
1208 state->scanbuf = NULL;
1212 * Reset lexer scanning state to start conditions. This is appropriate
1213 * for executing \r psql commands (or any other time that we discard the
1214 * prior contents of query_buf). It is not, however, necessary to do this
1215 * when we execute and clear the buffer after getting a PSCAN_SEMICOLON or
1216 * PSCAN_EOL scan result, because the scan state must be INITIAL when those
1217 * conditions are returned.
1219 * Note that this is unrelated to flushing unread input; that task is
1220 * done by psql_scan_finish().
1223 psql_scan_reset(PsqlScanState state)
1225 state->start_state = INITIAL;
1226 state->paren_depth = 0;
1227 state->xcdepth = 0; /* not really necessary */
1228 if (state->dolqstart)
1229 free(state->dolqstart);
1230 state->dolqstart = NULL;
1231 state->identifier_count = 0;
1232 state->begin_depth = 0;
1236 * Reselect this lexer (psqlscan.l) after using another one.
1238 * Currently and for foreseeable uses, it's sufficient to reset to INITIAL
1239 * state, because we'd never switch to another lexer in a different state.
1240 * However, we don't want to reset e.g. paren_depth, so this can't be
1241 * the same as psql_scan_reset().
1243 * Note: psql setjmp error recovery just calls psql_scan_reset(), so that
1244 * must be a superset of this.
1246 * Note: it seems likely that other lexers could just assign INITIAL for
1247 * themselves, since that probably has the value zero in every flex-generated
1248 * lexer. But let's not assume that.
1251 psql_scan_reselect_sql_lexer(PsqlScanState state)
1253 state->start_state = INITIAL;
1257 * Return true if lexer is currently in an "inside quotes" state.
1259 * This is pretty grotty but is needed to preserve the old behavior
1260 * that mainloop.c drops blank lines not inside quotes without even
1264 psql_scan_in_quote(PsqlScanState state)
1266 return state->start_state != INITIAL &&
1267 state->start_state != xqs;
1271 * Push the given string onto the stack of stuff to scan.
1273 * NOTE SIDE EFFECT: the new buffer is made the active flex input buffer.
1276 psqlscan_push_new_buffer(PsqlScanState state, const char *newstr,
1277 const char *varname)
1279 StackElem *stackelem;
1281 stackelem = (StackElem *) pg_malloc(sizeof(StackElem));
1284 * In current usage, the passed varname points at the current flex input
1285 * buffer; we must copy it before calling psqlscan_prepare_buffer()
1286 * because that will change the buffer state.
1288 stackelem->varname = varname ? pg_strdup(varname) : NULL;
1290 stackelem->buf = psqlscan_prepare_buffer(state, newstr, strlen(newstr),
1291 &stackelem->bufstring);
1292 state->curline = stackelem->bufstring;
1293 if (state->safe_encoding)
1295 stackelem->origstring = NULL;
1296 state->refline = stackelem->bufstring;
1300 stackelem->origstring = pg_strdup(newstr);
1301 state->refline = stackelem->origstring;
1303 stackelem->next = state->buffer_stack;
1304 state->buffer_stack = stackelem;
1308 * Pop the topmost buffer stack item (there must be one!)
1310 * NB: after this, the flex input state is unspecified; caller must
1311 * switch to an appropriate buffer to continue lexing.
1312 * See psqlscan_select_top_buffer().
1315 psqlscan_pop_buffer_stack(PsqlScanState state)
1317 StackElem *stackelem = state->buffer_stack;
1319 state->buffer_stack = stackelem->next;
1320 yy_delete_buffer(stackelem->buf, state->scanner);
1321 free(stackelem->bufstring);
1322 if (stackelem->origstring)
1323 free(stackelem->origstring);
1324 if (stackelem->varname)
1325 free(stackelem->varname);
1330 * Select the topmost surviving buffer as the active input.
1333 psqlscan_select_top_buffer(PsqlScanState state)
1335 StackElem *stackelem = state->buffer_stack;
1337 if (stackelem != NULL)
1339 yy_switch_to_buffer(stackelem->buf, state->scanner);
1340 state->curline = stackelem->bufstring;
1341 state->refline = stackelem->origstring ? stackelem->origstring : stackelem->bufstring;
1345 yy_switch_to_buffer(state->scanbufhandle, state->scanner);
1346 state->curline = state->scanbuf;
1347 state->refline = state->scanline;
1352 * Check if specified variable name is the source for any string
1353 * currently being scanned
1356 psqlscan_var_is_current_source(PsqlScanState state, const char *varname)
1358 StackElem *stackelem;
1360 for (stackelem = state->buffer_stack;
1362 stackelem = stackelem->next)
1364 if (stackelem->varname && strcmp(stackelem->varname, varname) == 0)
1371 * Set up a flex input buffer to scan the given data. We always make a
1372 * copy of the data. If working in an unsafe encoding, the copy has
1373 * multibyte sequences replaced by FFs to avoid fooling the lexer rules.
1375 * NOTE SIDE EFFECT: the new buffer is made the active flex input buffer.
1378 psqlscan_prepare_buffer(PsqlScanState state, const char *txt, int len,
1383 /* Flex wants two \0 characters after the actual data */
1384 newtxt = pg_malloc(len + 2);
1386 newtxt[len] = newtxt[len + 1] = YY_END_OF_BUFFER_CHAR;
1388 if (state->safe_encoding)
1389 memcpy(newtxt, txt, len);
1392 /* Gotta do it the hard way */
1397 int thislen = PQmblen(txt + i, state->encoding);
1399 /* first byte should always be okay... */
1402 while (--thislen > 0 && i < len)
1403 newtxt[i++] = (char) 0xFF;
1407 return yy_scan_buffer(newtxt, len + 2, state->scanner);
1411 * psqlscan_emit() --- body for ECHO macro
1413 * NB: this must be used for ALL and ONLY the text copied from the flex
1414 * input data. If you pass it something that is not part of the yytext
1415 * string, you are making a mistake. Internally generated text can be
1416 * appended directly to state->output_buf.
1419 psqlscan_emit(PsqlScanState state, const char *txt, int len)
1421 PQExpBuffer output_buf = state->output_buf;
1423 if (state->safe_encoding)
1424 appendBinaryPQExpBuffer(output_buf, txt, len);
1427 /* Gotta do it the hard way */
1428 const char *reference = state->refline;
1431 reference += (txt - state->curline);
1433 for (i = 0; i < len; i++)
1437 if (ch == (char) 0xFF)
1439 appendPQExpBufferChar(output_buf, ch);
1445 * psqlscan_extract_substring --- fetch value of (part of) the current token
1447 * This is like psqlscan_emit(), except that the data is returned as a
1448 * malloc'd string rather than being pushed directly to state->output_buf.
1451 psqlscan_extract_substring(PsqlScanState state, const char *txt, int len)
1453 char *result = (char *) pg_malloc(len + 1);
1455 if (state->safe_encoding)
1456 memcpy(result, txt, len);
1459 /* Gotta do it the hard way */
1460 const char *reference = state->refline;
1463 reference += (txt - state->curline);
1465 for (i = 0; i < len; i++)
1469 if (ch == (char) 0xFF)
1479 * psqlscan_escape_variable --- process :'VARIABLE' or :"VARIABLE"
1481 * If the variable name is found, escape its value using the appropriate
1482 * quoting method and emit the value to output_buf. (Since the result is
1483 * surely quoted, there is never any reason to rescan it.) If we don't
1484 * find the variable or escaping fails, emit the token as-is.
1487 psqlscan_escape_variable(PsqlScanState state, const char *txt, int len,
1488 PsqlScanQuoteType quote)
1493 /* Variable lookup. */
1494 varname = psqlscan_extract_substring(state, txt + 2, len - 3);
1495 if (state->callbacks->get_variable)
1496 value = state->callbacks->get_variable(varname, quote,
1497 state->cb_passthrough);
1504 /* Emit the suitably-escaped value */
1505 appendPQExpBufferStr(state->output_buf, value);
1510 /* Emit original token as-is */
1511 psqlscan_emit(state, txt, len);
1516 psqlscan_test_variable(PsqlScanState state, const char *txt, int len)
1521 varname = psqlscan_extract_substring(state, txt + 3, len - 4);
1522 if (state->callbacks->get_variable)
1523 value = state->callbacks->get_variable(varname, PQUOTE_PLAIN,
1524 state->cb_passthrough);
1531 psqlscan_emit(state, "TRUE", 4);
1536 psqlscan_emit(state, "FALSE", 5);