|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * parser.c |
| 4 | + * Main entry point/driver for PostgreSQL grammar |
| 5 | + * |
| 6 | + * Note that the grammar is not allowed to perform any table access |
| 7 | + * (since we need to be able to do basic parsing even while inside an |
| 8 | + * aborted transaction). Therefore, the data structures returned by |
| 9 | + * the grammar are "raw" parsetrees that still need to be analyzed by |
| 10 | + * analyze.c and related files. |
| 11 | + * |
| 12 | + * |
| 13 | + * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group |
| 14 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 15 | + * |
| 16 | + * IDENTIFICATION |
| 17 | + * $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/parser.c,v 1.1 2007/10/26 14:17:53 tgl Exp $ |
| 18 | + * |
| 19 | + *------------------------------------------------------------------------- |
| 20 | + */ |
| 21 | + |
| 22 | +#include "postgres_fe.h" |
| 23 | + |
| 24 | +#include "extern.h" |
| 25 | +#include "preproc.h" |
| 26 | + |
| 27 | + |
| 28 | +static bool have_lookahead; /* is lookahead info valid? */ |
| 29 | +static int lookahead_token; /* one-token lookahead */ |
| 30 | +static YYSTYPE lookahead_yylval; /* yylval for lookahead token */ |
| 31 | +static YYLTYPE lookahead_yylloc; /* yylloc for lookahead token */ |
| 32 | + |
| 33 | + |
| 34 | +/* |
| 35 | + * Intermediate filter between parser and base lexer (base_yylex in scan.l). |
| 36 | + * |
| 37 | + * The filter is needed because in some cases the standard SQL grammar |
| 38 | + * requires more than one token lookahead. We reduce these cases to one-token |
| 39 | + * lookahead by combining tokens here, in order to keep the grammar LALR(1). |
| 40 | + * |
| 41 | + * Using a filter is simpler than trying to recognize multiword tokens |
| 42 | + * directly in scan.l, because we'd have to allow for comments between the |
| 43 | + * words. Furthermore it's not clear how to do it without re-introducing |
| 44 | + * scanner backtrack, which would cost more performance than this filter |
| 45 | + * layer does. |
| 46 | + */ |
| 47 | +int |
| 48 | +filtered_base_yylex(void) |
| 49 | +{ |
| 50 | + int cur_token; |
| 51 | + int next_token; |
| 52 | + YYSTYPE cur_yylval; |
| 53 | + YYLTYPE cur_yylloc; |
| 54 | + |
| 55 | + /* Get next token --- we might already have it */ |
| 56 | + if (have_lookahead) |
| 57 | + { |
| 58 | + cur_token = lookahead_token; |
| 59 | + base_yylval = lookahead_yylval; |
| 60 | + base_yylloc = lookahead_yylloc; |
| 61 | + have_lookahead = false; |
| 62 | + } |
| 63 | + else |
| 64 | + cur_token = base_yylex(); |
| 65 | + |
| 66 | + /* Do we need to look ahead for a possible multiword token? */ |
| 67 | + switch (cur_token) |
| 68 | + { |
| 69 | + case NULLS_P: |
| 70 | + /* |
| 71 | + * NULLS FIRST and NULLS LAST must be reduced to one token |
| 72 | + */ |
| 73 | + cur_yylval = base_yylval; |
| 74 | + cur_yylloc = base_yylloc; |
| 75 | + next_token = base_yylex(); |
| 76 | + switch (next_token) |
| 77 | + { |
| 78 | + case FIRST_P: |
| 79 | + cur_token = NULLS_FIRST; |
| 80 | + break; |
| 81 | + case LAST_P: |
| 82 | + cur_token = NULLS_LAST; |
| 83 | + break; |
| 84 | + default: |
| 85 | + /* save the lookahead token for next time */ |
| 86 | + lookahead_token = next_token; |
| 87 | + lookahead_yylval = base_yylval; |
| 88 | + lookahead_yylloc = base_yylloc; |
| 89 | + have_lookahead = true; |
| 90 | + /* and back up the output info to cur_token */ |
| 91 | + base_yylval = cur_yylval; |
| 92 | + base_yylloc = cur_yylloc; |
| 93 | + break; |
| 94 | + } |
| 95 | + break; |
| 96 | + |
| 97 | + case WITH: |
| 98 | + /* |
| 99 | + * WITH CASCADED, LOCAL, or CHECK must be reduced to one token |
| 100 | + * |
| 101 | + * XXX an alternative way is to recognize just WITH_TIME and put |
| 102 | + * the ugliness into the datetime datatype productions instead of |
| 103 | + * WITH CHECK OPTION. However that requires promoting WITH to a |
| 104 | + * fully reserved word. If we ever have to do that anyway |
| 105 | + * (perhaps for SQL99 recursive queries), come back and simplify |
| 106 | + * this code. |
| 107 | + */ |
| 108 | + cur_yylval = base_yylval; |
| 109 | + cur_yylloc = base_yylloc; |
| 110 | + next_token = base_yylex(); |
| 111 | + switch (next_token) |
| 112 | + { |
| 113 | + case CASCADED: |
| 114 | + cur_token = WITH_CASCADED; |
| 115 | + break; |
| 116 | + case LOCAL: |
| 117 | + cur_token = WITH_LOCAL; |
| 118 | + break; |
| 119 | + case CHECK: |
| 120 | + cur_token = WITH_CHECK; |
| 121 | + break; |
| 122 | + default: |
| 123 | + /* save the lookahead token for next time */ |
| 124 | + lookahead_token = next_token; |
| 125 | + lookahead_yylval = base_yylval; |
| 126 | + lookahead_yylloc = base_yylloc; |
| 127 | + have_lookahead = true; |
| 128 | + /* and back up the output info to cur_token */ |
| 129 | + base_yylval = cur_yylval; |
| 130 | + base_yylloc = cur_yylloc; |
| 131 | + break; |
| 132 | + } |
| 133 | + break; |
| 134 | + |
| 135 | + default: |
| 136 | + break; |
| 137 | + } |
| 138 | + |
| 139 | + return cur_token; |
| 140 | +} |
0 commit comments