1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* parser.c
|
2000-10-07 02:58:23 +02: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
|
2006-03-07 02:00:19 +01:00
|
|
|
* analyze.c and related files.
|
2000-09-12 23:07:18 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2008-01-01 20:46:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2008-01-01 20:46:01 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/parser/parser.c,v 1.73 2008/01/01 19:45:51 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1996-10-31 12:09:44 +01:00
|
|
|
#include "postgres.h"
|
2000-09-12 23:07:18 +02:00
|
|
|
|
2006-07-15 05:35:21 +02:00
|
|
|
#include "parser/gramparse.h" /* required before parser/parse.h! */
|
2006-05-27 19:38:46 +02:00
|
|
|
#include "parser/parse.h"
|
1997-11-26 02:14:33 +01:00
|
|
|
#include "parser/parser.h"
|
1996-11-08 07:02:30 +01:00
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
|
1999-05-13 09:29:22 +02:00
|
|
|
List *parsetree; /* result of parsing is left here */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2007-11-15 22:14:46 +01:00
|
|
|
static bool have_lookahead; /* is lookahead info valid? */
|
|
|
|
static int lookahead_token; /* one-token lookahead */
|
2007-01-06 20:14:17 +01:00
|
|
|
static YYSTYPE lookahead_yylval; /* yylval for lookahead token */
|
|
|
|
static YYLTYPE lookahead_yylloc; /* yylloc for lookahead token */
|
2006-05-27 19:38:46 +02:00
|
|
|
|
1999-05-13 09:29:22 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2003-04-30 00:13:11 +02:00
|
|
|
* raw_parser
|
|
|
|
* Given a query in string form, do lexical and grammatical analysis.
|
2000-10-07 02:58:23 +02:00
|
|
|
*
|
|
|
|
* Returns a list of raw (un-analyzed) parse trees.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1999-05-13 09:29:22 +02:00
|
|
|
List *
|
2003-04-30 00:13:11 +02:00
|
|
|
raw_parser(const char *str)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int yyresult;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2003-04-30 00:13:11 +02:00
|
|
|
parsetree = NIL; /* in case grammar forgets to set it */
|
2006-05-27 19:38:46 +02:00
|
|
|
have_lookahead = false;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2002-04-20 23:56:15 +02:00
|
|
|
scanner_init(str);
|
2002-08-27 06:55:12 +02:00
|
|
|
parser_init();
|
2000-03-17 06:29:07 +01:00
|
|
|
|
2006-03-07 02:00:19 +01:00
|
|
|
yyresult = base_yyparse();
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2002-04-20 23:56:15 +02:00
|
|
|
scanner_finish();
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
if (yyresult) /* error */
|
2000-10-07 02:58:23 +02:00
|
|
|
return NIL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-10-07 02:58:23 +02:00
|
|
|
return parsetree;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2006-05-27 19:38:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Intermediate filter between parser and base lexer (base_yylex in scan.l).
|
|
|
|
*
|
|
|
|
* The filter is needed because in some cases the standard SQL grammar
|
2006-10-04 02:30:14 +02:00
|
|
|
* requires more than one token lookahead. We reduce these cases to one-token
|
2006-05-27 19:38:46 +02:00
|
|
|
* lookahead by combining tokens here, in order to keep the grammar LALR(1).
|
|
|
|
*
|
|
|
|
* Using a filter is simpler than trying to recognize multiword tokens
|
|
|
|
* directly in scan.l, because we'd have to allow for comments between the
|
|
|
|
* words. Furthermore it's not clear how to do it without re-introducing
|
|
|
|
* scanner backtrack, which would cost more performance than this filter
|
|
|
|
* layer does.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
filtered_base_yylex(void)
|
|
|
|
{
|
|
|
|
int cur_token;
|
2007-01-06 20:14:17 +01:00
|
|
|
int next_token;
|
|
|
|
YYSTYPE cur_yylval;
|
|
|
|
YYLTYPE cur_yylloc;
|
2006-05-27 19:38:46 +02:00
|
|
|
|
|
|
|
/* Get next token --- we might already have it */
|
|
|
|
if (have_lookahead)
|
|
|
|
{
|
|
|
|
cur_token = lookahead_token;
|
2007-01-06 20:14:17 +01:00
|
|
|
base_yylval = lookahead_yylval;
|
|
|
|
base_yylloc = lookahead_yylloc;
|
2006-05-27 19:38:46 +02:00
|
|
|
have_lookahead = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cur_token = base_yylex();
|
|
|
|
|
|
|
|
/* Do we need to look ahead for a possible multiword token? */
|
|
|
|
switch (cur_token)
|
|
|
|
{
|
2007-01-09 03:14:16 +01:00
|
|
|
case NULLS_P:
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2007-01-09 03:14:16 +01:00
|
|
|
/*
|
|
|
|
* NULLS FIRST and NULLS LAST must be reduced to one token
|
|
|
|
*/
|
|
|
|
cur_yylval = base_yylval;
|
|
|
|
cur_yylloc = base_yylloc;
|
|
|
|
next_token = base_yylex();
|
|
|
|
switch (next_token)
|
|
|
|
{
|
|
|
|
case FIRST_P:
|
|
|
|
cur_token = NULLS_FIRST;
|
|
|
|
break;
|
|
|
|
case LAST_P:
|
|
|
|
cur_token = NULLS_LAST;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* save the lookahead token for next time */
|
|
|
|
lookahead_token = next_token;
|
|
|
|
lookahead_yylval = base_yylval;
|
|
|
|
lookahead_yylloc = base_yylloc;
|
|
|
|
have_lookahead = true;
|
|
|
|
/* and back up the output info to cur_token */
|
|
|
|
base_yylval = cur_yylval;
|
|
|
|
base_yylloc = cur_yylloc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2007-01-09 03:14:16 +01:00
|
|
|
case WITH:
|
2007-11-15 22:14:46 +01:00
|
|
|
|
2006-05-27 19:38:46 +02:00
|
|
|
/*
|
|
|
|
* WITH CASCADED, LOCAL, or CHECK must be reduced to one token
|
|
|
|
*
|
2006-10-04 02:30:14 +02:00
|
|
|
* XXX an alternative way is to recognize just WITH_TIME and put
|
|
|
|
* the ugliness into the datetime datatype productions instead of
|
|
|
|
* WITH CHECK OPTION. However that requires promoting WITH to a
|
|
|
|
* fully reserved word. If we ever have to do that anyway
|
|
|
|
* (perhaps for SQL99 recursive queries), come back and simplify
|
|
|
|
* this code.
|
2006-05-27 19:38:46 +02:00
|
|
|
*/
|
2007-01-06 20:14:17 +01:00
|
|
|
cur_yylval = base_yylval;
|
|
|
|
cur_yylloc = base_yylloc;
|
|
|
|
next_token = base_yylex();
|
|
|
|
switch (next_token)
|
2006-05-27 19:38:46 +02:00
|
|
|
{
|
|
|
|
case CASCADED:
|
|
|
|
cur_token = WITH_CASCADED;
|
|
|
|
break;
|
|
|
|
case LOCAL:
|
|
|
|
cur_token = WITH_LOCAL;
|
|
|
|
break;
|
|
|
|
case CHECK:
|
|
|
|
cur_token = WITH_CHECK;
|
|
|
|
break;
|
|
|
|
default:
|
2007-01-06 20:14:17 +01:00
|
|
|
/* save the lookahead token for next time */
|
|
|
|
lookahead_token = next_token;
|
|
|
|
lookahead_yylval = base_yylval;
|
|
|
|
lookahead_yylloc = base_yylloc;
|
2006-05-27 19:38:46 +02:00
|
|
|
have_lookahead = true;
|
2007-01-06 20:14:17 +01:00
|
|
|
/* and back up the output info to cur_token */
|
|
|
|
base_yylval = cur_yylval;
|
|
|
|
base_yylloc = cur_yylloc;
|
2006-05-27 19:38:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cur_token;
|
|
|
|
}
|