PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/postgresql-9.1.4/src/pl/plpgsql/src/gram.y

#
Happy | 3476 lines | 3085 code | 391 blank | 0 comment | 0 complexity | 1a2bbdc2f6cff0efb54deb4bb786622e MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. %{
  2. /*-------------------------------------------------------------------------
  3. *
  4. * gram.y - Parser for the PL/pgSQL procedural language
  5. *
  6. * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. *
  10. * IDENTIFICATION
  11. * src/pl/plpgsql/src/gram.y
  12. *
  13. *-------------------------------------------------------------------------
  14. */
  15. #include "plpgsql.h"
  16. #include "catalog/namespace.h"
  17. #include "catalog/pg_type.h"
  18. #include "parser/parser.h"
  19. #include "parser/parse_type.h"
  20. #include "parser/scanner.h"
  21. #include "parser/scansup.h"
  22. #include "utils/builtins.h"
  23. /* Location tracking support --- simpler than bison's default */
  24. #define YYLLOC_DEFAULT(Current, Rhs, N) \
  25. do { \
  26. if (N) \
  27. (Current) = (Rhs)[1]; \
  28. else \
  29. (Current) = (Rhs)[0]; \
  30. } while (0)
  31. /*
  32. * Bison doesn't allocate anything that needs to live across parser calls,
  33. * so we can easily have it use palloc instead of malloc. This prevents
  34. * memory leaks if we error out during parsing. Note this only works with
  35. * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
  36. * if possible, so there's not really much problem anyhow, at least if
  37. * you're building with gcc.
  38. */
  39. #define YYMALLOC palloc
  40. #define YYFREE pfree
  41. typedef struct
  42. {
  43. int location;
  44. int leaderlen;
  45. } sql_error_callback_arg;
  46. #define parser_errposition(pos) plpgsql_scanner_errposition(pos)
  47. union YYSTYPE; /* need forward reference for tok_is_keyword */
  48. static bool tok_is_keyword(int token, union YYSTYPE *lval,
  49. int kw_token, const char *kw_str);
  50. static void word_is_not_variable(PLword *word, int location);
  51. static void cword_is_not_variable(PLcword *cword, int location);
  52. static void current_token_is_not_variable(int tok);
  53. static PLpgSQL_expr *read_sql_construct(int until,
  54. int until2,
  55. int until3,
  56. const char *expected,
  57. const char *sqlstart,
  58. bool isexpression,
  59. bool valid_sql,
  60. int *startloc,
  61. int *endtoken);
  62. static PLpgSQL_expr *read_sql_expression(int until,
  63. const char *expected);
  64. static PLpgSQL_expr *read_sql_expression2(int until, int until2,
  65. const char *expected,
  66. int *endtoken);
  67. static PLpgSQL_expr *read_sql_stmt(const char *sqlstart);
  68. static PLpgSQL_type *read_datatype(int tok);
  69. static PLpgSQL_stmt *make_execsql_stmt(int firsttoken, int location);
  70. static PLpgSQL_stmt_fetch *read_fetch_direction(void);
  71. static void complete_direction(PLpgSQL_stmt_fetch *fetch,
  72. bool *check_FROM);
  73. static PLpgSQL_stmt *make_return_stmt(int location);
  74. static PLpgSQL_stmt *make_return_next_stmt(int location);
  75. static PLpgSQL_stmt *make_return_query_stmt(int location);
  76. static PLpgSQL_stmt *make_case(int location, PLpgSQL_expr *t_expr,
  77. List *case_when_list, List *else_stmts);
  78. static char *NameOfDatum(PLwdatum *wdatum);
  79. static void check_assignable(PLpgSQL_datum *datum, int location);
  80. static void read_into_target(PLpgSQL_rec **rec, PLpgSQL_row **row,
  81. bool *strict);
  82. static PLpgSQL_row *read_into_scalar_list(char *initial_name,
  83. PLpgSQL_datum *initial_datum,
  84. int initial_location);
  85. static PLpgSQL_row *make_scalar_list1(char *initial_name,
  86. PLpgSQL_datum *initial_datum,
  87. int lineno, int location);
  88. static void check_sql_expr(const char *stmt, int location,
  89. int leaderlen);
  90. static void plpgsql_sql_error_callback(void *arg);
  91. static PLpgSQL_type *parse_datatype(const char *string, int location);
  92. static void check_labels(const char *start_label,
  93. const char *end_label,
  94. int end_location);
  95. static PLpgSQL_expr *read_cursor_args(PLpgSQL_var *cursor,
  96. int until, const char *expected);
  97. static List *read_raise_options(void);
  98. %}
  99. %expect 0
  100. %name-prefix="plpgsql_yy"
  101. %locations
  102. %union {
  103. core_YYSTYPE core_yystype;
  104. /* these fields must match core_YYSTYPE: */
  105. int ival;
  106. char *str;
  107. const char *keyword;
  108. PLword word;
  109. PLcword cword;
  110. PLwdatum wdatum;
  111. bool boolean;
  112. Oid oid;
  113. struct
  114. {
  115. char *name;
  116. int lineno;
  117. } varname;
  118. struct
  119. {
  120. char *name;
  121. int lineno;
  122. PLpgSQL_datum *scalar;
  123. PLpgSQL_rec *rec;
  124. PLpgSQL_row *row;
  125. } forvariable;
  126. struct
  127. {
  128. char *label;
  129. int n_initvars;
  130. int *initvarnos;
  131. } declhdr;
  132. struct
  133. {
  134. List *stmts;
  135. char *end_label;
  136. int end_label_location;
  137. } loop_body;
  138. List *list;
  139. PLpgSQL_type *dtype;
  140. PLpgSQL_datum *datum;
  141. PLpgSQL_var *var;
  142. PLpgSQL_expr *expr;
  143. PLpgSQL_stmt *stmt;
  144. PLpgSQL_condition *condition;
  145. PLpgSQL_exception *exception;
  146. PLpgSQL_exception_block *exception_block;
  147. PLpgSQL_nsitem *nsitem;
  148. PLpgSQL_diag_item *diagitem;
  149. PLpgSQL_stmt_fetch *fetch;
  150. PLpgSQL_case_when *casewhen;
  151. }
  152. %type <declhdr> decl_sect
  153. %type <varname> decl_varname
  154. %type <boolean> decl_const decl_notnull exit_type
  155. %type <expr> decl_defval decl_cursor_query
  156. %type <dtype> decl_datatype
  157. %type <oid> decl_collate
  158. %type <datum> decl_cursor_args
  159. %type <list> decl_cursor_arglist
  160. %type <nsitem> decl_aliasitem
  161. %type <expr> expr_until_semi expr_until_rightbracket
  162. %type <expr> expr_until_then expr_until_loop opt_expr_until_when
  163. %type <expr> opt_exitcond
  164. %type <ival> assign_var foreach_slice
  165. %type <var> cursor_variable
  166. %type <datum> decl_cursor_arg
  167. %type <forvariable> for_variable
  168. %type <stmt> for_control
  169. %type <str> any_identifier opt_block_label opt_label
  170. %type <list> proc_sect proc_stmts stmt_else
  171. %type <loop_body> loop_body
  172. %type <stmt> proc_stmt pl_block
  173. %type <stmt> stmt_assign stmt_if stmt_loop stmt_while stmt_exit
  174. %type <stmt> stmt_return stmt_raise stmt_execsql
  175. %type <stmt> stmt_dynexecute stmt_for stmt_perform stmt_getdiag
  176. %type <stmt> stmt_open stmt_fetch stmt_move stmt_close stmt_null
  177. %type <stmt> stmt_case stmt_foreach_a
  178. %type <list> proc_exceptions
  179. %type <exception_block> exception_sect
  180. %type <exception> proc_exception
  181. %type <condition> proc_conditions proc_condition
  182. %type <casewhen> case_when
  183. %type <list> case_when_list opt_case_else
  184. %type <list> getdiag_list
  185. %type <diagitem> getdiag_list_item
  186. %type <ival> getdiag_item getdiag_target
  187. %type <ival> opt_scrollable
  188. %type <fetch> opt_fetch_direction
  189. %type <keyword> unreserved_keyword
  190. /*
  191. * Basic non-keyword token types. These are hard-wired into the core lexer.
  192. * They must be listed first so that their numeric codes do not depend on
  193. * the set of keywords. Keep this list in sync with backend/parser/gram.y!
  194. *
  195. * Some of these are not directly referenced in this file, but they must be
  196. * here anyway.
  197. */
  198. %token <str> IDENT FCONST SCONST BCONST XCONST Op
  199. %token <ival> ICONST PARAM
  200. %token TYPECAST DOT_DOT COLON_EQUALS
  201. /*
  202. * Other tokens recognized by plpgsql's lexer interface layer (pl_scanner.c).
  203. */
  204. %token <word> T_WORD /* unrecognized simple identifier */
  205. %token <cword> T_CWORD /* unrecognized composite identifier */
  206. %token <wdatum> T_DATUM /* a VAR, ROW, REC, or RECFIELD variable */
  207. %token LESS_LESS
  208. %token GREATER_GREATER
  209. /*
  210. * Keyword tokens. Some of these are reserved and some are not;
  211. * see pl_scanner.c for info. Be sure unreserved keywords are listed
  212. * in the "unreserved_keyword" production below.
  213. */
  214. %token <keyword> K_ABSOLUTE
  215. %token <keyword> K_ALIAS
  216. %token <keyword> K_ALL
  217. %token <keyword> K_ARRAY
  218. %token <keyword> K_BACKWARD
  219. %token <keyword> K_BEGIN
  220. %token <keyword> K_BY
  221. %token <keyword> K_CASE
  222. %token <keyword> K_CLOSE
  223. %token <keyword> K_COLLATE
  224. %token <keyword> K_CONSTANT
  225. %token <keyword> K_CONTINUE
  226. %token <keyword> K_CURSOR
  227. %token <keyword> K_DEBUG
  228. %token <keyword> K_DECLARE
  229. %token <keyword> K_DEFAULT
  230. %token <keyword> K_DETAIL
  231. %token <keyword> K_DIAGNOSTICS
  232. %token <keyword> K_DUMP
  233. %token <keyword> K_ELSE
  234. %token <keyword> K_ELSIF
  235. %token <keyword> K_END
  236. %token <keyword> K_ERRCODE
  237. %token <keyword> K_ERROR
  238. %token <keyword> K_EXCEPTION
  239. %token <keyword> K_EXECUTE
  240. %token <keyword> K_EXIT
  241. %token <keyword> K_FETCH
  242. %token <keyword> K_FIRST
  243. %token <keyword> K_FOR
  244. %token <keyword> K_FOREACH
  245. %token <keyword> K_FORWARD
  246. %token <keyword> K_FROM
  247. %token <keyword> K_GET
  248. %token <keyword> K_HINT
  249. %token <keyword> K_IF
  250. %token <keyword> K_IN
  251. %token <keyword> K_INFO
  252. %token <keyword> K_INSERT
  253. %token <keyword> K_INTO
  254. %token <keyword> K_IS
  255. %token <keyword> K_LAST
  256. %token <keyword> K_LOG
  257. %token <keyword> K_LOOP
  258. %token <keyword> K_MESSAGE
  259. %token <keyword> K_MOVE
  260. %token <keyword> K_NEXT
  261. %token <keyword> K_NO
  262. %token <keyword> K_NOT
  263. %token <keyword> K_NOTICE
  264. %token <keyword> K_NULL
  265. %token <keyword> K_OPEN
  266. %token <keyword> K_OPTION
  267. %token <keyword> K_OR
  268. %token <keyword> K_PERFORM
  269. %token <keyword> K_PRIOR
  270. %token <keyword> K_QUERY
  271. %token <keyword> K_RAISE
  272. %token <keyword> K_RELATIVE
  273. %token <keyword> K_RESULT_OID
  274. %token <keyword> K_RETURN
  275. %token <keyword> K_REVERSE
  276. %token <keyword> K_ROWTYPE
  277. %token <keyword> K_ROW_COUNT
  278. %token <keyword> K_SCROLL
  279. %token <keyword> K_SLICE
  280. %token <keyword> K_SQLSTATE
  281. %token <keyword> K_STRICT
  282. %token <keyword> K_THEN
  283. %token <keyword> K_TO
  284. %token <keyword> K_TYPE
  285. %token <keyword> K_USE_COLUMN
  286. %token <keyword> K_USE_VARIABLE
  287. %token <keyword> K_USING
  288. %token <keyword> K_VARIABLE_CONFLICT
  289. %token <keyword> K_WARNING
  290. %token <keyword> K_WHEN
  291. %token <keyword> K_WHILE
  292. %%
  293. pl_function : comp_options pl_block opt_semi
  294. {
  295. plpgsql_parse_result = (PLpgSQL_stmt_block *) $2;
  296. }
  297. ;
  298. comp_options :
  299. | comp_options comp_option
  300. ;
  301. comp_option : '#' K_OPTION K_DUMP
  302. {
  303. plpgsql_DumpExecTree = true;
  304. }
  305. | '#' K_VARIABLE_CONFLICT K_ERROR
  306. {
  307. plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_ERROR;
  308. }
  309. | '#' K_VARIABLE_CONFLICT K_USE_VARIABLE
  310. {
  311. plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_VARIABLE;
  312. }
  313. | '#' K_VARIABLE_CONFLICT K_USE_COLUMN
  314. {
  315. plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_COLUMN;
  316. }
  317. ;
  318. opt_semi :
  319. | ';'
  320. ;
  321. pl_block : decl_sect K_BEGIN proc_sect exception_sect K_END opt_label
  322. {
  323. PLpgSQL_stmt_block *new;
  324. new = palloc0(sizeof(PLpgSQL_stmt_block));
  325. new->cmd_type = PLPGSQL_STMT_BLOCK;
  326. new->lineno = plpgsql_location_to_lineno(@2);
  327. new->label = $1.label;
  328. new->n_initvars = $1.n_initvars;
  329. new->initvarnos = $1.initvarnos;
  330. new->body = $3;
  331. new->exceptions = $4;
  332. check_labels($1.label, $6, @6);
  333. plpgsql_ns_pop();
  334. $$ = (PLpgSQL_stmt *)new;
  335. }
  336. ;
  337. decl_sect : opt_block_label
  338. {
  339. /* done with decls, so resume identifier lookup */
  340. plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
  341. $$.label = $1;
  342. $$.n_initvars = 0;
  343. $$.initvarnos = NULL;
  344. }
  345. | opt_block_label decl_start
  346. {
  347. plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
  348. $$.label = $1;
  349. $$.n_initvars = 0;
  350. $$.initvarnos = NULL;
  351. }
  352. | opt_block_label decl_start decl_stmts
  353. {
  354. plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
  355. $$.label = $1;
  356. /* Remember variables declared in decl_stmts */
  357. $$.n_initvars = plpgsql_add_initdatums(&($$.initvarnos));
  358. }
  359. ;
  360. decl_start : K_DECLARE
  361. {
  362. /* Forget any variables created before block */
  363. plpgsql_add_initdatums(NULL);
  364. /*
  365. * Disable scanner lookup of identifiers while
  366. * we process the decl_stmts
  367. */
  368. plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
  369. }
  370. ;
  371. decl_stmts : decl_stmts decl_stmt
  372. | decl_stmt
  373. ;
  374. decl_stmt : decl_statement
  375. | K_DECLARE
  376. {
  377. /* We allow useless extra DECLAREs */
  378. }
  379. | LESS_LESS any_identifier GREATER_GREATER
  380. {
  381. /*
  382. * Throw a helpful error if user tries to put block
  383. * label just before BEGIN, instead of before DECLARE.
  384. */
  385. ereport(ERROR,
  386. (errcode(ERRCODE_SYNTAX_ERROR),
  387. errmsg("block label must be placed before DECLARE, not after"),
  388. parser_errposition(@1)));
  389. }
  390. ;
  391. decl_statement : decl_varname decl_const decl_datatype decl_collate decl_notnull decl_defval
  392. {
  393. PLpgSQL_variable *var;
  394. /*
  395. * If a collation is supplied, insert it into the
  396. * datatype. We assume decl_datatype always returns
  397. * a freshly built struct not shared with other
  398. * variables.
  399. */
  400. if (OidIsValid($4))
  401. {
  402. if (!OidIsValid($3->collation))
  403. ereport(ERROR,
  404. (errcode(ERRCODE_DATATYPE_MISMATCH),
  405. errmsg("collations are not supported by type %s",
  406. format_type_be($3->typoid)),
  407. parser_errposition(@4)));
  408. $3->collation = $4;
  409. }
  410. var = plpgsql_build_variable($1.name, $1.lineno,
  411. $3, true);
  412. if ($2)
  413. {
  414. if (var->dtype == PLPGSQL_DTYPE_VAR)
  415. ((PLpgSQL_var *) var)->isconst = $2;
  416. else
  417. ereport(ERROR,
  418. (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
  419. errmsg("row or record variable cannot be CONSTANT"),
  420. parser_errposition(@2)));
  421. }
  422. if ($5)
  423. {
  424. if (var->dtype == PLPGSQL_DTYPE_VAR)
  425. ((PLpgSQL_var *) var)->notnull = $5;
  426. else
  427. ereport(ERROR,
  428. (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
  429. errmsg("row or record variable cannot be NOT NULL"),
  430. parser_errposition(@4)));
  431. }
  432. if ($6 != NULL)
  433. {
  434. if (var->dtype == PLPGSQL_DTYPE_VAR)
  435. ((PLpgSQL_var *) var)->default_val = $6;
  436. else
  437. ereport(ERROR,
  438. (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
  439. errmsg("default value for row or record variable is not supported"),
  440. parser_errposition(@5)));
  441. }
  442. }
  443. | decl_varname K_ALIAS K_FOR decl_aliasitem ';'
  444. {
  445. plpgsql_ns_additem($4->itemtype,
  446. $4->itemno, $1.name);
  447. }
  448. | decl_varname opt_scrollable K_CURSOR
  449. { plpgsql_ns_push($1.name); }
  450. decl_cursor_args decl_is_for decl_cursor_query
  451. {
  452. PLpgSQL_var *new;
  453. PLpgSQL_expr *curname_def;
  454. char buf[1024];
  455. char *cp1;
  456. char *cp2;
  457. /* pop local namespace for cursor args */
  458. plpgsql_ns_pop();
  459. new = (PLpgSQL_var *)
  460. plpgsql_build_variable($1.name, $1.lineno,
  461. plpgsql_build_datatype(REFCURSOROID,
  462. -1,
  463. InvalidOid),
  464. true);
  465. curname_def = palloc0(sizeof(PLpgSQL_expr));
  466. curname_def->dtype = PLPGSQL_DTYPE_EXPR;
  467. strcpy(buf, "SELECT ");
  468. cp1 = new->refname;
  469. cp2 = buf + strlen(buf);
  470. /*
  471. * Don't trust standard_conforming_strings here;
  472. * it might change before we use the string.
  473. */
  474. if (strchr(cp1, '\\') != NULL)
  475. *cp2++ = ESCAPE_STRING_SYNTAX;
  476. *cp2++ = '\'';
  477. while (*cp1)
  478. {
  479. if (SQL_STR_DOUBLE(*cp1, true))
  480. *cp2++ = *cp1;
  481. *cp2++ = *cp1++;
  482. }
  483. strcpy(cp2, "'::pg_catalog.refcursor");
  484. curname_def->query = pstrdup(buf);
  485. new->default_val = curname_def;
  486. new->cursor_explicit_expr = $7;
  487. if ($5 == NULL)
  488. new->cursor_explicit_argrow = -1;
  489. else
  490. new->cursor_explicit_argrow = $5->dno;
  491. new->cursor_options = CURSOR_OPT_FAST_PLAN | $2;
  492. }
  493. ;
  494. opt_scrollable :
  495. {
  496. $$ = 0;
  497. }
  498. | K_NO K_SCROLL
  499. {
  500. $$ = CURSOR_OPT_NO_SCROLL;
  501. }
  502. | K_SCROLL
  503. {
  504. $$ = CURSOR_OPT_SCROLL;
  505. }
  506. ;
  507. decl_cursor_query :
  508. {
  509. $$ = read_sql_stmt("");
  510. }
  511. ;
  512. decl_cursor_args :
  513. {
  514. $$ = NULL;
  515. }
  516. | '(' decl_cursor_arglist ')'
  517. {
  518. PLpgSQL_row *new;
  519. int i;
  520. ListCell *l;
  521. new = palloc0(sizeof(PLpgSQL_row));
  522. new->dtype = PLPGSQL_DTYPE_ROW;
  523. new->lineno = plpgsql_location_to_lineno(@1);
  524. new->rowtupdesc = NULL;
  525. new->nfields = list_length($2);
  526. new->fieldnames = palloc(new->nfields * sizeof(char *));
  527. new->varnos = palloc(new->nfields * sizeof(int));
  528. i = 0;
  529. foreach (l, $2)
  530. {
  531. PLpgSQL_variable *arg = (PLpgSQL_variable *) lfirst(l);
  532. new->fieldnames[i] = arg->refname;
  533. new->varnos[i] = arg->dno;
  534. i++;
  535. }
  536. list_free($2);
  537. plpgsql_adddatum((PLpgSQL_datum *) new);
  538. $$ = (PLpgSQL_datum *) new;
  539. }
  540. ;
  541. decl_cursor_arglist : decl_cursor_arg
  542. {
  543. $$ = list_make1($1);
  544. }
  545. | decl_cursor_arglist ',' decl_cursor_arg
  546. {
  547. $$ = lappend($1, $3);
  548. }
  549. ;
  550. decl_cursor_arg : decl_varname decl_datatype
  551. {
  552. $$ = (PLpgSQL_datum *)
  553. plpgsql_build_variable($1.name, $1.lineno,
  554. $2, true);
  555. }
  556. ;
  557. decl_is_for : K_IS | /* Oracle */
  558. K_FOR; /* SQL standard */
  559. decl_aliasitem : T_WORD
  560. {
  561. PLpgSQL_nsitem *nsi;
  562. nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
  563. $1.ident, NULL, NULL,
  564. NULL);
  565. if (nsi == NULL)
  566. ereport(ERROR,
  567. (errcode(ERRCODE_UNDEFINED_OBJECT),
  568. errmsg("variable \"%s\" does not exist",
  569. $1.ident),
  570. parser_errposition(@1)));
  571. $$ = nsi;
  572. }
  573. | T_CWORD
  574. {
  575. PLpgSQL_nsitem *nsi;
  576. if (list_length($1.idents) == 2)
  577. nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
  578. strVal(linitial($1.idents)),
  579. strVal(lsecond($1.idents)),
  580. NULL,
  581. NULL);
  582. else if (list_length($1.idents) == 3)
  583. nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
  584. strVal(linitial($1.idents)),
  585. strVal(lsecond($1.idents)),
  586. strVal(lthird($1.idents)),
  587. NULL);
  588. else
  589. nsi = NULL;
  590. if (nsi == NULL)
  591. ereport(ERROR,
  592. (errcode(ERRCODE_UNDEFINED_OBJECT),
  593. errmsg("variable \"%s\" does not exist",
  594. NameListToString($1.idents)),
  595. parser_errposition(@1)));
  596. $$ = nsi;
  597. }
  598. ;
  599. decl_varname : T_WORD
  600. {
  601. $$.name = $1.ident;
  602. $$.lineno = plpgsql_location_to_lineno(@1);
  603. /*
  604. * Check to make sure name isn't already declared
  605. * in the current block.
  606. */
  607. if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
  608. $1.ident, NULL, NULL,
  609. NULL) != NULL)
  610. yyerror("duplicate declaration");
  611. }
  612. | unreserved_keyword
  613. {
  614. $$.name = pstrdup($1);
  615. $$.lineno = plpgsql_location_to_lineno(@1);
  616. /*
  617. * Check to make sure name isn't already declared
  618. * in the current block.
  619. */
  620. if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
  621. $1, NULL, NULL,
  622. NULL) != NULL)
  623. yyerror("duplicate declaration");
  624. }
  625. ;
  626. decl_const :
  627. { $$ = false; }
  628. | K_CONSTANT
  629. { $$ = true; }
  630. ;
  631. decl_datatype :
  632. {
  633. /*
  634. * If there's a lookahead token, read_datatype
  635. * should consume it.
  636. */
  637. $$ = read_datatype(yychar);
  638. yyclearin;
  639. }
  640. ;
  641. decl_collate :
  642. { $$ = InvalidOid; }
  643. | K_COLLATE T_WORD
  644. {
  645. $$ = get_collation_oid(list_make1(makeString($2.ident)),
  646. false);
  647. }
  648. | K_COLLATE T_CWORD
  649. {
  650. $$ = get_collation_oid($2.idents, false);
  651. }
  652. ;
  653. decl_notnull :
  654. { $$ = false; }
  655. | K_NOT K_NULL
  656. { $$ = true; }
  657. ;
  658. decl_defval : ';'
  659. { $$ = NULL; }
  660. | decl_defkey
  661. {
  662. $$ = read_sql_expression(';', ";");
  663. }
  664. ;
  665. decl_defkey : assign_operator
  666. | K_DEFAULT
  667. ;
  668. assign_operator : '='
  669. | COLON_EQUALS
  670. ;
  671. proc_sect :
  672. { $$ = NIL; }
  673. | proc_stmts
  674. { $$ = $1; }
  675. ;
  676. proc_stmts : proc_stmts proc_stmt
  677. {
  678. if ($2 == NULL)
  679. $$ = $1;
  680. else
  681. $$ = lappend($1, $2);
  682. }
  683. | proc_stmt
  684. {
  685. if ($1 == NULL)
  686. $$ = NIL;
  687. else
  688. $$ = list_make1($1);
  689. }
  690. ;
  691. proc_stmt : pl_block ';'
  692. { $$ = $1; }
  693. | stmt_assign
  694. { $$ = $1; }
  695. | stmt_if
  696. { $$ = $1; }
  697. | stmt_case
  698. { $$ = $1; }
  699. | stmt_loop
  700. { $$ = $1; }
  701. | stmt_while
  702. { $$ = $1; }
  703. | stmt_for
  704. { $$ = $1; }
  705. | stmt_foreach_a
  706. { $$ = $1; }
  707. | stmt_exit
  708. { $$ = $1; }
  709. | stmt_return
  710. { $$ = $1; }
  711. | stmt_raise
  712. { $$ = $1; }
  713. | stmt_execsql
  714. { $$ = $1; }
  715. | stmt_dynexecute
  716. { $$ = $1; }
  717. | stmt_perform
  718. { $$ = $1; }
  719. | stmt_getdiag
  720. { $$ = $1; }
  721. | stmt_open
  722. { $$ = $1; }
  723. | stmt_fetch
  724. { $$ = $1; }
  725. | stmt_move
  726. { $$ = $1; }
  727. | stmt_close
  728. { $$ = $1; }
  729. | stmt_null
  730. { $$ = $1; }
  731. ;
  732. stmt_perform : K_PERFORM expr_until_semi
  733. {
  734. PLpgSQL_stmt_perform *new;
  735. new = palloc0(sizeof(PLpgSQL_stmt_perform));
  736. new->cmd_type = PLPGSQL_STMT_PERFORM;
  737. new->lineno = plpgsql_location_to_lineno(@1);
  738. new->expr = $2;
  739. $$ = (PLpgSQL_stmt *)new;
  740. }
  741. ;
  742. stmt_assign : assign_var assign_operator expr_until_semi
  743. {
  744. PLpgSQL_stmt_assign *new;
  745. new = palloc0(sizeof(PLpgSQL_stmt_assign));
  746. new->cmd_type = PLPGSQL_STMT_ASSIGN;
  747. new->lineno = plpgsql_location_to_lineno(@1);
  748. new->varno = $1;
  749. new->expr = $3;
  750. $$ = (PLpgSQL_stmt *)new;
  751. }
  752. ;
  753. stmt_getdiag : K_GET K_DIAGNOSTICS getdiag_list ';'
  754. {
  755. PLpgSQL_stmt_getdiag *new;
  756. new = palloc0(sizeof(PLpgSQL_stmt_getdiag));
  757. new->cmd_type = PLPGSQL_STMT_GETDIAG;
  758. new->lineno = plpgsql_location_to_lineno(@1);
  759. new->diag_items = $3;
  760. $$ = (PLpgSQL_stmt *)new;
  761. }
  762. ;
  763. getdiag_list : getdiag_list ',' getdiag_list_item
  764. {
  765. $$ = lappend($1, $3);
  766. }
  767. | getdiag_list_item
  768. {
  769. $$ = list_make1($1);
  770. }
  771. ;
  772. getdiag_list_item : getdiag_target assign_operator getdiag_item
  773. {
  774. PLpgSQL_diag_item *new;
  775. new = palloc(sizeof(PLpgSQL_diag_item));
  776. new->target = $1;
  777. new->kind = $3;
  778. $$ = new;
  779. }
  780. ;
  781. getdiag_item :
  782. {
  783. int tok = yylex();
  784. if (tok_is_keyword(tok, &yylval,
  785. K_ROW_COUNT, "row_count"))
  786. $$ = PLPGSQL_GETDIAG_ROW_COUNT;
  787. else if (tok_is_keyword(tok, &yylval,
  788. K_RESULT_OID, "result_oid"))
  789. $$ = PLPGSQL_GETDIAG_RESULT_OID;
  790. else
  791. yyerror("unrecognized GET DIAGNOSTICS item");
  792. }
  793. ;
  794. getdiag_target : T_DATUM
  795. {
  796. check_assignable($1.datum, @1);
  797. if ($1.datum->dtype == PLPGSQL_DTYPE_ROW ||
  798. $1.datum->dtype == PLPGSQL_DTYPE_REC)
  799. ereport(ERROR,
  800. (errcode(ERRCODE_SYNTAX_ERROR),
  801. errmsg("\"%s\" is not a scalar variable",
  802. NameOfDatum(&($1))),
  803. parser_errposition(@1)));
  804. $$ = $1.datum->dno;
  805. }
  806. | T_WORD
  807. {
  808. /* just to give a better message than "syntax error" */
  809. word_is_not_variable(&($1), @1);
  810. }
  811. | T_CWORD
  812. {
  813. /* just to give a better message than "syntax error" */
  814. cword_is_not_variable(&($1), @1);
  815. }
  816. ;
  817. assign_var : T_DATUM
  818. {
  819. check_assignable($1.datum, @1);
  820. $$ = $1.datum->dno;
  821. }
  822. | assign_var '[' expr_until_rightbracket
  823. {
  824. PLpgSQL_arrayelem *new;
  825. new = palloc0(sizeof(PLpgSQL_arrayelem));
  826. new->dtype = PLPGSQL_DTYPE_ARRAYELEM;
  827. new->subscript = $3;
  828. new->arrayparentno = $1;
  829. plpgsql_adddatum((PLpgSQL_datum *) new);
  830. $$ = new->dno;
  831. }
  832. ;
  833. stmt_if : K_IF expr_until_then proc_sect stmt_else K_END K_IF ';'
  834. {
  835. PLpgSQL_stmt_if *new;
  836. new = palloc0(sizeof(PLpgSQL_stmt_if));
  837. new->cmd_type = PLPGSQL_STMT_IF;
  838. new->lineno = plpgsql_location_to_lineno(@1);
  839. new->cond = $2;
  840. new->true_body = $3;
  841. new->false_body = $4;
  842. $$ = (PLpgSQL_stmt *)new;
  843. }
  844. ;
  845. stmt_else :
  846. {
  847. $$ = NIL;
  848. }
  849. | K_ELSIF expr_until_then proc_sect stmt_else
  850. {
  851. /*----------
  852. * Translate the structure: into:
  853. *
  854. * IF c1 THEN IF c1 THEN
  855. * ... ...
  856. * ELSIF c2 THEN ELSE
  857. * IF c2 THEN
  858. * ... ...
  859. * ELSE ELSE
  860. * ... ...
  861. * END IF END IF
  862. * END IF
  863. *----------
  864. */
  865. PLpgSQL_stmt_if *new_if;
  866. /* first create a new if-statement */
  867. new_if = palloc0(sizeof(PLpgSQL_stmt_if));
  868. new_if->cmd_type = PLPGSQL_STMT_IF;
  869. new_if->lineno = plpgsql_location_to_lineno(@1);
  870. new_if->cond = $2;
  871. new_if->true_body = $3;
  872. new_if->false_body = $4;
  873. /* wrap the if-statement in a "container" list */
  874. $$ = list_make1(new_if);
  875. }
  876. | K_ELSE proc_sect
  877. {
  878. $$ = $2;
  879. }
  880. ;
  881. stmt_case : K_CASE opt_expr_until_when case_when_list opt_case_else K_END K_CASE ';'
  882. {
  883. $$ = make_case(@1, $2, $3, $4);
  884. }
  885. ;
  886. opt_expr_until_when :
  887. {
  888. PLpgSQL_expr *expr = NULL;
  889. int tok = yylex();
  890. if (tok != K_WHEN)
  891. {
  892. plpgsql_push_back_token(tok);
  893. expr = read_sql_expression(K_WHEN, "WHEN");
  894. }
  895. plpgsql_push_back_token(K_WHEN);
  896. $$ = expr;
  897. }
  898. ;
  899. case_when_list : case_when_list case_when
  900. {
  901. $$ = lappend($1, $2);
  902. }
  903. | case_when
  904. {
  905. $$ = list_make1($1);
  906. }
  907. ;
  908. case_when : K_WHEN expr_until_then proc_sect
  909. {
  910. PLpgSQL_case_when *new = palloc(sizeof(PLpgSQL_case_when));
  911. new->lineno = plpgsql_location_to_lineno(@1);
  912. new->expr = $2;
  913. new->stmts = $3;
  914. $$ = new;
  915. }
  916. ;
  917. opt_case_else :
  918. {
  919. $$ = NIL;
  920. }
  921. | K_ELSE proc_sect
  922. {
  923. /*
  924. * proc_sect could return an empty list, but we
  925. * must distinguish that from not having ELSE at all.
  926. * Simplest fix is to return a list with one NULL
  927. * pointer, which make_case() must take care of.
  928. */
  929. if ($2 != NIL)
  930. $$ = $2;
  931. else
  932. $$ = list_make1(NULL);
  933. }
  934. ;
  935. stmt_loop : opt_block_label K_LOOP loop_body
  936. {
  937. PLpgSQL_stmt_loop *new;
  938. new = palloc0(sizeof(PLpgSQL_stmt_loop));
  939. new->cmd_type = PLPGSQL_STMT_LOOP;
  940. new->lineno = plpgsql_location_to_lineno(@2);
  941. new->label = $1;
  942. new->body = $3.stmts;
  943. check_labels($1, $3.end_label, $3.end_label_location);
  944. plpgsql_ns_pop();
  945. $$ = (PLpgSQL_stmt *)new;
  946. }
  947. ;
  948. stmt_while : opt_block_label K_WHILE expr_until_loop loop_body
  949. {
  950. PLpgSQL_stmt_while *new;
  951. new = palloc0(sizeof(PLpgSQL_stmt_while));
  952. new->cmd_type = PLPGSQL_STMT_WHILE;
  953. new->lineno = plpgsql_location_to_lineno(@2);
  954. new->label = $1;
  955. new->cond = $3;
  956. new->body = $4.stmts;
  957. check_labels($1, $4.end_label, $4.end_label_location);
  958. plpgsql_ns_pop();
  959. $$ = (PLpgSQL_stmt *)new;
  960. }
  961. ;
  962. stmt_for : opt_block_label K_FOR for_control loop_body
  963. {
  964. /* This runs after we've scanned the loop body */
  965. if ($3->cmd_type == PLPGSQL_STMT_FORI)
  966. {
  967. PLpgSQL_stmt_fori *new;
  968. new = (PLpgSQL_stmt_fori *) $3;
  969. new->lineno = plpgsql_location_to_lineno(@2);
  970. new->label = $1;
  971. new->body = $4.stmts;
  972. $$ = (PLpgSQL_stmt *) new;
  973. }
  974. else
  975. {
  976. PLpgSQL_stmt_forq *new;
  977. Assert($3->cmd_type == PLPGSQL_STMT_FORS ||
  978. $3->cmd_type == PLPGSQL_STMT_FORC ||
  979. $3->cmd_type == PLPGSQL_STMT_DYNFORS);
  980. /* forq is the common supertype of all three */
  981. new = (PLpgSQL_stmt_forq *) $3;
  982. new->lineno = plpgsql_location_to_lineno(@2);
  983. new->label = $1;
  984. new->body = $4.stmts;
  985. $$ = (PLpgSQL_stmt *) new;
  986. }
  987. check_labels($1, $4.end_label, $4.end_label_location);
  988. /* close namespace started in opt_block_label */
  989. plpgsql_ns_pop();
  990. }
  991. ;
  992. for_control : for_variable K_IN
  993. {
  994. int tok = yylex();
  995. int tokloc = yylloc;
  996. if (tok == K_EXECUTE)
  997. {
  998. /* EXECUTE means it's a dynamic FOR loop */
  999. PLpgSQL_stmt_dynfors *new;
  1000. PLpgSQL_expr *expr;
  1001. int term;
  1002. expr = read_sql_expression2(K_LOOP, K_USING,
  1003. "LOOP or USING",
  1004. &term);
  1005. new = palloc0(sizeof(PLpgSQL_stmt_dynfors));
  1006. new->cmd_type = PLPGSQL_STMT_DYNFORS;
  1007. if ($1.rec)
  1008. {
  1009. new->rec = $1.rec;
  1010. check_assignable((PLpgSQL_datum *) new->rec, @1);
  1011. }
  1012. else if ($1.row)
  1013. {
  1014. new->row = $1.row;
  1015. check_assignable((PLpgSQL_datum *) new->row, @1);
  1016. }
  1017. else if ($1.scalar)
  1018. {
  1019. /* convert single scalar to list */
  1020. new->row = make_scalar_list1($1.name, $1.scalar,
  1021. $1.lineno, @1);
  1022. /* no need for check_assignable */
  1023. }
  1024. else
  1025. {
  1026. ereport(ERROR,
  1027. (errcode(ERRCODE_DATATYPE_MISMATCH),
  1028. errmsg("loop variable of loop over rows must be a record or row variable or list of scalar variables"),
  1029. parser_errposition(@1)));
  1030. }
  1031. new->query = expr;
  1032. if (term == K_USING)
  1033. {
  1034. do
  1035. {
  1036. expr = read_sql_expression2(',', K_LOOP,
  1037. ", or LOOP",
  1038. &term);
  1039. new->params = lappend(new->params, expr);
  1040. } while (term == ',');
  1041. }
  1042. $$ = (PLpgSQL_stmt *) new;
  1043. }
  1044. else if (tok == T_DATUM &&
  1045. yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_VAR &&
  1046. ((PLpgSQL_var *) yylval.wdatum.datum)->datatype->typoid == REFCURSOROID)
  1047. {
  1048. /* It's FOR var IN cursor */
  1049. PLpgSQL_stmt_forc *new;
  1050. PLpgSQL_var *cursor = (PLpgSQL_var *) yylval.wdatum.datum;
  1051. new = (PLpgSQL_stmt_forc *) palloc0(sizeof(PLpgSQL_stmt_forc));
  1052. new->cmd_type = PLPGSQL_STMT_FORC;
  1053. new->curvar = cursor->dno;
  1054. /* Should have had a single variable name */
  1055. if ($1.scalar && $1.row)
  1056. ereport(ERROR,
  1057. (errcode(ERRCODE_SYNTAX_ERROR),
  1058. errmsg("cursor FOR loop must have only one target variable"),
  1059. parser_errposition(@1)));
  1060. /* can't use an unbound cursor this way */
  1061. if (cursor->cursor_explicit_expr == NULL)
  1062. ereport(ERROR,
  1063. (errcode(ERRCODE_SYNTAX_ERROR),
  1064. errmsg("cursor FOR loop must use a bound cursor variable"),
  1065. parser_errposition(tokloc)));
  1066. /* collect cursor's parameters if any */
  1067. new->argquery = read_cursor_args(cursor,
  1068. K_LOOP,
  1069. "LOOP");
  1070. /* create loop's private RECORD variable */
  1071. new->rec = plpgsql_build_record($1.name,
  1072. $1.lineno,
  1073. true);
  1074. $$ = (PLpgSQL_stmt *) new;
  1075. }
  1076. else
  1077. {
  1078. PLpgSQL_expr *expr1;
  1079. int expr1loc;
  1080. bool reverse = false;
  1081. /*
  1082. * We have to distinguish between two
  1083. * alternatives: FOR var IN a .. b and FOR
  1084. * var IN query. Unfortunately this is
  1085. * tricky, since the query in the second
  1086. * form needn't start with a SELECT
  1087. * keyword. We use the ugly hack of
  1088. * looking for two periods after the first
  1089. * token. We also check for the REVERSE
  1090. * keyword, which means it must be an
  1091. * integer loop.
  1092. */
  1093. if (tok_is_keyword(tok, &yylval,
  1094. K_REVERSE, "reverse"))
  1095. reverse = true;
  1096. else
  1097. plpgsql_push_back_token(tok);
  1098. /*
  1099. * Read tokens until we see either a ".."
  1100. * or a LOOP. The text we read may not
  1101. * necessarily be a well-formed SQL
  1102. * statement, so we need to invoke
  1103. * read_sql_construct directly.
  1104. */
  1105. expr1 = read_sql_construct(DOT_DOT,
  1106. K_LOOP,
  1107. 0,
  1108. "LOOP",
  1109. "SELECT ",
  1110. true,
  1111. false,
  1112. &expr1loc,
  1113. &tok);
  1114. if (tok == DOT_DOT)
  1115. {
  1116. /* Saw "..", so it must be an integer loop */
  1117. PLpgSQL_expr *expr2;
  1118. PLpgSQL_expr *expr_by;
  1119. PLpgSQL_var *fvar;
  1120. PLpgSQL_stmt_fori *new;
  1121. /* Check first expression is well-formed */
  1122. check_sql_expr(expr1->query, expr1loc, 7);
  1123. /* Read and check the second one */
  1124. expr2 = read_sql_expression2(K_LOOP, K_BY,
  1125. "LOOP",
  1126. &tok);
  1127. /* Get the BY clause if any */
  1128. if (tok == K_BY)
  1129. expr_by = read_sql_expression(K_LOOP,
  1130. "LOOP");
  1131. else
  1132. expr_by = NULL;
  1133. /* Should have had a single variable name */
  1134. if ($1.scalar && $1.row)
  1135. ereport(ERROR,
  1136. (errcode(ERRCODE_SYNTAX_ERROR),
  1137. errmsg("integer FOR loop must have only one target variable"),
  1138. parser_errposition(@1)));
  1139. /* create loop's private variable */
  1140. fvar = (PLpgSQL_var *)
  1141. plpgsql_build_variable($1.name,
  1142. $1.lineno,
  1143. plpgsql_build_datatype(INT4OID,
  1144. -1,
  1145. InvalidOid),
  1146. true);
  1147. new = palloc0(sizeof(PLpgSQL_stmt_fori));
  1148. new->cmd_type = PLPGSQL_STMT_FORI;
  1149. new->var = fvar;
  1150. new->reverse = reverse;
  1151. new->lower = expr1;
  1152. new->upper = expr2;
  1153. new->step = expr_by;
  1154. $$ = (PLpgSQL_stmt *) new;
  1155. }
  1156. else
  1157. {
  1158. /*
  1159. * No "..", so it must be a query loop. We've
  1160. * prefixed an extra SELECT to the query text,
  1161. * so we need to remove that before performing
  1162. * syntax checking.
  1163. */
  1164. char *tmp_query;
  1165. PLpgSQL_stmt_fors *new;
  1166. if (reverse)
  1167. ereport(ERROR,
  1168. (errcode(ERRCODE_SYNTAX_ERROR),
  1169. errmsg("cannot specify REVERSE in query FOR loop"),
  1170. parser_errposition(tokloc)));
  1171. Assert(strncmp(expr1->query, "SELECT ", 7) == 0);
  1172. tmp_query = pstrdup(expr1->query + 7);
  1173. pfree(expr1->query);
  1174. expr1->query = tmp_query;
  1175. check_sql_expr(expr1->query, expr1loc, 0);
  1176. new = palloc0(sizeof(PLpgSQL_stmt_fors));
  1177. new->cmd_type = PLPGSQL_STMT_FORS;
  1178. if ($1.rec)
  1179. {
  1180. new->rec = $1.rec;
  1181. check_assignable((PLpgSQL_datum *) new->rec, @1);
  1182. }
  1183. else if ($1.row)
  1184. {
  1185. new->row = $1.row;
  1186. check_assignable((PLpgSQL_datum *) new->row, @1);
  1187. }
  1188. else if ($1.scalar)
  1189. {
  1190. /* convert single scalar to list */
  1191. new->row = make_scalar_list1($1.name, $1.scalar,
  1192. $1.lineno, @1);
  1193. /* no need for check_assignable */
  1194. }
  1195. else
  1196. {
  1197. ereport(ERROR,
  1198. (errcode(ERRCODE_SYNTAX_ERROR),
  1199. errmsg("loop variable of loop over rows must be a record or row variable or list of scalar variables"),
  1200. parser_errposition(@1)));
  1201. }
  1202. new->query = expr1;
  1203. $$ = (PLpgSQL_stmt *) new;
  1204. }
  1205. }
  1206. }
  1207. ;
  1208. /*
  1209. * Processing the for_variable is tricky because we don't yet know if the
  1210. * FOR is an integer FOR loop or a loop over query results. In the former
  1211. * case, the variable is just a name that we must instantiate as a loop
  1212. * local variable, regardless of any other definition it might have.
  1213. * Therefore, we always save the actual identifier into $$.name where it
  1214. * can be used for that case. We also save the outer-variable definition,
  1215. * if any, because that's what we need for the loop-over-query case. Note
  1216. * that we must NOT apply check_assignable() or any other semantic check
  1217. * until we know what's what.
  1218. *
  1219. * However, if we see a comma-separated list of names, we know that it
  1220. * can't be an integer FOR loop and so it's OK to check the variables
  1221. * immediately. In particular, for T_WORD followed by comma, we should
  1222. * complain that the name is not known rather than say it's a syntax error.
  1223. * Note that the non-error result of this case sets *both* $$.scalar and
  1224. * $$.row; see the for_control production.
  1225. */
  1226. for_variable : T_DATUM
  1227. {
  1228. $$.name = NameOfDatum(&($1));
  1229. $$.lineno = plpgsql_location_to_lineno(@1);
  1230. if ($1.datum->dtype == PLPGSQL_DTYPE_ROW)
  1231. {
  1232. $$.scalar = NULL;
  1233. $$.rec = NULL;
  1234. $$.row = (PLpgSQL_row *) $1.datum;
  1235. }
  1236. else if ($1.datum->dtype == PLPGSQL_DTYPE_REC)
  1237. {
  1238. $$.scalar = NULL;
  1239. $$.rec = (PLpgSQL_rec *) $1.datum;
  1240. $$.row = NULL;
  1241. }
  1242. else
  1243. {
  1244. int tok;
  1245. $$.scalar = $1.datum;
  1246. $$.rec = NULL;
  1247. $$.row = NULL;
  1248. /* check for comma-separated list */
  1249. tok = yylex();
  1250. plpgsql_push_back_token(tok);
  1251. if (tok == ',')
  1252. $$.row = read_into_scalar_list($$.name,
  1253. $$.scalar,
  1254. @1);
  1255. }
  1256. }
  1257. | T_WORD
  1258. {
  1259. int tok;
  1260. $$.name = $1.ident;
  1261. $$.lineno = plpgsql_location_to_lineno(@1);
  1262. $$.scalar = NULL;
  1263. $$.rec = NULL;
  1264. $$.row = NULL;
  1265. /* check for comma-separated list */
  1266. tok = yylex();
  1267. plpgsql_push_back_token(tok);
  1268. if (tok == ',')
  1269. word_is_not_variable(&($1), @1);
  1270. }
  1271. | T_CWORD
  1272. {
  1273. /* just to give a better message than "syntax error" */
  1274. cword_is_not_variable(&($1), @1);
  1275. }
  1276. ;
  1277. stmt_foreach_a : opt_block_label K_FOREACH for_variable foreach_slice K_IN K_ARRAY expr_until_loop loop_body
  1278. {
  1279. PLpgSQL_stmt_foreach_a *new;
  1280. new = palloc0(sizeof(PLpgSQL_stmt_foreach_a));
  1281. new->cmd_type = PLPGSQL_STMT_FOREACH_A;
  1282. new->lineno = plpgsql_location_to_lineno(@2);
  1283. new->label = $1;
  1284. new->slice = $4;
  1285. new->expr = $7;
  1286. new->body = $8.stmts;
  1287. if ($3.rec)
  1288. {
  1289. new->varno = $3.rec->dno;
  1290. check_assignable((PLpgSQL_datum *) $3.rec, @3);
  1291. }
  1292. else if ($3.row)
  1293. {
  1294. new->varno = $3.row->dno;
  1295. check_assignable((PLpgSQL_datum *) $3.row, @3);
  1296. }
  1297. else if ($3.scalar)
  1298. {
  1299. new->varno = $3.scalar->dno;
  1300. check_assignable($3.scalar, @3);
  1301. }
  1302. else
  1303. {
  1304. ereport(ERROR,
  1305. (errcode(ERRCODE_SYNTAX_ERROR),
  1306. errmsg("loop variable of FOREACH must be a known variable or list of variables"),
  1307. parser_errposition(@3)));
  1308. }
  1309. check_labels($1, $8.end_label, $8.end_label_location);
  1310. plpgsql_ns_pop();
  1311. $$ = (PLpgSQL_stmt *) new;
  1312. }
  1313. ;
  1314. foreach_slice :
  1315. {
  1316. $$ = 0;
  1317. }
  1318. | K_SLICE ICONST
  1319. {
  1320. $$ = $2;
  1321. }
  1322. ;
  1323. stmt_exit : exit_type opt_label opt_exitcond
  1324. {
  1325. PLpgSQL_stmt_exit *new;
  1326. new = palloc0(sizeof(PLpgSQL_stmt_exit));
  1327. new->cmd_type = PLPGSQL_STMT_EXIT;
  1328. new->is_exit = $1;
  1329. new->lineno = plpgsql_location_to_lineno(@1);
  1330. new->label = $2;
  1331. new->cond = $3;
  1332. $$ = (PLpgSQL_stmt *)new;
  1333. }
  1334. ;
  1335. exit_type : K_EXIT
  1336. {
  1337. $$ = true;
  1338. }
  1339. | K_CONTINUE
  1340. {
  1341. $$ = false;
  1342. }
  1343. ;
  1344. stmt_return : K_RETURN
  1345. {
  1346. int tok;
  1347. tok = yylex();
  1348. if (tok == 0)
  1349. yyerror("unexpected end of function definition");
  1350. if (tok_is_keyword(tok, &yylval,
  1351. K_NEXT, "next"))
  1352. {
  1353. $$ = make_return_next_stmt(@1);
  1354. }
  1355. else if (tok_is_keyword(tok, &yylval,
  1356. K_QUERY, "query"))
  1357. {
  1358. $$ = make_return_query_stmt(@1);
  1359. }
  1360. else
  1361. {
  1362. plpgsql_push_back_token(tok);
  1363. $$ = make_return_stmt(@1);
  1364. }
  1365. }
  1366. ;
  1367. stmt_raise : K_RAISE
  1368. {
  1369. PLpgSQL_stmt_raise *new;
  1370. int tok;
  1371. new = palloc(sizeof(PLpgSQL_stmt_raise));
  1372. new->cmd_type = PLPGSQL_STMT_RAISE;
  1373. new->lineno = plpgsql_location_to_lineno(@1);
  1374. new->elog_level = ERROR; /* default */
  1375. new->condname = NULL;
  1376. new->message = NULL;
  1377. new->params = NIL;
  1378. new->options = NIL;
  1379. tok = yylex();
  1380. if (tok == 0)
  1381. yyerror("unexpected end of function definition");
  1382. /*
  1383. * We could have just RAISE, meaning to re-throw
  1384. * the current error.
  1385. */
  1386. if (tok != ';')
  1387. {
  1388. /*
  1389. * First is an optional elog severity level.
  1390. */
  1391. if (tok_is_keyword(tok, &yylval,
  1392. K_EXCEPTION, "exception"))
  1393. {
  1394. new->elog_level = ERROR;
  1395. tok = yylex();
  1396. }
  1397. else if (tok_is_keyword(tok, &yylval,
  1398. K_WARNING, "warning"))
  1399. {
  1400. new->elog_level = WARNING;
  1401. tok = yylex();
  1402. }
  1403. else if (tok_is_keyword(tok, &yylval,
  1404. K_NOTICE, "notice"))
  1405. {
  1406. new->elog_level = NOTICE;
  1407. tok = yylex();
  1408. }
  1409. else if (tok_is_keyword(tok, &yylval,
  1410. K_INFO, "info"))
  1411. {
  1412. new->elog_level = INFO;
  1413. tok = yylex();
  1414. }
  1415. else if (tok_is_keyword(tok, &yylval,
  1416. K_LOG, "log"))
  1417. {
  1418. new->elog_level = LOG;
  1419. tok = yylex();
  1420. }
  1421. else if (tok_is_keyword(tok, &yylval,
  1422. K_DEBUG, "debug"))
  1423. {
  1424. new->elog_level = DEBUG1;
  1425. tok = yylex();
  1426. }
  1427. if (tok == 0)
  1428. yyerror("unexpected end of function definition");
  1429. /*
  1430. * Next we can have a condition name, or
  1431. * equivalently SQLSTATE 'xxxxx', or a string
  1432. * literal that is the old-style message format,
  1433. * or USING to start the option list immediately.
  1434. */
  1435. if (tok == SCONST)
  1436. {
  1437. /* old style message and parameters */
  1438. new->message = yylval.str;
  1439. /*
  1440. * We expect either a semi-colon, which
  1441. * indicates no parameters, or a comma that
  1442. * begins the list of parameter expressions,
  1443. * or USING to begin the options list.
  1444. */
  1445. tok = yylex();
  1446. if (tok != ',' && tok != ';' && tok != K_USING)
  1447. yyerror("syntax error");
  1448. while (tok == ',')
  1449. {
  1450. PLpgSQL_expr *expr;
  1451. expr = read_sql_construct(',', ';', K_USING,
  1452. ", or ; or USING",
  1453. "SELECT ",
  1454. true, true,
  1455. NULL, &tok);
  1456. new->params = lappend(new->params, expr);
  1457. }
  1458. }
  1459. else if (tok != K_USING)
  1460. {
  1461. /* must be condition name or SQLSTATE */
  1462. if (tok_is_keyword(tok, &yylval,
  1463. K_SQLSTATE, "sqlstate"))
  1464. {
  1465. /* next token should be a string literal */
  1466. char *sqlstatestr;
  1467. if (yylex() != SCONST)
  1468. yyerror("syntax error");
  1469. sqlstatestr = yylval.str;
  1470. if (strlen(sqlstatestr) != 5)
  1471. yyerror("invalid SQLSTATE code");
  1472. if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
  1473. yyerror("invalid SQLSTATE code");
  1474. new->condname = sqlstatestr;
  1475. }
  1476. else
  1477. {
  1478. if (tok != T_WORD)
  1479. yyerror("syntax error");
  1480. new->condname = yylval.word.ident;
  1481. plpgsql_recognize_err_condition(new->condname,
  1482. false);
  1483. }
  1484. tok = yylex();
  1485. if (tok != ';' && tok != K_USING)
  1486. yyerror("syntax error");
  1487. }
  1488. if (tok == K_USING)
  1489. new->options = read_raise_options();
  1490. }
  1491. $$ = (PLpgSQL_stmt *)new;
  1492. }
  1493. ;
  1494. loop_body : proc_sect K_END K_LOOP opt_label ';'
  1495. {
  1496. $$.stmts = $1;
  1497. $$.end_label = $4;
  1498. $$.end_label_location = @4;
  1499. }
  1500. ;
  1501. /*
  1502. * T_WORD+T_CWORD match any initial identifier that is not a known plpgsql
  1503. * variable. (The composite case is probably a syntax error, but we'll let
  1504. * the core parser decide that.) Normally, we should assume that such a
  1505. * word is a SQL statement keyword that isn't also a plpgsql keyword.
  1506. * However, if the next token is assignment or '[', it can't be a valid
  1507. * SQL statement, and what we're probably looking at is an intended variable
  1508. * assignment. Give an appropriate complaint for that, instead of letting
  1509. * the core parser throw an unhelpful "syntax error".
  1510. */
  1511. stmt_execsql : K_INSERT
  1512. {
  1513. $$ = make_execsql_stmt(K_INSERT, @1);
  1514. }
  1515. | T_WORD
  1516. {
  1517. int tok;
  1518. tok = yylex();
  1519. plpgsql_push_back_token(tok);
  1520. if (tok == '=' || tok == COLON_EQUALS || tok == '[')
  1521. word_is_not_variable(&($1), @1);
  1522. $$ = make_execsql_stmt(T_WORD, @1);
  1523. }
  1524. | T_CWORD
  1525. {
  1526. int tok;
  1527. tok = yylex();
  1528. plpgsql_push_back_token(tok);
  1529. if (tok == '=' || tok == COLON_EQUALS || tok == '[')
  1530. cword_is_not_variable(&($1), @1);
  1531. $$ = make_execsql_stmt(T_CWORD, @1);
  1532. }
  1533. ;
  1534. stmt_dynexecute : K_EXECUTE
  1535. {
  1536. PLpgSQL_stmt_dynexecute *new;
  1537. PLpgSQL_expr *expr;
  1538. int endtoken;
  1539. expr = read_sql_construct(K_INTO, K_USING, ';',
  1540. "INTO or USING or ;",
  1541. "SELECT ",
  1542. true, true,
  1543. NULL, &endtoken);
  1544. new = palloc(sizeof(PLpgSQL_stmt_dynexecute));
  1545. new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
  1546. new->lineno = plpgsql_location_to_lineno(@1);
  1547. new->query = expr;
  1548. new->into = false;
  1549. new->strict = false;
  1550. new->rec = NULL;
  1551. new->row = NULL;
  1552. new->params = NIL;
  1553. /*
  1554. * We loop to allow the INTO and USING clauses to
  1555. * appear in either order, since people easily get
  1556. * that wrong. This coding also prevents "INTO foo"
  1557. * from getting absorbed into a USING expression,
  1558. * which is *really* confusing.
  1559. */
  1560. for (;;)
  1561. {
  1562. if (endtoken == K_INTO)
  1563. {
  1564. if (new->into) /* multiple INTO */
  1565. yyerror("syntax error");
  1566. new->into = true;
  1567. read_into_target(&new->rec, &new->row, &new->strict);
  1568. endtoken = yylex();
  1569. }
  1570. else if (endtoken == K_USING)
  1571. {
  1572. if (new->params) /* multiple USING */
  1573. yyerror("syntax error");
  1574. do
  1575. {
  1576. expr = read_sql_construct(',', ';', K_INTO,
  1577. ", or ; or INTO",
  1578. "SELECT ",
  1579. true, true,
  1580. NULL, &endtoken);
  1581. new->params = lappend(new->params, expr);
  1582. } while (endtoken == ',');
  1583. }
  1584. else if (endtoken == ';')
  1585. break;
  1586. else
  1587. yyerror("syntax error");
  1588. }
  1589. $$ = (PLpgSQL_stmt *)new;
  1590. }
  1591. ;
  1592. stmt_open : K_OPEN cursor_variable
  1593. {
  1594. PLpgSQL_stmt_open *new;
  1595. int tok;
  1596. new = palloc0(sizeof(PLpgSQL_stmt_open));
  1597. new->cmd_type = PLPGSQL_STMT_OPEN;
  1598. new->lineno = plpgsql_location_to_lineno(@1);
  1599. new->curvar = $2->dno;
  1600. new->cursor_options = CURSOR_OPT_FAST_PLAN;
  1601. if ($2->cursor_explicit_expr == NULL)
  1602. {
  1603. /* be nice if we could use opt_scrollable here */
  1604. tok = yylex();
  1605. if (tok_is_keyword(tok, &yylval,
  1606. K_NO, "no"))
  1607. {
  1608. tok = yylex();
  1609. if (tok_is_keyword(tok, &yylval,
  1610. K_SCROLL, "scroll"))
  1611. {
  1612. new->cursor_options |= CURSOR_OPT_NO_SCROLL;
  1613. tok = yylex();
  1614. }
  1615. }
  1616. else if (tok_is_keyword(tok, &yylval,
  1617. K_SCROLL, "scroll"))
  1618. {
  1619. new->cursor_options |= CURSOR_OPT_SCROLL;
  1620. tok = yylex();
  1621. }
  1622. if (tok != K_FOR)
  1623. yyerror("syntax error, expected \"FOR\"");
  1624. tok = yylex();
  1625. if (tok == K_EXECUTE)
  1626. {
  1627. int endtoken;
  1628. new->dynquery =
  1629. read_sql_expression2(K_USING, ';',
  1630. "USING or ;",
  1631. &endtoken);
  1632. /* If we found "USING", collect argument(s) */
  1633. if (endtoken == K_USING)
  1634. {
  1635. PLpgSQL_expr *expr;
  1636. do
  1637. {
  1638. expr = read_sql_expression2(',', ';',
  1639. ", or ;",
  1640. &endtoken);
  1641. new->params = lappend(new->params,
  1642. expr);
  1643. } while (endtoken == ',');
  1644. }
  1645. }
  1646. else
  1647. {
  1648. plpgsql_push_back_token(tok);
  1649. new->query = read_sql_stmt("");
  1650. }
  1651. }
  1652. else
  1653. {
  1654. /* predefined cursor query, so read args */
  1655. new->argquery = read_cursor_args($2, ';', ";");
  1656. }
  1657. $$ = (PLpgSQL_stmt *)new;
  1658. }
  1659. ;
  1660. stmt_fetch : K_FETCH opt_fetch_direction cursor_variable K_INTO
  1661. {
  1662. PLpgSQL_stmt_fetch *fetch = $2;
  1663. PLpgSQL_rec *rec;
  1664. PLpgSQL_row *row;
  1665. /* We have already parsed everything through the INTO keyword */
  1666. read_into_target(&rec, &row, NULL);
  1667. if (yylex() != ';')
  1668. yyerror("syntax error");
  1669. /*
  1670. * We don't allow multiple rows in PL/pgSQL's FETCH
  1671. * statement, only in MOVE.
  1672. */
  1673. if (fetch->returns_multiple_rows)
  1674. ereport(ERROR,
  1675. (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
  1676. errmsg("FETCH statement cannot return multiple rows"),
  1677. parser_errposition(@1)));
  1678. fetch->lineno = plpgsql_location_to_lineno(@1);
  1679. fetch->rec = rec;
  1680. fetch->row = row;
  1681. fetch->curvar = $3->dno;
  1682. fetch->is_move = false;
  1683. $$ = (PLpgSQL_stmt *)fetch;
  1684. }
  1685. ;
  1686. stmt_move : K_MOVE opt_fetch_direction cursor_variable ';'
  1687. {
  1688. PLpgSQL_stmt_fetch *fetch = $2;
  1689. fetch->lineno = plpgsql_location_to_lineno(@1);
  1690. fetch->curvar = $3->dno;
  1691. fetch->is_move = true;
  1692. $$ = (PLpgSQL_stmt *)fetch;
  1693. }
  1694. ;
  1695. opt_fetch_direction :
  1696. {
  1697. $$ = read_fetch_direction();
  1698. }
  1699. ;
  1700. stmt_close : K_CLOSE cursor_variable ';'
  1701. {
  1702. PLpgSQL_stmt_close *new;
  1703. new = palloc(sizeof(PLpgSQL_stmt_close));
  1704. new->cmd_type = PLPGSQL_STMT_CLOSE;
  1705. new->lineno = plpgsql_location_to_lineno(@1);
  1706. new->curvar = $2->dno;
  1707. $$ = (PLpgSQL_stmt *)new;
  1708. }
  1709. ;
  1710. stmt_null : K_NULL ';'
  1711. {
  1712. /* We do not bother building a node for NULL */
  1713. $$ = NULL;
  1714. }
  1715. ;
  1716. cursor_variable : T_DATUM
  1717. {
  1718. if ($1.datum->dtype != PLPGSQL_DTYPE_VAR)
  1719. ereport(ERROR,
  1720. (errcode(ERRCODE_DATATYPE_MISMATCH),
  1721. errmsg("cursor variable must be a simple variable"),
  1722. parser_errposition(@1)));

Large files files are truncated, but you can click here to view the full file