mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Added filters parameter to peglint and playground
This commit is contained in:
parent
3c58f26635
commit
370067f013
@ -1,14 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>PEG Playground - v0.1.5</title>
|
||||
<title>PEG Playground - v0.1.6</title>
|
||||
<link rel="stylesheet" href="style.css" media="all">
|
||||
</head>
|
||||
<body>
|
||||
<div id="main">
|
||||
<div class="editor-container">
|
||||
<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>
|
||||
</ul>
|
||||
<pre id="grammar-editor" class="editor-area">{{syntax}}</pre>
|
||||
@ -16,11 +16,16 @@
|
||||
</div>
|
||||
<div class="editor-container">
|
||||
<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>
|
||||
</ul>
|
||||
<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>
|
||||
<pre id="code-ast-optimized" class="editor-area"></pre>
|
||||
<div id="code-info" class="editor-info"></div>
|
||||
</div>
|
||||
|
@ -27,6 +27,9 @@ codeAstOptimized.setOptions({
|
||||
})
|
||||
codeAstOptimized.renderer.$cursorLayer.element.style.opacity=0;
|
||||
|
||||
$('#optimize').prop('checked', localStorage.getItem('optimize') == 'true');
|
||||
$('#filters').val(localStorage.getItem('filters'));
|
||||
|
||||
function generateErrorListHTML(errors) {
|
||||
let html = '<ul>';
|
||||
|
||||
@ -48,8 +51,14 @@ function parse() {
|
||||
const $codeInfo = $('#code-info');
|
||||
const codeText = code.getValue();
|
||||
|
||||
const optimize = $('#optimize').prop('checked');
|
||||
const filters = $('#filters').val();
|
||||
console.log(optimize, filters);
|
||||
|
||||
localStorage.setItem('grammarText', grammarText);
|
||||
localStorage.setItem('codeText', codeText);
|
||||
localStorage.setItem('optimize', optimize);
|
||||
localStorage.setItem('filters', filters);
|
||||
|
||||
$grammarInfo.html('');
|
||||
$grammarValidation.hide();
|
||||
@ -62,7 +71,7 @@ function parse() {
|
||||
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;
|
||||
if (isValid) {
|
||||
@ -105,6 +114,10 @@ function makeOnClickInInfo(editor) {
|
||||
$('#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);
|
||||
|
||||
// Show page
|
||||
$('#main').css({
|
||||
'display': 'flex',
|
||||
|
@ -1,12 +1,12 @@
|
||||
#include <emscripten/bind.h>
|
||||
#include "../peglib.h"
|
||||
#include <cstdio>
|
||||
#include <emscripten/bind.h>
|
||||
#include <functional>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
// https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784
|
||||
std::string escape_json(const std::string& s) {
|
||||
std::string escape_json(const std::string &s) {
|
||||
std::ostringstream o;
|
||||
for (auto c : s) {
|
||||
if (c == '"' || c == '\\' || ('\x00' <= c && c <= '\x1f')) {
|
||||
@ -18,12 +18,11 @@ std::string escape_json(const std::string& s) {
|
||||
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;
|
||||
return [&](size_t ln, size_t col, const std::string& msg) mutable {
|
||||
if (!init) {
|
||||
json += ",";
|
||||
}
|
||||
return [&](size_t ln, size_t col, const std::string &msg) mutable {
|
||||
if (!init) { json += ","; }
|
||||
json += "{";
|
||||
json += R"("ln":)" + std::to_string(ln) + ",";
|
||||
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;
|
||||
peg.log = makeJSONFormatter(json, init);
|
||||
json += "[";
|
||||
@ -43,8 +43,8 @@ bool parse_grammar(const std::string& text, peg::parser& peg, std::string& json)
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool parse_code(const std::string& text, peg::parser& peg, std::string& json,
|
||||
std::shared_ptr<peg::Ast>& ast) {
|
||||
bool parse_code(const std::string &text, peg::parser &peg, std::string &json,
|
||||
std::shared_ptr<peg::Ast> &ast) {
|
||||
peg.enable_ast();
|
||||
bool init;
|
||||
peg.log = makeJSONFormatter(json, init);
|
||||
@ -54,7 +54,20 @@ bool parse_code(const std::string& text, peg::parser& peg, std::string& json,
|
||||
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 codeResult;
|
||||
std::string astResult;
|
||||
@ -67,8 +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));
|
||||
astResultOptimized =
|
||||
escape_json(peg::ast_to_s(peg::AstOptimizer(true).optimize(ast)));
|
||||
auto filters = splitFiltersText(filtersText);
|
||||
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 {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.editor-sub-header {
|
||||
padding: 4px 8px;
|
||||
}
|
||||
|
@ -22,9 +22,20 @@ inline bool read_file(const char *path, vector<char> &buff) {
|
||||
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) {
|
||||
auto opt_ast = false;
|
||||
auto opt_optimize_ast_nodes = false;
|
||||
vector<string> optimize_ast_nodes_filters;
|
||||
auto opt_help = false;
|
||||
auto opt_source = false;
|
||||
vector<char> source;
|
||||
@ -38,9 +49,13 @@ 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) {
|
||||
} else if (string("--opt") == arg) {
|
||||
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) {
|
||||
opt_source = true;
|
||||
if (argi < argc) {
|
||||
@ -55,9 +70,16 @@ int main(int argc, const char **argv) {
|
||||
}
|
||||
|
||||
if (path_list.empty() || opt_help) {
|
||||
cerr << "usage: peglint [--ast] [--optimize_ast_nodes|--opt] [--source "
|
||||
"text] [--trace] [grammar file path] [source file path]"
|
||||
<< endl;
|
||||
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
|
||||
<< " --trace: show trace messages" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -142,7 +164,12 @@ int main(int argc, const char **argv) {
|
||||
std::shared_ptr<peg::Ast> ast;
|
||||
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);
|
||||
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user