Skip to content

Commit 7232457

Browse files
authored
Merge pull request #8138 from tautschnig/cleanup/statement_list_parsert
statement_list_parsert: construct with message handler
2 parents 689c552 + 1434e97 commit 7232457

File tree

5 files changed

+60
-40
lines changed

5 files changed

+60
-40
lines changed

src/statement-list/parser.y

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@
2323

2424
#include <iterator>
2525

26-
int yystatement_listlex();
27-
extern char *yystatement_listtext;
26+
int yystatement_listlex(void *);
27+
char *yystatement_listget_text(void *);
28+
29+
int yystatement_listerror(
30+
statement_list_parsert &statement_list_parser,
31+
void *scanner,
32+
const std::string &error)
33+
{
34+
statement_list_parser.parse_error(error, yystatement_listget_text(scanner));
35+
return 0;
36+
}
2837

2938
#define YYSTYPE unsigned
3039
#define YYSTYPE_IS_TRIVIAL 1
@@ -43,9 +52,13 @@ extern char *yystatement_listtext;
4352
// Disable warning for unreachable code.
4453
#pragma warning(disable:4702)
4554
#endif
55+
%}
56+
57+
%parse-param {statement_list_parsert &statement_list_parser}
58+
%parse-param {void *scanner}
59+
%lex-param {void *scanner}
4660

4761
/*** Token declaration *******************************************************/
48-
%}
4962

5063
/*** STL file structure keywords *********************************************/
5164
%token TOK_VERSION "VERSION"

src/statement-list/scanner.l

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static int isatty(int) { return 0; }
4141
#endif
4242

4343
// Value inside of statement_list_parser.h.
44-
#define PARSER statement_list_parser
44+
#define PARSER (*yyextra)
4545

4646
// Sets the type of yystatement_listlval so that it can be used as the stack
4747
// index.
@@ -58,22 +58,12 @@ static int isatty(int) { return 0; }
5858
#define loc() \
5959
{ newstack(yystatement_listlval); \
6060
PARSER.set_source_location(parser_stack(yystatement_listlval)); }
61-
62-
#ifdef STATEMENT_LIST_DEBUG
63-
extern int yystatement_listdebug;
64-
#endif
65-
void statement_list_scanner_init()
66-
{
67-
#ifdef STATEMENT_LIST_DEBUG
68-
yystatement_listdebug=1;
69-
#endif
70-
YY_FLUSH_BUFFER;
71-
BEGIN(0);
72-
}
7361
%}
7462
%option noyywrap
7563
%option noinput
7664
%option nounput
65+
%option reentrant
66+
%option extra-type="statement_list_parsert *"
7767

7868
%x GRAMMAR
7969
%x TAG_NAME

src/statement-list/statement_list_language.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@ bool statement_list_languaget::parse(
6060
const std::string &path,
6161
message_handlert &message_handler)
6262
{
63-
statement_list_parser.clear();
63+
statement_list_parsert statement_list_parser{message_handler};
6464
parse_path = path;
6565
statement_list_parser.set_line_no(0);
6666
statement_list_parser.set_file(path);
6767
statement_list_parser.in = &instream;
68-
statement_list_scanner_init();
6968
bool result = statement_list_parser.parse();
7069

7170
// store result

src/statement-list/statement_list_parser.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ Author: Matthias Weiss, [email protected]
2121
#include <iostream>
2222
#include <iterator>
2323

24-
statement_list_parsert statement_list_parser;
25-
26-
extern char *yystatement_listtext;
24+
int statement_list_parsert::instance_count = 0;
2725

2826
/// Searches for the name of the TIA module inside of its root
2927
/// expression.
@@ -335,15 +333,23 @@ void statement_list_parsert::add_function(const exprt &function)
335333
parse_tree.add_function(fn);
336334
}
337335

338-
bool statement_list_parsert::parse()
339-
{
340-
return yystatement_listparse() != 0;
341-
}
336+
int yystatement_listlex_init_extra(statement_list_parsert *, void **);
337+
int yystatement_listlex_destroy(void *);
338+
/// Defined in statement_list_y.tab.cpp. Main function for the parse process
339+
/// generated by bison, performs all necessary steps to fill the parse tree.
340+
int yystatement_listparse(statement_list_parsert &, void *);
341+
void yystatement_listset_debug(int, void *);
342342

343-
int yystatement_listerror(const std::string &error)
343+
bool statement_list_parsert::parse()
344344
{
345-
statement_list_parser.parse_error(error, yystatement_listtext);
346-
return 0;
345+
void *scanner;
346+
yystatement_listlex_init_extra(this, &scanner);
347+
#ifdef STATEMENT_LIST_DEBUG
348+
yystatement_listset_debug(1, scanner);
349+
#endif
350+
bool parse_fail = yystatement_listparse(*this, scanner) != 0;
351+
yystatement_listlex_destroy(scanner);
352+
return parse_fail;
347353
}
348354

349355
void statement_list_parsert::clear()

src/statement-list/statement_list_parser.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ Author: Matthias Weiss, [email protected]
1616

1717
#include "statement_list_parse_tree.h"
1818

19-
/// Defined in statement_list_y.tab.cpp. Main function for the parse process
20-
/// generated by bison, performs all necessary steps to fill the parse tree.
21-
int yystatement_listparse();
22-
2319
/// Responsible for starting the parse process and to translate the result into
2420
/// a statement_list_parse_treet. This parser works by using the expression
2521
/// stack of its base class. During the parse process, expressions with
@@ -34,6 +30,22 @@ int yystatement_listparse();
3430
class statement_list_parsert : public parsert
3531
{
3632
public:
33+
/// Constructor
34+
explicit statement_list_parsert(message_handlert &message_handler)
35+
: parsert(message_handler)
36+
{
37+
// Simplistic check that we don't attempt to do reentrant parsing as the
38+
// Bison-generated parser has global state.
39+
PRECONDITION(++instance_count == 1);
40+
}
41+
42+
statement_list_parsert(const statement_list_parsert &) = delete;
43+
44+
~statement_list_parsert() override
45+
{
46+
--instance_count;
47+
}
48+
3749
/// Starts the parsing process and saves the result inside of this instance's
3850
/// parse tree.
3951
/// \return False if successful.
@@ -69,19 +81,19 @@ class statement_list_parsert : public parsert
6981
private:
7082
/// Tree that is being filled by the parsing process.
7183
statement_list_parse_treet parse_tree;
72-
};
7384

74-
/// Instance of the parser, used by other modules.
75-
extern statement_list_parsert statement_list_parser;
85+
static int instance_count;
86+
};
7687

7788
/// Forwards any errors that are encountered during the parse process. This
7889
/// function gets called by the generated files of flex and bison.
90+
/// \param parser: Parser object.
91+
/// \param scanner: Lexer state.
7992
/// \param error: Error message.
8093
/// \return Always 0.
81-
int yystatement_listerror(const std::string &error);
82-
83-
/// Defined in scanner.l. This function initialises the scanner by setting
84-
/// debug flags (if present) and its initial state.
85-
void statement_list_scanner_init();
94+
int yystatement_listerror(
95+
statement_list_parsert &parser,
96+
void *scanner,
97+
const std::string &error);
8698

8799
#endif // CPROVER_STATEMENT_LIST_STATEMENT_LIST_PARSER_H

0 commit comments

Comments
 (0)