This repository has been archived by the owner on Apr 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.cpp
199 lines (170 loc) · 5.92 KB
/
json.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//===- tools/json/json.cpp ------------------------------------------------===//
//* _ *
//* (_)___ ___ _ __ *
//* | / __|/ _ \| '_ \ *
//* | \__ \ (_) | | | | *
//* _/ |___/\___/|_| |_| *
//* |__/ *
//===----------------------------------------------------------------------===//
//
// Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
// See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
// information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <array>
#include <fstream>
#include <iostream>
#include "pstore/command_line/tchar.hpp"
#include "pstore/dump/value.hpp"
#include "pstore/json/json.hpp"
#include "pstore/support/portab.hpp"
#include "pstore/support/utf.hpp"
using pstore::command_line::error_stream;
using pstore::command_line::out_stream;
namespace {
class yaml_output {
public:
using result_type = std::shared_ptr<pstore::dump::value>;
std::error_code null_value ();
std::error_code boolean_value (bool v);
std::error_code string_value (std::string const & s);
std::error_code int64_value (std::int64_t v);
std::error_code uint64_value (std::uint64_t v);
std::error_code double_value (double v);
std::error_code begin_array ();
std::error_code end_array ();
std::error_code begin_object ();
std::error_code key (std::string const & s);
std::error_code end_object ();
result_type result () const {
PSTORE_ASSERT (out_.size () == 1U);
return out_.top ();
}
private:
std::stack<result_type> out_;
};
std::error_code yaml_output::null_value () {
out_.emplace (new pstore::dump::null ());
return {};
}
std::error_code yaml_output::boolean_value (bool v) {
out_.emplace (new pstore::dump::boolean (v));
return {};
}
std::error_code yaml_output::string_value (std::string const & s) {
out_.emplace (new pstore::dump::string (s));
return {};
}
std::error_code yaml_output::int64_value (std::int64_t v) {
out_.emplace (pstore::dump::make_number (v));
return {};
}
std::error_code yaml_output::uint64_value (std::uint64_t v) {
out_.emplace (pstore::dump::make_number (v));
return {};
}
std::error_code yaml_output::double_value (double v) {
out_.emplace (pstore::dump::make_number (v));
return {};
}
std::error_code yaml_output::begin_array () {
out_.emplace (nullptr);
return {};
}
std::error_code yaml_output::end_array () {
pstore::dump::array::container content;
for (;;) {
auto v = out_.top ();
out_.pop ();
if (!v) {
break;
}
content.push_back (std::move (v));
}
std::reverse (std::begin (content), std::end (content));
out_.emplace (new pstore::dump::array (std::move (content)));
return {};
}
std::error_code yaml_output::begin_object () {
out_.emplace (nullptr);
return {};
}
std::error_code yaml_output::key (std::string const & s) {
string_value (s);
return {};
}
std::error_code yaml_output::end_object () {
auto object = std::make_shared<pstore::dump::object> ();
for (;;) {
auto value = out_.top ();
out_.pop ();
if (!value) {
break;
}
auto key = out_.top ();
out_.pop ();
PSTORE_ASSERT (key);
auto key_str = key->dynamic_cast_string ();
PSTORE_ASSERT (key_str);
object->insert (key_str->get (), value);
}
out_.emplace (std::move (object));
return {};
}
template <typename IStream>
int slurp (IStream & in) {
std::array<char, 256> buffer{{0}};
pstore::json::parser<yaml_output> p;
while ((in.rdstate () &
(std::ios_base::badbit | std::ios_base::failbit | std::ios_base::eofbit)) == 0 &&
!p.has_error ()) {
in.read (buffer.data (), buffer.size ());
p.input (pstore::gsl::make_span (buffer.data (),
std::max (in.gcount (), std::streamsize{0})));
}
p.eof ();
if (in.bad ()) {
std::cerr << "There was an I/O error while reading.\n";
return EXIT_FAILURE;
}
auto err = p.last_error ();
if (err) {
pstore::json::coord const position = p.coordinate ();
std::cerr << "Parse error: " << p.last_error ().message () << " (Line " << position.row
<< ", column " << position.column << ")\n";
return EXIT_FAILURE;
}
auto obj = p.callbacks ().result ();
out_stream << PSTORE_NATIVE_TEXT ("\n----\n") << *obj << PSTORE_NATIVE_TEXT ('\n');
return EXIT_SUCCESS;
}
} // end anonymous namespace
#ifdef _WIN32
int _tmain (int argc, TCHAR const * argv[]) {
#else
int main (int argc, char const * argv[]) {
#endif
int exit_code = EXIT_SUCCESS;
PSTORE_TRY {
if (argc < 2) {
exit_code = slurp (std::cin);
} else {
std::ifstream input (argv[1]);
exit_code = slurp (input);
}
}
// clang-format off
PSTORE_CATCH (std::exception const & ex, { // clang-format on
error_stream << PSTORE_NATIVE_TEXT ("Error: ") << pstore::utf::to_native_string (ex.what ())
<< PSTORE_NATIVE_TEXT ('\n');
exit_code = EXIT_FAILURE;
})
// clang-format off
PSTORE_CATCH (..., { // clang-format on
error_stream << PSTORE_NATIVE_TEXT ("Unknown exception.\n");
exit_code = EXIT_FAILURE;
})
return exit_code;
}