Added AST and command line string features in peglint.

This commit is contained in:
yhirose 2015-06-12 13:45:58 -04:00
parent 57a3ecc620
commit 1a9fddd3f8

View File

@ -26,13 +26,29 @@ bool read_file(const char* path, vector<char>& buff)
int main(int argc, const char** argv) int main(int argc, const char** argv)
{ {
if (argc < 2 || string("--help") == argv[1]) { auto opt_ast = false;
cerr << "usage: peglint [grammar file path] [source file path]" << endl; 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;
} else {
path_list.push_back(arg);
}
}
if (path_list.empty() || opt_help) {
cerr << "usage: peglint [--ast] [grammar file path] [source file path]" << endl;
return 1; return 1;
} }
// Check PEG grammar // Check PEG grammar
auto syntax_path = argv[1]; auto syntax_path = path_list[0];
vector<char> syntax; vector<char> syntax;
if (!read_file(syntax_path, syntax)) { if (!read_file(syntax_path, syntax)) {
@ -50,25 +66,38 @@ int main(int argc, const char** argv)
return -1; return -1;
} }
if (argc < 3) { if (path_list.size() < 2) {
return 0; return 0;
} }
// Check source // Check source
auto source_path = argv[2]; auto source_path = path_list[1];
vector<char> source; vector<char> source;
if (!read_file(source_path, source)) { if (!read_file(source_path, source)) {
cerr << "can't open the source file." << endl; auto beg = source_path;
return -1; auto end = source_path + strlen(source_path);
source.assign(beg, end);
source_path = "[commendline]";
} }
peg.log = [&](size_t ln, size_t col, const string& msg) { peg.log = [&](size_t ln, size_t col, const string& msg) {
cerr << source_path << ":" << ln << ":" << col << ": " << msg << endl; cerr << source_path << ":" << ln << ":" << col << ": " << msg << endl;
}; };
if (!peg.parse_n(source.data(), source.size())) { if (opt_ast) {
return -1; peg.enable_ast();
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;
}
} }
return 0; return 0;