PostgreSQL Source Code  git master
pl_scanner.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pl_scanner.c
4  * lexical scanning for PL/pgSQL
5  *
6  *
7  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  * src/pl/plpgsql/src/pl_scanner.c
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17 
18 #include "mb/pg_wchar.h"
19 #include "parser/scanner.h"
20 
21 #include "plpgsql.h"
22 #include "pl_gram.h" /* must be after parser/scanner.h */
23 
24 
25 #define PG_KEYWORD(a,b,c) {a,b,c},
26 
27 
28 /* Klugy flag to tell scanner how to look up identifiers */
30 
31 /*
32  * A word about keywords:
33  *
34  * We keep reserved and unreserved keywords in separate arrays. The
35  * reserved keywords are passed to the core scanner, so they will be
36  * recognized before (and instead of) any variable name. Unreserved words
37  * are checked for separately, usually after determining that the identifier
38  * isn't a known variable name. If plpgsql_IdentifierLookup is DECLARE then
39  * no variable names will be recognized, so the unreserved words always work.
40  * (Note in particular that this helps us avoid reserving keywords that are
41  * only needed in DECLARE sections.)
42  *
43  * In certain contexts it is desirable to prefer recognizing an unreserved
44  * keyword over recognizing a variable name. In particular, at the start
45  * of a statement we should prefer unreserved keywords unless the statement
46  * looks like an assignment (i.e., first token is followed by ':=' or '[').
47  * This rule allows most statement-introducing keywords to be kept unreserved.
48  * (We still have to reserve initial keywords that might follow a block
49  * label, unfortunately, since the method used to determine if we are at
50  * start of statement doesn't recognize such cases. We'd also have to
51  * reserve any keyword that could legitimately be followed by ':=' or '['.)
52  * Some additional cases are handled in pl_gram.y using tok_is_keyword().
53  *
54  * We try to avoid reserving more keywords than we have to; but there's
55  * little point in not reserving a word if it's reserved in the core grammar.
56  * Currently, the following words are reserved here but not in the core:
57  * BEGIN BY DECLARE EXECUTE FOREACH IF LOOP STRICT WHILE
58  */
59 
60 /*
61  * Lists of keyword (name, token-value, category) entries.
62  *
63  * !!WARNING!!: These lists must be sorted by ASCII name, because binary
64  * search is used to locate entries.
65  *
66  * Be careful not to put the same word in both lists. Also be sure that
67  * pl_gram.y's unreserved_keyword production agrees with the second list.
68  */
69 
70 static const ScanKeyword reserved_keywords[] = {
71  PG_KEYWORD("all", K_ALL, RESERVED_KEYWORD)
72  PG_KEYWORD("begin", K_BEGIN, RESERVED_KEYWORD)
73  PG_KEYWORD("by", K_BY, RESERVED_KEYWORD)
74  PG_KEYWORD("case", K_CASE, RESERVED_KEYWORD)
75  PG_KEYWORD("declare", K_DECLARE, RESERVED_KEYWORD)
76  PG_KEYWORD("else", K_ELSE, RESERVED_KEYWORD)
77  PG_KEYWORD("end", K_END, RESERVED_KEYWORD)
78  PG_KEYWORD("execute", K_EXECUTE, RESERVED_KEYWORD)
79  PG_KEYWORD("for", K_FOR, RESERVED_KEYWORD)
80  PG_KEYWORD("foreach", K_FOREACH, RESERVED_KEYWORD)
81  PG_KEYWORD("from", K_FROM, RESERVED_KEYWORD)
82  PG_KEYWORD("if", K_IF, RESERVED_KEYWORD)
83  PG_KEYWORD("in", K_IN, RESERVED_KEYWORD)
84  PG_KEYWORD("into", K_INTO, RESERVED_KEYWORD)
85  PG_KEYWORD("loop", K_LOOP, RESERVED_KEYWORD)
86  PG_KEYWORD("not", K_NOT, RESERVED_KEYWORD)
87  PG_KEYWORD("null", K_NULL, RESERVED_KEYWORD)
88  PG_KEYWORD("or", K_OR, RESERVED_KEYWORD)
89  PG_KEYWORD("strict", K_STRICT, RESERVED_KEYWORD)
90  PG_KEYWORD("then", K_THEN, RESERVED_KEYWORD)
91  PG_KEYWORD("to", K_TO, RESERVED_KEYWORD)
92  PG_KEYWORD("using", K_USING, RESERVED_KEYWORD)
93  PG_KEYWORD("when", K_WHEN, RESERVED_KEYWORD)
94  PG_KEYWORD("while", K_WHILE, RESERVED_KEYWORD)
95 };
96 
97 static const int num_reserved_keywords = lengthof(reserved_keywords);
98 
99 static const ScanKeyword unreserved_keywords[] = {
100  PG_KEYWORD("absolute", K_ABSOLUTE, UNRESERVED_KEYWORD)
101  PG_KEYWORD("alias", K_ALIAS, UNRESERVED_KEYWORD)
102  PG_KEYWORD("array", K_ARRAY, UNRESERVED_KEYWORD)
103  PG_KEYWORD("assert", K_ASSERT, UNRESERVED_KEYWORD)
104  PG_KEYWORD("backward", K_BACKWARD, UNRESERVED_KEYWORD)
105  PG_KEYWORD("close", K_CLOSE, UNRESERVED_KEYWORD)
106  PG_KEYWORD("collate", K_COLLATE, UNRESERVED_KEYWORD)
107  PG_KEYWORD("column", K_COLUMN, UNRESERVED_KEYWORD)
108  PG_KEYWORD("column_name", K_COLUMN_NAME, UNRESERVED_KEYWORD)
109  PG_KEYWORD("commit", K_COMMIT, UNRESERVED_KEYWORD)
110  PG_KEYWORD("constant", K_CONSTANT, UNRESERVED_KEYWORD)
111  PG_KEYWORD("constraint", K_CONSTRAINT, UNRESERVED_KEYWORD)
112  PG_KEYWORD("constraint_name", K_CONSTRAINT_NAME, UNRESERVED_KEYWORD)
113  PG_KEYWORD("continue", K_CONTINUE, UNRESERVED_KEYWORD)
114  PG_KEYWORD("current", K_CURRENT, UNRESERVED_KEYWORD)
115  PG_KEYWORD("cursor", K_CURSOR, UNRESERVED_KEYWORD)
116  PG_KEYWORD("datatype", K_DATATYPE, UNRESERVED_KEYWORD)
117  PG_KEYWORD("debug", K_DEBUG, UNRESERVED_KEYWORD)
118  PG_KEYWORD("default", K_DEFAULT, UNRESERVED_KEYWORD)
119  PG_KEYWORD("detail", K_DETAIL, UNRESERVED_KEYWORD)
120  PG_KEYWORD("diagnostics", K_DIAGNOSTICS, UNRESERVED_KEYWORD)
121  PG_KEYWORD("dump", K_DUMP, UNRESERVED_KEYWORD)
122  PG_KEYWORD("elseif", K_ELSIF, UNRESERVED_KEYWORD)
123  PG_KEYWORD("elsif", K_ELSIF, UNRESERVED_KEYWORD)
124  PG_KEYWORD("errcode", K_ERRCODE, UNRESERVED_KEYWORD)
125  PG_KEYWORD("error", K_ERROR, UNRESERVED_KEYWORD)
126  PG_KEYWORD("exception", K_EXCEPTION, UNRESERVED_KEYWORD)
127  PG_KEYWORD("exit", K_EXIT, UNRESERVED_KEYWORD)
128  PG_KEYWORD("fetch", K_FETCH, UNRESERVED_KEYWORD)
129  PG_KEYWORD("first", K_FIRST, UNRESERVED_KEYWORD)
130  PG_KEYWORD("forward", K_FORWARD, UNRESERVED_KEYWORD)
131  PG_KEYWORD("get", K_GET, UNRESERVED_KEYWORD)
132  PG_KEYWORD("hint", K_HINT, UNRESERVED_KEYWORD)
133  PG_KEYWORD("import", K_IMPORT, UNRESERVED_KEYWORD)
134  PG_KEYWORD("info", K_INFO, UNRESERVED_KEYWORD)
135  PG_KEYWORD("insert", K_INSERT, UNRESERVED_KEYWORD)
136  PG_KEYWORD("is", K_IS, UNRESERVED_KEYWORD)
137  PG_KEYWORD("last", K_LAST, UNRESERVED_KEYWORD)
138  PG_KEYWORD("log", K_LOG, UNRESERVED_KEYWORD)
139  PG_KEYWORD("message", K_MESSAGE, UNRESERVED_KEYWORD)
140  PG_KEYWORD("message_text", K_MESSAGE_TEXT, UNRESERVED_KEYWORD)
141  PG_KEYWORD("move", K_MOVE, UNRESERVED_KEYWORD)
142  PG_KEYWORD("next", K_NEXT, UNRESERVED_KEYWORD)
143  PG_KEYWORD("no", K_NO, UNRESERVED_KEYWORD)
144  PG_KEYWORD("notice", K_NOTICE, UNRESERVED_KEYWORD)
145  PG_KEYWORD("open", K_OPEN, UNRESERVED_KEYWORD)
146  PG_KEYWORD("option", K_OPTION, UNRESERVED_KEYWORD)
147  PG_KEYWORD("perform", K_PERFORM, UNRESERVED_KEYWORD)
148  PG_KEYWORD("pg_context", K_PG_CONTEXT, UNRESERVED_KEYWORD)
149  PG_KEYWORD("pg_datatype_name", K_PG_DATATYPE_NAME, UNRESERVED_KEYWORD)
150  PG_KEYWORD("pg_exception_context", K_PG_EXCEPTION_CONTEXT, UNRESERVED_KEYWORD)
151  PG_KEYWORD("pg_exception_detail", K_PG_EXCEPTION_DETAIL, UNRESERVED_KEYWORD)
152  PG_KEYWORD("pg_exception_hint", K_PG_EXCEPTION_HINT, UNRESERVED_KEYWORD)
153  PG_KEYWORD("print_strict_params", K_PRINT_STRICT_PARAMS, UNRESERVED_KEYWORD)
154  PG_KEYWORD("prior", K_PRIOR, UNRESERVED_KEYWORD)
155  PG_KEYWORD("query", K_QUERY, UNRESERVED_KEYWORD)
156  PG_KEYWORD("raise", K_RAISE, UNRESERVED_KEYWORD)
157  PG_KEYWORD("relative", K_RELATIVE, UNRESERVED_KEYWORD)
158  PG_KEYWORD("result_oid", K_RESULT_OID, UNRESERVED_KEYWORD)
159  PG_KEYWORD("return", K_RETURN, UNRESERVED_KEYWORD)
160  PG_KEYWORD("returned_sqlstate", K_RETURNED_SQLSTATE, UNRESERVED_KEYWORD)
161  PG_KEYWORD("reverse", K_REVERSE, UNRESERVED_KEYWORD)
162  PG_KEYWORD("rollback", K_ROLLBACK, UNRESERVED_KEYWORD)
163  PG_KEYWORD("row_count", K_ROW_COUNT, UNRESERVED_KEYWORD)
164  PG_KEYWORD("rowtype", K_ROWTYPE, UNRESERVED_KEYWORD)
165  PG_KEYWORD("schema", K_SCHEMA, UNRESERVED_KEYWORD)
166  PG_KEYWORD("schema_name", K_SCHEMA_NAME, UNRESERVED_KEYWORD)
167  PG_KEYWORD("scroll", K_SCROLL, UNRESERVED_KEYWORD)
168  PG_KEYWORD("slice", K_SLICE, UNRESERVED_KEYWORD)
169  PG_KEYWORD("sqlstate", K_SQLSTATE, UNRESERVED_KEYWORD)
170  PG_KEYWORD("stacked", K_STACKED, UNRESERVED_KEYWORD)
171  PG_KEYWORD("table", K_TABLE, UNRESERVED_KEYWORD)
172  PG_KEYWORD("table_name", K_TABLE_NAME, UNRESERVED_KEYWORD)
173  PG_KEYWORD("type", K_TYPE, UNRESERVED_KEYWORD)
174  PG_KEYWORD("use_column", K_USE_COLUMN, UNRESERVED_KEYWORD)
175  PG_KEYWORD("use_variable", K_USE_VARIABLE, UNRESERVED_KEYWORD)
176  PG_KEYWORD("variable_conflict", K_VARIABLE_CONFLICT, UNRESERVED_KEYWORD)
177  PG_KEYWORD("warning", K_WARNING, UNRESERVED_KEYWORD)
178 };
179 
180 static const int num_unreserved_keywords = lengthof(unreserved_keywords);
181 
182 /*
183  * This macro must recognize all tokens that can immediately precede a
184  * PL/pgSQL executable statement (that is, proc_sect or proc_stmt in the
185  * grammar). Fortunately, there are not very many, so hard-coding in this
186  * fashion seems sufficient.
187  */
188 #define AT_STMT_START(prev_token) \
189  ((prev_token) == ';' || \
190  (prev_token) == K_BEGIN || \
191  (prev_token) == K_THEN || \
192  (prev_token) == K_ELSE || \
193  (prev_token) == K_LOOP)
194 
195 
196 /* Auxiliary data about a token (other than the token type) */
197 typedef struct
198 {
199  YYSTYPE lval; /* semantic information */
200  YYLTYPE lloc; /* offset in scanbuf */
201  int leng; /* length in bytes */
202 } TokenAuxData;
203 
204 /*
205  * Scanner working state. At some point we might wish to fold all this
206  * into a YY_EXTRA struct. For the moment, there is no need for plpgsql's
207  * lexer to be re-entrant, and the notational burden of passing a yyscanner
208  * pointer around is great enough to not want to do it without need.
209  */
210 
211 /* The stuff the core lexer needs */
212 static core_yyscan_t yyscanner = NULL;
214 
215 /* The original input string */
216 static const char *scanorig;
217 
218 /* Current token's length (corresponds to plpgsql_yylval and plpgsql_yylloc) */
219 static int plpgsql_yyleng;
220 
221 /* Current token's code (corresponds to plpgsql_yylval and plpgsql_yylloc) */
222 static int plpgsql_yytoken;
223 
224 /* Token pushback stack */
225 #define MAX_PUSHBACKS 4
226 
227 static int num_pushbacks;
230 
231 /* State for plpgsql_location_to_lineno() */
232 static const char *cur_line_start;
233 static const char *cur_line_end;
234 static int cur_line_num;
235 
236 /* Internal functions */
237 static int internal_yylex(TokenAuxData *auxdata);
238 static void push_back_token(int token, TokenAuxData *auxdata);
239 static void location_lineno_init(void);
240 
241 
242 /*
243  * This is the yylex routine called from the PL/pgSQL grammar.
244  * It is a wrapper around the core lexer, with the ability to recognize
245  * PL/pgSQL variables and return them as special T_DATUM tokens. If a
246  * word or compound word does not match any variable name, or if matching
247  * is turned off by plpgsql_IdentifierLookup, it is returned as
248  * T_WORD or T_CWORD respectively, or as an unreserved keyword if it
249  * matches one of those.
250  */
251 int
253 {
254  int tok1;
255  TokenAuxData aux1;
256  const ScanKeyword *kw;
257 
258  tok1 = internal_yylex(&aux1);
259  if (tok1 == IDENT || tok1 == PARAM)
260  {
261  int tok2;
262  TokenAuxData aux2;
263 
264  tok2 = internal_yylex(&aux2);
265  if (tok2 == '.')
266  {
267  int tok3;
268  TokenAuxData aux3;
269 
270  tok3 = internal_yylex(&aux3);
271  if (tok3 == IDENT)
272  {
273  int tok4;
274  TokenAuxData aux4;
275 
276  tok4 = internal_yylex(&aux4);
277  if (tok4 == '.')
278  {
279  int tok5;
280  TokenAuxData aux5;
281 
282  tok5 = internal_yylex(&aux5);
283  if (tok5 == IDENT)
284  {
285  if (plpgsql_parse_tripword(aux1.lval.str,
286  aux3.lval.str,
287  aux5.lval.str,
288  &aux1.lval.wdatum,
289  &aux1.lval.cword))
290  tok1 = T_DATUM;
291  else
292  tok1 = T_CWORD;
293  }
294  else
295  {
296  /* not A.B.C, so just process A.B */
297  push_back_token(tok5, &aux5);
298  push_back_token(tok4, &aux4);
299  if (plpgsql_parse_dblword(aux1.lval.str,
300  aux3.lval.str,
301  &aux1.lval.wdatum,
302  &aux1.lval.cword))
303  tok1 = T_DATUM;
304  else
305  tok1 = T_CWORD;
306  }
307  }
308  else
309  {
310  /* not A.B.C, so just process A.B */
311  push_back_token(tok4, &aux4);
312  if (plpgsql_parse_dblword(aux1.lval.str,
313  aux3.lval.str,
314  &aux1.lval.wdatum,
315  &aux1.lval.cword))
316  tok1 = T_DATUM;
317  else
318  tok1 = T_CWORD;
319  }
320  }
321  else
322  {
323  /* not A.B, so just process A */
324  push_back_token(tok3, &aux3);
325  push_back_token(tok2, &aux2);
326  if (plpgsql_parse_word(aux1.lval.str,
327  core_yy.scanbuf + aux1.lloc,
328  &aux1.lval.wdatum,
329  &aux1.lval.word))
330  tok1 = T_DATUM;
331  else if (!aux1.lval.word.quoted &&
332  (kw = ScanKeywordLookup(aux1.lval.word.ident,
333  unreserved_keywords,
335  {
336  aux1.lval.keyword = kw->name;
337  tok1 = kw->value;
338  }
339  else
340  tok1 = T_WORD;
341  }
342  }
343  else
344  {
345  /* not A.B, so just process A */
346  push_back_token(tok2, &aux2);
347 
348  /*
349  * If we are at start of statement, prefer unreserved keywords
350  * over variable names, unless the next token is assignment or
351  * '[', in which case prefer variable names. (Note we need not
352  * consider '.' as the next token; that case was handled above,
353  * and we always prefer variable names in that case.) If we are
354  * not at start of statement, always prefer variable names over
355  * unreserved keywords.
356  */
358  !(tok2 == '=' || tok2 == COLON_EQUALS || tok2 == '['))
359  {
360  /* try for unreserved keyword, then for variable name */
361  if (core_yy.scanbuf[aux1.lloc] != '"' &&
362  (kw = ScanKeywordLookup(aux1.lval.str,
363  unreserved_keywords,
365  {
366  aux1.lval.keyword = kw->name;
367  tok1 = kw->value;
368  }
369  else if (plpgsql_parse_word(aux1.lval.str,
370  core_yy.scanbuf + aux1.lloc,
371  &aux1.lval.wdatum,
372  &aux1.lval.word))
373  tok1 = T_DATUM;
374  else
375  tok1 = T_WORD;
376  }
377  else
378  {
379  /* try for variable name, then for unreserved keyword */
380  if (plpgsql_parse_word(aux1.lval.str,
381  core_yy.scanbuf + aux1.lloc,
382  &aux1.lval.wdatum,
383  &aux1.lval.word))
384  tok1 = T_DATUM;
385  else if (!aux1.lval.word.quoted &&
386  (kw = ScanKeywordLookup(aux1.lval.word.ident,
387  unreserved_keywords,
389  {
390  aux1.lval.keyword = kw->name;
391  tok1 = kw->value;
392  }
393  else
394  tok1 = T_WORD;
395  }
396  }
397  }
398  else
399  {
400  /*
401  * Not a potential plpgsql variable name, just return the data.
402  *
403  * Note that we also come through here if the grammar pushed back a
404  * T_DATUM, T_CWORD, T_WORD, or unreserved-keyword token returned by a
405  * previous lookup cycle; thus, pushbacks do not incur extra lookup
406  * work, since we'll never do the above code twice for the same token.
407  * This property also makes it safe to rely on the old value of
408  * plpgsql_yytoken in the is-this-start-of-statement test above.
409  */
410  }
411 
412  plpgsql_yylval = aux1.lval;
413  plpgsql_yylloc = aux1.lloc;
414  plpgsql_yyleng = aux1.leng;
415  plpgsql_yytoken = tok1;
416  return tok1;
417 }
418 
419 /*
420  * Internal yylex function. This wraps the core lexer and adds one feature:
421  * a token pushback stack. We also make a couple of trivial single-token
422  * translations from what the core lexer does to what we want, in particular
423  * interfacing from the core_YYSTYPE to YYSTYPE union.
424  */
425 static int
427 {
428  int token;
429  const char *yytext;
430 
431  if (num_pushbacks > 0)
432  {
433  num_pushbacks--;
434  token = pushback_token[num_pushbacks];
435  *auxdata = pushback_auxdata[num_pushbacks];
436  }
437  else
438  {
439  token = core_yylex(&auxdata->lval.core_yystype,
440  &auxdata->lloc,
441  yyscanner);
442 
443  /* remember the length of yytext before it gets changed */
444  yytext = core_yy.scanbuf + auxdata->lloc;
445  auxdata->leng = strlen(yytext);
446 
447  /* Check for << >> and #, which the core considers operators */
448  if (token == Op)
449  {
450  if (strcmp(auxdata->lval.str, "<<") == 0)
451  token = LESS_LESS;
452  else if (strcmp(auxdata->lval.str, ">>") == 0)
453  token = GREATER_GREATER;
454  else if (strcmp(auxdata->lval.str, "#") == 0)
455  token = '#';
456  }
457 
458  /* The core returns PARAM as ival, but we treat it like IDENT */
459  else if (token == PARAM)
460  {
461  auxdata->lval.str = pstrdup(yytext);
462  }
463  }
464 
465  return token;
466 }
467 
468 /*
469  * Push back a token to be re-read by next internal_yylex() call.
470  */
471 static void
472 push_back_token(int token, TokenAuxData *auxdata)
473 {
475  elog(ERROR, "too many tokens pushed back");
476  pushback_token[num_pushbacks] = token;
477  pushback_auxdata[num_pushbacks] = *auxdata;
478  num_pushbacks++;
479 }
480 
481 /*
482  * Push back a single token to be re-read by next plpgsql_yylex() call.
483  *
484  * NOTE: this does not cause yylval or yylloc to "back up". Also, it
485  * is not a good idea to push back a token code other than what you read.
486  */
487 void
489 {
490  TokenAuxData auxdata;
491 
492  auxdata.lval = plpgsql_yylval;
493  auxdata.lloc = plpgsql_yylloc;
494  auxdata.leng = plpgsql_yyleng;
495  push_back_token(token, &auxdata);
496 }
497 
498 /*
499  * Tell whether a token is an unreserved keyword.
500  *
501  * (If it is, its lowercased form was returned as the token value, so we
502  * do not need to offer that data here.)
503  */
504 bool
506 {
507  int i;
508 
509  for (i = 0; i < num_unreserved_keywords; i++)
510  {
511  if (unreserved_keywords[i].value == token)
512  return true;
513  }
514  return false;
515 }
516 
517 /*
518  * Append the function text starting at startlocation and extending to
519  * (not including) endlocation onto the existing contents of "buf".
520  */
521 void
523  int startlocation, int endlocation)
524 {
525  Assert(startlocation <= endlocation);
526  appendBinaryStringInfo(buf, scanorig + startlocation,
527  endlocation - startlocation);
528 }
529 
530 /*
531  * Peek one token ahead in the input stream. Only the token code is
532  * made available, not any of the auxiliary info such as location.
533  *
534  * NB: no variable or unreserved keyword lookup is performed here, they will
535  * be returned as IDENT. Reserved keywords are resolved as usual.
536  */
537 int
539 {
540  int tok1;
541  TokenAuxData aux1;
542 
543  tok1 = internal_yylex(&aux1);
544  push_back_token(tok1, &aux1);
545  return tok1;
546 }
547 
548 /*
549  * Peek two tokens ahead in the input stream. The first token and its
550  * location in the query are returned in *tok1_p and *tok1_loc, second token
551  * and its location in *tok2_p and *tok2_loc.
552  *
553  * NB: no variable or unreserved keyword lookup is performed here, they will
554  * be returned as IDENT. Reserved keywords are resolved as usual.
555  */
556 void
557 plpgsql_peek2(int *tok1_p, int *tok2_p, int *tok1_loc, int *tok2_loc)
558 {
559  int tok1,
560  tok2;
561  TokenAuxData aux1,
562  aux2;
563 
564  tok1 = internal_yylex(&aux1);
565  tok2 = internal_yylex(&aux2);
566 
567  *tok1_p = tok1;
568  if (tok1_loc)
569  *tok1_loc = aux1.lloc;
570  *tok2_p = tok2;
571  if (tok2_loc)
572  *tok2_loc = aux2.lloc;
573 
574  push_back_token(tok2, &aux2);
575  push_back_token(tok1, &aux1);
576 }
577 
578 /*
579  * plpgsql_scanner_errposition
580  * Report an error cursor position, if possible.
581  *
582  * This is expected to be used within an ereport() call. The return value
583  * is a dummy (always 0, in fact).
584  *
585  * Note that this can only be used for messages emitted during initial
586  * parsing of a plpgsql function, since it requires the scanorig string
587  * to still be available.
588  */
589 int
591 {
592  int pos;
593 
594  if (location < 0 || scanorig == NULL)
595  return 0; /* no-op if location is unknown */
596 
597  /* Convert byte offset to character number */
598  pos = pg_mbstrlen_with_len(scanorig, location) + 1;
599  /* And pass it to the ereport mechanism */
600  (void) internalerrposition(pos);
601  /* Also pass the function body string */
602  return internalerrquery(scanorig);
603 }
604 
605 /*
606  * plpgsql_yyerror
607  * Report a lexer or grammar error.
608  *
609  * The message's cursor position refers to the current token (the one
610  * last returned by plpgsql_yylex()).
611  * This is OK for syntax error messages from the Bison parser, because Bison
612  * parsers report error as soon as the first unparsable token is reached.
613  * Beware of using yyerror for other purposes, as the cursor position might
614  * be misleading!
615  */
616 void
617 plpgsql_yyerror(const char *message)
618 {
619  char *yytext = core_yy.scanbuf + plpgsql_yylloc;
620 
621  if (*yytext == '\0')
622  {
623  ereport(ERROR,
624  (errcode(ERRCODE_SYNTAX_ERROR),
625  /* translator: %s is typically the translation of "syntax error" */
626  errmsg("%s at end of input", _(message)),
627  plpgsql_scanner_errposition(plpgsql_yylloc)));
628  }
629  else
630  {
631  /*
632  * If we have done any lookahead then flex will have restored the
633  * character after the end-of-token. Zap it again so that we report
634  * only the single token here. This modifies scanbuf but we no longer
635  * care about that.
636  */
637  yytext[plpgsql_yyleng] = '\0';
638 
639  ereport(ERROR,
640  (errcode(ERRCODE_SYNTAX_ERROR),
641  /* translator: first %s is typically the translation of "syntax error" */
642  errmsg("%s at or near \"%s\"", _(message), yytext),
643  plpgsql_scanner_errposition(plpgsql_yylloc)));
644  }
645 }
646 
647 /*
648  * Given a location (a byte offset in the function source text),
649  * return a line number.
650  *
651  * We expect that this is typically called for a sequence of increasing
652  * location values, so optimize accordingly by tracking the endpoints
653  * of the "current" line.
654  */
655 int
657 {
658  const char *loc;
659 
660  if (location < 0 || scanorig == NULL)
661  return 0; /* garbage in, garbage out */
662  loc = scanorig + location;
663 
664  /* be correct, but not fast, if input location goes backwards */
665  if (loc < cur_line_start)
667 
668  while (cur_line_end != NULL && loc > cur_line_end)
669  {
671  cur_line_num++;
672  cur_line_end = strchr(cur_line_start, '\n');
673  }
674 
675  return cur_line_num;
676 }
677 
678 /* initialize or reset the state for plpgsql_location_to_lineno */
679 static void
681 {
683  cur_line_num = 1;
684 
685  cur_line_end = strchr(cur_line_start, '\n');
686 }
687 
688 /* return the most recently computed lineno */
689 int
691 {
692  return cur_line_num;
693 }
694 
695 
696 /*
697  * Called before any actual parsing is done
698  *
699  * Note: the passed "str" must remain valid until plpgsql_scanner_finish().
700  * Although it is not fed directly to flex, we need the original string
701  * to cite in error messages.
702  */
703 void
705 {
706  /* Start up the core scanner */
707  yyscanner = scanner_init(str, &core_yy,
708  reserved_keywords, num_reserved_keywords);
709 
710  /*
711  * scanorig points to the original string, which unlike the scanner's
712  * scanbuf won't be modified on-the-fly by flex. Notice that although
713  * yytext points into scanbuf, we rely on being able to apply locations
714  * (offsets from string start) to scanorig as well.
715  */
716  scanorig = str;
717 
718  /* Other setup */
720  plpgsql_yytoken = 0;
721 
722  num_pushbacks = 0;
723 
725 }
726 
727 /*
728  * Called after parsing is done to clean up after plpgsql_scanner_init()
729  */
730 void
732 {
733  /* release storage */
735  /* avoid leaving any dangling pointers */
736  yyscanner = NULL;
737  scanorig = NULL;
738 }
#define MAX_PUSHBACKS
Definition: pl_scanner.c:225
int plpgsql_latest_lineno(void)
Definition: pl_scanner.c:690
static int cur_line_num
Definition: pl_scanner.c:234
bool plpgsql_parse_dblword(char *word1, char *word2, PLwdatum *wdatum, PLcword *cword)
Definition: pl_comp.c:1417
static struct @130 value
void * core_yyscan_t
Definition: scanner.h:116
static TokenAuxData pushback_auxdata[MAX_PUSHBACKS]
Definition: pl_scanner.c:229
const ScanKeyword * ScanKeywordLookup(const char *text, const ScanKeyword *keywords, int num_keywords)
Definition: keywords.c:64
IdentifierLookup
Definition: plpgsql.h:1097
char * pstrdup(const char *in)
Definition: mcxt.c:1063
YYLTYPE lloc
Definition: pl_scanner.c:200
void plpgsql_append_source_text(StringInfo buf, int startlocation, int endlocation)
Definition: pl_scanner.c:522
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_KEYWORD(a, b, c)
Definition: pl_scanner.c:25
static const ScanKeyword reserved_keywords[]
Definition: pl_scanner.c:70
#define UNRESERVED_KEYWORD
Definition: keywords.h:18
#define lengthof(array)
Definition: c.h:618
static void location_lineno_init(void)
Definition: pl_scanner.c:680
static const int num_unreserved_keywords
Definition: pl_scanner.c:180
YYSTYPE lval
Definition: pl_scanner.c:199
static const char * cur_line_start
Definition: pl_scanner.c:232
void plpgsql_yyerror(const char *message)
Definition: pl_scanner.c:617
char * scanbuf
Definition: scanner.h:72
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:794
int plpgsql_peek(void)
Definition: pl_scanner.c:538
bool plpgsql_token_is_unreserved_keyword(int token)
Definition: pl_scanner.c:505
void plpgsql_scanner_finish(void)
Definition: pl_scanner.c:731
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:67
static int plpgsql_yytoken
Definition: pl_scanner.c:222
const char * name
Definition: keywords.h:26
#define YYLTYPE
Definition: scanner.h:44
static const ScanKeyword unreserved_keywords[]
Definition: pl_scanner.c:99
static int internal_yylex(TokenAuxData *auxdata)
Definition: pl_scanner.c:426
#define ereport(elevel, rest)
Definition: elog.h:122
bool plpgsql_parse_word(char *word1, const char *yytxt, PLwdatum *wdatum, PLword *word)
Definition: pl_comp.c:1362
void plpgsql_peek2(int *tok1_p, int *tok2_p, int *tok1_loc, int *tok2_loc)
Definition: pl_scanner.c:557
static const char * scanorig
Definition: pl_scanner.c:216
IdentifierLookup plpgsql_IdentifierLookup
Definition: pl_scanner.c:29
static int num_pushbacks
Definition: pl_scanner.c:227
static int pushback_token[MAX_PUSHBACKS]
Definition: pl_scanner.c:228
#define RESERVED_KEYWORD
Definition: keywords.h:21
int core_yylex(core_YYSTYPE *lvalp, YYLTYPE *llocp, core_yyscan_t yyscanner)
int16 value
Definition: keywords.h:27
int plpgsql_location_to_lineno(int location)
Definition: pl_scanner.c:656
int internalerrquery(const char *query)
Definition: elog.c:1161
static core_yy_extra_type core_yy
Definition: pl_scanner.c:213
#define Assert(condition)
Definition: c.h:688
static int plpgsql_yyleng
Definition: pl_scanner.c:219
#define AT_STMT_START(prev_token)
Definition: pl_scanner.c:188
int plpgsql_scanner_errposition(int location)
Definition: pl_scanner.c:590
core_yyscan_t scanner_init(const char *str, core_yy_extra_type *yyext, const ScanKeyword *keywords, int num_keywords)
static core_yyscan_t yyscanner
Definition: pl_scanner.c:212
void plpgsql_scanner_init(const char *str)
Definition: pl_scanner.c:704
int errmsg(const char *fmt,...)
Definition: elog.c:797
static const char * cur_line_end
Definition: pl_scanner.c:233
bool plpgsql_parse_tripword(char *word1, char *word2, char *word3, PLwdatum *wdatum, PLcword *cword)
Definition: pl_comp.c:1497
int i
static const int num_reserved_keywords
Definition: pl_scanner.c:97
void scanner_finish(core_yyscan_t yyscanner)
#define elog
Definition: elog.h:219
int plpgsql_yylex(void)
Definition: pl_scanner.c:252
static void push_back_token(int token, TokenAuxData *auxdata)
Definition: pl_scanner.c:472
void plpgsql_push_back_token(int token)
Definition: pl_scanner.c:488
#define _(x)
Definition: elog.c:84
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
int internalerrposition(int cursorpos)
Definition: elog.c:1141