Changeset: ac00dcf01c75 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/ac00dcf01c75 Modified Files: monetdb5/modules/atoms/pg_jsonpath/jsonpath.h monetdb5/modules/atoms/pg_jsonpath/jsonpath_gram.y monetdb5/modules/atoms/pg_jsonpath/jsonpath_internal.h monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l Branch: json-extend Log Message:
make the parser reentrant, i.e. get rid of global variables diffs (truncated from 793 to 300 lines): diff --git a/monetdb5/modules/atoms/pg_jsonpath/jsonpath.h b/monetdb5/modules/atoms/pg_jsonpath/jsonpath.h --- a/monetdb5/modules/atoms/pg_jsonpath/jsonpath.h +++ b/monetdb5/modules/atoms/pg_jsonpath/jsonpath.h @@ -174,8 +174,6 @@ extern const char *jspOperationName(Json * Parsing support data structures. */ -extern struct Node * init_escontext(char* errmsg); - extern JsonPathParseResult *parsejsonpath(const char *str, int len, allocator* sa, char* errmsg); /* diff --git a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_gram.y b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_gram.y --- a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_gram.y +++ b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_gram.y @@ -15,6 +15,8 @@ */ +typedef void* yyscan_t; + #include "jsonpath_internal.h" static JsonPathParseItem *makeItemType(struct Node *escontext, JsonPathItemType type); @@ -50,9 +52,10 @@ static bool makeItemLikeRegex(JsonPathPa %} /* BISON Declarations */ -%pure-parser +%define api.pure full %expect 0 %name-prefix="jsonpath_yy" +%param {yyscan_t scanner} %parse-param {JsonPathParseResult **result} %parse-param {struct Node *escontext} %lex-param {JsonPathParseResult **result} diff --git a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_internal.h b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_internal.h --- a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_internal.h +++ b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_internal.h @@ -14,6 +14,10 @@ #ifndef JSONPATH_INTERNAL_H #define JSONPATH_INTERNAL_H +#include "postgres_defines.h" +#include "postgres_defines_internal.h" + +#include "jsonpath.h" /* struct JsonPathString is shared between scan and gram */ typedef struct JsonPathString @@ -23,19 +27,17 @@ typedef struct JsonPathString int total; } JsonPathString; -#include "jsonpath.h" -#include "postgres_defines_internal.h" #include "jsonpath_gram.h" -#define YY_USER_INIT (void) result; -#define YY_DECL extern int jsonpath_yylex(YYSTYPE *yylval_param, \ +#define YY_DECL extern int jsonpath_yylex(YYSTYPE *yylval_param, yyscan_t yyscanner, \ JsonPathParseResult **result, \ struct Node *escontext) YY_DECL; -extern int jsonpath_yyparse(JsonPathParseResult **result, - struct Node *escontext); -extern void jsonpath_yyerror(JsonPathParseResult **result, +extern void jsonpath_yyerror(yyscan_t yyscanner, JsonPathParseResult **result, struct Node *escontext, const char *message); +extern int jsonpath_yyparse(yyscan_t yyscanner, JsonPathParseResult **result, + struct Node *escontext); + #endif /* JSONPATH_INTERNAL_H */ diff --git a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l --- a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l +++ b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l @@ -19,29 +19,27 @@ * NB: include jsonpath_gram.h only AFTER including jsonpath_internal.h, * because jsonpath_internal.h contains the declaration for JsonPathString. */ -#include "jsonpath_internal.h" -#include "jsonpath_gram.h" + +/* struct JsonPathString is shared between scan and gram */ } %{ -static JsonPathString scanstring; -/* Handles to the buffer that the lexer uses internally */ -static YY_BUFFER_STATE scanbufhandle; -static char *scanbuf; -static int scanbuflen; +#include "jsonpath_internal.h" -static void addstring(bool init, char *s, int l); -static void addchar(bool init, char c); -static enum yytokentype checkKeyword(void); -static bool parseUnicode(char *s, int l, struct Node *escontext); -static bool parseHexChar(char *s, struct Node *escontext); - -#define palloc GDKmalloc -#define repalloc GDKrealloc -#define pfree GDKfree +static void addstring(yyscan_t yyscanner, bool init, char *s, int l); +static void addchar(yyscan_t yyscanner, bool init, char c); +static enum yytokentype checkKeyword(yyscan_t yyscanner); +static bool parseUnicode(yyscan_t yyscanner, char *s, int l, struct Node *escontext); +static bool parseHexChar(yyscan_t yyscanner, char *s, struct Node *escontext); /* LCOV_EXCL_START */ +#define YY_USER_INIT (void) result; + +typedef struct extra_data { + JsonPathString scanstring; +} extra_data; + %} @@ -57,7 +55,8 @@ static bool parseHexChar(char *s, struct %option noyyalloc %option noyyrealloc %option noyyfree - +%option reentrant +%option extra-type="extra_data *" /* * We use exclusive states for quoted and non-quoted strings, * quoted variable names and C-style comments. @@ -108,63 +107,63 @@ hex_fail \\x{hexdigit}{0,1} %% <xnq>{other}+ { - addstring(false, yytext, yyleng); + addstring(yyscanner, false, yytext, yyleng); } <xnq>{blank}+ { - yylval->str = scanstring; + yylval->str = yyextra->scanstring; BEGIN INITIAL; - return checkKeyword(); + return checkKeyword(yyscanner); } <xnq>\/\* { - yylval->str = scanstring; + yylval->str = yyextra->scanstring; BEGIN xc; } <xnq>({special}|\") { - yylval->str = scanstring; + yylval->str = yyextra->scanstring; yyless(0); BEGIN INITIAL; - return checkKeyword(); + return checkKeyword(yyscanner); } <xnq><<EOF>> { - yylval->str = scanstring; + yylval->str = yyextra->scanstring; BEGIN INITIAL; - return checkKeyword(); + return checkKeyword(yyscanner); } -<xnq,xq,xvq>\\b { addchar(false, '\b'); } +<xnq,xq,xvq>\\b { addchar(yyscanner, false, '\b'); } -<xnq,xq,xvq>\\f { addchar(false, '\f'); } +<xnq,xq,xvq>\\f { addchar(yyscanner, false, '\f'); } -<xnq,xq,xvq>\\n { addchar(false, '\n'); } +<xnq,xq,xvq>\\n { addchar(yyscanner, false, '\n'); } -<xnq,xq,xvq>\\r { addchar(false, '\r'); } +<xnq,xq,xvq>\\r { addchar(yyscanner, false, '\r'); } -<xnq,xq,xvq>\\t { addchar(false, '\t'); } +<xnq,xq,xvq>\\t { addchar(yyscanner, false, '\t'); } -<xnq,xq,xvq>\\v { addchar(false, '\v'); } +<xnq,xq,xvq>\\v { addchar(yyscanner, false, '\v'); } <xnq,xq,xvq>{unicode}+ { - if (!parseUnicode(yytext, yyleng, escontext)) + if (!parseUnicode(yyscanner, yytext, yyleng, escontext)) yyterminate(); } <xnq,xq,xvq>{hex_char} { - if (!parseHexChar(yytext, escontext)) + if (!parseHexChar(yyscanner, yytext, escontext)) yyterminate(); } <xnq,xq,xvq>{unicode}*{unicodefail} { - jsonpath_yyerror(NULL, escontext, + jsonpath_yyerror(yyscanner, NULL, escontext, "invalid Unicode escape sequence"); yyterminate(); } <xnq,xq,xvq>{hex_fail} { - jsonpath_yyerror(NULL, escontext, + jsonpath_yyerror(yyscanner, NULL, escontext, "invalid hexadecimal character sequence"); yyterminate(); } @@ -172,37 +171,37 @@ hex_fail \\x{hexdigit}{0,1} <xnq,xq,xvq>{unicode}+\\ { /* throw back the \\, and treat as unicode */ yyless(yyleng - 1); - if (!parseUnicode(yytext, yyleng, escontext)) + if (!parseUnicode(yyscanner, yytext, yyleng, escontext)) yyterminate(); } -<xnq,xq,xvq>\\. { addchar(false, yytext[1]); } +<xnq,xq,xvq>\\. { addchar(yyscanner, false, yytext[1]); } <xnq,xq,xvq>\\ { - jsonpath_yyerror(NULL, escontext, + jsonpath_yyerror(yyscanner, NULL, escontext, "unexpected end after backslash"); yyterminate(); } <xq,xvq><<EOF>> { - jsonpath_yyerror(NULL, escontext, + jsonpath_yyerror(yyscanner, NULL, escontext, "unterminated quoted string"); yyterminate(); } <xq>\" { - yylval->str = scanstring; + yylval->str = yyextra->scanstring; BEGIN INITIAL; return STRING_P; } <xvq>\" { - yylval->str = scanstring; + yylval->str = yyextra->scanstring; BEGIN INITIAL; return VARIABLE_P; } -<xq,xvq>[^\\\"]+ { addstring(false, yytext, yyleng); } +<xq,xvq>[^\\\"]+ { addstring(yyscanner, false, yytext, yyleng); } <xc>\*\/ { BEGIN INITIAL; } @@ -211,7 +210,7 @@ hex_fail \\x{hexdigit}{0,1} <xc>\* { } <xc><<EOF>> { - jsonpath_yyerror( + jsonpath_yyerror(yyscanner, NULL, escontext, "unexpected end of comment"); yyterminate(); @@ -239,14 +238,14 @@ hex_fail \\x{hexdigit}{0,1} \> { return GREATER_P; } \${other}+ { - addstring(true, yytext + 1, yyleng - 1); - addchar(false, '\0'); - yylval->str = scanstring; + addstring(yyscanner, true, yytext + 1, yyleng - 1); + addchar(yyscanner, false, '\0'); + yylval->str = yyextra->scanstring; return VARIABLE_P; } \$\" { - addchar(true, '\0'); + addchar(yyscanner, true, '\0'); BEGIN xvq; } @@ -255,89 +254,89 @@ hex_fail \\x{hexdigit}{0,1} {blank}+ { /* ignore */ } \/\* { - addchar(true, '\0'); + addchar(yyscanner, true, '\0'); BEGIN xc; } {real} { - addstring(true, yytext, yyleng); - addchar(false, '\0'); - yylval->str = scanstring; _______________________________________________ checkin-list mailing list -- checkin-list@monetdb.org To unsubscribe send an email to checkin-list-le...@monetdb.org