Skip to content

Commit ede5ea4

Browse files
committed
JSIL front-end: make parser and lexer reentrant
This avoids hand-crafted init functions with possibly insufficient guards against reentrant use and also removes (file-local) global variables.
1 parent 27ccfb7 commit ede5ea4

File tree

4 files changed

+164
-155
lines changed

4 files changed

+164
-155
lines changed

src/jsil/jsil_parser.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,16 @@ Author: Michael Tautschnig, [email protected]
1010
/// Jsil Language
1111

1212
#include "jsil_parser.h"
13+
14+
int yyjsillex_init_extra(jsil_parsert *, void **);
15+
int yyjsillex_destroy(void *);
16+
int yyjsilparse(jsil_parsert &, void *);
17+
18+
bool jsil_parsert::parse()
19+
{
20+
void *scanner;
21+
yyjsillex_init_extra(this, &scanner);
22+
bool parse_fail = yyjsilparse(*this, scanner) != 0;
23+
yyjsillex_destroy(scanner);
24+
return parse_fail;
25+
}

src/jsil/jsil_parser.h

+2-10
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ Author: Michael Tautschnig, [email protected]
1616

1717
#include "jsil_parse_tree.h"
1818

19-
class jsil_parsert;
20-
int yyjsilparse(jsil_parsert &);
21-
void jsil_scanner_init(jsil_parsert &);
22-
2319
class jsil_parsert:public parsert
2420
{
2521
public:
@@ -30,13 +26,9 @@ class jsil_parsert:public parsert
3026

3127
jsil_parse_treet parse_tree;
3228

33-
virtual bool parse() override
34-
{
35-
jsil_scanner_init(*this);
36-
return yyjsilparse(*this) != 0;
37-
}
29+
bool parse() override;
3830

39-
virtual void clear() override
31+
void clear() override
4032
{
4133
parsert::clear();
4234
parse_tree.clear();

src/jsil/parser.y

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
%{
22

33
// #define YYDEBUG 1
4-
#define PARSER (*jsil_parser)
4+
#define PARSER jsil_parser
55

66
#include "jsil_parser.h"
77

8-
int yyjsillex();
9-
extern char *yyjsiltext;
8+
int yyjsillex(unsigned *, void *);
9+
char *yyjsilget_text(void *);
1010

11-
static jsil_parsert *jsil_parser;
12-
int yyjsilparse(void);
13-
int yyjsilparse(jsil_parsert &_jsil_parser)
11+
int yyjsilerror(
12+
jsil_parsert &jsil_parser,
13+
void *scanner,
14+
const std::string &error)
1415
{
15-
jsil_parser = &_jsil_parser;
16-
return yyjsilparse();
17-
}
18-
19-
int yyjsilerror(const std::string &error)
20-
{
21-
jsil_parser->parse_error(error, yyjsiltext);
16+
jsil_parser.parse_error(error, yyjsilget_text(scanner));
2217
return 0;
2318
}
2419

@@ -44,9 +39,23 @@ int yyjsilerror(const std::string &error)
4439
#pragma warning(disable:4702)
4540
#endif
4641

47-
/*** token declaration **************************************************/
42+
// yynerrs may never be used. Will be fixed when bison > 3.8.2 is released (see
43+
// http://git.savannah.gnu.org/cgit/bison.git/commit/?id=a166d5450e3f47587b98f6005f9f5627dbe21a5b)
44+
#ifdef __clang__
45+
# pragma clang diagnostic ignored "-Wunused-but-set-variable"
46+
#endif
4847
%}
4948

49+
// Should be "%define api.pure full" instead of "%pure-parser", but macOS ships
50+
// bison 2.3, which doesn't yet support this. We have to invoke bison with
51+
// -Wno-deprecated on all platforms other than macOS.
52+
%pure-parser
53+
%parse-param {jsil_parsert &jsil_parser}
54+
%parse-param {void *scanner}
55+
%lex-param {void *scanner}
56+
57+
/*** token declaration **************************************************/
58+
5059
/*** special scanner reports ***/
5160

5261
%token TOK_SCANNER_ERROR /* used by scanner to report errors */

0 commit comments

Comments
 (0)