-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
159 lines (145 loc) · 4.16 KB
/
parse.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <minjsoncpp.h>
#include <iostream>
#include <iterator>
using namespace std::string_view_literals;
// auxiliary stuff defined below
std::string readStdin();
void print(const minjson::Value &value);
void printIssues(std::string_view input, const minjson::ParsingResult::Issues &issues);
int main(int argc, const char *argv[]) {
if (argc <= 1) {
const auto string = R"({
"decimal": 3.14,
"null": null,
"boolean": true,
"integer": 42,
"string": "hello there",
"array": [ 1, 2, 3 ],
"object": {
"nested number": 23,
"nested string": "General Kenobi",
"nested array": [ 4, 5, 6 ],
"nested object": {
"foo": "bar"
}
}
})"sv;
// parsing JSON
const auto value = minjson::parse(string).value;
std::cout << "parsed JSON:\n\n";
print(value);
std::cout << '\n';
return 0;
}
std::string_view input;
// read stdin if '-i' switch is provided in the command line
std::string cin;
if (argv[1] == "-i"sv) {
cin = readStdin();
input = cin; // using input from stdin to parse
} else {
input = argv[1]; // using command line argument to parse
}
// parsing input JSON
const auto [value, status, parsedSize, issues] = minjson::parse(input);
// 'status' tells if the parsing succeeded
// 'parsedSize' tells how many bytes are successfully parsed from the input
// 'issues' contain information about different issues and errors encountered during parsing
switch (status) {
case minjson::ParsingResultStatus::Success:
break;
case minjson::ParsingResultStatus::PartialSuccess:
std::cerr << "partial success";
printIssues(input, issues);
std::cerr << '\n';
break;
case minjson::ParsingResultStatus::Failure:
std::cerr <<
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
"\x1b[91m"
#endif
"failure"
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
"\x1B[0m"
#endif
;
printIssues(input, issues);
std::cerr << '\n';
return 1;
}
std::cout << "parsed JSON:\n\n";
print(value);
std::cout << '\n';
}
// auxiliary stuff
std::string readStdin() {
std::cin >> std::noskipws;
return std::string{ std::istream_iterator<char>{ std::cin }, std::istream_iterator<char>{} };
}
void print(const minjson::Value &value) {
minjson::SerializationOptions options;
options.indent = 2;
options.objectKeyValueSeparator = ": "sv;
minjson::serializeToStream(std::cout, value, options);
}
size_t printChar(char c) {
constexpr auto hexDigits = "0123456789ABCDEF"sv;
if ('\0' <= c && c < '\x20') {
std::cerr <<
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
"\x1b[4m"
#endif
"\\x" << hexDigits[static_cast<uint8_t>(c) >> 4]
<< hexDigits[c & '\xf'];
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
std::cerr << "\x1B[0m";
#endif
return 4;
} else {
std::cerr << c;
return 1;
}
}
size_t printString(std::string_view s) {
size_t size = 0;
for (char c : s) {
size += printChar(c);
}
return size;
}
void printIssues(std::string_view input, const minjson::ParsingResult::Issues &issues) {
if (issues.empty())
return;
std::cerr << "\n*** issues: ***";
for (const auto &i : issues) {
std::cerr << "\n " << i.description;
// also 'i.code' can be used to distinguish issues and errors
if (i.offset != input.size())
std::cerr << " @ " << i.offset;
std::cerr << "\n ";
const size_t start = std::max(i.offset, size_t{ 9 }) - 9;
if (start > 0)
std::cerr << "...";
size_t offset = printString(input.substr(start, i.offset - start));
const size_t count = std::min(i.offset + 9, input.size()) - i.offset;
if (i.offset != input.size()) {
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
std::cerr << "\x1b[91m";
#endif
printChar(input[i.offset]);
#if defined(USE_TERMINAL_COLOR_SEQUENCES)
std::cerr << "\x1B[0m";
#endif
printString(input.substr(i.offset + 1, count - 1));
if (i.offset + count < input.size())
std::cerr << "...";
}
std::cerr << '\n';
for (; offset; --offset)
std::cerr << ' ';
if (start > 0)
std::cerr << " ";
std::cerr << " ^";
}
std::cerr << "\n***************\n";
}