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
|
|
|
*
|
2012-01-01 18:01:58 -05:00
|
|
|
* Portions Copyright (c) 1996-2012, 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
|
2010-09-20 16:08:53 -04:00
|
|
|
* src/backend/parser/parser.c
|
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
|
|
|
|
2009-07-12 13:12:34 -04:00
|
|
|
#include "parser/gramparse.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
|
|
|
|
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
|
|
|
{
|
2009-11-09 13:38:48 -05:00
|
|
|
core_yyscan_t yyscanner;
|
2009-07-12 22:02:20 -04:00
|
|
|
base_yy_extra_type yyextra;
|
1997-09-07 22:41:22 -04:00
|
|
|
int yyresult;
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2009-07-12 22:02:20 -04:00
|
|
|
/* initialize the flex scanner */
|
2009-11-09 13:38:48 -05:00
|
|
|
yyscanner = scanner_init(str, &yyextra.core_yy_extra,
|
|
|
|
|
ScanKeywords, NumScanKeywords);
|
2009-07-12 22:02:20 -04:00
|
|
|
|
2009-11-09 13:38:48 -05:00
|
|
|
/* base_yylex() only needs this much initialization */
|
2009-07-12 22:02:20 -04:00
|
|
|
yyextra.have_lookahead = false;
|
1997-09-07 01:04:48 -04:00
|
|
|
|
2009-07-12 22:02:20 -04:00
|
|
|
/* initialize the bison parser */
|
|
|
|
|
parser_init(&yyextra);
|
2000-03-17 00:29:07 -05:00
|
|
|
|
2009-07-12 22:02:20 -04:00
|
|
|
/* Parse! */
|
|
|
|
|
yyresult = base_yyparse(yyscanner);
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2009-07-12 22:02:20 -04:00
|
|
|
/* Clean up (release memory) */
|
|
|
|
|
scanner_finish(yyscanner);
|
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
|
|
|
|
2009-07-12 22:02:20 -04:00
|
|
|
return yyextra.parsetree;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|
2006-05-27 13:38:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2009-11-09 13:38:48 -05:00
|
|
|
* Intermediate filter between parser and core lexer (core_yylex in scan.l).
|
2006-05-27 13:38:46 -04:00
|
|
|
*
|
|
|
|
|
* 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.
|
2009-11-09 13:38:48 -05:00
|
|
|
*
|
|
|
|
|
* The filter also provides a convenient place to translate between
|
|
|
|
|
* the core_YYSTYPE and YYSTYPE representations (which are really the
|
|
|
|
|
* same thing anyway, but notationally they're different).
|
2006-05-27 13:38:46 -04:00
|
|
|
*/
|
|
|
|
|
int
|
2009-11-09 13:38:48 -05:00
|
|
|
base_yylex(YYSTYPE *lvalp, YYLTYPE *llocp, core_yyscan_t yyscanner)
|
2006-05-27 13:38:46 -04:00
|
|
|
{
|
2009-07-12 22:02:20 -04:00
|
|
|
base_yy_extra_type *yyextra = pg_yyget_extra(yyscanner);
|
2006-05-27 13:38:46 -04:00
|
|
|
int cur_token;
|
2007-01-06 14:14:17 -05:00
|
|
|
int next_token;
|
2009-11-09 13:38:48 -05:00
|
|
|
core_YYSTYPE cur_yylval;
|
2007-01-06 14:14:17 -05:00
|
|
|
YYLTYPE cur_yylloc;
|
2006-05-27 13:38:46 -04:00
|
|
|
|
|
|
|
|
/* Get next token --- we might already have it */
|
2009-07-12 22:02:20 -04:00
|
|
|
if (yyextra->have_lookahead)
|
2006-05-27 13:38:46 -04:00
|
|
|
{
|
2009-07-12 22:02:20 -04:00
|
|
|
cur_token = yyextra->lookahead_token;
|
2009-11-09 13:38:48 -05:00
|
|
|
lvalp->core_yystype = yyextra->lookahead_yylval;
|
2009-07-12 22:02:20 -04:00
|
|
|
*llocp = yyextra->lookahead_yylloc;
|
|
|
|
|
yyextra->have_lookahead = false;
|
2006-05-27 13:38:46 -04:00
|
|
|
}
|
|
|
|
|
else
|
2009-11-09 13:38:48 -05:00
|
|
|
cur_token = core_yylex(&(lvalp->core_yystype), llocp, yyscanner);
|
2006-05-27 13:38:46 -04:00
|
|
|
|
|
|
|
|
/* Do we need to look ahead for a possible multiword token? */
|
|
|
|
|
switch (cur_token)
|
|
|
|
|
{
|
2007-01-08 21:14:16 -05:00
|
|
|
case NULLS_P:
|
2007-11-15 16:14:46 -05:00
|
|
|
|
2007-01-08 21:14:16 -05:00
|
|
|
/*
|
|
|
|
|
* NULLS FIRST and NULLS LAST must be reduced to one token
|
|
|
|
|
*/
|
2009-11-09 13:38:48 -05:00
|
|
|
cur_yylval = lvalp->core_yystype;
|
2009-07-12 22:02:20 -04:00
|
|
|
cur_yylloc = *llocp;
|
2009-11-09 13:38:48 -05:00
|
|
|
next_token = core_yylex(&(lvalp->core_yystype), llocp, yyscanner);
|
2007-01-08 21:14:16 -05:00
|
|
|
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 */
|
2009-07-12 22:02:20 -04:00
|
|
|
yyextra->lookahead_token = next_token;
|
2009-11-09 13:38:48 -05:00
|
|
|
yyextra->lookahead_yylval = lvalp->core_yystype;
|
2009-07-12 22:02:20 -04:00
|
|
|
yyextra->lookahead_yylloc = *llocp;
|
|
|
|
|
yyextra->have_lookahead = true;
|
2007-01-08 21:14:16 -05:00
|
|
|
/* and back up the output info to cur_token */
|
2009-11-09 13:38:48 -05:00
|
|
|
lvalp->core_yystype = cur_yylval;
|
2009-07-12 22:02:20 -04:00
|
|
|
*llocp = cur_yylloc;
|
2007-01-08 21:14:16 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2006-10-03 20:30:14 -04:00
|
|
|
|
2007-01-08 21:14:16 -05:00
|
|
|
case WITH:
|
2007-11-15 16:14:46 -05:00
|
|
|
|
2006-05-27 13:38:46 -04:00
|
|
|
/*
|
2008-10-28 10:09:45 -04:00
|
|
|
* WITH TIME must be reduced to one token
|
2006-05-27 13:38:46 -04:00
|
|
|
*/
|
2009-11-09 13:38:48 -05:00
|
|
|
cur_yylval = lvalp->core_yystype;
|
2009-07-12 22:02:20 -04:00
|
|
|
cur_yylloc = *llocp;
|
2009-11-09 13:38:48 -05:00
|
|
|
next_token = core_yylex(&(lvalp->core_yystype), llocp, yyscanner);
|
2007-01-06 14:14:17 -05:00
|
|
|
switch (next_token)
|
2006-05-27 13:38:46 -04:00
|
|
|
{
|
2008-10-28 10:09:45 -04:00
|
|
|
case TIME:
|
|
|
|
|
cur_token = WITH_TIME;
|
2006-05-27 13:38:46 -04:00
|
|
|
break;
|
|
|
|
|
default:
|
2007-01-06 14:14:17 -05:00
|
|
|
/* save the lookahead token for next time */
|
2009-07-12 22:02:20 -04:00
|
|
|
yyextra->lookahead_token = next_token;
|
2009-11-09 13:38:48 -05:00
|
|
|
yyextra->lookahead_yylval = lvalp->core_yystype;
|
2009-07-12 22:02:20 -04:00
|
|
|
yyextra->lookahead_yylloc = *llocp;
|
|
|
|
|
yyextra->have_lookahead = true;
|
2007-01-06 14:14:17 -05:00
|
|
|
/* and back up the output info to cur_token */
|
2009-11-09 13:38:48 -05:00
|
|
|
lvalp->core_yystype = cur_yylval;
|
2009-07-12 22:02:20 -04:00
|
|
|
*llocp = cur_yylloc;
|
2006-05-27 13:38:46 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cur_token;
|
|
|
|
|
}
|