-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.cc
More file actions
82 lines (69 loc) · 1.56 KB
/
json.cc
File metadata and controls
82 lines (69 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <FlexLexer.h>
#include "json.h"
#include "json-grammar.tab.hh" // for yyparse
FlexLexer *lex_parser = nullptr;
json::json_object last_object;
json::json_array last_array;
json::json_stack curr_stack;
namespace json {
json::json(const char *filename, FlexLexer **lex) :
yyin(filename),
flex(new yyFlexLexer(yyin, std::cout))
{
*lex = flex;
::yyparse(last_object, last_array);
}
json::json(const std::string &filename) :
yyin(filename),
flex(new yyFlexLexer(yyin, std::cout))
{
::yyparse(last_object, last_array);
}
json::~json() {
if (yyin.is_open())
yyin.close();
if (flex != nullptr)
delete flex;
}
void json::load(std::istream &fin)
{
flex->switch_streams(fin, std::cout);
if (flex != nullptr)
delete flex;
flex = new yyFlexLexer(yyin, std::cout);
::yyparse(last_object, last_array);
}
}
int yylex(void)
{
if (lex_parser != nullptr)
return lex_parser->yylex();
else
return EOF;
}
void yyerror(json::json_object &jobj, json::json_array &array, const char *msg)
{
std::cerr << "Error: " << msg << '\n';
}
int main(int argc, char *argv[])
{
#if 0
if (argc < 2) {
fprintf(stderr, "Usage: %s json_file\n", argv[0]);
exit(1);
}
std::cout << "Filename: " << argv[1] << '\n';
#endif
json::json json(argv[1], &lex_parser);
using namespace std;
cout << "json parsed result:\n";
cout << "'";
if (!last_object.empty()) {
cout << last_object << '\n';
} else {
cout << last_array << '\n';
}
cout << "'\n";
return 0;
}