1996-07-09 06:22:35 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-13 23:22:53 +00:00
|
|
|
* parser.c
|
2000-10-07 00:58:23 +00:00
|
|
|
* Main entry point/driver for PostgreSQL grammar
|
|
|
|
*
|
|
|
|
* Note that the grammar is not allowed to perform any table access
|
|
|
|
* (since we need to be able to do basic parsing even while inside an
|
|
|
|
* aborted transaction). Therefore, the data structures returned by
|
|
|
|
* the grammar are "raw" parsetrees that still need to be analyzed by
|
|
|
|
* parse_analyze.
|
2000-09-12 21:07:18 +00:00
|
|
|
*
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
2002-06-20 20:29:54 +00:00
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 05:58:53 +00:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2003-04-27 20:09:44 +00:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/parser.c,v 1.56 2003/04/27 20:09:44 tgl Exp $
|
1996-07-09 06:22:35 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1996-10-31 11:09:44 +00:00
|
|
|
#include "postgres.h"
|
2000-09-12 21:07:18 +00:00
|
|
|
|
|
|
|
#include "nodes/parsenodes.h"
|
1996-11-08 20:46:33 +00:00
|
|
|
#include "parser/gramparse.h"
|
2000-09-12 21:07:18 +00:00
|
|
|
#include "parser/parse.h"
|
1997-11-26 01:14:33 +00:00
|
|
|
#include "parser/parser.h"
|
2000-03-17 05:29:07 +00:00
|
|
|
#include "parser/parse_expr.h"
|
1996-11-08 06:02:30 +00:00
|
|
|
|
2000-09-12 21:07:18 +00:00
|
|
|
|
1999-05-13 07:29:22 +00:00
|
|
|
List *parsetree; /* result of parsing is left here */
|
1996-07-09 06:22:35 +00:00
|
|
|
|
2002-09-04 20:31:48 +00:00
|
|
|
static Oid *param_type_info; /* state for param_type() */
|
2002-08-27 04:55:12 +00:00
|
|
|
static int param_count;
|
|
|
|
|
2000-09-12 21:07:18 +00:00
|
|
|
static int lookahead_token; /* one-token lookahead */
|
|
|
|
static bool have_lookahead; /* lookahead_token set? */
|
|
|
|
|
1999-05-13 07:29:22 +00:00
|
|
|
|
1996-07-09 06:22:35 +00:00
|
|
|
/*
|
2000-10-07 00:58:23 +00:00
|
|
|
* parser
|
|
|
|
* Given a query in string form, and optionally info about
|
|
|
|
* parameter types, do lexical and syntactic analysis.
|
|
|
|
*
|
|
|
|
* Returns a list of raw (un-analyzed) parse trees.
|
1996-07-09 06:22:35 +00:00
|
|
|
*/
|
1999-05-13 07:29:22 +00:00
|
|
|
List *
|
2003-04-27 20:09:44 +00:00
|
|
|
parser(const char *str, Oid *typev, int nargs)
|
1996-07-09 06:22:35 +00:00
|
|
|
{
|
1997-09-08 02:41:22 +00:00
|
|
|
int yyresult;
|
1996-07-09 06:22:35 +00:00
|
|
|
|
1999-05-13 07:29:22 +00:00
|
|
|
parsetree = NIL; /* in case parser forgets to set it */
|
2000-09-12 21:07:18 +00:00
|
|
|
have_lookahead = false;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2002-04-20 21:56:15 +00:00
|
|
|
scanner_init(str);
|
2002-08-27 04:55:12 +00:00
|
|
|
parser_init();
|
2000-03-17 05:29:07 +00:00
|
|
|
parse_expr_init();
|
2002-08-27 04:55:12 +00:00
|
|
|
parser_param_set(typev, nargs);
|
2000-03-17 05:29:07 +00:00
|
|
|
|
1997-09-07 05:04:48 +00:00
|
|
|
yyresult = yyparse();
|
1996-07-09 06:22:35 +00:00
|
|
|
|
2002-04-20 21:56:15 +00:00
|
|
|
scanner_finish();
|
1997-09-07 05:04:48 +00:00
|
|
|
clearerr(stdin);
|
|
|
|
|
1998-02-26 04:46:47 +00:00
|
|
|
if (yyresult) /* error */
|
2000-10-07 00:58:23 +00:00
|
|
|
return NIL;
|
1997-09-07 05:04:48 +00:00
|
|
|
|
2000-10-07 00:58:23 +00:00
|
|
|
return parsetree;
|
1996-07-09 06:22:35 +00:00
|
|
|
}
|
|
|
|
|
2000-09-12 21:07:18 +00:00
|
|
|
|
2002-08-27 04:55:12 +00:00
|
|
|
/*
|
|
|
|
* Save information needed to fill out the type of Param references ($n)
|
|
|
|
*
|
|
|
|
* This is used for SQL functions, PREPARE statements, etc. It's split
|
|
|
|
* out from parser() setup because PREPARE needs to change the info after
|
|
|
|
* the grammar runs and before parse analysis is done on the preparable
|
|
|
|
* query.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
parser_param_set(Oid *typev, int nargs)
|
|
|
|
{
|
|
|
|
param_type_info = typev;
|
|
|
|
param_count = nargs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* param_type()
|
|
|
|
*
|
|
|
|
* Fetch a parameter type previously passed to parser_param_set
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
param_type(int t)
|
|
|
|
{
|
|
|
|
if (t > param_count || t <= 0)
|
|
|
|
return InvalidOid;
|
|
|
|
return param_type_info[t - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-12 21:07:18 +00:00
|
|
|
/*
|
|
|
|
* Intermediate filter between parser and base lexer (base_yylex in scan.l).
|
|
|
|
*
|
|
|
|
* The filter is needed because in some cases SQL92 requires more than one
|
|
|
|
* token lookahead. We reduce these cases to one-token lookahead by combining
|
|
|
|
* tokens here, in order to keep the grammar LR(1).
|
|
|
|
*
|
2001-03-22 04:01:46 +00:00
|
|
|
* Using a filter is simpler than trying to recognize multiword tokens
|
2000-09-12 21:07:18 +00:00
|
|
|
* directly in scan.l, because we'd have to allow for comments between the
|
|
|
|
* words ...
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
yylex(void)
|
|
|
|
{
|
|
|
|
int cur_token;
|
|
|
|
|
|
|
|
/* Get next token --- we might already have it */
|
|
|
|
if (have_lookahead)
|
|
|
|
{
|
|
|
|
cur_token = lookahead_token;
|
|
|
|
have_lookahead = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cur_token = base_yylex();
|
|
|
|
|
|
|
|
/* Do we need to look ahead for a possible multiword token? */
|
|
|
|
switch (cur_token)
|
|
|
|
{
|
|
|
|
case UNION:
|
|
|
|
/* UNION JOIN must be reduced to a single UNIONJOIN token */
|
|
|
|
lookahead_token = base_yylex();
|
|
|
|
if (lookahead_token == JOIN)
|
|
|
|
cur_token = UNIONJOIN;
|
|
|
|
else
|
|
|
|
have_lookahead = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cur_token;
|
|
|
|
}
|