cpp-peglib/lint/peglint.cc

161 lines
4.3 KiB
C++
Raw Normal View History

2015-02-13 00:54:43 +00:00
//
// peglint.cc
//
// Copyright (c) 2015 Yuji Hirose. All rights reserved.
// MIT License
//
#include <peglib.h>
2015-02-19 02:18:20 +00:00
#include <fstream>
2015-02-13 00:54:43 +00:00
using namespace std;
2015-11-27 19:24:24 +00:00
int run_server(int port, const vector<char>& syntax, const vector<char>& source);
2015-02-19 02:18:20 +00:00
bool read_file(const char* path, vector<char>& buff)
{
ifstream ifs(path, ios::in | ios::binary);
if (ifs.fail()) {
return false;
}
2015-02-20 00:52:00 +00:00
buff.resize(static_cast<unsigned int>(ifs.seekg(0, ios::end).tellg()));
2015-02-26 00:29:42 +00:00
if (!buff.empty()) {
ifs.seekg(0, ios::beg).read(&buff[0], static_cast<streamsize>(buff.size()));
}
2015-02-19 02:18:20 +00:00
return true;
}
2015-02-13 00:54:43 +00:00
int main(int argc, const char** argv)
{
auto opt_ast = false;
2015-06-12 18:09:23 +00:00
auto opt_optimize_ast_nodes = false;
auto opt_help = false;
2015-11-27 19:24:24 +00:00
auto opt_server = false;
int port = 1234;
2015-11-30 04:07:02 +00:00
auto opt_trace = false;
vector<const char*> path_list;
2015-07-30 01:48:44 +00:00
auto argi = 1;
while (argi < argc) {
auto arg = argv[argi++];
if (string("--help") == arg) {
opt_help = true;
} else if (string("--ast") == arg) {
opt_ast = true;
2015-06-12 18:09:23 +00:00
} else if (string("--optimize_ast_nodes") == arg || string("--opt") == arg) {
opt_optimize_ast_nodes = true;
2015-11-27 19:24:24 +00:00
} else if (string("--server") == arg) {
opt_server = true;
if (argi < argc) {
port = std::stoi(argv[argi++]);
}
2015-11-30 04:07:02 +00:00
} else if (string("--trace") == arg) {
opt_trace = true;
} else {
path_list.push_back(arg);
}
}
2015-11-27 19:24:24 +00:00
if ((path_list.empty() && !opt_server) || opt_help) {
2015-11-30 04:19:09 +00:00
cerr << "usage: peglint [--ast] [--optimize_ast_nodes|--opt] [--server [PORT]] [--trace] [grammar file path] [source file path]" << endl;
2015-02-13 00:54:43 +00:00
return 1;
}
2015-11-27 19:24:24 +00:00
// Sever mode
if (opt_server) {
vector<char> syntax;
vector<char> source;
if (path_list.size() >= 1 && !read_file(path_list[0], syntax)) {
cerr << "can't open the grammar file." << endl;
return -1;
}
if (path_list.size() >= 2 && !read_file(path_list[1], source)) {
cerr << "can't open the code file." << endl;
return -1;
}
return run_server(port, syntax, source);
}
2015-02-13 00:54:43 +00:00
// Check PEG grammar
auto syntax_path = path_list[0];
2015-02-13 00:54:43 +00:00
2015-02-19 02:18:20 +00:00
vector<char> syntax;
if (!read_file(syntax_path, syntax)) {
2015-02-13 00:54:43 +00:00
cerr << "can't open the grammar file." << endl;
return -1;
}
2015-08-10 20:37:56 +00:00
peg::parser parser;
2015-03-09 18:58:43 +00:00
2015-08-10 20:37:56 +00:00
parser.log = [&](auto ln, auto col, const auto& msg) {
2015-02-13 03:14:28 +00:00
cerr << syntax_path << ":" << ln << ":" << col << ": " << msg << endl;
2015-03-09 18:58:43 +00:00
};
2015-02-13 00:54:43 +00:00
2015-08-10 20:37:56 +00:00
if (!parser.load_grammar(syntax.data(), syntax.size())) {
2015-02-13 00:54:43 +00:00
return -1;
}
if (path_list.size() < 2) {
2015-02-13 00:54:43 +00:00
return 0;
}
// Check source
auto source_path = path_list[1];
2015-02-13 00:54:43 +00:00
2015-02-19 02:18:20 +00:00
vector<char> source;
if (!read_file(source_path, source)) {
2015-07-30 11:52:58 +00:00
auto beg = source_path;
auto end = source_path + strlen(source_path);
source.assign(beg, end);
source_path = "[commendline]";
2015-02-13 00:54:43 +00:00
}
2015-08-10 20:37:56 +00:00
parser.log = [&](auto ln, auto col, const auto& msg) {
2015-03-11 21:49:08 +00:00
cerr << source_path << ":" << ln << ":" << col << ": " << msg << endl;
2015-03-09 18:58:43 +00:00
};
2015-02-13 00:54:43 +00:00
2015-11-30 04:07:02 +00:00
if (opt_trace) {
std::cout << "pos:lev\trule/ope" << std::endl;
std::cout << "-------\t--------" << std::endl;
size_t prev_pos = 0;
parser.enable_trace([&](auto name, auto s, auto /*n*/, auto& /*sv*/, auto& c, auto& /*dt*/) {
2016-05-25 07:18:41 +00:00
auto pos = static_cast<size_t>(s - c.s);
2015-11-30 04:07:02 +00:00
auto backtrack = (pos < prev_pos ? "*" : "");
string indent;
auto level = c.nest_level;
while (level--) {
indent += " ";
}
std::cout
<< pos << ":" << c.nest_level << backtrack << "\t"
<< indent << name << std::endl;
2016-05-25 07:18:41 +00:00
prev_pos = static_cast<size_t>(pos);
2015-11-30 04:07:02 +00:00
});
}
if (opt_ast) {
2015-08-10 20:37:56 +00:00
parser.enable_ast();
2015-08-10 20:37:56 +00:00
std::shared_ptr<peg::Ast> ast;
if (!parser.parse_n(source.data(), source.size(), ast)) {
return -1;
}
2015-08-10 20:37:56 +00:00
ast = peg::AstOptimizer(opt_optimize_ast_nodes).optimize(ast);
2015-11-27 20:32:17 +00:00
std::cout << peg::ast_to_s(ast);
} else {
2015-08-10 20:37:56 +00:00
if (!parser.parse_n(source.data(), source.size())) {
return -1;
}
}
2015-03-09 18:58:43 +00:00
return 0;
2015-02-13 00:54:43 +00:00
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix