Added --source option

This commit is contained in:
yhirose 2018-08-02 20:51:11 -04:00
parent fa5ffd8211
commit f46737acbf

View File

@ -31,6 +31,8 @@ int main(int argc, const char** argv)
auto opt_ast = false; auto opt_ast = false;
auto opt_optimize_ast_nodes = false; auto opt_optimize_ast_nodes = false;
auto opt_help = false; auto opt_help = false;
auto opt_source = false;
vector<char> source;
auto opt_server = false; auto opt_server = false;
int port = 1234; int port = 1234;
auto opt_trace = false; auto opt_trace = false;
@ -45,6 +47,12 @@ int main(int argc, const char** argv)
opt_ast = true; opt_ast = true;
} else if (string("--optimize_ast_nodes") == arg || string("--opt") == arg) { } else if (string("--optimize_ast_nodes") == arg || string("--opt") == arg) {
opt_optimize_ast_nodes = true; opt_optimize_ast_nodes = true;
} else if (string("--source") == arg) {
opt_source = true;
if (argi < argc) {
std::string text = argv[argi++];
source.assign(text.begin(), text.end());
}
} else if (string("--server") == arg) { } else if (string("--server") == arg) {
opt_server = true; opt_server = true;
if (argi < argc) { if (argi < argc) {
@ -58,14 +66,13 @@ int main(int argc, const char** argv)
} }
if ((path_list.empty() && !opt_server) || opt_help) { if ((path_list.empty() && !opt_server) || opt_help) {
cerr << "usage: peglint [--ast] [--optimize_ast_nodes|--opt] [--server [PORT]] [--trace] [grammar file path] [source file path]" << endl; cerr << "usage: peglint [--ast] [--optimize_ast_nodes|--opt] [--source text] [--server [PORT]] [--trace] [grammar file path] [source file path]" << endl;
return 1; return 1;
} }
// Sever mode // Sever mode
if (opt_server) { if (opt_server) {
vector<char> syntax; vector<char> syntax;
vector<char> source;
if (path_list.size() >= 1 && !read_file(path_list[0], syntax)) { if (path_list.size() >= 1 && !read_file(path_list[0], syntax)) {
cerr << "can't open the grammar file." << endl; cerr << "can't open the grammar file." << endl;
@ -96,22 +103,22 @@ int main(int argc, const char** argv)
}; };
if (!parser.load_grammar(syntax.data(), syntax.size())) { if (!parser.load_grammar(syntax.data(), syntax.size())) {
cerr << "can't open the grammar file." << endl;
return -1; return -1;
} }
if (path_list.size() < 2) { if (path_list.size() < 2 && !opt_source) {
return 0; return 0;
} }
// Check source // Check source
auto source_path = path_list[1]; std::string source_path = "[commendline]";
if (path_list.size() >= 2) {
vector<char> source; if (!read_file(path_list[1], source)) {
if (!read_file(source_path, source)) { cerr << "can't open the code file." << endl;
auto beg = source_path; return -1;
auto end = source_path + strlen(source_path); }
source.assign(beg, end); source_path = path_list[1];
source_path = "[commendline]";
} }
parser.log = [&](size_t ln, size_t col, const string& msg) { parser.log = [&](size_t ln, size_t col, const string& msg) {