From 341659f6999ea8a1708117a379f94096e3d9176e Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 12 Jun 2015 14:09:23 -0400 Subject: [PATCH] Added optimize_nodes parameter. --- language/parser.cc | 2 +- lint/peglint.cc | 7 +++++-- peglib.h | 18 +++++++++--------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/language/parser.cc b/language/parser.cc index 5ebf5e4..3eef436 100644 --- a/language/parser.cc +++ b/language/parser.cc @@ -73,7 +73,7 @@ peg& get_parser() throw logic_error("invalid peg grammar"); } - parser.enable_ast({ + parser.enable_ast(true, { /* Definition, Tag Optimize ---------------------- ------------------ ---------- */ diff --git a/lint/peglint.cc b/lint/peglint.cc index e6cbbf1..98283fb 100644 --- a/lint/peglint.cc +++ b/lint/peglint.cc @@ -27,6 +27,7 @@ bool read_file(const char* path, vector& buff) int main(int argc, const char** argv) { auto opt_ast = false; + auto opt_optimize_ast_nodes = false; auto opt_help = false; vector path_list; @@ -37,13 +38,15 @@ int main(int argc, const char** argv) opt_help = true; } else if (string("--ast") == arg) { opt_ast = true; + } else if (string("--optimize_ast_nodes") == arg || string("--opt") == arg) { + opt_optimize_ast_nodes = true; } else { path_list.push_back(arg); } } if (path_list.empty() || opt_help) { - cerr << "usage: peglint [--ast] [grammar file path] [source file path]" << endl; + cerr << "usage: peglint [--ast] [--optimize_ast_nodes|--opt] [grammar file path] [source file path]" << endl; return 1; } @@ -86,7 +89,7 @@ int main(int argc, const char** argv) }; if (opt_ast) { - peg.enable_ast(); + peg.enable_ast(opt_optimize_ast_nodes); std::shared_ptr ast; if (!peg.parse_n(source.data(), source.size(), ast)) { diff --git a/peglib.h b/peglib.h index 21cdd59..09aa655 100644 --- a/peglib.h +++ b/peglib.h @@ -2041,19 +2041,19 @@ public: struct AstNodeInfo { const char* name; int tag; // TODO: It should be calculated at compile-time from 'name' with constexpr hash function. - bool optimize; + bool optimize_nodes; }; - peg& enable_ast(std::initializer_list list) { + peg& enable_ast(bool optimize_nodes, std::initializer_list list) { for (const auto& info: list) { ast_node(info); } - ast_end(); + ast_end(optimize_nodes); return *this; } - peg& enable_ast() { - ast_end(); + peg& enable_ast(bool optimize_nodes = true) { + ast_end(optimize_nodes); return *this; } @@ -2080,7 +2080,7 @@ private: if (is_token) { return std::make_shared(info.name, info.tag, std::string(sv.s, sv.n)); } - if (info.optimize && sv.size() == 1) { + if (info.optimize_nodes && sv.size() == 1) { std::shared_ptr ast = sv[0].get>(); return ast; } @@ -2088,18 +2088,18 @@ private: }; } - void ast_end() { + void ast_end(bool optimize_nodes) { for (auto& x: *grammar_) { const auto& name = x.first; auto& rule = x.second; auto& action = rule.actions.front(); if (!action) { auto is_token = rule.is_token; - action = [name, is_token](const SemanticValues& sv) { + action = [=](const SemanticValues& sv) { if (is_token) { return std::make_shared(name.c_str(), AstDefaultTag, std::string(sv.s, sv.n)); } - if (sv.size() == 1) { + if (optimize_nodes && sv.size() == 1) { std::shared_ptr ast = sv[0].get>(); return ast; }