From 19c6c2615c0265f917746a2c75fd6cb39388fb9f Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 25 May 2020 17:31:22 -0400 Subject: [PATCH] Name refactoring --- README.md | 18 ++++++++++++++---- docs/index.html | 6 +++--- docs/index.js | 20 ++++++++++---------- docs/native.cpp | 8 ++++---- lint/peglint.cc | 39 +++++++++++++++++++++++---------------- peglib.h | 16 ++++++++-------- 6 files changed, 62 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 20aa76c..5fbd213 100644 --- a/README.md +++ b/README.md @@ -422,10 +422,12 @@ parser.enable_ast(); shared_ptr ast; if (parser.parse("...", ast)) { - cout << peg::ast_to_s(ast); + cout << peg::ast_to_s(ast); - ast = peg::AstOptimizer(true).optimize(ast); - cout << peg::ast_to_s(ast); + std::vector exceptions = { "defenition1", "defenition2 }; + ast = peg::AstOptimizer(true, exceptions).optimize(ast); + + cout << peg::ast_to_s(ast); } ``` @@ -529,7 +531,15 @@ peglint - PEG syntax lint utility > cmake .. > make > ./peglint -usage: peglint [--ast] [--optimize_ast_nodes|--opt] [--source text] [--trace] [grammar file path] [source file path] +usage: grammar_file_path [source_file_path] + + options: + --ast: show AST tree + --opt: optimaze all AST nodes except nodes selected with --opt-rules + --opt-only: optimaze only AST nodes selected with --opt-rules + --opt-rules rules: comma delimitted definition rules for optimazation + --source: source text + --trace: show trace messages ``` ### Lint grammar diff --git a/docs/index.html b/docs/index.html index 4588dcc..b7bcde2 100644 --- a/docs/index.html +++ b/docs/index.html @@ -22,9 +22,9 @@
{{source}}
AST

-    
Optimized AST      - On:    - Filters:  +
Optimized AST     + mode:    + rules: 

     
diff --git a/docs/index.js b/docs/index.js index b34b229..2e0e44b 100644 --- a/docs/index.js +++ b/docs/index.js @@ -27,8 +27,8 @@ codeAstOptimized.setOptions({ }) codeAstOptimized.renderer.$cursorLayer.element.style.opacity=0; -$('#optimize').prop('checked', localStorage.getItem('optimize') == 'true'); -$('#filters').val(localStorage.getItem('filters')); +$('#opt_mode').val(localStorage.getItem('optimazationMode')); +$('#opt_rules').val(localStorage.getItem('optimazationRules')); function generateErrorListHTML(errors) { let html = '
    '; @@ -51,14 +51,13 @@ function parse() { const $codeInfo = $('#code-info'); const codeText = code.getValue(); - const optimize = $('#optimize').prop('checked'); - const filters = $('#filters').val(); - console.log(optimize, filters); + const optimazationMode = $('#opt_mode').val(); + const optimazationRules = $('#opt_rules').val(); localStorage.setItem('grammarText', grammarText); localStorage.setItem('codeText', codeText); - localStorage.setItem('optimize', optimize); - localStorage.setItem('filters', filters); + localStorage.setItem('optimazationMode', optimazationMode); + localStorage.setItem('optimazationRules', optimazationRules); $grammarInfo.html(''); $grammarValidation.hide(); @@ -71,7 +70,8 @@ function parse() { return; } - const data = JSON.parse(Module.lint(grammarText, codeText, optimize, filters)); + const mode = optimazationMode == 'all'; + const data = JSON.parse(Module.lint(grammarText, codeText, mode, optimazationRules)); const isValid = data.grammar.length === 0; if (isValid) { @@ -115,8 +115,8 @@ $('#grammar-info').on('click', 'li', makeOnClickInInfo(grammar)); $('#code-info').on('click', 'li', makeOnClickInInfo(code)); // Event handing in the AST optimazation -$('#optimize').on('click', setupTimer); -$('#filters').on('keyup', setupTimer); +$('#opt_mode').on('change', setupTimer); +$('#opt_rules').on('keyup', setupTimer); // Show page $('#main').css({ diff --git a/docs/native.cpp b/docs/native.cpp index b889639..db37a8f 100644 --- a/docs/native.cpp +++ b/docs/native.cpp @@ -54,7 +54,7 @@ bool parse_code(const std::string &text, peg::parser &peg, std::string &json, return ret; } -inline std::vector splitFiltersText(const std::string &s) { +inline std::vector splitRulesText(const std::string &s) { std::vector elems; std::stringstream ss(s); std::string elem; @@ -67,7 +67,7 @@ inline std::vector splitFiltersText(const std::string &s) { } std::string lint(const std::string &grammarText, const std::string &codeText, - bool optimize, const std::string &filtersText) { + bool opt_mode, const std::string &opt_rules_text) { std::string grammarResult; std::string codeResult; std::string astResult; @@ -80,9 +80,9 @@ std::string lint(const std::string &grammarText, const std::string &codeText, std::shared_ptr ast; if (parse_code(codeText, peg, codeResult, ast)) { astResult = escape_json(peg::ast_to_s(ast)); - auto filters = splitFiltersText(filtersText); + auto rules = splitRulesText(opt_rules_text); astResultOptimized = escape_json( - peg::ast_to_s(peg::AstOptimizer(optimize, filters).optimize(ast))); + peg::ast_to_s(peg::AstOptimizer(opt_mode, rules).optimize(ast))); } } diff --git a/lint/peglint.cc b/lint/peglint.cc index a141b3b..f8a77ff 100644 --- a/lint/peglint.cc +++ b/lint/peglint.cc @@ -34,8 +34,9 @@ inline vector split(const string &s, char delim) { int main(int argc, const char **argv) { auto opt_ast = false; - auto opt_optimize_ast_nodes = false; - vector optimize_ast_nodes_filters; + auto opt_optimize = false; + auto opt_mode = true; + vector opt_rules; auto opt_help = false; auto opt_source = false; vector source; @@ -49,12 +50,16 @@ int main(int argc, const char **argv) { opt_help = true; } else if (string("--ast") == arg) { opt_ast = true; - } else if (string("--opt") == arg) { - opt_optimize_ast_nodes = true; - } else if (string("--filters") == arg) { + } else if (string("--opt-all") == arg) { + opt_optimize = true; + opt_mode = true; + } else if (string("--opt-only") == arg) { + opt_optimize = true; + opt_mode = false; + } else if (string("--opt-rules") == arg) { if (argi < argc) { std::string s = argv[argi++]; - optimize_ast_nodes_filters = split(s, ','); + opt_rules = split(s, ','); } } else if (string("--source") == arg) { opt_source = true; @@ -73,12 +78,16 @@ int main(int argc, const char **argv) { cerr << "usage: grammar_file_path [source_file_path]" << endl << endl << " options:" << endl - << " --ast: show AST tree" << endl - << " --opt: optimaze AST nodes" << endl - << " --filters definition_names: comma delimitted definition names " - "for optimazation" - << endl << " --source: source text" << endl + << " --ast: show AST tree" << endl + << " --opt-all: optimaze all AST nodes except nodes selected with " + "--opt-rules" + << endl + << " --opt-only: optimaze only AST nodes selected with --opt-rules" + << endl + << " --opt-rules rules: CSV definition rules to adjust AST " + "optimazation" + << endl << " --trace: show trace messages" << endl; return 1; } @@ -164,12 +173,10 @@ int main(int argc, const char **argv) { std::shared_ptr ast; if (!parser.parse_n(source.data(), source.size(), ast)) { return -1; } - std::cout << "filters..." << std::endl; - for (auto name : optimize_ast_nodes_filters) { - std::cout << name << std::endl; + if (opt_optimize) { + ast = peg::AstOptimizer(opt_mode, opt_rules).optimize(ast); } - ast = peg::AstOptimizer(opt_optimize_ast_nodes, optimize_ast_nodes_filters) - .optimize(ast); + std::cout << peg::ast_to_s(ast); } else { diff --git a/peglib.h b/peglib.h index 6369e61..f76d93f 100644 --- a/peglib.h +++ b/peglib.h @@ -3615,16 +3615,16 @@ ast_to_s(const std::shared_ptr &ptr, } struct AstOptimizer { - AstOptimizer(bool optimize_nodes, - const std::vector &filters = {}) - : optimize_nodes_(optimize_nodes), filters_(filters) {} + AstOptimizer(bool mode, + const std::vector &rules = {}) + : mode_(mode), rules_(rules) {} template std::shared_ptr optimize(std::shared_ptr original, std::shared_ptr parent = nullptr) { - auto found = std::find(filters_.begin(), filters_.end(), original->name) != - filters_.end(); - bool opt = optimize_nodes_ ? !found : found; + auto found = std::find(rules_.begin(), rules_.end(), original->name) != + rules_.end(); + bool opt = mode_ ? !found : found; if (opt && original->nodes.size() == 1) { auto child = optimize(original->nodes[0], parent); @@ -3644,8 +3644,8 @@ struct AstOptimizer { } private: - const bool optimize_nodes_; - const std::vector filters_; + const bool mode_; + const std::vector rules_; }; struct EmptyType {};