Skip to content

Commit

Permalink
json_parsert: construct with message handler
Browse files Browse the repository at this point in the history
This both avoids an object of static lifetime as well as it fixes the
(transitive) use of the deprecated messaget() constructor.
  • Loading branch information
tautschnig committed Dec 20, 2023
1 parent 856c641 commit c1b50c3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
11 changes: 8 additions & 3 deletions src/json/json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ Author: Daniel Kroening, [email protected]

#include <fstream>

json_parsert json_parser;
int yyjsonparse(json_parsert &);

bool json_parsert::parse()
{
return yyjsonparse(*this) != 0;

Check warning on line 17 in src/json/json_parser.cpp

View check run for this annotation

Codecov / codecov/patch

src/json/json_parser.cpp#L17

Added line #L17 was not covered by tests
}

// 'do it all' function
bool parse_json(
Expand All @@ -19,10 +24,10 @@ bool parse_json(
message_handlert &message_handler,
jsont &dest)
{
json_parser.clear();
json_parsert json_parser{message_handler};

json_parser.set_file(filename);
json_parser.in=&in;
json_parser.log.set_message_handler(message_handler);

bool result=json_parser.parse();

Expand Down
15 changes: 7 additions & 8 deletions src/json/json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ Author: Daniel Kroening, [email protected]
#include <util/parser.h>
#include <util/json.h>

int yyjsonparse();
void yyjsonrestart(FILE *input_file);

class json_parsert:public parsert
{
public:
explicit json_parsert(message_handlert &message_handler)
: parsert(message_handler)
{
}

typedef std::stack<jsont, std::vector<jsont> > stackt;
stackt stack;

jsont &top() { return stack.top(); }

virtual bool parse() override
{
return yyjsonparse()!=0;
}
bool parse() override;

void push(const jsont &x)
{
Expand All @@ -50,9 +51,7 @@ class json_parsert:public parsert
}
};

extern json_parsert json_parser;

int yyjsonerror(const std::string &error);
int yyjsonerror(json_parsert &, const std::string &error);

// 'do it all' functions
bool parse_json(
Expand Down
7 changes: 5 additions & 2 deletions src/json/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <util/unicode.h>

int yyjsonlex();
int yyjsonlex(json_parsert &json_parser);
extern char *yyjsontext;
extern int yyjsonleng; // really an int, not a size_t

Expand Down Expand Up @@ -75,14 +75,17 @@ static std::string convert_TOK_NUMBER()
return yyjsontext;
}

int yyjsonerror(const std::string &error)
int yyjsonerror(json_parsert &json_parser, const std::string &error)
{
json_parser.parse_error(error, yyjsontext);
return 0;
}

%}

%parse-param {json_parsert &json_parser}
%lex-param {json_parsert &json_parser}

%token TOK_STRING
%token TOK_NUMBER
%token TOK_TRUE
Expand Down
15 changes: 14 additions & 1 deletion src/json/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#pragma warning(disable:4005)
#endif

#define PARSER json_parser
#define PARSER (*json_parser)

#include "json_parser.h"
#include "json_y.tab.h"
Expand All @@ -33,6 +33,19 @@
#include <util/pragma_wnull_conversion.def> // IWYU pragma: keep
#include <util/pragma_wdeprecated_register.def> // IWYU pragma: keep

static json_parsert *json_parser;

int yyjsonlex(void);

int yyjsonlex(json_parsert &_json_parser)
{
// our scanner is not reentrant
PRECONDITION(!json_parser);
json_parser = &_json_parser;
int result = yyjsonlex();
json_parser = nullptr;
return result;
}
%}

string \"\"|\"{chars}\"
Expand Down

0 comments on commit c1b50c3

Please sign in to comment.