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-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)
|
|
|
|
{
|
2015-06-12 17:45:58 +00:00
|
|
|
auto opt_ast = false;
|
2015-06-12 18:09:23 +00:00
|
|
|
auto opt_optimize_ast_nodes = false;
|
2015-06-12 17:45:58 +00:00
|
|
|
auto opt_help = false;
|
|
|
|
vector<const char*> path_list;
|
|
|
|
|
|
|
|
int 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-06-12 17:45:58 +00:00
|
|
|
} else {
|
|
|
|
path_list.push_back(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path_list.empty() || opt_help) {
|
2015-06-12 18:09:23 +00:00
|
|
|
cerr << "usage: peglint [--ast] [--optimize_ast_nodes|--opt] [grammar file path] [source file path]" << endl;
|
2015-02-13 00:54:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check PEG grammar
|
2015-06-12 17:45:58 +00:00
|
|
|
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-03-09 18:58:43 +00:00
|
|
|
peglib::peg peg;
|
|
|
|
|
|
|
|
peg.log = [&](size_t ln, size_t col, const string& 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-03-09 18:58:43 +00:00
|
|
|
if (!peg.load_grammar(syntax.data(), syntax.size())) {
|
2015-02-13 00:54:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-12 17:45:58 +00:00
|
|
|
if (path_list.size() < 2) {
|
2015-02-13 00:54:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check source
|
2015-06-12 17:45:58 +00:00
|
|
|
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-06-12 17:45: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-03-09 18:58:43 +00:00
|
|
|
peg.log = [&](size_t ln, size_t col, const string& 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-06-12 17:45:58 +00:00
|
|
|
if (opt_ast) {
|
2015-06-12 18:09:23 +00:00
|
|
|
peg.enable_ast(opt_optimize_ast_nodes);
|
2015-06-12 17:45:58 +00:00
|
|
|
|
|
|
|
std::shared_ptr<peglib::Ast> ast;
|
|
|
|
if (!peg.parse_n(source.data(), source.size(), ast)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
peglib::AstPrint().print(*ast);
|
|
|
|
} else {
|
|
|
|
if (!peg.parse_n(source.data(), source.size())) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-18 03:35:07 +00:00
|
|
|
}
|
|
|
|
|
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
|