* procedural language
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/gram.y,v 1.14 2001/02/10 22:42:01 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/gram.y,v 1.15 2001/02/10 22:53:40 momjian Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
%union {
int32 ival;
char *str;
- struct {
+ struct
+ {
char *name;
int lineno;
} varname;
- struct {
+ struct
+ {
int nalloc;
int nused;
int *dtnums;
} dtlist;
- struct {
+ struct
+ {
int reverse;
PLpgSQL_expr *expr;
} forilow;
- struct {
+ struct
+ {
char *label;
int n_initvars;
int *initvarnos;
| opt_label decl_start decl_stmts
{
plpgsql_ns_setlocal(false);
- if ($3 != NULL) {
+ if ($3 != NULL)
$$.label = $3;
- } else {
+ else
$$.label = $1;
- }
$$.n_initvars = plpgsql_add_initdatums(&($$.initvarnos));
}
;
;
decl_stmts : decl_stmts decl_stmt
- {
- $$ = $2;
- }
+ { $$ = $2; }
| decl_stmt
- {
- $$ = $1;
- }
+ { $$ = $1; }
;
decl_stmt : '<' '<' opt_lblname '>' '>'
- {
- $$ = $3;
- }
+ { $$ = $3; }
| K_DECLARE
- {
- $$ = NULL;
- }
+ { $$ = NULL; }
| decl_statement
- {
- $$ = NULL;
- }
+ { $$ = NULL; }
;
decl_statement : decl_varname decl_const decl_datatype decl_notnull decl_defval
plpgsql_ns_setlocal(false);
name = plpgsql_tolower(yytext);
- if (name[0] != '$') {
+ if (name[0] != '$')
elog(ERROR, "can only alias positional parameters");
- }
nsi = plpgsql_ns_lookup(name, NULL);
- if (nsi == NULL) {
+ if (nsi == NULL)
elog(ERROR, "function has no parameter %s", name);
- }
plpgsql_ns_setlocal(true);
decl_varname : T_WORD
{
- /* name should be malloc'd for use as varname */
- $$.name = strdup(plpgsql_tolower(yytext));
- $$.lineno = yylineno;
+ /* name should be malloc'd for use as varname */
+ $$.name = strdup(plpgsql_tolower(yytext));
+ $$.lineno = yylineno;
}
;
decl_renname : T_WORD
{
- /* the result must be palloc'd, see plpgsql_ns_rename */
+ /* the result must be palloc'd, see plpgsql_ns_rename */
$$ = plpgsql_tolower(yytext);
}
;
;
decl_datatype : decl_dtypename
- {
- $$ = $1;
- }
+ { $$ = $1; }
;
decl_dtypename : T_DTYPE
- {
- $$ = yylval.dtype;
- }
+ { $$ = yylval.dtype; }
| T_CHAR decl_atttypmod
{
- if ($2 < 0) {
+ if ($2 < 0)
+ {
plpgsql_parse_word("char");
$$ = yylval.dtype;
- } else {
+ } else
+ {
plpgsql_parse_word("bpchar");
$$ = yylval.dtype;
$$->atttypmod = $2;
;
decl_atttypmod :
- {
- $$ = -1;
- }
+ { $$ = -1; }
| '(' decl_atttypmodval ')'
- {
- $$ = $2;
- }
+ { $$ = $2; }
;
decl_atttypmodval : T_NUMBER
expr->nparams = 0;
tok = yylex();
- switch (tok) {
+ switch (tok)
+ {
case 0:
plpgsql_error_lineno = lno;
plpgsql_comperrinfo();
elog(ERROR, "unexpected end of file");
case K_NULL:
- if (yylex() != ';') {
+ if (yylex() != ';')
+ {
plpgsql_error_lineno = lno;
plpgsql_comperrinfo();
elog(ERROR, "expectec ; after NULL");
default:
plpgsql_dstring_append(&ds, yytext);
- while ((tok = yylex()) != ';') {
- if (tok == 0) {
+ while ((tok = yylex()) != ';')
+ {
+ if (tok == 0)
+ {
plpgsql_error_lineno = lno;
plpgsql_comperrinfo();
elog(ERROR, "unterminated default value");
}
- if (plpgsql_SpaceScanned) {
+ if (plpgsql_SpaceScanned)
plpgsql_dstring_append(&ds, " ");
- }
plpgsql_dstring_append(&ds, yytext);
}
expr->query = strdup(plpgsql_dstring_get(&ds));
| K_DEFAULT
proc_sect :
- {
- PLpgSQL_stmts *new;
+ {
+ PLpgSQL_stmts *new;
- new = malloc(sizeof(PLpgSQL_stmts));
- memset(new, 0, sizeof(PLpgSQL_stmts));
- $$ = new;
- }
+ new = malloc(sizeof(PLpgSQL_stmts));
+ memset(new, 0, sizeof(PLpgSQL_stmts));
+ $$ = new;
+ }
| proc_stmts
- {
- $$ = $1;
- }
+ { $$ = $1; }
;
proc_stmts : proc_stmts proc_stmt
memcpy(new->items, $5.dtnums, sizeof(int) * $5.nused);
memcpy(new->targets, $7.dtnums, sizeof(int) * $7.nused);
- if (new->nitems != new->ntargets) {
+ if (new->nitems != new->ntargets)
+ {
plpgsql_error_lineno = new->lineno;
plpgsql_comperrinfo();
elog(ERROR, "number of diagnostic items does not match target list");
getdiag_items : getdiag_items ',' getdiag_item
{
- if ($1.nused == $1.nalloc) {
+ if ($1.nused == $1.nalloc)
+ {
$1.nalloc *= 2;
$1.dtnums = repalloc($1.dtnums, sizeof(int) * $1.nalloc);
}
getdiag_targets : getdiag_targets ',' getdiag_target
{
- if ($1.nused == $1.nalloc) {
+ if ($1.nused == $1.nalloc)
+ {
$1.nalloc *= 2;
$1.dtnums = repalloc($1.dtnums, sizeof(int) * $1.nalloc);
}
getdiag_target : T_VARIABLE
{
- if (yylval.var->isconst) {
+ if (yylval.var->isconst)
+ {
plpgsql_comperrinfo();
elog(ERROR, "%s is declared CONSTANT; can not receive diagnostics", yylval.var->refname);
}
assign_var : T_VARIABLE
{
- if (yylval.var->isconst) {
+ if (yylval.var->isconst)
+ {
plpgsql_comperrinfo();
elog(ERROR, "%s is declared CONSTANT", yylval.var->refname);
}
;
stmt_else :
- {
- PLpgSQL_stmts *new;
+ {
+ PLpgSQL_stmts *new;
- new = malloc(sizeof(PLpgSQL_stmts));
- memset(new, 0, sizeof(PLpgSQL_stmts));
- $$ = new;
- }
+ new = malloc(sizeof(PLpgSQL_stmts));
+ memset(new, 0, sizeof(PLpgSQL_stmts));
+ $$ = new;
+ }
| K_ELSE proc_sect
- { $$ = $2; }
+ { $$ = $2; }
;
stmt_loop : opt_label K_LOOP lno loop_body
plpgsql_dstring_append(&ds, "SELECT ");
$$.reverse = 0;
- while((tok = yylex()) != K_DOTDOT) {
- if (firsttok) {
+ while((tok = yylex()) != K_DOTDOT)
+ {
+ if (firsttok)
+ {
firsttok = 0;
- if (tok == K_REVERSE) {
+ if (tok == K_REVERSE)
+ {
$$.reverse = 1;
continue;
}
}
if (tok == ';') break;
- if (plpgsql_SpaceScanned) {
+ if (plpgsql_SpaceScanned)
plpgsql_dstring_append(&ds, " ");
- }
- switch (tok) {
+ switch (tok)
+ {
case T_VARIABLE:
params[nparams] = yylval.var->varno;
sprintf(buf, " $%d ", ++nparams);
break;
default:
- if (tok == 0) {
+ if (tok == 0)
+ {
plpgsql_error_lineno = lno;
plpgsql_comperrinfo();
elog(ERROR, "missing .. to terminate lower bound of for loop");
expr->query = strdup(plpgsql_dstring_get(&ds));
expr->plan = NULL;
expr->nparams = nparams;
- while(nparams-- > 0) {
+ while(nparams-- > 0)
expr->params[nparams] = params[nparams];
- }
plpgsql_dstring_free(&ds);
$$.expr = expr;
}
new->cmd_type = PLPGSQL_STMT_FORS;
new->lineno = $3;
new->label = $1;
- switch ($4->dtype) {
+ switch ($4->dtype)
+ {
case PLPGSQL_DTYPE_REC:
new->rec = $4;
break;
new->cmd_type = PLPGSQL_STMT_DYNFORS;
new->lineno = $3;
new->label = $1;
- switch ($4->dtype) {
+ switch ($4->dtype)
+ {
case PLPGSQL_DTYPE_REC:
new->rec = $4;
break;
}
fors_target : T_RECORD
- {
- $$ = yylval.rec;
- }
+ { $$ = yylval.rec; }
| T_ROW
{
$$ = (PLpgSQL_rec *)(yylval.row);
new = malloc(sizeof(PLpgSQL_stmt_return));
memset(new, 0, sizeof(PLpgSQL_stmt_return));
- if (plpgsql_curr_compile->fn_retistuple) {
+ if (plpgsql_curr_compile->fn_retistuple)
+ {
new->retistuple = true;
new->retrecno = -1;
- switch (tok = yylex()) {
+ switch (tok = yylex())
+ {
case K_NULL:
expr = NULL;
break;
yyerror("return type mismatch in function returning table row");
break;
}
- if (yylex() != ';') {
+ if (yylex() != ';')
yyerror("expected ';'");
- }
} else {
new->retistuple = false;
expr = plpgsql_read_expression(';', ";");
raise_params : raise_params raise_param
{
- if ($1.nused == $1.nalloc) {
+ if ($1.nused == $1.nalloc)
+ {
$1.nalloc *= 2;
$1.dtnums = repalloc($1.dtnums, sizeof(int) * $1.nalloc);
}
plpgsql_dstring_init(&ds);
plpgsql_dstring_append(&ds, sqlstart);
- while((tok = yylex()) != until) {
+ while((tok = yylex()) != until)
+ {
if (tok == ';') break;
- if (plpgsql_SpaceScanned) {
+ if (plpgsql_SpaceScanned)
plpgsql_dstring_append(&ds, " ");
- }
- switch (tok) {
+ switch (tok)
+ {
case T_VARIABLE:
params[nparams] = yylval.var->varno;
sprintf(buf, " $%d ", ++nparams);
break;
default:
- if (tok == 0) {
+ if (tok == 0)
+ {
plpgsql_error_lineno = lno;
plpgsql_comperrinfo();
elog(ERROR, "missing %s at end of SQL statement", s);
expr->query = strdup(plpgsql_dstring_get(&ds));
expr->plan = NULL;
expr->nparams = nparams;
- while(nparams-- > 0) {
+ while(nparams-- > 0)
expr->params[nparams] = params[nparams];
- }
plpgsql_dstring_free(&ds);
return expr;
plpgsql_dstring_init(&ds);
plpgsql_dstring_append(&ds, "SELECT ");
- while((tok = yylex()) != K_INTO) {
- if (tok == ';') {
+ while((tok = yylex()) != K_INTO)
+ {
+ if (tok == ';')
+ {
PLpgSQL_stmt_execsql *execsql;
expr = malloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - 1);
expr->query = strdup(plpgsql_dstring_get(&ds));
expr->plan = NULL;
expr->nparams = nparams;
- while(nparams-- > 0) {
+ while(nparams-- > 0)
expr->params[nparams] = params[nparams];
- }
plpgsql_dstring_free(&ds);
execsql = malloc(sizeof(PLpgSQL_stmt_execsql));
return (PLpgSQL_stmt *)execsql;
}
- if (plpgsql_SpaceScanned) {
+ if (plpgsql_SpaceScanned)
plpgsql_dstring_append(&ds, " ");
- }
- switch (tok) {
+ switch (tok)
+ {
case T_VARIABLE:
params[nparams] = yylval.var->varno;
sprintf(buf, " $%d ", ++nparams);
break;
default:
- if (tok == 0) {
+ if (tok == 0)
+ {
plpgsql_error_lineno = yylineno;
plpgsql_comperrinfo();
elog(ERROR, "unexpected end of file");
}
tok = yylex();
- switch (tok) {
+ switch (tok)
+ {
case T_ROW:
row = yylval.row;
break;
char *fieldnames[1024];
int varnos[1024];
- switch (tok) {
+ switch (tok)
+ {
case T_VARIABLE:
var = yylval.var;
fieldnames[0] = strdup(yytext);
break;
}
- while ((tok = yylex()) == ',') {
+ while ((tok = yylex()) == ',')
+ {
tok = yylex();
- switch(tok) {
+ switch(tok)
+ {
case T_VARIABLE:
var = yylval.var;
fieldnames[nfields] = strdup(yytext);
row->nfields = nfields;
row->fieldnames = malloc(sizeof(char *) * nfields);
row->varnos = malloc(sizeof(int) * nfields);
- while (--nfields >= 0) {
+ while (--nfields >= 0)
+ {
row->fieldnames[nfields] = fieldnames[nfields];
row->varnos[nfields] = varnos[nfields];
}
default:
{
- if (plpgsql_SpaceScanned) {
+ if (plpgsql_SpaceScanned)
plpgsql_dstring_append(&ds, " ");
- }
plpgsql_dstring_append(&ds, yytext);
- while(1) {
+ while(1)
+ {
tok = yylex();
- if (tok == ';') {
+ if (tok == ';')
+ {
PLpgSQL_stmt_execsql *execsql;
expr = malloc(sizeof(PLpgSQL_expr) + sizeof(int) * nparams - 1);
return (PLpgSQL_stmt *)execsql;
}
- if (plpgsql_SpaceScanned) {
+ if (plpgsql_SpaceScanned)
plpgsql_dstring_append(&ds, " ");
- }
- switch (tok) {
+ switch (tok)
+ {
case T_VARIABLE:
params[nparams] = yylval.var->varno;
sprintf(buf, " $%d ", ++nparams);
break;
default:
- if (tok == 0) {
+ if (tok == 0)
+ {
plpgsql_error_lineno = yylineno;
plpgsql_comperrinfo();
elog(ERROR, "unexpected end of file");
/************************************************************
* Eat up the rest of the statement after the target fields
************************************************************/
- while(1) {
+ while(1)
+ {
if (!have_nexttok) {
tok = yylex();
}
break;
}
- if (plpgsql_SpaceScanned) {
+ if (plpgsql_SpaceScanned)
plpgsql_dstring_append(&ds, " ");
- }
- switch (tok) {
+ switch (tok)
+ {
case T_VARIABLE:
params[nparams] = yylval.var->varno;
sprintf(buf, " $%d ", ++nparams);
break;
default:
- if (tok == 0) {
+ if (tok == 0)
+ {
plpgsql_error_lineno = yylineno;
plpgsql_comperrinfo();
elog(ERROR, "unexpected end of file");
expr->query = strdup(plpgsql_dstring_get(&ds));
expr->plan = NULL;
expr->nparams = nparams;
- while(nparams-- > 0) {
+ while(nparams-- > 0)
expr->params[nparams] = params[nparams];
- }
plpgsql_dstring_free(&ds);
select = malloc(sizeof(PLpgSQL_stmt_select));
plpgsql_dstring_init(&ds);
plpgsql_dstring_append(&ds, "SELECT ");
- for (i = 0; i < row->nfields; i++) {
+ for (i = 0; i < row->nfields; i++)
+ {
sprintf(buf, "%s$%d", (i > 0) ? "," : "", i + 1);
plpgsql_dstring_append(&ds, buf);
expr->params[i] = row->varnos[i];