/src/pl/plpgsql/src/gram.y
Happy | 2562 lines | 2293 code | 269 blank | 0 comment | 0 complexity | 74c6e45ac35110d58124d44e5cb2af0e MD5 | raw file
Possible License(s): AGPL-3.0
- %{
- /*-------------------------------------------------------------------------
- *
- * gram.y - Parser for the PL/pgSQL procedural language
- *
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- * src/pl/plpgsql/src/gram.y
- *
- *-------------------------------------------------------------------------
- */
- #include "plpgsql.h"
- #include "catalog/namespace.h"
- #include "catalog/pg_type.h"
- #include "parser/parser.h"
- #include "parser/parse_type.h"
- #include "parser/scanner.h"
- #include "parser/scansup.h"
- #include "utils/builtins.h"
- /* Location tracking support --- simpler than bison's default */
- #define YYLLOC_DEFAULT(Current, Rhs, N) \
- do { \
- if (N) \
- (Current) = (Rhs)[1]; \
- else \
- (Current) = (Rhs)[0]; \
- } while (0)
- /*
- * Bison doesn't allocate anything that needs to live across parser calls,
- * so we can easily have it use palloc instead of malloc. This prevents
- * memory leaks if we error out during parsing. Note this only works with
- * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
- * if possible, so there's not really much problem anyhow, at least if
- * you're building with gcc.
- */
- #define YYMALLOC palloc
- #define YYFREE pfree
- typedef struct
- {
- int location;
- int leaderlen;
- } sql_error_callback_arg;
- #define parser_errposition(pos) plpgsql_scanner_errposition(pos)
- union YYSTYPE; /* need forward reference for tok_is_keyword */
- static bool tok_is_keyword(int token, union YYSTYPE *lval,
- int kw_token, const char *kw_str);
- static void word_is_not_variable(PLword *word, int location);
- static void cword_is_not_variable(PLcword *cword, int location);
- static void current_token_is_not_variable(int tok);
- static PLpgSQL_expr *read_sql_construct(int until,
- int until2,
- int until3,
- const char *expected,
- const char *sqlstart,
- bool isexpression,
- bool valid_sql,
- bool trim,
- int *startloc,
- int *endtoken);
- static PLpgSQL_expr *read_sql_expression(int until,
- const char *expected);
- static PLpgSQL_expr *read_sql_expression2(int until, int until2,
- const char *expected,
- int *endtoken);
- static PLpgSQL_expr *read_sql_stmt(const char *sqlstart);
- static PLpgSQL_type *read_datatype(int tok);
- static PLpgSQL_stmt *make_execsql_stmt(int firsttoken, int location);
- static PLpgSQL_stmt_fetch *read_fetch_direction(void);
- static void complete_direction(PLpgSQL_stmt_fetch *fetch,
- bool *check_FROM);
- static PLpgSQL_stmt *make_return_stmt(int location);
- static PLpgSQL_stmt *make_return_next_stmt(int location);
- static PLpgSQL_stmt *make_return_query_stmt(int location);
- static PLpgSQL_stmt *make_case(int location, PLpgSQL_expr *t_expr,
- List *case_when_list, List *else_stmts);
- static char *NameOfDatum(PLwdatum *wdatum);
- static void check_assignable(PLpgSQL_datum *datum, int location);
- static void read_into_target(PLpgSQL_rec **rec, PLpgSQL_row **row,
- bool *strict);
- static PLpgSQL_row *read_into_scalar_list(char *initial_name,
- PLpgSQL_datum *initial_datum,
- int initial_location);
- static PLpgSQL_row *make_scalar_list1(char *initial_name,
- PLpgSQL_datum *initial_datum,
- int lineno, int location);
- static void check_sql_expr(const char *stmt, int location,
- int leaderlen);
- static void plpgsql_sql_error_callback(void *arg);
- static PLpgSQL_type *parse_datatype(const char *string, int location);
- static void check_labels(const char *start_label,
- const char *end_label,
- int end_location);
- static PLpgSQL_expr *read_cursor_args(PLpgSQL_var *cursor,
- int until, const char *expected);
- static List *read_raise_options(void);
- %}
- %expect 0
- %name-prefix="plpgsql_yy"
- %locations
- %union {
- core_YYSTYPE core_yystype;
- /* these fields must match core_YYSTYPE: */
- int ival;
- char *str;
- const char *keyword;
- PLword word;
- PLcword cword;
- PLwdatum wdatum;
- bool boolean;
- Oid oid;
- struct
- {
- char *name;
- int lineno;
- } varname;
- struct
- {
- char *name;
- int lineno;
- PLpgSQL_datum *scalar;
- PLpgSQL_rec *rec;
- PLpgSQL_row *row;
- } forvariable;
- struct
- {
- char *label;
- int n_initvars;
- int *initvarnos;
- } declhdr;
- struct
- {
- List *stmts;
- char *end_label;
- int end_label_location;
- } loop_body;
- List *list;
- PLpgSQL_type *dtype;
- PLpgSQL_datum *datum;
- PLpgSQL_var *var;
- PLpgSQL_expr *expr;
- PLpgSQL_stmt *stmt;
- PLpgSQL_condition *condition;
- PLpgSQL_exception *exception;
- PLpgSQL_exception_block *exception_block;
- PLpgSQL_nsitem *nsitem;
- PLpgSQL_diag_item *diagitem;
- PLpgSQL_stmt_fetch *fetch;
- PLpgSQL_case_when *casewhen;
- }
- %type <declhdr> decl_sect
- %type <varname> decl_varname
- %type <boolean> decl_const decl_notnull exit_type
- %type <expr> decl_defval decl_cursor_query
- %type <dtype> decl_datatype
- %type <oid> decl_collate
- %type <datum> decl_cursor_args
- %type <list> decl_cursor_arglist
- %type <nsitem> decl_aliasitem
- %type <expr> expr_until_semi expr_until_rightbracket
- %type <expr> expr_until_then expr_until_loop opt_expr_until_when
- %type <expr> opt_exitcond
- %type <ival> assign_var foreach_slice
- %type <var> cursor_variable
- %type <datum> decl_cursor_arg
- %type <forvariable> for_variable
- %type <stmt> for_control
- %type <str> any_identifier opt_block_label opt_label
- %type <list> proc_sect proc_stmts stmt_elsifs stmt_else
- %type <loop_body> loop_body
- %type <stmt> proc_stmt pl_block
- %type <stmt> stmt_assign stmt_if stmt_loop stmt_while stmt_exit
- %type <stmt> stmt_return stmt_raise stmt_execsql
- %type <stmt> stmt_dynexecute stmt_for stmt_perform stmt_getdiag
- %type <stmt> stmt_open stmt_fetch stmt_move stmt_close stmt_null
- %type <stmt> stmt_case stmt_foreach_a
- %type <list> proc_exceptions
- %type <exception_block> exception_sect
- %type <exception> proc_exception
- %type <condition> proc_conditions proc_condition
- %type <casewhen> case_when
- %type <list> case_when_list opt_case_else
- %type <boolean> getdiag_area_opt
- %type <list> getdiag_list
- %type <diagitem> getdiag_list_item
- %type <ival> getdiag_item getdiag_target
- %type <ival> opt_scrollable
- %type <fetch> opt_fetch_direction
- %type <keyword> unreserved_keyword
- /*
- * Basic non-keyword token types. These are hard-wired into the core lexer.
- * They must be listed first so that their numeric codes do not depend on
- * the set of keywords. Keep this list in sync with backend/parser/gram.y!
- *
- * Some of these are not directly referenced in this file, but they must be
- * here anyway.
- */
- %token <str> IDENT FCONST SCONST BCONST XCONST Op
- %token <ival> ICONST PARAM
- %token TYPECAST DOT_DOT COLON_EQUALS
- /*
- * Other tokens recognized by plpgsql's lexer interface layer (pl_scanner.c).
- */
- %token <word> T_WORD /* unrecognized simple identifier */
- %token <cword> T_CWORD /* unrecognized composite identifier */
- %token <wdatum> T_DATUM /* a VAR, ROW, REC, or RECFIELD variable */
- %token LESS_LESS
- %token GREATER_GREATER
- /*
- * Keyword tokens. Some of these are reserved and some are not;
- * see pl_scanner.c for info. Be sure unreserved keywords are listed
- * in the "unreserved_keyword" production below.
- */
- %token <keyword> K_ABSOLUTE
- %token <keyword> K_ALIAS
- %token <keyword> K_ALL
- %token <keyword> K_ARRAY
- %token <keyword> K_BACKWARD
- %token <keyword> K_BEGIN
- %token <keyword> K_BY
- %token <keyword> K_CASE
- %token <keyword> K_CLOSE
- %token <keyword> K_COLLATE
- %token <keyword> K_CONSTANT
- %token <keyword> K_CONTINUE
- %token <keyword> K_CURRENT
- %token <keyword> K_CURSOR
- %token <keyword> K_DEBUG
- %token <keyword> K_DECLARE
- %token <keyword> K_DEFAULT
- %token <keyword> K_DETAIL
- %token <keyword> K_DIAGNOSTICS
- %token <keyword> K_DUMP
- %token <keyword> K_ELSE
- %token <keyword> K_ELSIF
- %token <keyword> K_END
- %token <keyword> K_ERRCODE
- %token <keyword> K_ERROR
- %token <keyword> K_EXCEPTION
- %token <keyword> K_EXECUTE
- %token <keyword> K_EXIT
- %token <keyword> K_FETCH
- %token <keyword> K_FIRST
- %token <keyword> K_FOR
- %token <keyword> K_FOREACH
- %token <keyword> K_FORWARD
- %token <keyword> K_FROM
- %token <keyword> K_GET
- %token <keyword> K_HINT
- %token <keyword> K_IF
- %token <keyword> K_IN
- %token <keyword> K_INFO
- %token <keyword> K_INSERT
- %token <keyword> K_INTO
- %token <keyword> K_IS
- %token <keyword> K_LAST
- %token <keyword> K_LOG
- %token <keyword> K_LOOP
- %token <keyword> K_MESSAGE
- %token <keyword> K_MESSAGE_TEXT
- %token <keyword> K_MOVE
- %token <keyword> K_NEXT
- %token <keyword> K_NO
- %token <keyword> K_NOT
- %token <keyword> K_NOTICE
- %token <keyword> K_NULL
- %token <keyword> K_OPEN
- %token <keyword> K_OPTION
- %token <keyword> K_OR
- %token <keyword> K_PERFORM
- %token <keyword> K_PG_EXCEPTION_CONTEXT
- %token <keyword> K_PG_EXCEPTION_DETAIL
- %token <keyword> K_PG_EXCEPTION_HINT
- %token <keyword> K_PRIOR
- %token <keyword> K_QUERY
- %token <keyword> K_RAISE
- %token <keyword> K_RELATIVE
- %token <keyword> K_RESULT_OID
- %token <keyword> K_RETURN
- %token <keyword> K_RETURNED_SQLSTATE
- %token <keyword> K_REVERSE
- %token <keyword> K_ROWTYPE
- %token <keyword> K_ROW_COUNT
- %token <keyword> K_SCROLL
- %token <keyword> K_SLICE
- %token <keyword> K_SQLSTATE
- %token <keyword> K_STACKED
- %token <keyword> K_STRICT
- %token <keyword> K_THEN
- %token <keyword> K_TO
- %token <keyword> K_TYPE
- %token <keyword> K_USE_COLUMN
- %token <keyword> K_USE_VARIABLE
- %token <keyword> K_USING
- %token <keyword> K_VARIABLE_CONFLICT
- %token <keyword> K_WARNING
- %token <keyword> K_WHEN
- %token <keyword> K_WHILE
- %%
- pl_function : comp_options pl_block opt_semi
- {
- plpgsql_parse_result = (PLpgSQL_stmt_block *) $2;
- }
- ;
- comp_options :
- | comp_options comp_option
- ;
- comp_option : '#' K_OPTION K_DUMP
- {
- plpgsql_DumpExecTree = true;
- }
- | '#' K_VARIABLE_CONFLICT K_ERROR
- {
- plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_ERROR;
- }
- | '#' K_VARIABLE_CONFLICT K_USE_VARIABLE
- {
- plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_VARIABLE;
- }
- | '#' K_VARIABLE_CONFLICT K_USE_COLUMN
- {
- plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_COLUMN;
- }
- ;
- opt_semi :
- | ';'
- ;
- pl_block : decl_sect K_BEGIN proc_sect exception_sect K_END opt_label
- {
- PLpgSQL_stmt_block *new;
- new = palloc0(sizeof(PLpgSQL_stmt_block));
- new->cmd_type = PLPGSQL_STMT_BLOCK;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1.label;
- new->n_initvars = $1.n_initvars;
- new->initvarnos = $1.initvarnos;
- new->body = $3;
- new->exceptions = $4;
- check_labels($1.label, $6, @6);
- plpgsql_ns_pop();
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- decl_sect : opt_block_label
- {
- /* done with decls, so resume identifier lookup */
- plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
- $$.label = $1;
- $$.n_initvars = 0;
- $$.initvarnos = NULL;
- }
- | opt_block_label decl_start
- {
- plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
- $$.label = $1;
- $$.n_initvars = 0;
- $$.initvarnos = NULL;
- }
- | opt_block_label decl_start decl_stmts
- {
- plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
- $$.label = $1;
- /* Remember variables declared in decl_stmts */
- $$.n_initvars = plpgsql_add_initdatums(&($$.initvarnos));
- }
- ;
- decl_start : K_DECLARE
- {
- /* Forget any variables created before block */
- plpgsql_add_initdatums(NULL);
- /*
- * Disable scanner lookup of identifiers while
- * we process the decl_stmts
- */
- plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
- }
- ;
- decl_stmts : decl_stmts decl_stmt
- | decl_stmt
- ;
- decl_stmt : decl_statement
- | K_DECLARE
- {
- /* We allow useless extra DECLAREs */
- }
- | LESS_LESS any_identifier GREATER_GREATER
- {
- /*
- * Throw a helpful error if user tries to put block
- * label just before BEGIN, instead of before DECLARE.
- */
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("block label must be placed before DECLARE, not after"),
- parser_errposition(@1)));
- }
- ;
- decl_statement : decl_varname decl_const decl_datatype decl_collate decl_notnull decl_defval
- {
- PLpgSQL_variable *var;
- /*
- * If a collation is supplied, insert it into the
- * datatype. We assume decl_datatype always returns
- * a freshly built struct not shared with other
- * variables.
- */
- if (OidIsValid($4))
- {
- if (!OidIsValid($3->collation))
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("collations are not supported by type %s",
- format_type_be($3->typoid)),
- parser_errposition(@4)));
- $3->collation = $4;
- }
- var = plpgsql_build_variable($1.name, $1.lineno,
- $3, true);
- if ($2)
- {
- if (var->dtype == PLPGSQL_DTYPE_VAR)
- ((PLpgSQL_var *) var)->isconst = $2;
- else
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("row or record variable cannot be CONSTANT"),
- parser_errposition(@2)));
- }
- if ($5)
- {
- if (var->dtype == PLPGSQL_DTYPE_VAR)
- ((PLpgSQL_var *) var)->notnull = $5;
- else
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("row or record variable cannot be NOT NULL"),
- parser_errposition(@4)));
- }
- if ($6 != NULL)
- {
- if (var->dtype == PLPGSQL_DTYPE_VAR)
- ((PLpgSQL_var *) var)->default_val = $6;
- else
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("default value for row or record variable is not supported"),
- parser_errposition(@5)));
- }
- }
- | decl_varname K_ALIAS K_FOR decl_aliasitem ';'
- {
- plpgsql_ns_additem($4->itemtype,
- $4->itemno, $1.name);
- }
- | decl_varname opt_scrollable K_CURSOR
- { plpgsql_ns_push($1.name); }
- decl_cursor_args decl_is_for decl_cursor_query
- {
- PLpgSQL_var *new;
- PLpgSQL_expr *curname_def;
- char buf[1024];
- char *cp1;
- char *cp2;
- /* pop local namespace for cursor args */
- plpgsql_ns_pop();
- new = (PLpgSQL_var *)
- plpgsql_build_variable($1.name, $1.lineno,
- plpgsql_build_datatype(REFCURSOROID,
- -1,
- InvalidOid),
- true);
- curname_def = palloc0(sizeof(PLpgSQL_expr));
- curname_def->dtype = PLPGSQL_DTYPE_EXPR;
- strcpy(buf, "SELECT ");
- cp1 = new->refname;
- cp2 = buf + strlen(buf);
- /*
- * Don't trust standard_conforming_strings here;
- * it might change before we use the string.
- */
- if (strchr(cp1, '\\') != NULL)
- *cp2++ = ESCAPE_STRING_SYNTAX;
- *cp2++ = '\'';
- while (*cp1)
- {
- if (SQL_STR_DOUBLE(*cp1, true))
- *cp2++ = *cp1;
- *cp2++ = *cp1++;
- }
- strcpy(cp2, "'::pg_catalog.refcursor");
- curname_def->query = pstrdup(buf);
- new->default_val = curname_def;
- new->cursor_explicit_expr = $7;
- if ($5 == NULL)
- new->cursor_explicit_argrow = -1;
- else
- new->cursor_explicit_argrow = $5->dno;
- new->cursor_options = CURSOR_OPT_FAST_PLAN | $2;
- }
- ;
- opt_scrollable :
- {
- $$ = 0;
- }
- | K_NO K_SCROLL
- {
- $$ = CURSOR_OPT_NO_SCROLL;
- }
- | K_SCROLL
- {
- $$ = CURSOR_OPT_SCROLL;
- }
- ;
- decl_cursor_query :
- {
- $$ = read_sql_stmt("");
- }
- ;
- decl_cursor_args :
- {
- $$ = NULL;
- }
- | '(' decl_cursor_arglist ')'
- {
- PLpgSQL_row *new;
- int i;
- ListCell *l;
- new = palloc0(sizeof(PLpgSQL_row));
- new->dtype = PLPGSQL_DTYPE_ROW;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->rowtupdesc = NULL;
- new->nfields = list_length($2);
- new->fieldnames = palloc(new->nfields * sizeof(char *));
- new->varnos = palloc(new->nfields * sizeof(int));
- i = 0;
- foreach (l, $2)
- {
- PLpgSQL_variable *arg = (PLpgSQL_variable *) lfirst(l);
- new->fieldnames[i] = arg->refname;
- new->varnos[i] = arg->dno;
- i++;
- }
- list_free($2);
- plpgsql_adddatum((PLpgSQL_datum *) new);
- $$ = (PLpgSQL_datum *) new;
- }
- ;
- decl_cursor_arglist : decl_cursor_arg
- {
- $$ = list_make1($1);
- }
- | decl_cursor_arglist ',' decl_cursor_arg
- {
- $$ = lappend($1, $3);
- }
- ;
- decl_cursor_arg : decl_varname decl_datatype
- {
- $$ = (PLpgSQL_datum *)
- plpgsql_build_variable($1.name, $1.lineno,
- $2, true);
- }
- ;
- decl_is_for : K_IS | /* Oracle */
- K_FOR; /* SQL standard */
- decl_aliasitem : T_WORD
- {
- PLpgSQL_nsitem *nsi;
- nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
- $1.ident, NULL, NULL,
- NULL);
- if (nsi == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_UNDEFINED_OBJECT),
- errmsg("variable \"%s\" does not exist",
- $1.ident),
- parser_errposition(@1)));
- $$ = nsi;
- }
- | T_CWORD
- {
- PLpgSQL_nsitem *nsi;
- if (list_length($1.idents) == 2)
- nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
- strVal(linitial($1.idents)),
- strVal(lsecond($1.idents)),
- NULL,
- NULL);
- else if (list_length($1.idents) == 3)
- nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
- strVal(linitial($1.idents)),
- strVal(lsecond($1.idents)),
- strVal(lthird($1.idents)),
- NULL);
- else
- nsi = NULL;
- if (nsi == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_UNDEFINED_OBJECT),
- errmsg("variable \"%s\" does not exist",
- NameListToString($1.idents)),
- parser_errposition(@1)));
- $$ = nsi;
- }
- ;
- decl_varname : T_WORD
- {
- $$.name = $1.ident;
- $$.lineno = plpgsql_location_to_lineno(@1);
- /*
- * Check to make sure name isn't already declared
- * in the current block.
- */
- if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
- $1.ident, NULL, NULL,
- NULL) != NULL)
- yyerror("duplicate declaration");
- }
- | unreserved_keyword
- {
- $$.name = pstrdup($1);
- $$.lineno = plpgsql_location_to_lineno(@1);
- /*
- * Check to make sure name isn't already declared
- * in the current block.
- */
- if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
- $1, NULL, NULL,
- NULL) != NULL)
- yyerror("duplicate declaration");
- }
- ;
- decl_const :
- { $$ = false; }
- | K_CONSTANT
- { $$ = true; }
- ;
- decl_datatype :
- {
- /*
- * If there's a lookahead token, read_datatype
- * should consume it.
- */
- $$ = read_datatype(yychar);
- yyclearin;
- }
- ;
- decl_collate :
- { $$ = InvalidOid; }
- | K_COLLATE T_WORD
- {
- $$ = get_collation_oid(list_make1(makeString($2.ident)),
- false);
- }
- | K_COLLATE T_CWORD
- {
- $$ = get_collation_oid($2.idents, false);
- }
- ;
- decl_notnull :
- { $$ = false; }
- | K_NOT K_NULL
- { $$ = true; }
- ;
- decl_defval : ';'
- { $$ = NULL; }
- | decl_defkey
- {
- $$ = read_sql_expression(';', ";");
- }
- ;
- decl_defkey : assign_operator
- | K_DEFAULT
- ;
- assign_operator : '='
- | COLON_EQUALS
- ;
- proc_sect :
- { $$ = NIL; }
- | proc_stmts
- { $$ = $1; }
- ;
- proc_stmts : proc_stmts proc_stmt
- {
- if ($2 == NULL)
- $$ = $1;
- else
- $$ = lappend($1, $2);
- }
- | proc_stmt
- {
- if ($1 == NULL)
- $$ = NIL;
- else
- $$ = list_make1($1);
- }
- ;
- proc_stmt : pl_block ';'
- { $$ = $1; }
- | stmt_assign
- { $$ = $1; }
- | stmt_if
- { $$ = $1; }
- | stmt_case
- { $$ = $1; }
- | stmt_loop
- { $$ = $1; }
- | stmt_while
- { $$ = $1; }
- | stmt_for
- { $$ = $1; }
- | stmt_foreach_a
- { $$ = $1; }
- | stmt_exit
- { $$ = $1; }
- | stmt_return
- { $$ = $1; }
- | stmt_raise
- { $$ = $1; }
- | stmt_execsql
- { $$ = $1; }
- | stmt_dynexecute
- { $$ = $1; }
- | stmt_perform
- { $$ = $1; }
- | stmt_getdiag
- { $$ = $1; }
- | stmt_open
- { $$ = $1; }
- | stmt_fetch
- { $$ = $1; }
- | stmt_move
- { $$ = $1; }
- | stmt_close
- { $$ = $1; }
- | stmt_null
- { $$ = $1; }
- ;
- stmt_perform : K_PERFORM expr_until_semi
- {
- PLpgSQL_stmt_perform *new;
- new = palloc0(sizeof(PLpgSQL_stmt_perform));
- new->cmd_type = PLPGSQL_STMT_PERFORM;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->expr = $2;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_assign : assign_var assign_operator expr_until_semi
- {
- PLpgSQL_stmt_assign *new;
- new = palloc0(sizeof(PLpgSQL_stmt_assign));
- new->cmd_type = PLPGSQL_STMT_ASSIGN;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->varno = $1;
- new->expr = $3;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_getdiag : K_GET getdiag_area_opt K_DIAGNOSTICS getdiag_list ';'
- {
- PLpgSQL_stmt_getdiag *new;
- ListCell *lc;
- new = palloc0(sizeof(PLpgSQL_stmt_getdiag));
- new->cmd_type = PLPGSQL_STMT_GETDIAG;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->is_stacked = $2;
- new->diag_items = $4;
- /*
- * Check information items are valid for area option.
- */
- foreach(lc, new->diag_items)
- {
- PLpgSQL_diag_item *ditem = (PLpgSQL_diag_item *) lfirst(lc);
- switch (ditem->kind)
- {
- /* these fields are disallowed in stacked case */
- case PLPGSQL_GETDIAG_ROW_COUNT:
- case PLPGSQL_GETDIAG_RESULT_OID:
- if (new->is_stacked)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("diagnostics item %s is not allowed in GET STACKED DIAGNOSTICS",
- plpgsql_getdiag_kindname(ditem->kind)),
- parser_errposition(@1)));
- break;
- /* these fields are disallowed in current case */
- case PLPGSQL_GETDIAG_ERROR_CONTEXT:
- case PLPGSQL_GETDIAG_ERROR_DETAIL:
- case PLPGSQL_GETDIAG_ERROR_HINT:
- case PLPGSQL_GETDIAG_RETURNED_SQLSTATE:
- case PLPGSQL_GETDIAG_MESSAGE_TEXT:
- if (!new->is_stacked)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("diagnostics item %s is not allowed in GET CURRENT DIAGNOSTICS",
- plpgsql_getdiag_kindname(ditem->kind)),
- parser_errposition(@1)));
- break;
- default:
- elog(ERROR, "unrecognized diagnostic item kind: %d",
- ditem->kind);
- break;
- }
- }
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- getdiag_area_opt :
- {
- $$ = false;
- }
- | K_CURRENT
- {
- $$ = false;
- }
- | K_STACKED
- {
- $$ = true;
- }
- ;
- getdiag_list : getdiag_list ',' getdiag_list_item
- {
- $$ = lappend($1, $3);
- }
- | getdiag_list_item
- {
- $$ = list_make1($1);
- }
- ;
- getdiag_list_item : getdiag_target assign_operator getdiag_item
- {
- PLpgSQL_diag_item *new;
- new = palloc(sizeof(PLpgSQL_diag_item));
- new->target = $1;
- new->kind = $3;
- $$ = new;
- }
- ;
- getdiag_item :
- {
- int tok = yylex();
- if (tok_is_keyword(tok, &yylval,
- K_ROW_COUNT, "row_count"))
- $$ = PLPGSQL_GETDIAG_ROW_COUNT;
- else if (tok_is_keyword(tok, &yylval,
- K_RESULT_OID, "result_oid"))
- $$ = PLPGSQL_GETDIAG_RESULT_OID;
- else if (tok_is_keyword(tok, &yylval,
- K_PG_EXCEPTION_DETAIL, "pg_exception_detail"))
- $$ = PLPGSQL_GETDIAG_ERROR_DETAIL;
- else if (tok_is_keyword(tok, &yylval,
- K_PG_EXCEPTION_HINT, "pg_exception_hint"))
- $$ = PLPGSQL_GETDIAG_ERROR_HINT;
- else if (tok_is_keyword(tok, &yylval,
- K_PG_EXCEPTION_CONTEXT, "pg_exception_context"))
- $$ = PLPGSQL_GETDIAG_ERROR_CONTEXT;
- else if (tok_is_keyword(tok, &yylval,
- K_MESSAGE_TEXT, "message_text"))
- $$ = PLPGSQL_GETDIAG_MESSAGE_TEXT;
- else if (tok_is_keyword(tok, &yylval,
- K_RETURNED_SQLSTATE, "returned_sqlstate"))
- $$ = PLPGSQL_GETDIAG_RETURNED_SQLSTATE;
- else
- yyerror("unrecognized GET DIAGNOSTICS item");
- }
- ;
- getdiag_target : T_DATUM
- {
- check_assignable($1.datum, @1);
- if ($1.datum->dtype == PLPGSQL_DTYPE_ROW ||
- $1.datum->dtype == PLPGSQL_DTYPE_REC)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("\"%s\" is not a scalar variable",
- NameOfDatum(&($1))),
- parser_errposition(@1)));
- $$ = $1.datum->dno;
- }
- | T_WORD
- {
- /* just to give a better message than "syntax error" */
- word_is_not_variable(&($1), @1);
- }
- | T_CWORD
- {
- /* just to give a better message than "syntax error" */
- cword_is_not_variable(&($1), @1);
- }
- ;
- assign_var : T_DATUM
- {
- check_assignable($1.datum, @1);
- $$ = $1.datum->dno;
- }
- | assign_var '[' expr_until_rightbracket
- {
- PLpgSQL_arrayelem *new;
- new = palloc0(sizeof(PLpgSQL_arrayelem));
- new->dtype = PLPGSQL_DTYPE_ARRAYELEM;
- new->subscript = $3;
- new->arrayparentno = $1;
- /* initialize cached type data to "not valid" */
- new->parenttypoid = InvalidOid;
- plpgsql_adddatum((PLpgSQL_datum *) new);
- $$ = new->dno;
- }
- ;
- stmt_if : K_IF expr_until_then proc_sect stmt_elsifs stmt_else K_END K_IF ';'
- {
- PLpgSQL_stmt_if *new;
- new = palloc0(sizeof(PLpgSQL_stmt_if));
- new->cmd_type = PLPGSQL_STMT_IF;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->cond = $2;
- new->then_body = $3;
- new->elsif_list = $4;
- new->else_body = $5;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_elsifs :
- {
- $$ = NIL;
- }
- | stmt_elsifs K_ELSIF expr_until_then proc_sect
- {
- PLpgSQL_if_elsif *new;
- new = palloc0(sizeof(PLpgSQL_if_elsif));
- new->lineno = plpgsql_location_to_lineno(@2);
- new->cond = $3;
- new->stmts = $4;
- $$ = lappend($1, new);
- }
- ;
- stmt_else :
- {
- $$ = NIL;
- }
- | K_ELSE proc_sect
- {
- $$ = $2;
- }
- ;
- stmt_case : K_CASE opt_expr_until_when case_when_list opt_case_else K_END K_CASE ';'
- {
- $$ = make_case(@1, $2, $3, $4);
- }
- ;
- opt_expr_until_when :
- {
- PLpgSQL_expr *expr = NULL;
- int tok = yylex();
- if (tok != K_WHEN)
- {
- plpgsql_push_back_token(tok);
- expr = read_sql_expression(K_WHEN, "WHEN");
- }
- plpgsql_push_back_token(K_WHEN);
- $$ = expr;
- }
- ;
- case_when_list : case_when_list case_when
- {
- $$ = lappend($1, $2);
- }
- | case_when
- {
- $$ = list_make1($1);
- }
- ;
- case_when : K_WHEN expr_until_then proc_sect
- {
- PLpgSQL_case_when *new = palloc(sizeof(PLpgSQL_case_when));
- new->lineno = plpgsql_location_to_lineno(@1);
- new->expr = $2;
- new->stmts = $3;
- $$ = new;
- }
- ;
- opt_case_else :
- {
- $$ = NIL;
- }
- | K_ELSE proc_sect
- {
- /*
- * proc_sect could return an empty list, but we
- * must distinguish that from not having ELSE at all.
- * Simplest fix is to return a list with one NULL
- * pointer, which make_case() must take care of.
- */
- if ($2 != NIL)
- $$ = $2;
- else
- $$ = list_make1(NULL);
- }
- ;
- stmt_loop : opt_block_label K_LOOP loop_body
- {
- PLpgSQL_stmt_loop *new;
- new = palloc0(sizeof(PLpgSQL_stmt_loop));
- new->cmd_type = PLPGSQL_STMT_LOOP;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1;
- new->body = $3.stmts;
- check_labels($1, $3.end_label, $3.end_label_location);
- plpgsql_ns_pop();
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_while : opt_block_label K_WHILE expr_until_loop loop_body
- {
- PLpgSQL_stmt_while *new;
- new = palloc0(sizeof(PLpgSQL_stmt_while));
- new->cmd_type = PLPGSQL_STMT_WHILE;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1;
- new->cond = $3;
- new->body = $4.stmts;
- check_labels($1, $4.end_label, $4.end_label_location);
- plpgsql_ns_pop();
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_for : opt_block_label K_FOR for_control loop_body
- {
- /* This runs after we've scanned the loop body */
- if ($3->cmd_type == PLPGSQL_STMT_FORI)
- {
- PLpgSQL_stmt_fori *new;
- new = (PLpgSQL_stmt_fori *) $3;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1;
- new->body = $4.stmts;
- $$ = (PLpgSQL_stmt *) new;
- }
- else
- {
- PLpgSQL_stmt_forq *new;
- Assert($3->cmd_type == PLPGSQL_STMT_FORS ||
- $3->cmd_type == PLPGSQL_STMT_FORC ||
- $3->cmd_type == PLPGSQL_STMT_DYNFORS);
- /* forq is the common supertype of all three */
- new = (PLpgSQL_stmt_forq *) $3;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1;
- new->body = $4.stmts;
- $$ = (PLpgSQL_stmt *) new;
- }
- check_labels($1, $4.end_label, $4.end_label_location);
- /* close namespace started in opt_block_label */
- plpgsql_ns_pop();
- }
- ;
- for_control : for_variable K_IN
- {
- int tok = yylex();
- int tokloc = yylloc;
- if (tok == K_EXECUTE)
- {
- /* EXECUTE means it's a dynamic FOR loop */
- PLpgSQL_stmt_dynfors *new;
- PLpgSQL_expr *expr;
- int term;
- expr = read_sql_expression2(K_LOOP, K_USING,
- "LOOP or USING",
- &term);
- new = palloc0(sizeof(PLpgSQL_stmt_dynfors));
- new->cmd_type = PLPGSQL_STMT_DYNFORS;
- if ($1.rec)
- {
- new->rec = $1.rec;
- check_assignable((PLpgSQL_datum *) new->rec, @1);
- }
- else if ($1.row)
- {
- new->row = $1.row;
- check_assignable((PLpgSQL_datum *) new->row, @1);
- }
- else if ($1.scalar)
- {
- /* convert single scalar to list */
- new->row = make_scalar_list1($1.name, $1.scalar,
- $1.lineno, @1);
- /* no need for check_assignable */
- }
- else
- {
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("loop variable of loop over rows must be a record or row variable or list of scalar variables"),
- parser_errposition(@1)));
- }
- new->query = expr;
- if (term == K_USING)
- {
- do
- {
- expr = read_sql_expression2(',', K_LOOP,
- ", or LOOP",
- &term);
- new->params = lappend(new->params, expr);
- } while (term == ',');
- }
- $$ = (PLpgSQL_stmt *) new;
- }
- else if (tok == T_DATUM &&
- yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_VAR &&
- ((PLpgSQL_var *) yylval.wdatum.datum)->datatype->typoid == REFCURSOROID)
- {
- /* It's FOR var IN cursor */
- PLpgSQL_stmt_forc *new;
- PLpgSQL_var *cursor = (PLpgSQL_var *) yylval.wdatum.datum;
- new = (PLpgSQL_stmt_forc *) palloc0(sizeof(PLpgSQL_stmt_forc));
- new->cmd_type = PLPGSQL_STMT_FORC;
- new->curvar = cursor->dno;
- /* Should have had a single variable name */
- if ($1.scalar && $1.row)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("cursor FOR loop must have only one target variable"),
- parser_errposition(@1)));
- /* can't use an unbound cursor this way */
- if (cursor->cursor_explicit_expr == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("cursor FOR loop must use a bound cursor variable"),
- parser_errposition(tokloc)));
- /* collect cursor's parameters if any */
- new->argquery = read_cursor_args(cursor,
- K_LOOP,
- "LOOP");
- /* create loop's private RECORD variable */
- new->rec = plpgsql_build_record($1.name,
- $1.lineno,
- true);
- $$ = (PLpgSQL_stmt *) new;
- }
- else
- {
- PLpgSQL_expr *expr1;
- int expr1loc;
- bool reverse = false;
- /*
- * We have to distinguish between two
- * alternatives: FOR var IN a .. b and FOR
- * var IN query. Unfortunately this is
- * tricky, since the query in the second
- * form needn't start with a SELECT
- * keyword. We use the ugly hack of
- * looking for two periods after the first
- * token. We also check for the REVERSE
- * keyword, which means it must be an
- * integer loop.
- */
- if (tok_is_keyword(tok, &yylval,
- K_REVERSE, "reverse"))
- reverse = true;
- else
- plpgsql_push_back_token(tok);
- /*
- * Read tokens until we see either a ".."
- * or a LOOP. The text we read may not
- * necessarily be a well-formed SQL
- * statement, so we need to invoke
- * read_sql_construct directly.
- */
- expr1 = read_sql_construct(DOT_DOT,
- K_LOOP,
- 0,
- "LOOP",
- "SELECT ",
- true,
- false,
- true,
- &expr1loc,
- &tok);
- if (tok == DOT_DOT)
- {
- /* Saw "..", so it must be an integer loop */
- PLpgSQL_expr *expr2;
- PLpgSQL_expr *expr_by;
- PLpgSQL_var *fvar;
- PLpgSQL_stmt_fori *new;
- /* Check first expression is well-formed */
- check_sql_expr(expr1->query, expr1loc, 7);
- /* Read and check the second one */
- expr2 = read_sql_expression2(K_LOOP, K_BY,
- "LOOP",
- &tok);
- /* Get the BY clause if any */
- if (tok == K_BY)
- expr_by = read_sql_expression(K_LOOP,
- "LOOP");
- else
- expr_by = NULL;
- /* Should have had a single variable name */
- if ($1.scalar && $1.row)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("integer FOR loop must have only one target variable"),
- parser_errposition(@1)));
- /* create loop's private variable */
- fvar = (PLpgSQL_var *)
- plpgsql_build_variable($1.name,
- $1.lineno,
- plpgsql_build_datatype(INT4OID,
- -1,
- InvalidOid),
- true);
- new = palloc0(sizeof(PLpgSQL_stmt_fori));
- new->cmd_type = PLPGSQL_STMT_FORI;
- new->var = fvar;
- new->reverse = reverse;
- new->lower = expr1;
- new->upper = expr2;
- new->step = expr_by;
- $$ = (PLpgSQL_stmt *) new;
- }
- else
- {
- /*
- * No "..", so it must be a query loop. We've
- * prefixed an extra SELECT to the query text,
- * so we need to remove that before performing
- * syntax checking.
- */
- char *tmp_query;
- PLpgSQL_stmt_fors *new;
- if (reverse)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("cannot specify REVERSE in query FOR loop"),
- parser_errposition(tokloc)));
- Assert(strncmp(expr1->query, "SELECT ", 7) == 0);
- tmp_query = pstrdup(expr1->query + 7);
- pfree(expr1->query);
- expr1->query = tmp_query;
- check_sql_expr(expr1->query, expr1loc, 0);
- new = palloc0(sizeof(PLpgSQL_stmt_fors));
- new->cmd_type = PLPGSQL_STMT_FORS;
- if ($1.rec)
- {
- new->rec = $1.rec;
- check_assignable((PLpgSQL_datum *) new->rec, @1);
- }
- else if ($1.row)
- {
- new->row = $1.row;
- check_assignable((PLpgSQL_datum *) new->row, @1);
- }
- else if ($1.scalar)
- {
- /* convert single scalar to list */
- new->row = make_scalar_list1($1.name, $1.scalar,
- $1.lineno, @1);
- /* no need for check_assignable */
- }
- else
- {
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("loop variable of loop over rows must be a record or row variable or list of scalar variables"),
- parser_errposition(@1)));
- }
- new->query = expr1;
- $$ = (PLpgSQL_stmt *) new;
- }
- }
- }
- ;
- /*
- * Processing the for_variable is tricky because we don't yet know if the
- * FOR is an integer FOR loop or a loop over query results. In the former
- * case, the variable is just a name that we must instantiate as a loop
- * local variable, regardless of any other definition it might have.
- * Therefore, we always save the actual identifier into $$.name where it
- * can be used for that case. We also save the outer-variable definition,
- * if any, because that's what we need for the loop-over-query case. Note
- * that we must NOT apply check_assignable() or any other semantic check
- * until we know what's what.
- *
- * However, if we see a comma-separated list of names, we know that it
- * can't be an integer FOR loop and so it's OK to check the variables
- * immediately. In particular, for T_WORD followed by comma, we should
- * complain that the name is not known rather than say it's a syntax error.
- * Note that the non-error result of this case sets *both* $$.scalar and
- * $$.row; see the for_control production.
- */
- for_variable : T_DATUM
- {
- $$.name = NameOfDatum(&($1));
- $$.lineno = plpgsql_location_to_lineno(@1);
- if ($1.datum->dtype == PLPGSQL_DTYPE_ROW)
- {
- $$.scalar = NULL;
- $$.rec = NULL;
- $$.row = (PLpgSQL_row *) $1.datum;
- }
- else if ($1.datum->dtype == PLPGSQL_DTYPE_REC)
- {
- $$.scalar = NULL;
- $$.rec = (PLpgSQL_rec *) $1.datum;
- $$.row = NULL;
- }
- else
- {
- int tok;
- $$.scalar = $1.datum;
- $$.rec = NULL;
- $$.row = NULL;
- /* check for comma-separated list */
- tok = yylex();
- plpgsql_push_back_token(tok);
- if (tok == ',')
- $$.row = read_into_scalar_list($$.name,
- $$.scalar,
- @1);
- }
- }
- | T_WORD
- {
- int tok;
- $$.name = $1.ident;
- $$.lineno = plpgsql_location_to_lineno(@1);
- $$.scalar = NULL;
- $$.rec = NULL;
- $$.row = NULL;
- /* check for comma-separated list */
- tok = yylex();
- plpgsql_push_back_token(tok);
- if (tok == ',')
- word_is_not_variable(&($1), @1);
- }
- | T_CWORD
- {
- /* just to give a better message than "syntax error" */
- cword_is_not_variable(&($1), @1);
- }
- ;
- stmt_foreach_a : opt_block_label K_FOREACH for_variable foreach_slice K_IN K_ARRAY expr_until_loop loop_body
- {
- PLpgSQL_stmt_foreach_a *new;
- new = palloc0(sizeof(PLpgSQL_stmt_foreach_a));
- new->cmd_type = PLPGSQL_STMT_FOREACH_A;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1;
- new->slice = $4;
- new->expr = $7;
- new->body = $8.stmts;
- if ($3.rec)
- {
- new->varno = $3.rec->dno;
- check_assignable((PLpgSQL_datum *) $3.rec, @3);
- }
- else if ($3.row)
- {
- new->varno = $3.row->dno;
- check_assignable((PLpgSQL_datum *) $3.row, @3);
- }
- else if ($3.scalar)
- {
- new->varno = $3.scalar->dno;
- check_assignable($3.scalar, @3);
- }
- else
- {
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("loop variable of FOREACH must be a known variable or list of variables"),
- parser_errposition(@3)));
- }
- check_labels($1, $8.end_label, $8.end_label_location);
- plpgsql_ns_pop();
- $$ = (PLpgSQL_stmt *) new;
- }
- ;
- foreach_slice :
- {
- $$ = 0;
- }
- | K_SLICE ICONST
- {
- $$ = $2;
- }
- ;
- stmt_exit : exit_type opt_label opt_exitcond
- {
- PLpgSQL_stmt_exit *new;
- new = palloc0(sizeof(PLpgSQL_stmt_exit));
- new->cmd_type = PLPGSQL_STMT_EXIT;
- new->is_exit = $1;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->label = $2;
- new->cond = $3;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- exit_type : K_EXIT
- {
- $$ = true;
- }
- | K_CONTINUE
- {
- $$ = false;
- }
- ;
- stmt_return : K_RETURN
- {
- int tok;
- tok = yylex();
- if (tok == 0)
- yyerror("unexpected end of function definition");
- if (tok_is_keyword(tok, &yylval,
- K_NEXT, "next"))
- {
- $$ = make_return_next_stmt(@1);
- }
- else if (tok_is_keyword(tok, &yylval,
- K_QUERY, "query"))
- {
- $$ = make_return_query_stmt(@1);
- }
- else
- {
- plpgsql_push_back_token(tok);
- $$ = make_return_stmt(@1);
- }
- }
- ;
- stmt_raise : K_RAISE
- {
- PLpgSQL_stmt_raise *new;
- int tok;
- new = palloc(sizeof(PLpgSQL_stmt_raise));
- new->cmd_type = PLPGSQL_STMT_RAISE;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->elog_level = ERROR; /* default */
- new->condname = NULL;
- new->message = NULL;
- new->params = NIL;
- new->options = NIL;
- tok = yylex();
- if (tok == 0)
- yyerror("unexpected end of function definition");
- /*
- * We could have just RAISE, meaning to re-throw
- * the current error.
- */
- if (tok != ';')
- {
- /*
- * First is an optional elog severity level.
- */
- if (tok_is_keyword(tok, &yylval,
- K_EXCEPTION, "exception"))
- {
- new->elog_level = ERROR;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_WARNING, "warning"))
- {
- new->elog_level = WARNING;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_NOTICE, "notice"))
- {
- new->elog_level = NOTICE;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_INFO, "info"))
- {
- new->elog_level = INFO;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_LOG, "log"))
- {
- new->elog_level = LOG;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_DEBUG, "debug"))
- {
- new->elog_level = DEBUG1;
- tok = yylex();
- }
- if (tok == 0)
- yyerror("unexpected end of function definition");
- /*
- * Next we can have a condition name, or
- * equivalently SQLSTATE 'xxxxx', or a string
- * literal that is the old-style message format,
- * or USING to start the option list immediately.
- */
- if (tok == SCONST)
- {
- /* old style message and parameters */
- new->message = yylval.str;
- /*
- * We expect either a semi-colon, which
- * indicates no parameters, or a comma that
- * begins the list of parameter expressions,
- * or USING to begin the options list.
- */
- tok = yylex();
- if (tok != ',' && tok != ';' && tok != K_USING)
- yyerror("syntax error");
- while (tok == ',')
- {
- PLpgSQL_expr *expr;
- expr = read_sql_construct(',', ';', K_USING,
- ", or ; or USING",
- "SELECT ",
- true, true, true,
- NULL, &tok);
- new->params = lappend(new->params, expr);
- }
- }
- else if (tok != K_USING)
- {
- /* must be condition name or SQLSTATE */
- if (tok_is_keyword(tok, &yylval,
- K_SQLSTATE, "sqlstate"))
- {
- /* next token should be a string literal */
- char *sqlstatestr;
- if (yylex() != SCONST)
- yyerror("syntax error");
- sqlstatestr = yylval.str;
- if (strlen(sqlstatestr) != 5)
- yyerror("invalid SQLSTATE code");
- if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
- yyerror("invalid SQLSTATE code");
- new->condname = sqlstatestr;
- }
- else
- {
- if (tok != T_WORD)
- yyerror("syntax error");
- new->condname = yylval.word.ident;
- plpgsql_recognize_err_condition(new->condname,
- false);
- }
- tok = yylex();
- if (tok != ';' && tok != K_USING)
- yyerror("syntax error");
- }
- if (tok == K_USING)
- new->options = read_raise_options();
- }
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- loop_body : proc_sect K_END K_LOOP opt_label ';'
- {
- $$.stmts = $1;
- $$.end_label = $4;
- $$.end_label_location = @4;
- }
- ;
- /*
- * T_WORD+T_CWORD match any initial identifier that is not a known plpgsql
- * variable. (The composite case is probably a syntax error, but we'll let
- * the core parser decide that.) Normally, we should assume that such a
- * word is a SQL statement keyword that isn't also a plpgsql keyword.
- * However, if the next token is assignment or '[', it can't be a valid
- * SQL statement, and what we're probably looking at is an intended variable
- * assignment. Give an appropriate complaint for that, instead of letting
- * the core parser throw an unhelpful "syntax error".
- */
- stmt_execsql : K_INSERT
- {
- $$ = make_execsql_stmt(K_INSERT, @1);
- }
- | T_WORD
- {
- int tok;
- tok = yylex();
- plpgsql_push_back_token(tok);
- if (tok == '=' || tok == COLON_EQUALS || tok == '[')
- word_is_not_variable(&($1), @1);
- $$ = make_execsql_stmt(T_WORD, @1);
- }
- | T_CWORD
- {
- int tok;
- tok = yylex();
- plpgsql_push_back_token(tok);
- if (tok == '=' || tok == COLON_EQUALS || tok == '[')
- cword_is_not_variable(&($1), @1);
- $$ = make_execsql_stmt(T_CWORD, @1);
- }
- ;
- stmt_dynexecute : K_EXECUTE
- {
- PLpgSQL_stmt_dynexecute *new;
- PLpgSQL_expr *expr;
- int endtoken;
- expr = read_sql_construct(K_INTO, K_USING, ';',
- "INTO or USING or ;",
- "SELECT ",
- true, true, true,
- NULL, &endtoken);
- new = palloc(sizeof(PLpgSQL_stmt_dynexecute));
- new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->query = expr;
- new->into = false;
- new->strict = false;
- new->rec = NULL;
- new->row = NULL;
- new->params = NIL;
- /*
- * We loop to allow the INTO and USING clauses to
- * appear in either order, since people easily get
- * that wrong. This coding also prevents "INTO foo"
- * from getting absorbed into a USING expression,
- * which is *really* confusing.
- */
- for (;;)
- {
- if (endtoken == K_INTO)
- {
- if (new->into) /* multiple INTO */
- yyerror("syntax error");
- new->into = true;
- read_into_target(&new->rec, &new->row, &new->strict);
- endtoken = yylex();
- }
- else if (endtoken == K_USING)
- {
- if (new->params) /* multiple USING */
- yyerror("syntax error");
- do
- {
- expr = read_sql_construct(',', ';', K_INTO,
- ", or ; or INTO",
- "SELECT ",
- true, true, true,
- NULL, &endtoken);
- new->params = lappend(new->params, expr);
- } while (endtoken == ',');
- }
- else if (endtoken == ';')
- break;
- else
- yyerror("syntax error");
- }
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_open : K_OPEN cursor_variable
- {
- PLpgSQL_stmt_open *new;
- int tok;
- new = palloc0(sizeof(PLpgSQL_stmt_open));
- new->cmd_type = PLPGSQL_STMT_OPEN;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->curvar = $2->dno;
- new->cursor_options = CURSOR_OPT_FAST_PLAN;
- if ($2->cursor_explicit_expr == NULL)
- {
- /* be nice if we could use opt_scrollable here */
- tok = yylex();
- if (tok_is_keyword(tok, &yylval,
- K_NO, "no"))
- {
- tok = yylex();
- if (tok_is_keyword(tok, &yylval,
- K_SCROLL, "scroll"))
- {
- new->cursor_options |= CURSOR_OPT_NO_SCROLL;
- tok = yylex();
- }
- }
- else if (tok_is_keyword(tok, &yylval,
- K_SCROLL, "scroll"))
- {
- new->cursor_options |= CURSOR_OPT_SCROLL;
- tok = yylex();
- }
- if (tok != K_FOR)
- yyerror("syntax error, expected \"FOR\"");
- tok = yylex();
- if (tok == K_EXECUTE)
- {
- int endtoken;
- new->dynquery =
- read_sql_expression2(K_USING, ';',
- "USING or ;",
- &endtoken);
- /* If we found "USING", collect argument(s) */
- if (endtoken == K_USING)
- {
- PLpgSQL_expr *expr;
- do
- {
- expr = read_sql_expression2(',', ';',
- ", or ;",
- &endtoken);
- new->params = lappend(new->params,
- expr);
- } while (endtoken == ',');
- }
- }
- else
- {
- plpgsql_push_back_token(tok);
- new->query = read_sql_stmt("");
- }
- }
- else
- {
- /* predefined cursor query, so read args */
- new->argquery = read_cursor_args($2, ';', ";");
- }
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_fetch : K_FETCH opt_fetch_direction cursor_variable K_INTO
- {
- PLpgSQL_stmt_fetch *fetch = $2;
- PLpgSQL_rec *rec;
- PLpgSQL_row *row;
- /* We have already parsed everything through the INTO keyword */
- read_into_target(&rec, &row, NULL);
- if (yylex() != ';')
- yyerror("syntax error");
- /*
- * We don't allow multiple rows in PL/pgSQL's FETCH
- * statement, only in MOVE.
- */
- if (fetch->returns_multiple_rows)
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("FETCH statement cannot return multiple rows"),
- parser_errposition(@1)));
- fetch->lineno = plpgsql_location_to_lineno(@1);
- fetch->rec = rec;
- fetch->row = row;
- fetch->curvar = $3->dno;
- fetch->is_move = false;
- $$ = (PLpgSQL_stmt *)fetch;
- }
- ;
- stmt_move : K_MOVE opt_fetch_direction cursor_variable ';'
- {
- PLpgSQL_stmt_fetch *fetch = $2;
- fetch->lineno = plpgsql_location_to_lineno(@1);
- fetch->curvar = $3->dno;
- fetch->is_move = true;
- $$ = (PLpgSQL_stmt *)fetch;
- }
- ;
- opt_fetch_direction :
- {
- $$ = read_fetch_direction();
- }
- ;
- stmt_close : K_CLOSE cursor_variable ';'
- {
- PLpgSQL_stmt_close *new;
- new = palloc(sizeof(PLpgSQL_stmt_close));
- new->cmd_type = PLPGSQL_STMT_CLOSE;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->curvar = $2->dno;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_null : K_NULL ';'
- {
- /* We do not bother building a node for NULL */
- $$ = NULL;
- }
- ;
- cursor_variable : T_DATUM
- {
- if ($1.datum->dtype != PLPGSQL_DTYPE_VAR)
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("cursor variable must be a simple variable"),
- parser_errposition(@1)));
- if (((PLpgSQL_var *) $1.datum)->datatype->typoid != REFCURSOROID)
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("variable \"%s\" must be of type cursor or refcursor",
- ((PLpgSQL_var *) $1.datum)->refname),
- parser_errposition(@1)));
- $$ = (PLpgSQL_var *) $1.datum;
- }
- | T_WORD
- {
- /* just to give a better message than "syntax error" */
- word_is_not_variable(&($1), @1);
- }
- | T_CWORD
- {
- /* just to give a better message than "syntax error" */
- cword_is_not_variable(&($1), @1);
- }
- ;
- exception_sect :
- { $$ = NULL; }
- | K_EXCEPTION
- {
- /*
- * We use a mid-rule action to add these
- * special variables to the namespace before
- * parsing the WHEN clauses themselves. The
- * scope of the names extends to the end of the
- * current block.
- */
- int lineno = plpgsql_location_to_lineno(@1);
- PLpgSQL_exception_block *new = palloc(sizeof(PLpgSQL_exception_block));
- PLpgSQL_variable *var;
- var = plpgsql_build_variable("sqlstate", lineno,
- plpgsql_build_datatype(TEXTOID,
- -1,
- plpgsql_curr_compile->fn_input_collation),
- true);
- ((PLpgSQL_var *) var)->isconst = true;
- new->sqlstate_varno = var->dno;
- var = plpgsql_build_variable("sqlerrm", lineno,
- plpgsql_build_datatype(TEXTOID,
- -1,
- plpgsql_curr_compile->fn_input_collation),
- true);
- ((PLpgSQL_var *) var)->isconst = true;
- new->sqlerrm_varno = var->dno;
- $<exception_block>$ = new;
- }
- proc_exceptions
- {
- PLpgSQL_exception_block *new = $<exception_block>2;
- new->exc_list = $3;
- $$ = new;
- }
- ;
- proc_exceptions : proc_exceptions proc_exception
- {
- $$ = lappend($1, $2);
- }
- | proc_exception
- {
- $$ = list_make1($1);
- }
- ;
- proc_exception : K_WHEN proc_conditions K_THEN proc_sect
- {
- PLpgSQL_exception *new;
- new = palloc0(sizeof(PLpgSQL_exception));
- new->lineno = plpgsql_location_to_lineno(@1);
- new->conditions = $2;
- new->action = $4;
- $$ = new;
- }
- ;
- proc_conditions : proc_conditions K_OR proc_condition
- {
- PLpgSQL_condition *old;
- for (old = $1; old->next != NULL; old = old->next)
- /* skip */ ;
- old->next = $3;
- $$ = $1;
- }
- | proc_condition
- {
- $$ = $1;
- }
- ;
- proc_condition : any_identifier
- {
- if (strcmp($1, "sqlstate") != 0)
- {
- $$ = plpgsql_parse_err_condition($1);
- }
- else
- {
- PLpgSQL_condition *new;
- char *sqlstatestr;
- /* next token should be a string literal */
- if (yylex() != SCONST)
- yyerror("syntax error");
- sqlstatestr = yylval.str;
- if (strlen(sqlstatestr) != 5)
- yyerror("invalid SQLSTATE code");
- if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
- yyerror("invalid SQLSTATE code");
- new = palloc(sizeof(PLpgSQL_condition));
- new->sqlerrstate =
- MAKE_SQLSTATE(sqlstatestr[0],
- sqlstatestr[1],
- sqlstatestr[2],
- sqlstatestr[3],
- sqlstatestr[4]);
- new->condname = sqlstatestr;
- new->next = NULL;
- $$ = new;
- }
- }
- ;
- expr_until_semi :
- { $$ = read_sql_expression(';', ";"); }
- ;
- expr_until_rightbracket :
- { $$ = read_sql_expression(']', "]"); }
- ;
- expr_until_then :
- { $$ = read_sql_expression(K_THEN, "THEN"); }
- ;
- expr_until_loop :
- { $$ = read_sql_expression(K_LOOP, "LOOP"); }
- ;
- opt_block_label :
- {
- plpgsql_ns_push(NULL);
- $$ = NULL;
- }
- | LESS_LESS any_identifier GREATER_GREATER
- {
- plpgsql_ns_push($2);
- $$ = $2;
- }
- ;
- opt_label :
- {
- $$ = NULL;
- }
- | any_identifier
- {
- if (plpgsql_ns_lookup_label(plpgsql_ns_top(), $1) == NULL)
- yyerror("label does not exist");
- $$ = $1;
- }
- ;
- opt_exitcond : ';'
- { $$ = NULL; }
- | K_WHEN expr_until_semi
- { $$ = $2; }
- ;
- /*
- * need both options because scanner will have tried to resolve as variable
- */
- any_identifier : T_WORD
- {
- $$ = $1.ident;
- }
- | T_DATUM
- {
- if ($1.ident == NULL) /* composite name not OK */
- yyerror("syntax error");
- $$ = $1.ident;
- }
- ;
- unreserved_keyword :
- K_ABSOLUTE
- | K_ALIAS
- | K_ARRAY
- | K_BACKWARD
- | K_CONSTANT
- | K_CURRENT
- | K_CURSOR
- | K_DEBUG
- | K_DETAIL
- | K_DUMP
- | K_ERRCODE
- | K_ERROR
- | K_FIRST
- | K_FORWARD
- | K_HINT
- | K_INFO
- | K_IS
- | K_LAST
- | K_LOG
- | K_MESSAGE
- | K_MESSAGE_TEXT
- | K_NEXT
- | K_NO
- | K_NOTICE
- | K_OPTION
- | K_PG_EXCEPTION_CONTEXT
- | K_PG_EXCEPTION_DETAIL
- | K_PG_EXCEPTION_HINT
- | K_PRIOR
- | K_QUERY
- | K_RELATIVE
- | K_RESULT_OID
- | K_RETURNED_SQLSTATE
- | K_REVERSE
- | K_ROW_COUNT
- | K_ROWTYPE
- | K_SCROLL
- | K_SLICE
- | K_SQLSTATE
- | K_STACKED
- | K_TYPE
- | K_USE_COLUMN
- | K_USE_VARIABLE
- | K_VARIABLE_CONFLICT
- | K_WARNING
- ;
- %%
- /*
- * Check whether a token represents an "unreserved keyword".
- * We have various places where we want to recognize a keyword in preference
- * to a variable name, but not reserve that keyword in other contexts.
- * Hence, this kluge.
- */
- static bool
- tok_is_keyword(int token, union YYSTYPE *lval,
- int kw_token, const char *kw_str)
- {
- if (token == kw_token)
- {
- /* Normal case, was recognized by scanner (no conflicting variable) */
- return true;
- }
- else if (token == T_DATUM)
- {
- /*
- * It's a variable, so recheck the string name. Note we will not
- * match composite names (hence an unreserved word followed by "."
- * will not be recognized).
- */
- if (!lval->wdatum.quoted && lval->wdatum.ident != NULL &&
- strcmp(lval->wdatum.ident, kw_str) == 0)
- return true;
- }
- return false; /* not the keyword */
- }
- /*
- * Convenience routine to complain when we expected T_DATUM and got T_WORD,
- * ie, unrecognized variable.
- */
- static void
- word_is_not_variable(PLword *word, int location)
- {
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("\"%s\" is not a known variable",
- word->ident),
- parser_errposition(location)));
- }
- /* Same, for a CWORD */
- static void
- cword_is_not_variable(PLcword *cword, int location)
- {
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("\"%s\" is not a known variable",
- NameListToString(cword->idents)),
- parser_errposition(location)));
- }
- /*
- * Convenience routine to complain when we expected T_DATUM and got
- * something else. "tok" must be the current token, since we also
- * look at yylval and yylloc.
- */
- static void
- current_token_is_not_variable(int tok)
- {
- if (tok == T_WORD)
- word_is_not_variable(&(yylval.word), yylloc);
- else if (tok == T_CWORD)
- cword_is_not_variable(&(yylval.cword), yylloc);
- else
- yyerror("syntax error");
- }
- /* Convenience routine to read an expression with one possible terminator */
- static PLpgSQL_expr *
- read_sql_expression(int until, const char *expected)
- {
- return read_sql_construct(until, 0, 0, expected,
- "SELECT ", true, true, true, NULL, NULL);
- }
- /* Convenience routine to read an expression with two possible terminators */
- static PLpgSQL_expr *
- read_sql_expression2(int until, int until2, const char *expected,
- int *endtoken)
- {
- return read_sql_construct(until, until2, 0, expected,
- "SELECT ", true, true, true, NULL, endtoken);
- }
- /* Convenience routine to read a SQL statement that must end with ';' */
- static PLpgSQL_expr *
- read_sql_stmt(const char *sqlstart)
- {
- return read_sql_construct(';', 0, 0, ";",
- sqlstart, false, true, true, NULL, NULL);
- }
- /*
- * Read a SQL construct and build a PLpgSQL_expr for it.
- *
- * until: token code for expected terminator
- * until2: token code for alternate terminator (pass 0 if none)
- * until3: token code for another alternate terminator (pass 0 if none)
- * expected: text to use in complaining that terminator was not found
- * sqlstart: text to prefix to the accumulated SQL text
- * isexpression: whether to say we're reading an "expression" or a "statement"
- * valid_sql: whether to check the syntax of the expr (prefixed with sqlstart)
- * trim: trim trailing whitespace
- * startloc: if not NULL, location of first token is stored at *startloc
- * endtoken: if not NULL, ending token is stored at *endtoken
- * (this is only interesting if until2 or until3 isn't zero)
- */
- static PLpgSQL_expr *
- read_sql_construct(int until,
- int until2,
- int until3,
- const char *expected,
- const char *sqlstart,
- bool isexpression,
- bool valid_sql,
- bool trim,
- int *startloc,
- int *endtoken)
- {
- int tok;
- StringInfoData ds;
- IdentifierLookup save_IdentifierLookup;
- int startlocation = -1;
- int parenlevel = 0;
- PLpgSQL_expr *expr;
- initStringInfo(&ds);
- appendStringInfoString(&ds, sqlstart);
- /* special lookup mode for identifiers within the SQL text */
- save_IdentifierLookup = plpgsql_IdentifierLookup;
- plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_EXPR;
- for (;;)
- {
- tok = yylex();
- if (startlocation < 0) /* remember loc of first token */
- startlocation = yylloc;
- if (tok == until && parenlevel == 0)
- break;
- if (tok == until2 && parenlevel == 0)
- break;
- if (tok == until3 && parenlevel == 0)
- break;
- if (tok == '(' || tok == '[')
- parenlevel++;
- else if (tok == ')' || tok == ']')
- {
- parenlevel--;
- if (parenlevel < 0)
- yyerror("mismatched parentheses");
- }
- /*
- * End of function definition is an error, and we don't expect to
- * hit a semicolon either (unless it's the until symbol, in which
- * case we should have fallen out above).
- */
- if (tok == 0 || tok == ';')
- {
- if (parenlevel != 0)
- yyerror("mismatched parentheses");
- if (isexpression)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("missing \"%s\" at end of SQL expression",
- expected),
- parser_errposition(yylloc)));
- else
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("missing \"%s\" at end of SQL statement",
- expected),
- parser_errposition(yylloc)));
- }
- }
- plpgsql_IdentifierLookup = save_IdentifierLookup;
- if (startloc)
- *startloc = startlocation;
- if (endtoken)
- *endtoken = tok;
- /* give helpful complaint about empty input */
- if (startlocation >= yylloc)
- {
- if (isexpression)
- yyerror("missing expression");
- else
- yyerror("missing SQL statement");
- }
- plpgsql_append_source_text(&ds, startlocation, yylloc);
- /* trim any trailing whitespace, for neatness */
- if (trim)
- {
- while (ds.len > 0 && scanner_isspace(ds.data[ds.len - 1]))
- ds.data[--ds.len] = '\0';
- }
- expr = palloc0(sizeof(PLpgSQL_expr));
- expr->dtype = PLPGSQL_DTYPE_EXPR;
- expr->query = pstrdup(ds.data);
- expr->plan = NULL;
- expr->paramnos = NULL;
- expr->ns = plpgsql_ns_top();
- pfree(ds.data);
- if (valid_sql)
- check_sql_expr(expr->query, startlocation, strlen(sqlstart));
- return expr;
- }
- static PLpgSQL_type *
- read_datatype(int tok)
- {
- StringInfoData ds;
- char *type_name;
- int startlocation;
- PLpgSQL_type *result;
- int parenlevel = 0;
- /* Should only be called while parsing DECLARE sections */
- Assert(plpgsql_IdentifierLookup == IDENTIFIER_LOOKUP_DECLARE);
- /* Often there will be a lookahead token, but if not, get one */
- if (tok == YYEMPTY)
- tok = yylex();
- startlocation = yylloc;
- /*
- * If we have a simple or composite identifier, check for %TYPE
- * and %ROWTYPE constructs.
- */
- if (tok == T_WORD)
- {
- char *dtname = yylval.word.ident;
- tok = yylex();
- if (tok == '%')
- {
- tok = yylex();
- if (tok_is_keyword(tok, &yylval,
- K_TYPE, "type"))
- {
- result = plpgsql_parse_wordtype(dtname);
- if (result)
- return result;
- }
- else if (tok_is_keyword(tok, &yylval,
- K_ROWTYPE, "rowtype"))
- {
- result = plpgsql_parse_wordrowtype(dtname);
- if (result)
- return result;
- }
- }
- }
- else if (tok == T_CWORD)
- {
- List *dtnames = yylval.cword.idents;
- tok = yylex();
- if (tok == '%')
- {
- tok = yylex();
- if (tok_is_keyword(tok, &yylval,
- K_TYPE, "type"))
- {
- result = plpgsql_parse_cwordtype(dtnames);
- if (result)
- return result;
- }
- else if (tok_is_keyword(tok, &yylval,
- K_ROWTYPE, "rowtype"))
- {
- result = plpgsql_parse_cwordrowtype(dtnames);
- if (result)
- return result;
- }
- }
- }
- while (tok != ';')
- {
- if (tok == 0)
- {
- if (parenlevel != 0)
- yyerror("mismatched parentheses");
- else
- yyerror("incomplete data type declaration");
- }
- /* Possible followers for datatype in a declaration */
- if (tok == K_COLLATE || tok == K_NOT ||
- tok == '=' || tok == COLON_EQUALS || tok == K_DEFAULT)
- break;
- /* Possible followers for datatype in a cursor_arg list */
- if ((tok == ',' || tok == ')') && parenlevel == 0)
- break;
- if (tok == '(')
- parenlevel++;
- else if (tok == ')')
- parenlevel--;
- t