postgresql/src/backend/parser/gramparse.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
2.2 KiB
C
Raw Normal View History

1996-11-08 21:46:33 +01:00
/*-------------------------------------------------------------------------
*
* gramparse.h
* Shared definitions for the "raw" parser (flex and bison phases only)
*
* NOTE: this file is only meant to be included in the core parsing files,
* i.e., parser.c, gram.y, and scan.l.
* Definitions that are needed outside the core parser should be in parser.h.
*
1996-11-08 21:46:33 +01:00
*
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
1996-11-08 21:46:33 +01:00
*
* src/backend/parser/gramparse.h
1996-11-08 21:46:33 +01:00
*
*-------------------------------------------------------------------------
*/
#ifndef GRAMPARSE_H
#define GRAMPARSE_H
1996-11-08 21:46:33 +01:00
#include "nodes/parsenodes.h"
#include "parser/scanner.h"
/*
* NB: include gram.h only AFTER including scanner.h, because scanner.h
* is what #defines YYLTYPE.
*/
#include "gram.h"
/*
* The YY_EXTRA data that a flex scanner allows us to pass around. Private
* state needed for raw parsing/lexing goes here.
*/
typedef struct base_yy_extra_type
{
/*
* Fields used by the core scanner.
*/
core_yy_extra_type core_yy_extra;
/*
* State variables for base_yylex().
*/
bool have_lookahead; /* is lookahead info valid? */
int lookahead_token; /* one-token lookahead */
core_YYSTYPE lookahead_yylval; /* yylval for lookahead token */
YYLTYPE lookahead_yylloc; /* yylloc for lookahead token */
Improve parser's one-extra-token lookahead mechanism. There are a couple of places in our grammar that fail to be strict LALR(1), by requiring more than a single token of lookahead to decide what to do. Up to now we've dealt with that by using a filter between the lexer and parser that merges adjacent tokens into one in the places where two tokens of lookahead are necessary. But that creates a number of user-visible anomalies, for instance that you can't name a CTE "ordinality" because "WITH ordinality AS ..." triggers folding of WITH and ORDINALITY into one token. I realized that there's a better way. In this patch, we still do the lookahead basically as before, but we never merge the second token into the first; we replace just the first token by a special lookahead symbol when one of the lookahead pairs is seen. This requires a couple extra productions in the grammar, but it involves fewer special tokens, so that the grammar tables come out a bit smaller than before. The filter logic is no slower than before, perhaps a bit faster. I also fixed the filter logic so that when backing up after a lookahead, the current token's terminator is correctly restored; this eliminates some weird behavior in error message issuance, as is shown by the one change in existing regression test outputs. I believe that this patch entirely eliminates odd behaviors caused by lookahead for WITH. It doesn't really improve the situation for NULLS followed by FIRST/LAST unfortunately: those sequences still act like a reserved word, even though there are cases where they should be seen as two ordinary identifiers, eg "SELECT nulls first FROM ...". I experimented with additional grammar hacks but couldn't find any simple solution for that. Still, this is better than before, and it seems much more likely that we *could* somehow solve the NULLS case on the basis of this filter behavior than the previous one.
2015-02-24 23:53:42 +01:00
char *lookahead_end; /* end of current token */
char lookahead_hold_char; /* to be put back at *lookahead_end */
/*
* State variables that belong to the grammar.
*/
List *parsetree; /* final parse result is delivered here */
} base_yy_extra_type;
/*
* In principle we should use yyget_extra() to fetch the yyextra field
* from a yyscanner struct. However, flex always puts that field first,
* and this is sufficiently performance-critical to make it seem worth
* cheating a bit to use an inline macro.
*/
#define pg_yyget_extra(yyscanner) (*((base_yy_extra_type **) (yyscanner)))
/* from parser.c */
extern int base_yylex(YYSTYPE *lvalp, YYLTYPE *llocp,
core_yyscan_t yyscanner);
1996-11-08 21:46:33 +01:00
/* from gram.y */
extern void parser_init(base_yy_extra_type *yyext);
extern int base_yyparse(core_yyscan_t yyscanner);
1996-11-08 21:46:33 +01:00
#endif /* GRAMPARSE_H */