cpp-peglib/lint/peglint.cc

74 lines
1.7 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-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-19 02:18:20 +00:00
ifs.seekg(0, ios::beg).read(&buff[0], static_cast<streamsize>(buff.size()));
return true;
}
2015-02-13 00:54:43 +00:00
int main(int argc, const char** argv)
{
if (argc < 2 || string("--help") == argv[1]) {
cerr << "usage: peglint [grammar file path] [source file path]" << endl;
return 1;
}
// Check PEG grammar
2015-02-13 03:14:28 +00:00
auto syntax_path = argv[1];
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;
}
peglib::peg peg(syntax.data(), syntax.size(), [&](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-02-13 00:54:43 +00:00
});
if (!peg) {
2015-02-13 00:54:43 +00:00
return -1;
}
if (argc < 3) {
return 0;
}
// Check source
2015-02-13 03:14:28 +00:00
auto source_path = argv[2];
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-02-13 00:54:43 +00:00
cerr << "can't open the source file." << endl;
return -1;
}
auto ret = peg.lint(source.data(), source.size(), true, [&](size_t ln, size_t col, const string& msg) {
2015-02-13 03:14:28 +00:00
cerr << source_path << ":" << ln << ":" << col << ": " << msg << endl;
});
2015-02-13 00:54:43 +00:00
if (ret) {
peg.parse(source.data(), source.size());
}
2015-02-13 03:14:28 +00:00
return ret ? 0 : -1;
2015-02-13 00:54:43 +00:00
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix