1996-07-09 02:22:35 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
1999-02-13 18:22:53 -05:00
|
|
|
* parser.c
|
2000-10-06 20:58:23 -04: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-06 20:00:19 -05:00
|
|
|
* analyze.c and related files.
|
2000-09-12 17:07:18 -04:00
|
|
|
*
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
2006-03-05 10:59:11 -05:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2000-01-26 00:58:53 -05:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
|
|
|
|
* IDENTIFICATION
|
2006-10-03 20:30:14 -04:00
|
|
|
* $PostgreSQL: pgsql/src/backend/parser/parser.c,v 1.68 2006/10/04 00:29:56 momjian Exp $
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
1996-10-31 06:09:44 -05:00
|
|
|
#include "postgres.h"
|
2000-09-12 17:07:18 -04:00
|
|
|
|
2006-07-14 23:35:21 -04:00
|
|
|
#include "parser/gramparse.h" /* required before parser/parse.h! */
|
2006-05-27 13:38:46 -04:00
|
|
|
#include "parser/parse.h"
|
1997-11-25 20:14:33 -05:00
|
|
|
#include "parser/parser.h"
|
1996-11-08 01:02:30 -05:00
|
|
|
|
2000-09-12 17:07:18 -04:00
|
|
|
|
1999-05-13 03:29:22 -04:00
|
|
|
List *parsetree; /* result of parsing is left here */
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2006-05-27 13:38:46 -04:00
|
|
|
static int lookahead_token; /* one-token lookahead */
|
|
|
|
|
static bool have_lookahead; /* lookahead_token set? */
|
|
|
|
|
|
1999-05-13 03:29:22 -04:00
|
|
|
|
1996-07-09 02:22:35 -04:00
|
|
|
/*
|
2003-04-29 18:13:11 -04:00
|
|
|
* raw_parser
|
|
|
|
|
* Given a query in string form, do lexical and grammatical analysis.
|
2000-10-06 20:58:23 -04:00
|
|
|
*
|
|
|
|
|
* Returns a list of raw (un-analyzed) parse trees.
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
1999-05-13 03:29:22 -04:00
|
|
|
List *
|
2003-04-29 18:13:11 -04:00
|
|
|
raw_parser(const char *str)
|
1996-07-09 02:22:35 -04:00
|
|
|
{
|
1997-09-07 22:41:22 -04:00
|
|
|
int yyresult;
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2003-04-29 18:13:11 -04:00
|
|
|
parsetree = NIL; /* in case grammar forgets to set it */
|
2006-05-27 13:38:46 -04:00
|
|
|
have_lookahead = false;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2002-04-20 17:56:15 -04:00
|
|
|
scanner_init(str);
|
2002-08-27 00:55:12 -04:00
|
|
|
parser_init();
|
2000-03-17 00:29:07 -05:00
|
|
|
|
2006-03-06 20:00:19 -05:00
|
|
|
yyresult = base_yyparse();
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2002-04-20 17:56:15 -04:00
|
|
|
scanner_finish();
|
1997-09-07 01:04:48 -04:00
|
|
|
|
1998-02-25 23:46:47 -05:00
|
|
|
if (yyresult) /* error */
|
2000-10-06 20:58:23 -04:00
|
|
|
return NIL;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2000-10-06 20:58:23 -04:00
|
|
|
return parsetree;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
2006-05-27 13:38:46 -04: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-03 20:30:14 -04:00
|
|
|
* requires more than one token lookahead. We reduce these cases to one-token
|
2006-05-27 13:38:46 -04: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;
|
|
|
|
|
|
|
|
|
|
/* 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 WITH:
|
2006-10-03 20:30:14 -04:00
|
|
|
|
2006-05-27 13:38:46 -04:00
|
|
|
/*
|
|
|
|
|
* WITH CASCADED, LOCAL, or CHECK must be reduced to one token
|
|
|
|
|
*
|
2006-10-03 20:30:14 -04: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 13:38:46 -04:00
|
|
|
*/
|
|
|
|
|
lookahead_token = base_yylex();
|
|
|
|
|
switch (lookahead_token)
|
|
|
|
|
{
|
|
|
|
|
case CASCADED:
|
|
|
|
|
cur_token = WITH_CASCADED;
|
|
|
|
|
break;
|
|
|
|
|
case LOCAL:
|
|
|
|
|
cur_token = WITH_LOCAL;
|
|
|
|
|
break;
|
|
|
|
|
case CHECK:
|
|
|
|
|
cur_token = WITH_CHECK;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
have_lookahead = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cur_token;
|
|
|
|
|
}
|