blob: 4ed0e84869350af088c3a313046c0f9af9d0445a (
plain)
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
|
/* broken, i don't understand c++ templates */
#include <cctype>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <variant>
#include <vector>
using namespace std;
struct El
{
variant<unique_ptr<vector<El>>, int> v;
template<typename T>
El(T v)
{
this->v = v;
}
void
print()
{
if (holds_alternative<int>(v)) {
cout << get<int>(v);
} else {
cout << "[";
for (auto &e : *get<unique_ptr<vector<El>>>(v)) {
e.print();
cout << ",";
}
cout << "]";
}
}
};
El
parse(istream &s)
{
char c = s.get();
if (c == '[') {
auto v = make_unique<vector<El>>();
for (;;) {
v->push_back(parse(s));
do {
c = s.get();
} while (isspace(c));
if (c == ']') break;
if (c != ',') throw invalid_argument("array sep");
}
return El(move(v));
}
if (isdigit(c)) {
int v;
s.putback(c);
s >> v;
return El(v);
}
throw invalid_argument("unexpected char");
}
int
main()
{
El a = parse(cin);
a.print();
}
|