-
Notifications
You must be signed in to change notification settings - Fork 108
/
peglint.cc
155 lines (128 loc) · 3.96 KB
/
peglint.cc
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
//
// peglint.cc
//
// Copyright (c) 2022 Yuji Hirose. All rights reserved.
// MIT License
//
#include <fstream>
#include <peglib.h>
#include <sstream>
using namespace std;
inline bool read_file(const char *path, vector<char> &buff) {
ifstream ifs(path, ios::in | ios::binary);
if (ifs.fail()) { return false; }
buff.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
if (!buff.empty()) {
ifs.seekg(0, ios::beg).read(&buff[0], static_cast<streamsize>(buff.size()));
}
return true;
}
inline vector<string> split(const string &s, char delim) {
vector<string> elems;
stringstream ss(s);
string elem;
while (getline(ss, elem, delim)) {
elems.push_back(elem);
}
return elems;
}
int main(int argc, const char **argv) {
auto opt_packrat = false;
auto opt_ast = false;
auto opt_optimize = false;
auto opt_mode = true;
auto opt_help = false;
auto opt_source = false;
vector<char> source;
auto opt_trace = false;
auto opt_verbose = false;
auto opt_profile = false;
vector<const char *> path_list;
auto argi = 1;
while (argi < argc) {
auto arg = argv[argi++];
if (string("--help") == arg) {
opt_help = true;
} else if (string("--packrat") == arg) {
opt_packrat = true;
} else if (string("--ast") == arg) {
opt_ast = true;
} else if (string("--opt") == arg || string("--opt-all") == arg) {
opt_optimize = true;
opt_mode = true;
} else if (string("--opt-only") == arg) {
opt_optimize = true;
opt_mode = false;
} else if (string("--source") == arg) {
opt_source = true;
if (argi < argc) {
std::string text = argv[argi++];
source.assign(text.begin(), text.end());
}
} else if (string("--trace") == arg) {
opt_trace = true;
} else if (string("--profile") == arg) {
opt_profile = true;
} else if (string("--verbose") == arg) {
opt_verbose = true;
} else {
path_list.push_back(arg);
}
}
if (path_list.empty() || opt_help) {
cerr << R"(usage: grammar_file_path [source_file_path]
options:
--source: source text
--packrat: enable packrat memoise
--ast: show AST tree
--opt, --opt-all: optimize all AST nodes except nodes selected with `no_ast_opt` instruction
--opt-only: optimize only AST nodes selected with `no_ast_opt` instruction
--trace: show concise trace messages
--profile: show profile report
--verbose: verbose output for trace and profile
)";
return 1;
}
// Check PEG grammar
auto syntax_path = path_list[0];
vector<char> syntax;
if (!read_file(syntax_path, syntax)) {
cerr << "can't open the grammar file." << endl;
return -1;
}
peg::parser parser;
parser.set_logger([&](size_t ln, size_t col, const string &msg) {
cerr << syntax_path << ":" << ln << ":" << col << ": " << msg << endl;
});
if (!parser.load_grammar(syntax.data(), syntax.size())) { return -1; }
if (path_list.size() < 2 && !opt_source) { return 0; }
// Check source
std::string source_path = "[commandline]";
if (path_list.size() >= 2) {
if (!read_file(path_list[1], source)) {
cerr << "can't open the code file." << endl;
return -1;
}
source_path = path_list[1];
}
parser.set_logger([&](size_t ln, size_t col, const string &msg) {
cerr << source_path << ":" << ln << ":" << col << ": " << msg << endl;
});
if (opt_packrat) { parser.enable_packrat_parsing(); }
if (opt_trace) { enable_tracing(parser, std::cout); }
if (opt_profile) { enable_profiling(parser, std::cout); }
parser.set_verbose_trace(opt_verbose);
if (opt_ast) {
parser.enable_ast();
std::shared_ptr<peg::Ast> ast;
auto ret = parser.parse_n(source.data(), source.size(), ast);
if (ast) {
if (opt_optimize) { ast = parser.optimize_ast(ast, opt_mode); }
std::cout << peg::ast_to_s(ast);
}
if (!ret) { return -1; }
} else {
if (!parser.parse_n(source.data(), source.size())) { return -1; }
}
return 0;
}