mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Name refactoring
This commit is contained in:
parent
370067f013
commit
19c6c2615c
14
README.md
14
README.md
@ -424,7 +424,9 @@ shared_ptr<peg::Ast> ast;
|
||||
if (parser.parse("...", 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);
|
||||
}
|
||||
```
|
||||
@ -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
|
||||
|
@ -22,9 +22,9 @@
|
||||
<pre id="code-editor" class="editor-area">{{source}}</pre>
|
||||
<div class="editor-sub-header">AST</div>
|
||||
<pre id="code-ast" class="editor-area"></pre>
|
||||
<div class="editor-sub-header">Optimized AST
|
||||
On: <input id="optimize" type="checkbox"></input>
|
||||
Filters: <input id="filters" type="text" size="48" placeholder="Enter definition names to filter delimited by comma..."></input>
|
||||
<div class="editor-sub-header">Optimized AST
|
||||
mode: <select id="opt_mode" type="checkbox"><option value="all">All</option><option value="only">Only</option></select>
|
||||
rules: <input id="opt_rules" type="text" size="40" placeholder="Enter definition rules separated by comma."></input>
|
||||
</div>
|
||||
<pre id="code-ast-optimized" class="editor-area"></pre>
|
||||
<div id="code-info" class="editor-info"></div>
|
||||
|
@ -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 = '<ul>';
|
||||
@ -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({
|
||||
|
@ -54,7 +54,7 @@ bool parse_code(const std::string &text, peg::parser &peg, std::string &json,
|
||||
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::stringstream ss(s);
|
||||
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,
|
||||
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<peg::Ast> 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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,8 +34,9 @@ inline vector<string> split(const string &s, char delim) {
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
auto opt_ast = false;
|
||||
auto opt_optimize_ast_nodes = false;
|
||||
vector<string> optimize_ast_nodes_filters;
|
||||
auto opt_optimize = false;
|
||||
auto opt_mode = true;
|
||||
vector<string> opt_rules;
|
||||
auto opt_help = false;
|
||||
auto opt_source = false;
|
||||
vector<char> 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<peg::Ast> 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 {
|
||||
|
16
peglib.h
16
peglib.h
@ -3615,16 +3615,16 @@ ast_to_s(const std::shared_ptr<T> &ptr,
|
||||
}
|
||||
|
||||
struct AstOptimizer {
|
||||
AstOptimizer(bool optimize_nodes,
|
||||
const std::vector<std::string> &filters = {})
|
||||
: optimize_nodes_(optimize_nodes), filters_(filters) {}
|
||||
AstOptimizer(bool mode,
|
||||
const std::vector<std::string> &rules = {})
|
||||
: mode_(mode), rules_(rules) {}
|
||||
|
||||
template <typename T>
|
||||
std::shared_ptr<T> optimize(std::shared_ptr<T> original,
|
||||
std::shared_ptr<T> 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<std::string> filters_;
|
||||
const bool mode_;
|
||||
const std::vector<std::string> rules_;
|
||||
};
|
||||
|
||||
struct EmptyType {};
|
||||
|
Loading…
Reference in New Issue
Block a user