mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Added filters parameter to peglint and playground
This commit is contained in:
parent
3c58f26635
commit
370067f013
@ -1,14 +1,14 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>PEG Playground - v0.1.5</title>
|
<title>PEG Playground - v0.1.6</title>
|
||||||
<link rel="stylesheet" href="style.css" media="all">
|
<link rel="stylesheet" href="style.css" media="all">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="main">
|
<div id="main">
|
||||||
<div class="editor-container">
|
<div class="editor-container">
|
||||||
<ul class="editor-header">
|
<ul class="editor-header">
|
||||||
<li><span>Grammar:</span></li>
|
<li><span>Grammar</span></li>
|
||||||
<li><span id="grammar-validation" class="editor-validation">Valid</span></li>
|
<li><span id="grammar-validation" class="editor-validation">Valid</span></li>
|
||||||
</ul>
|
</ul>
|
||||||
<pre id="grammar-editor" class="editor-area">{{syntax}}</pre>
|
<pre id="grammar-editor" class="editor-area">{{syntax}}</pre>
|
||||||
@ -16,11 +16,16 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="editor-container">
|
<div class="editor-container">
|
||||||
<ul class="editor-header">
|
<ul class="editor-header">
|
||||||
<li><span>Code:</span></li>
|
<li><span>Source Code</span></li>
|
||||||
<li><span id="code-validation" class="editor-validation">Valid</span></li>
|
<li><span id="code-validation" class="editor-validation">Valid</span></li>
|
||||||
</ul>
|
</ul>
|
||||||
<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>
|
||||||
<pre id="code-ast" class="editor-area"></pre>
|
<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>
|
||||||
<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>
|
||||||
</div>
|
</div>
|
||||||
|
@ -27,6 +27,9 @@ codeAstOptimized.setOptions({
|
|||||||
})
|
})
|
||||||
codeAstOptimized.renderer.$cursorLayer.element.style.opacity=0;
|
codeAstOptimized.renderer.$cursorLayer.element.style.opacity=0;
|
||||||
|
|
||||||
|
$('#optimize').prop('checked', localStorage.getItem('optimize') == 'true');
|
||||||
|
$('#filters').val(localStorage.getItem('filters'));
|
||||||
|
|
||||||
function generateErrorListHTML(errors) {
|
function generateErrorListHTML(errors) {
|
||||||
let html = '<ul>';
|
let html = '<ul>';
|
||||||
|
|
||||||
@ -48,8 +51,14 @@ function parse() {
|
|||||||
const $codeInfo = $('#code-info');
|
const $codeInfo = $('#code-info');
|
||||||
const codeText = code.getValue();
|
const codeText = code.getValue();
|
||||||
|
|
||||||
|
const optimize = $('#optimize').prop('checked');
|
||||||
|
const filters = $('#filters').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('filters', filters);
|
||||||
|
|
||||||
$grammarInfo.html('');
|
$grammarInfo.html('');
|
||||||
$grammarValidation.hide();
|
$grammarValidation.hide();
|
||||||
@ -62,7 +71,7 @@ function parse() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = JSON.parse(Module.lint(grammarText, codeText));
|
const data = JSON.parse(Module.lint(grammarText, codeText, optimize, filters));
|
||||||
|
|
||||||
const isValid = data.grammar.length === 0;
|
const isValid = data.grammar.length === 0;
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
@ -105,6 +114,10 @@ function makeOnClickInInfo(editor) {
|
|||||||
$('#grammar-info').on('click', 'li', makeOnClickInInfo(grammar));
|
$('#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
|
||||||
|
$('#optimize').on('click', setupTimer);
|
||||||
|
$('#filters').on('keyup', setupTimer);
|
||||||
|
|
||||||
// Show page
|
// Show page
|
||||||
$('#main').css({
|
$('#main').css({
|
||||||
'display': 'flex',
|
'display': 'flex',
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include <emscripten/bind.h>
|
|
||||||
#include "../peglib.h"
|
#include "../peglib.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <emscripten/bind.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -18,12 +18,11 @@ std::string escape_json(const std::string& s) {
|
|||||||
return o.str();
|
return o.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::function<void(size_t, size_t, const std::string&)> makeJSONFormatter(std::string& json, bool& init) {
|
std::function<void(size_t, size_t, const std::string &)>
|
||||||
|
makeJSONFormatter(std::string &json, bool &init) {
|
||||||
init = true;
|
init = true;
|
||||||
return [&](size_t ln, size_t col, const std::string &msg) mutable {
|
return [&](size_t ln, size_t col, const std::string &msg) mutable {
|
||||||
if (!init) {
|
if (!init) { json += ","; }
|
||||||
json += ",";
|
|
||||||
}
|
|
||||||
json += "{";
|
json += "{";
|
||||||
json += R"("ln":)" + std::to_string(ln) + ",";
|
json += R"("ln":)" + std::to_string(ln) + ",";
|
||||||
json += R"("col":)" + std::to_string(col) + ",";
|
json += R"("col":)" + std::to_string(col) + ",";
|
||||||
@ -34,7 +33,8 @@ std::function<void(size_t, size_t, const std::string&)> makeJSONFormatter(std::s
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool parse_grammar(const std::string& text, peg::parser& peg, std::string& json) {
|
bool parse_grammar(const std::string &text, peg::parser &peg,
|
||||||
|
std::string &json) {
|
||||||
bool init;
|
bool init;
|
||||||
peg.log = makeJSONFormatter(json, init);
|
peg.log = makeJSONFormatter(json, init);
|
||||||
json += "[";
|
json += "[";
|
||||||
@ -54,7 +54,20 @@ bool parse_code(const std::string& text, peg::parser& peg, std::string& json,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string lint(const std::string& grammarText, const std::string& codeText) {
|
inline std::vector<std::string> splitFiltersText(const std::string &s) {
|
||||||
|
std::vector<std::string> elems;
|
||||||
|
std::stringstream ss(s);
|
||||||
|
std::string elem;
|
||||||
|
while (getline(ss, elem, ',')) {
|
||||||
|
if (!elem.empty()) {
|
||||||
|
elems.push_back(elem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return elems;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string lint(const std::string &grammarText, const std::string &codeText,
|
||||||
|
bool optimize, const std::string &filtersText) {
|
||||||
std::string grammarResult;
|
std::string grammarResult;
|
||||||
std::string codeResult;
|
std::string codeResult;
|
||||||
std::string astResult;
|
std::string astResult;
|
||||||
@ -67,8 +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));
|
||||||
astResultOptimized =
|
auto filters = splitFiltersText(filtersText);
|
||||||
escape_json(peg::ast_to_s(peg::AstOptimizer(true).optimize(ast)));
|
astResultOptimized = escape_json(
|
||||||
|
peg::ast_to_s(peg::AstOptimizer(optimize, filters).optimize(ast)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File diff suppressed because one or more lines are too long
BIN
docs/native.wasm
BIN
docs/native.wasm
Binary file not shown.
@ -65,4 +65,6 @@ body {
|
|||||||
.editor-info li {
|
.editor-info li {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
.editor-sub-header {
|
||||||
|
padding: 4px 8px;
|
||||||
|
}
|
||||||
|
@ -22,9 +22,20 @@ inline bool read_file(const char *path, vector<char> &buff) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline vector<string> split(const string &s, char delim) {
|
||||||
|
vector<string> elems;
|
||||||
|
stringstream ss(s);
|
||||||
|
string elem;
|
||||||
|
while (getline(ss, elem, delim)) {
|
||||||
|
elems.push_back(elem);
|
||||||
|
}
|
||||||
|
return elems;
|
||||||
|
}
|
||||||
|
|
||||||
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_ast_nodes = false;
|
||||||
|
vector<string> optimize_ast_nodes_filters;
|
||||||
auto opt_help = false;
|
auto opt_help = false;
|
||||||
auto opt_source = false;
|
auto opt_source = false;
|
||||||
vector<char> source;
|
vector<char> source;
|
||||||
@ -38,9 +49,13 @@ 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("--optimize_ast_nodes") == arg ||
|
} else if (string("--opt") == arg) {
|
||||||
string("--opt") == arg) {
|
|
||||||
opt_optimize_ast_nodes = true;
|
opt_optimize_ast_nodes = true;
|
||||||
|
} else if (string("--filters") == arg) {
|
||||||
|
if (argi < argc) {
|
||||||
|
std::string s = argv[argi++];
|
||||||
|
optimize_ast_nodes_filters = split(s, ',');
|
||||||
|
}
|
||||||
} else if (string("--source") == arg) {
|
} else if (string("--source") == arg) {
|
||||||
opt_source = true;
|
opt_source = true;
|
||||||
if (argi < argc) {
|
if (argi < argc) {
|
||||||
@ -55,9 +70,16 @@ int main(int argc, const char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (path_list.empty() || opt_help) {
|
if (path_list.empty() || opt_help) {
|
||||||
cerr << "usage: peglint [--ast] [--optimize_ast_nodes|--opt] [--source "
|
cerr << "usage: grammar_file_path [source_file_path]" << endl
|
||||||
"text] [--trace] [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
|
||||||
|
<< " --trace: show trace messages" << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,7 +164,12 @@ 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; }
|
||||||
|
|
||||||
ast = peg::AstOptimizer(opt_optimize_ast_nodes).optimize(ast);
|
std::cout << "filters..." << std::endl;
|
||||||
|
for (auto name : optimize_ast_nodes_filters) {
|
||||||
|
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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user