Name refactoring

This commit is contained in:
yhirose 2020-05-25 17:31:22 -04:00
parent 370067f013
commit 19c6c2615c
6 changed files with 62 additions and 45 deletions

View File

@ -424,7 +424,9 @@ shared_ptr<peg::Ast> ast;
if (parser.parse("...", ast)) { if (parser.parse("...", ast)) {
cout << peg::ast_to_s(ast); cout << peg::ast_to_s(ast);
ast = peg::AstOptimizer(true).optimize(ast); std::vector<std::string> exceptions = { "defenition1", "defenition2 };
ast = peg::AstOptimizer(true, exceptions).optimize(ast);
cout << peg::ast_to_s(ast); cout << peg::ast_to_s(ast);
} }
``` ```
@ -529,7 +531,15 @@ peglint - PEG syntax lint utility
> cmake .. > cmake ..
> make > make
> ./peglint > ./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 ### Lint grammar

View File

@ -22,9 +22,9 @@
<pre id="code-editor" class="editor-area">{{source}}</pre> <pre id="code-editor" class="editor-area">{{source}}</pre>
<div class="editor-sub-header">AST</div> <div class="editor-sub-header">AST</div>
<pre id="code-ast" class="editor-area"></pre> <pre id="code-ast" class="editor-area"></pre>
<div class="editor-sub-header">Optimized AST&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <div class="editor-sub-header">Optimized AST&nbsp;&nbsp;&nbsp;&nbsp;
On:&nbsp;<input id="optimize" type="checkbox"></input>&nbsp;&nbsp; mode:&nbsp;<select id="opt_mode" type="checkbox"><option value="all">All</option><option value="only">Only</option></select>&nbsp;&nbsp;
Filters:&nbsp;<input id="filters" type="text" size="48" placeholder="Enter definition names to filter delimited by comma..."></input> rules:&nbsp;<input id="opt_rules" type="text" size="40" placeholder="Enter definition rules separated by comma."></input>
</div> </div>
<pre id="code-ast-optimized" class="editor-area"></pre> <pre id="code-ast-optimized" class="editor-area"></pre>
<div id="code-info" class="editor-info"></div> <div id="code-info" class="editor-info"></div>

View File

@ -27,8 +27,8 @@ codeAstOptimized.setOptions({
}) })
codeAstOptimized.renderer.$cursorLayer.element.style.opacity=0; codeAstOptimized.renderer.$cursorLayer.element.style.opacity=0;
$('#optimize').prop('checked', localStorage.getItem('optimize') == 'true'); $('#opt_mode').val(localStorage.getItem('optimazationMode'));
$('#filters').val(localStorage.getItem('filters')); $('#opt_rules').val(localStorage.getItem('optimazationRules'));
function generateErrorListHTML(errors) { function generateErrorListHTML(errors) {
let html = '<ul>'; let html = '<ul>';
@ -51,14 +51,13 @@ function parse() {
const $codeInfo = $('#code-info'); const $codeInfo = $('#code-info');
const codeText = code.getValue(); const codeText = code.getValue();
const optimize = $('#optimize').prop('checked'); const optimazationMode = $('#opt_mode').val();
const filters = $('#filters').val(); const optimazationRules = $('#opt_rules').val();
console.log(optimize, filters);
localStorage.setItem('grammarText', grammarText); localStorage.setItem('grammarText', grammarText);
localStorage.setItem('codeText', codeText); localStorage.setItem('codeText', codeText);
localStorage.setItem('optimize', optimize); localStorage.setItem('optimazationMode', optimazationMode);
localStorage.setItem('filters', filters); localStorage.setItem('optimazationRules', optimazationRules);
$grammarInfo.html(''); $grammarInfo.html('');
$grammarValidation.hide(); $grammarValidation.hide();
@ -71,7 +70,8 @@ function parse() {
return; 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; const isValid = data.grammar.length === 0;
if (isValid) { if (isValid) {
@ -115,8 +115,8 @@ $('#grammar-info').on('click', 'li', makeOnClickInInfo(grammar));
$('#code-info').on('click', 'li', makeOnClickInInfo(code)); $('#code-info').on('click', 'li', makeOnClickInInfo(code));
// Event handing in the AST optimazation // Event handing in the AST optimazation
$('#optimize').on('click', setupTimer); $('#opt_mode').on('change', setupTimer);
$('#filters').on('keyup', setupTimer); $('#opt_rules').on('keyup', setupTimer);
// Show page // Show page
$('#main').css({ $('#main').css({

View File

@ -54,7 +54,7 @@ bool parse_code(const std::string &text, peg::parser &peg, std::string &json,
return ret; return ret;
} }
inline std::vector<std::string> splitFiltersText(const std::string &s) { inline std::vector<std::string> splitRulesText(const std::string &s) {
std::vector<std::string> elems; std::vector<std::string> elems;
std::stringstream ss(s); std::stringstream ss(s);
std::string elem; std::string elem;
@ -67,7 +67,7 @@ inline std::vector<std::string> splitFiltersText(const std::string &s) {
} }
std::string lint(const std::string &grammarText, const std::string &codeText, 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 grammarResult;
std::string codeResult; std::string codeResult;
std::string astResult; std::string astResult;
@ -80,9 +80,9 @@ std::string lint(const std::string &grammarText, const std::string &codeText,
std::shared_ptr<peg::Ast> ast; std::shared_ptr<peg::Ast> ast;
if (parse_code(codeText, peg, codeResult, ast)) { if (parse_code(codeText, peg, codeResult, ast)) {
astResult = escape_json(peg::ast_to_s(ast)); astResult = escape_json(peg::ast_to_s(ast));
auto filters = splitFiltersText(filtersText); auto rules = splitRulesText(opt_rules_text);
astResultOptimized = escape_json( astResultOptimized = escape_json(
peg::ast_to_s(peg::AstOptimizer(optimize, filters).optimize(ast))); peg::ast_to_s(peg::AstOptimizer(opt_mode, rules).optimize(ast)));
} }
} }

View File

@ -34,8 +34,9 @@ inline vector<string> split(const string &s, char delim) {
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
auto opt_ast = false; auto opt_ast = false;
auto opt_optimize_ast_nodes = false; auto opt_optimize = false;
vector<string> optimize_ast_nodes_filters; auto opt_mode = true;
vector<string> opt_rules;
auto opt_help = false; auto opt_help = false;
auto opt_source = false; auto opt_source = false;
vector<char> source; vector<char> source;
@ -49,12 +50,16 @@ int main(int argc, const char **argv) {
opt_help = true; opt_help = true;
} else if (string("--ast") == arg) { } else if (string("--ast") == arg) {
opt_ast = true; opt_ast = true;
} else if (string("--opt") == arg) { } else if (string("--opt-all") == arg) {
opt_optimize_ast_nodes = true; opt_optimize = true;
} else if (string("--filters") == arg) { opt_mode = true;
} else if (string("--opt-only") == arg) {
opt_optimize = true;
opt_mode = false;
} else if (string("--opt-rules") == arg) {
if (argi < argc) { if (argi < argc) {
std::string s = argv[argi++]; std::string s = argv[argi++];
optimize_ast_nodes_filters = split(s, ','); opt_rules = split(s, ',');
} }
} else if (string("--source") == arg) { } else if (string("--source") == arg) {
opt_source = true; opt_source = true;
@ -73,12 +78,16 @@ int main(int argc, const char **argv) {
cerr << "usage: grammar_file_path [source_file_path]" << endl cerr << "usage: grammar_file_path [source_file_path]" << endl
<< endl << endl
<< " options:" << 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 << " --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; << " --trace: show trace messages" << endl;
return 1; return 1;
} }
@ -164,12 +173,10 @@ int main(int argc, const char **argv) {
std::shared_ptr<peg::Ast> ast; std::shared_ptr<peg::Ast> ast;
if (!parser.parse_n(source.data(), source.size(), ast)) { return -1; } if (!parser.parse_n(source.data(), source.size(), ast)) { return -1; }
std::cout << "filters..." << std::endl; if (opt_optimize) {
for (auto name : optimize_ast_nodes_filters) { ast = peg::AstOptimizer(opt_mode, opt_rules).optimize(ast);
std::cout << name << std::endl;
} }
ast = peg::AstOptimizer(opt_optimize_ast_nodes, optimize_ast_nodes_filters)
.optimize(ast);
std::cout << peg::ast_to_s(ast); std::cout << peg::ast_to_s(ast);
} else { } else {

View File

@ -3615,16 +3615,16 @@ ast_to_s(const std::shared_ptr<T> &ptr,
} }
struct AstOptimizer { struct AstOptimizer {
AstOptimizer(bool optimize_nodes, AstOptimizer(bool mode,
const std::vector<std::string> &filters = {}) const std::vector<std::string> &rules = {})
: optimize_nodes_(optimize_nodes), filters_(filters) {} : mode_(mode), rules_(rules) {}
template <typename T> template <typename T>
std::shared_ptr<T> optimize(std::shared_ptr<T> original, std::shared_ptr<T> optimize(std::shared_ptr<T> original,
std::shared_ptr<T> parent = nullptr) { std::shared_ptr<T> parent = nullptr) {
auto found = std::find(filters_.begin(), filters_.end(), original->name) != auto found = std::find(rules_.begin(), rules_.end(), original->name) !=
filters_.end(); rules_.end();
bool opt = optimize_nodes_ ? !found : found; bool opt = mode_ ? !found : found;
if (opt && original->nodes.size() == 1) { if (opt && original->nodes.size() == 1) {
auto child = optimize(original->nodes[0], parent); auto child = optimize(original->nodes[0], parent);
@ -3644,8 +3644,8 @@ struct AstOptimizer {
} }
private: private:
const bool optimize_nodes_; const bool mode_;
const std::vector<std::string> filters_; const std::vector<std::string> rules_;
}; };
struct EmptyType {}; struct EmptyType {};