PostgreSQL Source Code git master
Loading...
Searching...
No Matches
psqlscan.l File Reference
#include "postgres_fe.h"
#include "common/logging.h"
#include "fe_utils/psqlscan.h"
#include "libpq-fe.h"
#include "fe_utils/psqlscan_int.h"

Go to the source code of this file.

Macros

#define LEXRES_EOL   0 /* end of input */
 
#define LEXRES_SEMI   1 /* command-terminating semicolon found */
 
#define LEXRES_BACKSLASH   2 /* backslash command start */
 
#define ECHO   psqlscan_emit(cur_state, yytext, yyleng)
 

Typedefs

typedef int YYSTYPE
 

Functions

int yylex (YYSTYPE *yylval_param, yyscan_t yyscanner)
 
PsqlScanState psql_scan_create (const PsqlScanCallbacks *callbacks)
 
void psql_scan_destroy (PsqlScanState state)
 
void psql_scan_set_passthrough (PsqlScanState state, void *passthrough)
 
void psql_scan_setup (PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)
 
PsqlScanResult psql_scan (PsqlScanState state, PQExpBuffer query_buf, promptStatus_t *prompt)
 
void psql_scan_finish (PsqlScanState state)
 
void psql_scan_reset (PsqlScanState state)
 
void psql_scan_reselect_sql_lexer (PsqlScanState state)
 
bool psql_scan_in_quote (PsqlScanState state)
 
void psql_scan_get_location (PsqlScanState state, int *lineno, int *offset)
 
void psqlscan_push_new_buffer (PsqlScanState state, const char *newstr, const char *varname)
 
void psqlscan_pop_buffer_stack (PsqlScanState state)
 
void psqlscan_select_top_buffer (PsqlScanState state)
 
bool psqlscan_var_is_current_source (PsqlScanState state, const char *varname)
 
YY_BUFFER_STATE psqlscan_prepare_buffer (PsqlScanState state, const char *txt, int len, char **txtcopy)
 
void psqlscan_emit (PsqlScanState state, const char *txt, int len)
 
charpsqlscan_extract_substring (PsqlScanState state, const char *txt, int len)
 
void psqlscan_escape_variable (PsqlScanState state, const char *txt, int len, PsqlScanQuoteType quote)
 
void psqlscan_test_variable (PsqlScanState state, const char *txt, int len)
 

Macro Definition Documentation

◆ ECHO

Definition at line 62 of file psqlscan.l.

◆ LEXRES_BACKSLASH

#define LEXRES_BACKSLASH   2 /* backslash command start */

Definition at line 59 of file psqlscan.l.

◆ LEXRES_EOL

#define LEXRES_EOL   0 /* end of input */

Definition at line 57 of file psqlscan.l.

◆ LEXRES_SEMI

#define LEXRES_SEMI   1 /* command-terminating semicolon found */

Definition at line 58 of file psqlscan.l.

Typedef Documentation

◆ YYSTYPE

Definition at line 53 of file psqlscan.l.

Function Documentation

◆ psql_scan()

PsqlScanResult psql_scan ( PsqlScanState  state,
PQExpBuffer  query_buf,
promptStatus_t prompt 
)

Definition at line 1127 of file psqlscan.l.

1130{
1131 PsqlScanResult result;
1132 int lexresult;
static int fb(int x)
PsqlScanResult
Definition psqlscan.h:31
1133
1134 /* Must be scanning already */
1135 Assert(state->scanbufhandle != NULL);
#define Assert(condition)
Definition c.h:945
1136
1137 /* Set current output target */
1138 state->output_buf = query_buf;
1139
1140 /* Set input source */
1141 if (state->buffer_stack != NULL)
1142 yy_switch_to_buffer(state->buffer_stack->buf, state->scanner);
1143 else
1144 yy_switch_to_buffer(state->scanbufhandle, state->scanner);
1145
1146 /* And lex. */
1147 lexresult = yylex(NULL, state->scanner);
int yylex(void)
Definition pgc.l:465
1148
1149 /* Notify psql_scan_get_location() that a yylex call has been made. */
1150 if (state->cur_line_no == 0)
1151 state->cur_line_no = 1;
1152
1153 /*
1154 * Check termination state and return appropriate result info.
1155 */
1156 switch (lexresult)
1157 {
1158 case LEXRES_EOL: /* end of input */
1159 switch (state->start_state)
1160 {
1161 case INITIAL:
1162 case xqs: /* we treat this like INITIAL */
1163 if (state->paren_depth > 0)
1164 {
1165 result = PSCAN_INCOMPLETE;
1167 }
1168 else if (state->begin_depth > 0)
1169 {
1170 result = PSCAN_INCOMPLETE;
1172 }
1173 else if (query_buf->len > 0)
1174 {
1175 result = PSCAN_EOL;
1177 }
1178 else
1179 {
1180 /* never bother to send an empty buffer */
1181 result = PSCAN_INCOMPLETE;
1183 }
1184 break;
1185 case xb:
1186 result = PSCAN_INCOMPLETE;
1188 break;
1189 case xc:
1190 result = PSCAN_INCOMPLETE;
1192 break;
1193 case xd:
1194 result = PSCAN_INCOMPLETE;
1196 break;
1197 case xh:
1198 result = PSCAN_INCOMPLETE;
1200 break;
1201 case xe:
1202 result = PSCAN_INCOMPLETE;
1204 break;
1205 case xq:
1206 result = PSCAN_INCOMPLETE;
1208 break;
1209 case xdolq:
1210 result = PSCAN_INCOMPLETE;
1212 break;
1213 case xui:
1214 result = PSCAN_INCOMPLETE;
1216 break;
1217 case xus:
1218 result = PSCAN_INCOMPLETE;
1220 break;
1221 default:
1222 /* can't get here */
1223 fprintf(stderr, "invalid YY_START\n");
1224 exit(1);
1225 }
1226 break;
1227 case LEXRES_SEMI: /* semicolon */
1228 result = PSCAN_SEMICOLON;
1230 break;
1231 case LEXRES_BACKSLASH: /* backslash */
1232 result = PSCAN_BACKSLASH;
1234 break;
1235 default:
1236 /* can't get here */
1237 fprintf(stderr, "invalid yylex result\n");
1238 exit(1);
1239 }
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
@ PSCAN_BACKSLASH
Definition psqlscan.h:33
@ PSCAN_EOL
Definition psqlscan.h:35
@ PSCAN_INCOMPLETE
Definition psqlscan.h:34
@ PSCAN_SEMICOLON
Definition psqlscan.h:32
@ PROMPT_READY
Definition psqlscan.h:41
@ PROMPT_PAREN
Definition psqlscan.h:47
@ PROMPT_COMMENT
Definition psqlscan.h:43
@ PROMPT_CONTINUE
Definition psqlscan.h:42
@ PROMPT_SINGLEQUOTE
Definition psqlscan.h:44
@ PROMPT_DOLLARQUOTE
Definition psqlscan.h:46
@ PROMPT_DOUBLEQUOTE
Definition psqlscan.h:45
#define LEXRES_BACKSLASH
Definition psqlscan.l:59
#define LEXRES_SEMI
Definition psqlscan.l:58
#define LEXRES_EOL
Definition psqlscan.l:57
1240
1241 return result;
1242}

References Assert, fb(), fprintf, LEXRES_BACKSLASH, LEXRES_EOL, LEXRES_SEMI, PROMPT_COMMENT, PROMPT_CONTINUE, PROMPT_DOLLARQUOTE, PROMPT_DOUBLEQUOTE, PROMPT_PAREN, PROMPT_READY, PROMPT_SINGLEQUOTE, PSCAN_BACKSLASH, PSCAN_EOL, PSCAN_INCOMPLETE, PSCAN_SEMICOLON, and yylex().

Referenced by MainLoop(), ParseScript(), and test_psql_parse().

◆ psql_scan_create()

PsqlScanState psql_scan_create ( const PsqlScanCallbacks callbacks)

Definition at line 1007 of file psqlscan.l.

1008{
1010
#define pg_malloc0_object(type)
Definition fe_memutils.h:51
1012
1013 state->callbacks = callbacks;
1014
1015 yylex_init(&state->scanner);
1016
1017 yyset_extra(state, state->scanner);
1018
void psql_scan_reset(PsqlScanState state)
Definition psqlscan.l:1281
1020
1021 return state;
1022}

References fb(), pg_malloc0_object, and psql_scan_reset().

Referenced by main(), MainLoop(), ParseScript(), and test_psql_parse().

◆ psql_scan_destroy()

void psql_scan_destroy ( PsqlScanState  state)

Definition at line 1028 of file psqlscan.l.

1029{
void psql_scan_finish(PsqlScanState state)
Definition psqlscan.l:1254
1031
1033
1034 yylex_destroy(state->scanner);
1035
1036 free(state);
#define free(a)
1037}

References fb(), free, psql_scan_finish(), and psql_scan_reset().

Referenced by main(), MainLoop(), ParseScript(), and test_psql_parse().

◆ psql_scan_finish()

void psql_scan_finish ( PsqlScanState  state)

Definition at line 1254 of file psqlscan.l.

1255{
1256 /* Drop any incomplete variable expansions. */
1257 while (state->buffer_stack != NULL)
void psqlscan_pop_buffer_stack(PsqlScanState state)
Definition psqlscan.l:1419
1259
1260 /* Done with the outer scan buffer, too */
1261 if (state->scanbufhandle)
1262 yy_delete_buffer(state->scanbufhandle, state->scanner);
1263 state->scanbufhandle = NULL;
1264 if (state->scanbuf)
1265 free(state->scanbuf);
1266 state->scanbuf = NULL;
1267}

References fb(), free, and psqlscan_pop_buffer_stack().

Referenced by MainLoop(), ParseScript(), and psql_scan_destroy().

◆ psql_scan_get_location()

void psql_scan_get_location ( PsqlScanState  state,
int lineno,
int offset 
)

Definition at line 1341 of file psqlscan.l.

1343{
1344 const char *line_end;
1345
1346 /*
1347 * We rely on flex's having stored a NUL after the current token in
1348 * scanbuf. Therefore we must specially handle the state before yylex()
1349 * has been called, when obviously that won't have happened yet.
1350 */
1351 if (state->cur_line_no == 0)
1352 {
1353 *lineno = 1;
1354 *offset = 0;
1355 return;
1356 }
1357
1358 /*
1359 * Advance cur_line_no/cur_line_ptr past whatever has been lexed so far.
1360 * Doing this prevents repeated calls from being O(N^2) for long inputs.
1361 */
1362 while ((line_end = strchr(state->cur_line_ptr, '\n')) != NULL)
1363 {
1364 state->cur_line_no++;
1365 state->cur_line_ptr = line_end + 1;
1366 }
1367 state->cur_line_ptr += strlen(state->cur_line_ptr);
1368
1369 /* Report current location. */
1370 *lineno = state->cur_line_no;
1371 *offset = state->cur_line_ptr - state->scanbuf;
1372}

References fb().

Referenced by expr_lex_one_word(), expr_yyerror_more(), and ParseScript().

◆ psql_scan_in_quote()

bool psql_scan_in_quote ( PsqlScanState  state)

Definition at line 1322 of file psqlscan.l.

1323{
1324 return state->start_state != INITIAL &&
1325 state->start_state != xqs;
1326}

References fb().

Referenced by MainLoop().

◆ psql_scan_reselect_sql_lexer()

void psql_scan_reselect_sql_lexer ( PsqlScanState  state)

Definition at line 1309 of file psqlscan.l.

1310{
1311 state->start_state = INITIAL;
1312}

References fb().

Referenced by expr_lex_one_word(), expr_scanner_finish(), psql_scan_slash_command(), psql_scan_slash_command_end(), and psql_scan_slash_option().

◆ psql_scan_reset()

void psql_scan_reset ( PsqlScanState  state)

Definition at line 1281 of file psqlscan.l.

1282{
1283 state->start_state = INITIAL;
1284 state->paren_depth = 0;
1285 state->xcdepth = 0; /* not really necessary */
1286 if (state->dolqstart)
1287 free(state->dolqstart);
1288 state->dolqstart = NULL;
1289 state->identifier_count = 0;
1290 state->begin_depth = 0;
1291}

References fb(), and free.

Referenced by exec_command_reset(), exec_command_watch(), MainLoop(), psql_scan_create(), and psql_scan_destroy().

◆ psql_scan_set_passthrough()

void psql_scan_set_passthrough ( PsqlScanState  state,
void passthrough 
)

Definition at line 1047 of file psqlscan.l.

1048{
1049 state->cb_passthrough = passthrough;
1050}

References fb().

Referenced by main(), and MainLoop().

◆ psql_scan_setup()

void psql_scan_setup ( PsqlScanState  state,
const char line,
int  line_len,
int  encoding,
bool  std_strings 
)

Definition at line 1065 of file psqlscan.l.

1068{
1069 /* Mustn't be scanning already */
1070 Assert(state->scanbufhandle == NULL);
1071 Assert(state->buffer_stack == NULL);
1072
1073 /* Do we need to hack the character set encoding? */
1074 state->encoding = encoding;
1075 state->safe_encoding = pg_valid_server_encoding_id(encoding);
static char * encoding
Definition initdb.c:139
#define pg_valid_server_encoding_id
Definition pg_wchar.h:632
1076
1077 /* Save standard-strings flag as well */
1078 state->std_strings = std_strings;
1079
1080 /* Set up flex input buffer with appropriate translation and padding */
1081 state->scanbufhandle = psqlscan_prepare_buffer(state, line, line_len,
1082 &state->scanbuf);
1083 state->scanline = line;
YY_BUFFER_STATE psqlscan_prepare_buffer(PsqlScanState state, const char *txt, int len, char **txtcopy)
Definition psqlscan.l:1482
1084
1085 /* Set lookaside data in case we have to map unsafe encoding */
1086 state->curline = state->scanbuf;
1087 state->refline = state->scanline;
1088
1089 /* Initialize state for psql_scan_get_location() */
1090 state->cur_line_no = 0; /* yylex not called yet */
1091 state->cur_line_ptr = state->scanbuf;
1092}

References Assert, encoding, fb(), pg_valid_server_encoding_id, and psqlscan_prepare_buffer().

Referenced by main(), MainLoop(), ParseScript(), and test_psql_parse().

◆ psqlscan_emit()

void psqlscan_emit ( PsqlScanState  state,
const char txt,
int  len 
)

Definition at line 1523 of file psqlscan.l.

1524{
1525 PQExpBuffer output_buf = state->output_buf;
1526
1527 if (state->safe_encoding)
1528 appendBinaryPQExpBuffer(output_buf, txt, len);
1529 else
1530 {
1531 /* Gotta do it the hard way */
1532 const char *reference = state->refline;
1533 int i;
int i
Definition isn.c:77
const void size_t len
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
1534
1535 reference += (txt - state->curline);
1536
1537 for (i = 0; i < len; i++)
1538 {
1539 char ch = txt[i];
1540
1541 if (ch == (char) 0xFF)
1542 ch = reference[i];
1543 appendPQExpBufferChar(output_buf, ch);
1544 }
1545 }
void appendPQExpBufferChar(PQExpBuffer str, char ch)
1546}

References appendBinaryPQExpBuffer(), appendPQExpBufferChar(), fb(), i, and len.

Referenced by psqlscan_escape_variable().

◆ psqlscan_escape_variable()

void psqlscan_escape_variable ( PsqlScanState  state,
const char txt,
int  len,
PsqlScanQuoteType  quote 
)

Definition at line 1591 of file psqlscan.l.

1593{
1594 char *varname;
1595 char *value;
static struct @174 value
1596
1597 /* Variable lookup. */
1598 varname = psqlscan_extract_substring(state, txt + 2, len - 3);
1599 if (state->callbacks->get_variable)
1600 value = state->callbacks->get_variable(varname, quote,
1601 state->cb_passthrough);
1602 else
1603 value = NULL;
1604 free(varname);
char * psqlscan_extract_substring(PsqlScanState state, const char *txt, int len)
Definition psqlscan.l:1555
1605
1606 if (value)
1607 {
1608 /* Emit the suitably-escaped value */
1609 appendPQExpBufferStr(state->output_buf, value);
1610 free(value);
1611 }
1612 else
1613 {
1614 /* Emit original token as-is */
1616 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void psqlscan_emit(PsqlScanState state, const char *txt, int len)
Definition psqlscan.l:1523
1617}

References appendPQExpBufferStr(), fb(), free, len, psqlscan_emit(), psqlscan_extract_substring(), and value.

◆ psqlscan_extract_substring()

char * psqlscan_extract_substring ( PsqlScanState  state,
const char txt,
int  len 
)

Definition at line 1555 of file psqlscan.l.

1556{
1557 char *result = pg_malloc_array(char, (len + 1));
#define pg_malloc_array(type, count)
Definition fe_memutils.h:56
1558
1559 if (state->safe_encoding)
1560 memcpy(result, txt, len);
1561 else
1562 {
1563 /* Gotta do it the hard way */
1564 const char *reference = state->refline;
1565 int i;
1566
1567 reference += (txt - state->curline);
1568
1569 for (i = 0; i < len; i++)
1570 {
1571 char ch = txt[i];
1572
1573 if (ch == (char) 0xFF)
1574 ch = reference[i];
1575 result[i] = ch;
1576 }
1577 }
1578 result[len] = '\0';
1579 return result;
1580}

References fb(), i, len, and pg_malloc_array.

Referenced by psqlscan_escape_variable(), and psqlscan_test_variable().

◆ psqlscan_pop_buffer_stack()

void psqlscan_pop_buffer_stack ( PsqlScanState  state)

Definition at line 1419 of file psqlscan.l.

1420{
1421 StackElem *stackelem = state->buffer_stack;
1422
1423 state->buffer_stack = stackelem->next;
1424 yy_delete_buffer(stackelem->buf, state->scanner);
1425 free(stackelem->bufstring);
1426 if (stackelem->origstring)
1427 free(stackelem->origstring);
1428 if (stackelem->varname)
1429 free(stackelem->varname);
1430 free(stackelem);
struct state * next
Definition regguts.h:332
1431}

References fb(), free, and state::next.

Referenced by psql_scan_finish().

◆ psqlscan_prepare_buffer()

YY_BUFFER_STATE psqlscan_prepare_buffer ( PsqlScanState  state,
const char txt,
int  len,
char **  txtcopy 
)

Definition at line 1482 of file psqlscan.l.

1484{
1485 char *newtxt;
1486
1487 /* Flex wants two \0 characters after the actual data */
1488 newtxt = pg_malloc_array(char, (len + 2));
1489 *txtcopy = newtxt;
1491
1492 if (state->safe_encoding)
1493 memcpy(newtxt, txt, len);
1494 else
1495 {
1496 /* Gotta do it the hard way */
1497 int i = 0;
1498
1499 while (i < len)
1500 {
1501 int thislen = PQmblen(txt + i, state->encoding);
int PQmblen(const char *s, int encoding)
Definition fe-misc.c:1255
1502
1503 /* first byte should always be okay... */
1504 newtxt[i] = txt[i];
1505 i++;
1506 while (--thislen > 0 && i < len)
1507 newtxt[i++] = (char) 0xFF;
1508 }
1509 }
1510
1511 return yy_scan_buffer(newtxt, len + 2, state->scanner);
1512}

References fb(), i, len, pg_malloc_array, and PQmblen().

Referenced by psql_scan_setup(), and psqlscan_push_new_buffer().

◆ psqlscan_push_new_buffer()

void psqlscan_push_new_buffer ( PsqlScanState  state,
const char newstr,
const char varname 
)

Definition at line 1380 of file psqlscan.l.

1382{
1384
#define pg_malloc_object(type)
Definition fe_memutils.h:50
1386
1387 /*
1388 * In current usage, the passed varname points at the current flex input
1389 * buffer; we must copy it before calling psqlscan_prepare_buffer()
1390 * because that will change the buffer state.
1391 */
1392 stackelem->varname = varname ? pg_strdup(varname) : NULL;
char * pg_strdup(const char *in)
Definition fe_memutils.c:85
1393
1395 &stackelem->bufstring);
1396 state->curline = stackelem->bufstring;
1397 if (state->safe_encoding)
1398 {
1399 stackelem->origstring = NULL;
1400 state->refline = stackelem->bufstring;
1401 }
1402 else
1403 {
1404 stackelem->origstring = pg_strdup(newstr);
1405 state->refline = stackelem->origstring;
1406 }
1407 stackelem->next = state->buffer_stack;
1408 state->buffer_stack = stackelem;
1409}

References fb(), state::next, pg_malloc_object, pg_strdup(), and psqlscan_prepare_buffer().

◆ psqlscan_select_top_buffer()

void psqlscan_select_top_buffer ( PsqlScanState  state)

Definition at line 1437 of file psqlscan.l.

1438{
1439 StackElem *stackelem = state->buffer_stack;
1440
1441 if (stackelem != NULL)
1442 {
1443 yy_switch_to_buffer(stackelem->buf, state->scanner);
1444 state->curline = stackelem->bufstring;
1445 state->refline = stackelem->origstring ? stackelem->origstring : stackelem->bufstring;
1446 }
1447 else
1448 {
1449 yy_switch_to_buffer(state->scanbufhandle, state->scanner);
1450 state->curline = state->scanbuf;
1451 state->refline = state->scanline;
1452 }
1453}

References fb().

◆ psqlscan_test_variable()

void psqlscan_test_variable ( PsqlScanState  state,
const char txt,
int  len 
)

Definition at line 1620 of file psqlscan.l.

1621{
1622 char *varname;
1623 char *value;
1624
1625 varname = psqlscan_extract_substring(state, txt + 3, len - 4);
1626 if (state->callbacks->get_variable)
1627 value = state->callbacks->get_variable(varname, PQUOTE_PLAIN,
1628 state->cb_passthrough);
1629 else
1630 value = NULL;
1631 free(varname);
@ PQUOTE_PLAIN
Definition psqlscan.h:54
1632
1633 if (value != NULL)
1634 {
1635 appendPQExpBufferStr(state->output_buf, "TRUE");
1636 free(value);
1637 }
1638 else
1639 {
1640 appendPQExpBufferStr(state->output_buf, "FALSE");
1641 }
1642}

References appendPQExpBufferStr(), fb(), free, len, PQUOTE_PLAIN, psqlscan_extract_substring(), and value.

◆ psqlscan_var_is_current_source()

bool psqlscan_var_is_current_source ( PsqlScanState  state,
const char varname 
)

Definition at line 1460 of file psqlscan.l.

1461{
1463
1464 for (stackelem = state->buffer_stack;
1465 stackelem != NULL;
1466 stackelem = stackelem->next)
1467 {
1468 if (stackelem->varname && strcmp(stackelem->varname, varname) == 0)
1469 return true;
1470 }
1471 return false;
1472}

References fb().

◆ yylex()

int yylex ( YYSTYPE yylval_param,
yyscan_t  yyscanner 
)

Definition at line 387 of file psqlscan.l.

389 {
390 /* Declare some local variables inside yylex(), for convenience */
392 PQExpBuffer output_buf = cur_state->output_buf;
#define yyextra
Definition scan.l:1102
393
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%}
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)
413 ECHO;
414 }
#define ECHO
Definition psqlscan.l:62
415
416{xcstart} {
417 cur_state->xcdepth = 0;
418 BEGIN(xc);
419 /* Put back any characters past slash-star; see above */
420 yyless(2);
421 ECHO;
422 }
423
424<xc>{
425{xcstart} {
426 cur_state->xcdepth++;
427 /* Put back any characters past slash-star; see above */
428 yyless(2);
429 ECHO;
430 }
431
432{xcstop} {
433 if (cur_state->xcdepth <= 0)
434 BEGIN(INITIAL);
435 else
436 cur_state->xcdepth--;
437 ECHO;
438 }
439
440{xcinside} {
441 ECHO;
442 }
443
444{op_chars} {
445 ECHO;
446 }
447
448\*+ {
449 ECHO;
450 }
451} /* <xc> */
452
453{xbstart} {
454 BEGIN(xb);
455 ECHO;
456 }
457<xh>{xhinside} |
458<xb>{xbinside} {
459 ECHO;
460 }
461
462{xhstart} {
463 /* 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 ECHO;
471 }
472
473{xnstart} {
474 yyless(1); /* eat only 'n' this time */
475 ECHO;
476 }
477
478{xqstart} {
479 if (cur_state->std_strings)
480 BEGIN(xq);
481 else
482 BEGIN(xe);
483 ECHO;
484 }
485{xestart} {
486 BEGIN(xe);
487 ECHO;
488 }
489{xusstart} {
490 BEGIN(xus);
491 ECHO;
492 }
493
494<xb,xh,xq,xe,xus>{quote} {
495 /*
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 BEGIN(xqs);
505 ECHO;
506 }
507<xqs>{quotecontinue} {
508 /*
509 * 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);
514 ECHO;
515 }
516<xqs>{quotecontinuefail} |
517<xqs>{other} {
518 /*
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);
524 BEGIN(INITIAL);
525 /* There's nothing to echo ... */
526 }
527
528<xq,xe,xus>{xqdouble} {
529 ECHO;
530 }
531<xq,xus>{xqinside} {
532 ECHO;
533 }
534<xe>{xeinside} {
535 ECHO;
536 }
537<xe>{xeunicode} {
538 ECHO;
539 }
540<xe>{xeunicodefail} {
541 ECHO;
542 }
543<xe>{xeescape} {
544 ECHO;
545 }
546<xe>{xeoctesc} {
547 ECHO;
548 }
549<xe>{xehexesc} {
550 ECHO;
551 }
552<xe>. {
553 /* This is only needed for \ just before EOF */
554 ECHO;
555 }
556
557{dolqdelim} {
558 cur_state->dolqstart = pg_strdup(yytext);
559 BEGIN(xdolq);
560 ECHO;
561 }
562{dolqfailed} {
563 /* throw back all but the initial "$" */
564 yyless(1);
565 ECHO;
566 }
567<xdolq>{dolqdelim} {
568 if (strcmp(yytext, cur_state->dolqstart) == 0)
569 {
570 free(cur_state->dolqstart);
571 cur_state->dolqstart = NULL;
572 BEGIN(INITIAL);
573 }
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 }
583 ECHO;
584 }
#define yyleng
Definition scan.l:1108
585<xdolq>{dolqinside} {
586 ECHO;
587 }
588<xdolq>{dolqfailed} {
589 ECHO;
590 }
591<xdolq>. {
592 /* This is only needed for $ inside the quoted text */
593 ECHO;
594 }
595
596{xdstart} {
597 BEGIN(xd);
598 ECHO;
599 }
600{xuistart} {
601 BEGIN(xui);
602 ECHO;
603 }
604<xd>{xdstop} {
605 BEGIN(INITIAL);
606 ECHO;
607 }
608<xui>{dquote} {
609 BEGIN(INITIAL);
610 ECHO;
611 }
612<xd,xui>{xddouble} {
613 ECHO;
614 }
615<xd,xui>{xdinside} {
616 ECHO;
617 }
618
619{xufailed} {
620 /* throw back all but the initial u/U */
621 yyless(1);
622 ECHO;
623 }
624
625{typecast} {
626 ECHO;
627 }
628
629{dot_dot} {
630 ECHO;
631 }
632
633{colon_equals} {
634 ECHO;
635 }
636
637{equals_greater} {
638 ECHO;
639 }
640
641{less_equals} {
642 ECHO;
643 }
644
645{greater_equals} {
646 ECHO;
647 }
648
649{less_greater} {
650 ECHO;
651 }
652
653{not_equals} {
654 ECHO;
655 }
656
657{right_arrow} {
658 ECHO;
659 }
660
661 /*
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"(" {
668 cur_state->paren_depth++;
669 ECHO;
670 }
671
672")" {
673 if (cur_state->paren_depth > 0)
674 cur_state->paren_depth--;
675 ECHO;
676 }
677
678";" {
679 ECHO;
680 if (cur_state->paren_depth == 0 && cur_state->begin_depth == 0)
681 {
682 /* Terminate lexing temporarily */
683 cur_state->start_state = YY_START;
684 cur_state->identifier_count = 0;
685 return LEXRES_SEMI;
686 }
687 }
688
689 /*
690 * psql-specific rules to handle backslash commands and variable
691 * substitution. We want these before {self}, also.
692 */
693
694"\\"[;:] {
695 /* Force a semi-colon or colon into the query buffer */
697 if (yytext[1] == ';')
698 cur_state->identifier_count = 0;
699 }
700
701"\\" {
702 /* Terminate lexing temporarily */
703 cur_state->start_state = YY_START;
704 return LEXRES_BACKSLASH;
705 }
706
707:{variable_char}+ {
708 /* Possible psql variable substitution */
709 char *varname;
710 char *value;
711
713 yytext + 1,
714 yyleng - 1);
715 if (cur_state->callbacks->get_variable)
716 value = cur_state->callbacks->get_variable(varname,
718 cur_state->cb_passthrough);
719 else
720 value = NULL;
721
722 if (value)
723 {
724 /* It is a variable, check for recursion */
726 {
727 /* Recursive expansion --- don't go there */
728 pg_log_warning("skipping recursive expansion of variable \"%s\"",
729 varname);
730 /* Instead copy the string as is */
731 ECHO;
732 }
733 else
734 {
735 /* OK, perform substitution */
737 /* yy_scan_string already made buffer active */
738 }
739 free(value);
740 }
741 else
742 {
743 /*
744 * if the variable doesn't exist we'll copy the string
745 * as is
746 */
747 ECHO;
748 }
#define pg_log_warning(...)
Definition pgfnames.c:24
void psqlscan_push_new_buffer(PsqlScanState state, const char *newstr, const char *varname)
Definition psqlscan.l:1380
bool psqlscan_var_is_current_source(PsqlScanState state, const char *varname)
Definition psqlscan.l:1460
749
750 free(varname);
751 }
752
753:'{variable_char}+' {
756 }
@ PQUOTE_SQL_LITERAL
Definition psqlscan.h:55
void psqlscan_escape_variable(PsqlScanState state, const char *txt, int len, PsqlScanQuoteType quote)
Definition psqlscan.l:1591
757
758:\"{variable_char}+\" {
761 }
@ PQUOTE_SQL_IDENT
Definition psqlscan.h:56
762
763:\{\?{variable_char}+\} {
765 }
void psqlscan_test_variable(PsqlScanState state, const char *txt, int len)
Definition psqlscan.l:1620
766
767 /*
768 * These rules just avoid the need for scanner backup if one of the
769 * three rules above fails to match completely.
770 */
771
772:'{variable_char}* {
773 /* Throw back everything but the colon */
774 yyless(1);
775 ECHO;
776 }
777
778:\"{variable_char}* {
779 /* Throw back everything but the colon */
780 yyless(1);
781 ECHO;
782 }
783
784:\{\?{variable_char}* {
785 /* Throw back everything but the colon */
786 yyless(1);
787 ECHO;
788 }
789:\{ {
790 /* Throw back everything but the colon */
791 yyless(1);
792 ECHO;
793 }
794
795 /*
796 * Back to backend-compatible rules.
797 */
798
799{self} {
800 ECHO;
801 }
802
803{operator} {
804 /*
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;
811 char *slashstar = strstr(yytext, "/*");
812 char *dashdash = strstr(yytext, "--");
813
814 if (slashstar && dashdash)
815 {
816 /* if both appear, take the first one */
817 if (slashstar > dashdash)
819 }
820 else if (!slashstar)
822 if (slashstar)
823 nchars = slashstar - yytext;
824
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 &&
834 (yytext[nchars - 1] == '+' ||
835 yytext[nchars - 1] == '-'))
836 {
837 int ic;
838
839 for (ic = nchars - 2; ic >= 0; ic--)
840 {
841 char c = yytext[ic];
842 if (c == '~' || c == '!' || c == '@' ||
843 c == '#' || c == '^' || c == '&' ||
844 c == '|' || c == '`' || c == '?' ||
845 c == '%')
846 break;
847 }
848 if (ic < 0)
849 {
850 /*
851 * didn't find a qualifying character, so remove
852 * all trailing [+-]
853 */
854 do {
855 nchars--;
856 } while (nchars > 1 &&
857 (yytext[nchars - 1] == '+' ||
858 yytext[nchars - 1] == '-'));
859 }
860 }
char * c
861
862 if (nchars < yyleng)
863 {
864 /* Strip the unwanted chars from the token */
865 yyless(nchars);
866 }
867 ECHO;
868 }
869
870{param} {
871 ECHO;
872 }
873{param_junk} {
874 ECHO;
875 }
876
877{decinteger} {
878 ECHO;
879 }
880{hexinteger} {
881 ECHO;
882 }
883{octinteger} {
884 ECHO;
885 }
886{bininteger} {
887 ECHO;
888 }
889{hexfail} {
890 ECHO;
891 }
892{octfail} {
893 ECHO;
894 }
895{binfail} {
896 ECHO;
897 }
898{numeric} {
899 ECHO;
900 }
901{numericfail} {
902 /* throw back the .., and treat as integer */
903 yyless(yyleng - 2);
904 ECHO;
905 }
906{real} {
907 ECHO;
908 }
909{realfail} {
910 ECHO;
911 }
912{integer_junk} {
913 ECHO;
914 }
915{numeric_junk} {
916 ECHO;
917 }
918{real_junk} {
919 ECHO;
920 }
921
922
923{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}
932 */
933
934 if (cur_state->identifier_count == 0)
935 memset(cur_state->identifiers, 0, sizeof(cur_state->identifiers));
936
937 if (pg_strcasecmp(yytext, "create") == 0 ||
938 pg_strcasecmp(yytext, "function") == 0 ||
939 pg_strcasecmp(yytext, "procedure") == 0 ||
940 pg_strcasecmp(yytext, "or") == 0 ||
941 pg_strcasecmp(yytext, "replace") == 0)
942 {
943 if (cur_state->identifier_count < sizeof(cur_state->identifiers))
944 cur_state->identifiers[cur_state->identifier_count] = pg_tolower((unsigned char) yytext[0]);
945 }
int pg_strcasecmp(const char *s1, const char *s2)
unsigned char pg_tolower(unsigned char ch)
946
947 cur_state->identifier_count++;
948
949 if (cur_state->identifiers[0] == 'c' &&
950 (cur_state->identifiers[1] == 'f' || cur_state->identifiers[1] == 'p' ||
951 (cur_state->identifiers[1] == 'o' && cur_state->identifiers[2] == 'r' &&
952 (cur_state->identifiers[3] == 'f' || cur_state->identifiers[3] == 'p'))) &&
953 cur_state->paren_depth == 0)
954 {
955 if (pg_strcasecmp(yytext, "begin") == 0)
956 cur_state->begin_depth++;
957 else if (pg_strcasecmp(yytext, "case") == 0)
958 {
959 /*
960 * CASE also ends with END. We only need to track
961 * this if we are already inside a BEGIN.
962 */
963 if (cur_state->begin_depth >= 1)
964 cur_state->begin_depth++;
965 }
966 else if (pg_strcasecmp(yytext, "end") == 0)
967 {
968 if (cur_state->begin_depth > 0)
969 cur_state->begin_depth--;
970 }
971 }
972
973 ECHO;
974 }
975
976{other} {
977 ECHO;
978 }
979
980<<EOF>> {
981 if (cur_state->buffer_stack == NULL)
982 {
983 cur_state->start_state = YY_START;
984 return LEXRES_EOL; /* end of input reached */
985 }
986
987 /*
988 * We were expanding a variable, so pop the inclusion
989 * stack and keep lexing
990 */
993 }
void psqlscan_select_top_buffer(PsqlScanState state)
Definition psqlscan.l:1437
994
995%%