PostgreSQL Source Code  git master
gramparse.h File Reference
#include "nodes/parsenodes.h"
#include "parser/scanner.h"
#include "parser/gram.h"
Include dependency graph for gramparse.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  base_yy_extra_type
 

Macros

#define pg_yyget_extra(yyscanner)   (*((base_yy_extra_type **) (yyscanner)))
 

Typedefs

typedef struct base_yy_extra_type base_yy_extra_type
 

Functions

int base_yylex (YYSTYPE *lvalp, YYLTYPE *llocp, core_yyscan_t yyscanner)
 
void parser_init (base_yy_extra_type *yyext)
 
int base_yyparse (core_yyscan_t yyscanner)
 

Macro Definition Documentation

◆ pg_yyget_extra

#define pg_yyget_extra (   yyscanner)    (*((base_yy_extra_type **) (yyscanner)))

Definition at line 64 of file gramparse.h.

Referenced by base_yylex().

Typedef Documentation

◆ base_yy_extra_type

Function Documentation

◆ base_yylex()

int base_yylex ( YYSTYPE *  lvalp,
YYLTYPE llocp,
core_yyscan_t  yyscanner 
)

Definition at line 111 of file parser.c.

References Assert, check_uescapechar(), base_yy_extra_type::core_yy_extra, core_yylex(), base_yy_extra_type::have_lookahead, base_yy_extra_type::lookahead_end, base_yy_extra_type::lookahead_hold_char, base_yy_extra_type::lookahead_token, base_yy_extra_type::lookahead_yylloc, base_yy_extra_type::lookahead_yylval, next_token(), pg_yyget_extra, core_yy_extra_type::scanbuf, scanner_yyerror(), core_YYSTYPE::str, str_udeescape(), truncate_identifier(), YYLTYPE, and yyscanner.

Referenced by filtered_base_yylex().

112 {
114  int cur_token;
115  int next_token;
116  int cur_token_length;
117  YYLTYPE cur_yylloc;
118 
119  /* Get next token --- we might already have it */
120  if (yyextra->have_lookahead)
121  {
122  cur_token = yyextra->lookahead_token;
123  lvalp->core_yystype = yyextra->lookahead_yylval;
124  *llocp = yyextra->lookahead_yylloc;
125  if (yyextra->lookahead_end)
126  *(yyextra->lookahead_end) = yyextra->lookahead_hold_char;
127  yyextra->have_lookahead = false;
128  }
129  else
130  cur_token = core_yylex(&(lvalp->core_yystype), llocp, yyscanner);
131 
132  /*
133  * If this token isn't one that requires lookahead, just return it. If it
134  * does, determine the token length. (We could get that via strlen(), but
135  * since we have such a small set of possibilities, hardwiring seems
136  * feasible and more efficient --- at least for the fixed-length cases.)
137  */
138  switch (cur_token)
139  {
140  case NOT:
141  cur_token_length = 3;
142  break;
143  case NULLS_P:
144  cur_token_length = 5;
145  break;
146  case WITH:
147  cur_token_length = 4;
148  break;
149  case UIDENT:
150  case USCONST:
151  cur_token_length = strlen(yyextra->core_yy_extra.scanbuf + *llocp);
152  break;
153  default:
154  return cur_token;
155  }
156 
157  /*
158  * Identify end+1 of current token. core_yylex() has temporarily stored a
159  * '\0' here, and will undo that when we call it again. We need to redo
160  * it to fully revert the lookahead call for error reporting purposes.
161  */
162  yyextra->lookahead_end = yyextra->core_yy_extra.scanbuf +
163  *llocp + cur_token_length;
164  Assert(*(yyextra->lookahead_end) == '\0');
165 
166  /*
167  * Save and restore *llocp around the call. It might look like we could
168  * avoid this by just passing &lookahead_yylloc to core_yylex(), but that
169  * does not work because flex actually holds onto the last-passed pointer
170  * internally, and will use that for error reporting. We need any error
171  * reports to point to the current token, not the next one.
172  */
173  cur_yylloc = *llocp;
174 
175  /* Get next token, saving outputs into lookahead variables */
176  next_token = core_yylex(&(yyextra->lookahead_yylval), llocp, yyscanner);
177  yyextra->lookahead_token = next_token;
178  yyextra->lookahead_yylloc = *llocp;
179 
180  *llocp = cur_yylloc;
181 
182  /* Now revert the un-truncation of the current token */
183  yyextra->lookahead_hold_char = *(yyextra->lookahead_end);
184  *(yyextra->lookahead_end) = '\0';
185 
186  yyextra->have_lookahead = true;
187 
188  /* Replace cur_token if needed, based on lookahead */
189  switch (cur_token)
190  {
191  case NOT:
192  /* Replace NOT by NOT_LA if it's followed by BETWEEN, IN, etc */
193  switch (next_token)
194  {
195  case BETWEEN:
196  case IN_P:
197  case LIKE:
198  case ILIKE:
199  case SIMILAR:
200  cur_token = NOT_LA;
201  break;
202  }
203  break;
204 
205  case NULLS_P:
206  /* Replace NULLS_P by NULLS_LA if it's followed by FIRST or LAST */
207  switch (next_token)
208  {
209  case FIRST_P:
210  case LAST_P:
211  cur_token = NULLS_LA;
212  break;
213  }
214  break;
215 
216  case WITH:
217  /* Replace WITH by WITH_LA if it's followed by TIME or ORDINALITY */
218  switch (next_token)
219  {
220  case TIME:
221  case ORDINALITY:
222  cur_token = WITH_LA;
223  break;
224  }
225  break;
226 
227  case UIDENT:
228  case USCONST:
229  /* Look ahead for UESCAPE */
230  if (next_token == UESCAPE)
231  {
232  /* Yup, so get third token, which had better be SCONST */
233  const char *escstr;
234 
235  /* Again save and restore *llocp */
236  cur_yylloc = *llocp;
237 
238  /* Un-truncate current token so errors point to third token */
239  *(yyextra->lookahead_end) = yyextra->lookahead_hold_char;
240 
241  /* Get third token */
242  next_token = core_yylex(&(yyextra->lookahead_yylval),
243  llocp, yyscanner);
244 
245  /* If we throw error here, it will point to third token */
246  if (next_token != SCONST)
247  scanner_yyerror("UESCAPE must be followed by a simple string literal",
248  yyscanner);
249 
250  escstr = yyextra->lookahead_yylval.str;
251  if (strlen(escstr) != 1 || !check_uescapechar(escstr[0]))
252  scanner_yyerror("invalid Unicode escape character",
253  yyscanner);
254 
255  /* Now restore *llocp; errors will point to first token */
256  *llocp = cur_yylloc;
257 
258  /* Apply Unicode conversion */
259  lvalp->core_yystype.str =
260  str_udeescape(lvalp->core_yystype.str,
261  escstr[0],
262  *llocp,
263  yyscanner);
264 
265  /*
266  * We don't need to revert the un-truncation of UESCAPE. What
267  * we do want to do is clear have_lookahead, thereby consuming
268  * all three tokens.
269  */
270  yyextra->have_lookahead = false;
271  }
272  else
273  {
274  /* No UESCAPE, so convert using default escape character */
275  lvalp->core_yystype.str =
276  str_udeescape(lvalp->core_yystype.str,
277  '\\',
278  *llocp,
279  yyscanner);
280  }
281 
282  if (cur_token == UIDENT)
283  {
284  /* It's an identifier, so truncate as appropriate */
285  truncate_identifier(lvalp->core_yystype.str,
286  strlen(lvalp->core_yystype.str),
287  true);
288  cur_token = IDENT;
289  }
290  else if (cur_token == USCONST)
291  {
292  cur_token = SCONST;
293  }
294  break;
295  }
296 
297  return cur_token;
298 }
char * lookahead_end
Definition: gramparse.h:49
core_YYSTYPE lookahead_yylval
Definition: gramparse.h:47
core_yy_extra_type core_yy_extra
Definition: gramparse.h:40
static bool next_token(char **lineptr, char *buf, int bufsz, bool *initial_quote, bool *terminating_comma, int elevel, char **err_msg)
Definition: hba.c:203
char * scanbuf
Definition: scanner.h:72
static bool check_uescapechar(unsigned char escape)
Definition: parser.c:326
void scanner_yyerror(const char *message, core_yyscan_t yyscanner) pg_attribute_noreturn()
void truncate_identifier(char *ident, int len, bool warn)
Definition: scansup.c:93
static char * str_udeescape(const char *str, char escape, int position, core_yyscan_t yyscanner)
Definition: parser.c:346
#define YYLTYPE
Definition: scanner.h:44
int core_yylex(core_YYSTYPE *lvalp, YYLTYPE *llocp, core_yyscan_t yyscanner)
#define pg_yyget_extra(yyscanner)
Definition: gramparse.h:64
char * str
Definition: scanner.h:32
#define Assert(condition)
Definition: c.h:804
YYLTYPE lookahead_yylloc
Definition: gramparse.h:48
char lookahead_hold_char
Definition: gramparse.h:50
static core_yyscan_t yyscanner
Definition: pl_scanner.c:106

◆ base_yyparse()

int base_yyparse ( core_yyscan_t  yyscanner)

Referenced by main(), and raw_parser().

◆ parser_init()

void parser_init ( base_yy_extra_type yyext)

Referenced by raw_parser().