-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (96 loc) · 3.47 KB
/
main.cpp
File metadata and controls
112 lines (96 loc) · 3.47 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
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
#include "antlr4-runtime.h"
#include "JSONLexer.h"
#include "JSONParser.h"
#include "CSTVisitor.h"
#include <iostream>
#include <fstream>
#include <string>
#include <any>
#include <map>
#include <vector>
#include <iomanip>
void printAny(const std::any& data, int indent = 0) {
if (!data.has_value()) {
std::cout << "null";
return;
}
const auto& type = data.type();
if (type == typeid(nullptr)) {
std::cout << "null";
} else if (type == typeid(bool)) {
std::cout << std::boolalpha << std::any_cast<bool>(data);
} else if (type == typeid(int32_t)) {
std::cout << std::any_cast<int32_t>(data);
} else if (type == typeid(int64_t)) {
std::cout << std::any_cast<int64_t>(data);
} else if (type == typeid(double)) {
std::cout << std::fixed << std::setprecision(6) << std::any_cast<double>(data);
std::cout.unsetf(std::ios::fixed); // 重置为默认格式
} else if (type == typeid(std::string)) {
std::cout << "\"" << std::any_cast<std::string>(data) << "\"";
} else if (type == typeid(std::vector<std::any>)) {
const auto& arr = std::any_cast<const std::vector<std::any>&>(data);
std::cout << "[\n";
for (size_t i = 0; i < arr.size(); ++i) {
std::cout << std::string(indent + 4, ' ');
printAny(arr[i], indent + 4);
if (i != arr.size() - 1) {
std::cout << ",";
}
std::cout << "\n";
}
std::cout << std::string(indent, ' ') << "]";
} else if (type == typeid(std::map<std::string, std::any>)) {
const auto& obj = std::any_cast<const std::map<std::string, std::any>&>(data);
std::cout << "{\n";
size_t count = 0;
for (const auto& [key, value] : obj) {
std::cout << std::string(indent + 4, ' ') << "\"" << key << "\": ";
printAny(value, indent + 4);
if (count != obj.size() - 1) {
std::cout << ",";
}
std::cout << "\n";
count++;
}
std::cout << std::string(indent, ' ') << "}";
} else {
std::cout << "<unknown type>";
}
}
int main(int argc, const char* argv[]) {
std::string input;
if (argc > 1) {
std::ifstream file(argv[1]);
if (!file.is_open()) {
std::cerr << " - Error: file " << argv[1] << "open failed." << std::endl;
return 1;
}
input = std::string((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
} else {
std::cout << "Input JSON content (Ctrl+D to end):\n";
input = std::string((std::istreambuf_iterator<char>(std::cin)),
std::istreambuf_iterator<char>());
}
if(input.empty()) {
std::cerr << " - Error: empty input stream." << std::endl;
return 1;
}
antlr4::ANTLRInputStream stream(input);
JSONLexer lexer(&stream);
antlr4::CommonTokenStream tokens(&lexer);
tokens.fill();
JSONParser parser(&tokens);
antlr4::tree::ParseTree* tree = parser.json();
if (parser.getNumberOfSyntaxErrors() > 0) {
std::cerr << "- Error: " << parser.getNumberOfSyntaxErrors() << " syntax error(s) in JSON." << std::endl;
return 1;
}
CSTVisitor visitor;
std::any result = visitor.visit(tree);
std::cout << "\nParse Result:\n";
printAny(result);
std::cout << std::endl;
return 0;
}