mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-02-17 19:30:00 +08:00
syncrep parser: pure parser and reentrant scanner
Use the flex %option reentrant and the bison option %pure-parser to make the generated scanner and parser pure, reentrant, and thread-safe. Make the generated scanner use palloc() etc. instead of malloc() etc. Previously, we only used palloc() for the buffer, but flex would still use malloc() for its internal structures. Now, all the memory is under palloc() control. Simplify flex scan buffer management: Instead of constructing the buffer from pieces and then using yy_scan_buffer(), we can just use yy_scan_string(), which does the same thing internally. The previous code was necessary because we allocated the buffer with palloc() and the rest of the state was handled by malloc(). But this is no longer the case; everything is under palloc() now. Use flex yyextra to handle context information, instead of global variables. This complements the other changes to make the scanner reentrant. Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Reviewed-by: Andreas Karlsson <andreas@proxel.se> Discussion: https://www.postgresql.org/message-id/flat/eb6faeac-2a8a-4b69-9189-c33c520e5b7b@eisentraut.org
This commit is contained in:
parent
e4a8fb8fef
commit
db6856c991
@ -11,7 +11,7 @@ GETTEXT_TRIGGERS = $(BACKEND_COMMON_GETTEXT_TRIGGERS) \
|
||||
parser_yyerror \
|
||||
replication_yyerror:2 \
|
||||
scanner_yyerror \
|
||||
syncrep_yyerror \
|
||||
syncrep_yyerror:2 \
|
||||
report_invalid_record:2 \
|
||||
ereport_startup_progress \
|
||||
json_token_error:2 \
|
||||
|
@ -992,6 +992,7 @@ check_synchronous_standby_names(char **newval, void **extra, GucSource source)
|
||||
{
|
||||
if (*newval != NULL && (*newval)[0] != '\0')
|
||||
{
|
||||
yyscan_t scanner;
|
||||
int parse_rc;
|
||||
SyncRepConfigData *pconf;
|
||||
|
||||
@ -1000,9 +1001,9 @@ check_synchronous_standby_names(char **newval, void **extra, GucSource source)
|
||||
syncrep_parse_error_msg = NULL;
|
||||
|
||||
/* Parse the synchronous_standby_names string */
|
||||
syncrep_scanner_init(*newval);
|
||||
parse_rc = syncrep_yyparse();
|
||||
syncrep_scanner_finish();
|
||||
syncrep_scanner_init(*newval, &scanner);
|
||||
parse_rc = syncrep_yyparse(scanner);
|
||||
syncrep_scanner_finish(scanner);
|
||||
|
||||
if (parse_rc != 0 || syncrep_parse_result == NULL)
|
||||
{
|
||||
|
@ -26,10 +26,6 @@ char *syncrep_parse_error_msg;
|
||||
static SyncRepConfigData *create_syncrep_config(const char *num_sync,
|
||||
List *members, uint8 syncrep_method);
|
||||
|
||||
/* silence -Wmissing-variable-declarations */
|
||||
extern int syncrep_yychar;
|
||||
extern int syncrep_yynerrs;
|
||||
|
||||
/*
|
||||
* Bison doesn't allocate anything that needs to live across parser calls,
|
||||
* so we can easily have it use palloc instead of malloc. This prevents
|
||||
@ -40,6 +36,9 @@ extern int syncrep_yynerrs;
|
||||
|
||||
%}
|
||||
|
||||
%parse-param {yyscan_t yyscanner}
|
||||
%lex-param {yyscan_t yyscanner}
|
||||
%pure-parser
|
||||
%expect 0
|
||||
%name-prefix="syncrep_yy"
|
||||
|
||||
@ -60,7 +59,10 @@ extern int syncrep_yynerrs;
|
||||
|
||||
%%
|
||||
result:
|
||||
standby_config { syncrep_parse_result = $1; }
|
||||
standby_config {
|
||||
syncrep_parse_result = $1;
|
||||
(void) yynerrs; /* suppress compiler warning */
|
||||
}
|
||||
;
|
||||
|
||||
standby_config:
|
||||
|
@ -37,21 +37,27 @@ fprintf_to_ereport(const char *fmt, const char *msg)
|
||||
ereport(ERROR, (errmsg_internal("%s", msg)));
|
||||
}
|
||||
|
||||
/* Handles to the buffer that the lexer uses internally */
|
||||
static YY_BUFFER_STATE scanbufhandle;
|
||||
|
||||
static StringInfoData xdbuf;
|
||||
struct syncrep_yy_extra_type
|
||||
{
|
||||
StringInfoData xdbuf;
|
||||
};
|
||||
#define YY_EXTRA_TYPE struct syncrep_yy_extra_type *
|
||||
|
||||
/* LCOV_EXCL_START */
|
||||
|
||||
%}
|
||||
|
||||
%option reentrant
|
||||
%option bison-bridge
|
||||
%option 8bit
|
||||
%option never-interactive
|
||||
%option nodefault
|
||||
%option noinput
|
||||
%option nounput
|
||||
%option noyywrap
|
||||
%option noyyalloc
|
||||
%option noyyrealloc
|
||||
%option noyyfree
|
||||
%option warn
|
||||
%option prefix="syncrep_yy"
|
||||
|
||||
@ -82,38 +88,38 @@ xdinside [^"]+
|
||||
[Ff][Ii][Rr][Ss][Tt] { return FIRST; }
|
||||
|
||||
{xdstart} {
|
||||
initStringInfo(&xdbuf);
|
||||
initStringInfo(&yyextra->xdbuf);
|
||||
BEGIN(xd);
|
||||
}
|
||||
<xd>{xddouble} {
|
||||
appendStringInfoChar(&xdbuf, '"');
|
||||
appendStringInfoChar(&yyextra->xdbuf, '"');
|
||||
}
|
||||
<xd>{xdinside} {
|
||||
appendStringInfoString(&xdbuf, yytext);
|
||||
appendStringInfoString(&yyextra->xdbuf, yytext);
|
||||
}
|
||||
<xd>{xdstop} {
|
||||
syncrep_yylval.str = xdbuf.data;
|
||||
xdbuf.data = NULL;
|
||||
yylval->str = yyextra->xdbuf.data;
|
||||
yyextra->xdbuf.data = NULL;
|
||||
BEGIN(INITIAL);
|
||||
return NAME;
|
||||
}
|
||||
<xd><<EOF>> {
|
||||
syncrep_yyerror("unterminated quoted identifier");
|
||||
syncrep_yyerror(yyscanner, "unterminated quoted identifier");
|
||||
return JUNK;
|
||||
}
|
||||
|
||||
{identifier} {
|
||||
syncrep_yylval.str = pstrdup(yytext);
|
||||
yylval->str = pstrdup(yytext);
|
||||
return NAME;
|
||||
}
|
||||
|
||||
{digit}+ {
|
||||
syncrep_yylval.str = pstrdup(yytext);
|
||||
yylval->str = pstrdup(yytext);
|
||||
return NUM;
|
||||
}
|
||||
|
||||
"*" {
|
||||
syncrep_yylval.str = "*";
|
||||
yylval->str = "*";
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@ -126,10 +132,16 @@ xdinside [^"]+
|
||||
|
||||
/* LCOV_EXCL_STOP */
|
||||
|
||||
/* see scan.l */
|
||||
#undef yyextra
|
||||
#define yyextra (((struct yyguts_t *) yyscanner)->yyextra_r)
|
||||
|
||||
/* Needs to be here for access to yytext */
|
||||
void
|
||||
syncrep_yyerror(const char *message)
|
||||
syncrep_yyerror(yyscan_t yyscanner, const char *message)
|
||||
{
|
||||
struct yyguts_t * yyg = (struct yyguts_t *) yyscanner; /* needed for yytext macro */
|
||||
|
||||
/* report only the first error in a parse operation */
|
||||
if (syncrep_parse_error_msg)
|
||||
return;
|
||||
@ -142,32 +154,51 @@ syncrep_yyerror(const char *message)
|
||||
}
|
||||
|
||||
void
|
||||
syncrep_scanner_init(const char *str)
|
||||
syncrep_scanner_init(const char *str, yyscan_t *yyscannerp)
|
||||
{
|
||||
Size slen = strlen(str);
|
||||
char *scanbuf;
|
||||
yyscan_t yyscanner;
|
||||
struct syncrep_yy_extra_type *yyext = palloc0_object(struct syncrep_yy_extra_type);
|
||||
|
||||
/*
|
||||
* Might be left over after ereport()
|
||||
*/
|
||||
if (YY_CURRENT_BUFFER)
|
||||
yy_delete_buffer(YY_CURRENT_BUFFER);
|
||||
if (yylex_init(yyscannerp) != 0)
|
||||
elog(ERROR, "yylex_init() failed: %m");
|
||||
|
||||
/*
|
||||
* Make a scan buffer with special termination needed by flex.
|
||||
*/
|
||||
scanbuf = (char *) palloc(slen + 2);
|
||||
memcpy(scanbuf, str, slen);
|
||||
scanbuf[slen] = scanbuf[slen + 1] = YY_END_OF_BUFFER_CHAR;
|
||||
scanbufhandle = yy_scan_buffer(scanbuf, slen + 2);
|
||||
yyscanner = *yyscannerp;
|
||||
|
||||
/* Make sure we start in proper state */
|
||||
BEGIN(INITIAL);
|
||||
yyset_extra(yyext, yyscanner);
|
||||
|
||||
yy_scan_string(str, yyscanner);
|
||||
}
|
||||
|
||||
void
|
||||
syncrep_scanner_finish(void)
|
||||
syncrep_scanner_finish(yyscan_t yyscanner)
|
||||
{
|
||||
yy_delete_buffer(scanbufhandle);
|
||||
scanbufhandle = NULL;
|
||||
pfree(yyextra);
|
||||
yylex_destroy(yyscanner);
|
||||
}
|
||||
|
||||
/*
|
||||
* Interface functions to make flex use palloc() instead of malloc().
|
||||
* It'd be better to make these static, but flex insists otherwise.
|
||||
*/
|
||||
|
||||
void *
|
||||
yyalloc(yy_size_t size, yyscan_t yyscanner)
|
||||
{
|
||||
return palloc(size);
|
||||
}
|
||||
|
||||
void *
|
||||
yyrealloc(void *ptr, yy_size_t size, yyscan_t yyscanner)
|
||||
{
|
||||
if (ptr)
|
||||
return repalloc(ptr, size);
|
||||
else
|
||||
return palloc(size);
|
||||
}
|
||||
|
||||
void
|
||||
yyfree(void *ptr, yyscan_t yyscanner)
|
||||
{
|
||||
if (ptr)
|
||||
pfree(ptr);
|
||||
}
|
||||
|
@ -100,10 +100,15 @@ extern void SyncRepUpdateSyncStandbysDefined(void);
|
||||
* Internal functions for parsing synchronous_standby_names grammar,
|
||||
* in syncrep_gram.y and syncrep_scanner.l
|
||||
*/
|
||||
extern int syncrep_yyparse(void);
|
||||
extern int syncrep_yylex(void);
|
||||
extern void syncrep_yyerror(const char *str);
|
||||
extern void syncrep_scanner_init(const char *str);
|
||||
extern void syncrep_scanner_finish(void);
|
||||
union YYSTYPE;
|
||||
#ifndef YY_TYPEDEF_YY_SCANNER_T
|
||||
#define YY_TYPEDEF_YY_SCANNER_T
|
||||
typedef void *yyscan_t;
|
||||
#endif
|
||||
extern int syncrep_yyparse(yyscan_t yyscanner);
|
||||
extern int syncrep_yylex(union YYSTYPE *yylval_param, yyscan_t yyscanner);
|
||||
extern void syncrep_yyerror(yyscan_t yyscanner, const char *str);
|
||||
extern void syncrep_scanner_init(const char *str, yyscan_t *yyscannerp);
|
||||
extern void syncrep_scanner_finish(yyscan_t yyscanner);
|
||||
|
||||
#endif /* _SYNCREP_H */
|
||||
|
Loading…
Reference in New Issue
Block a user