cpp-peglib/lint/peglint.cc

59 lines
1.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>
#include <iostream>
#include "mmap.h"
using namespace std;
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-13 03:14:28 +00:00
MemoryMappedFile syntax(syntax_path);
2015-02-13 00:54:43 +00:00
if (!syntax.is_open()) {
cerr << "can't open the grammar file." << endl;
return -1;
}
2015-02-14 15:13:10 +00:00
peglib::peg parser(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
});
2015-02-13 03:14:28 +00:00
if (!parser) {
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-13 03:14:28 +00:00
MemoryMappedFile source(source_path);
2015-02-13 00:54:43 +00:00
if (!source.is_open()) {
cerr << "can't open the source file." << endl;
return -1;
}
2015-02-13 03:14:28 +00:00
auto ret = parser.lint(source.data(), source.size(), true, [&](size_t ln, size_t col, const string& msg) {
cerr << source_path << ":" << ln << ":" << col << ": " << msg << endl;
});
2015-02-13 00:54:43 +00:00
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