mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Updated playground to show profiling result
This commit is contained in:
parent
ad74d9e8c0
commit
8f19894e32
@ -20,13 +20,21 @@
|
||||
<li><span id="code-validation" class="editor-validation">Valid</span></li>
|
||||
</ul>
|
||||
<pre id="code-editor" class="editor-area">{{source}}</pre>
|
||||
<div id="code-info" class="editor-info"></div>
|
||||
</div>
|
||||
<div class="editor-container">
|
||||
<ul class="editor-header">
|
||||
<li><span>Output</span></li>
|
||||
<li><span></span></li>
|
||||
</ul>
|
||||
<div class="editor-sub-header">AST</div>
|
||||
<pre id="code-ast" class="editor-area"></pre>
|
||||
<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>
|
||||
</div>
|
||||
<pre id="code-ast-optimized" class="editor-area"></pre>
|
||||
<div id="code-info" class="editor-info"></div>
|
||||
<div class="editor-sub-header">Profile</div>
|
||||
<div id="profile" class="editor-area"></div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.2/ace.js"></script>
|
||||
|
@ -1,34 +1,34 @@
|
||||
// Setup editros
|
||||
const grammar = ace.edit("grammar-editor");
|
||||
grammar.setShowPrintMargin(false);
|
||||
grammar.setValue(localStorage.getItem('grammarText') || '');
|
||||
grammar.moveCursorTo(0, 0);
|
||||
|
||||
const code = ace.edit("code-editor");
|
||||
code.setShowPrintMargin(false);
|
||||
code.setValue(localStorage.getItem('codeText') || '');
|
||||
code.moveCursorTo(0, 0);
|
||||
|
||||
const codeAst = ace.edit("code-ast");
|
||||
codeAst.setShowPrintMargin(false);
|
||||
codeAst.setOptions({
|
||||
function setupInfoArea(id) {
|
||||
const e = ace.edit(id);
|
||||
e.setShowPrintMargin(false);
|
||||
e.setOptions({
|
||||
readOnly: true,
|
||||
highlightActiveLine: false,
|
||||
highlightGutterLine: false
|
||||
})
|
||||
codeAst.renderer.$cursorLayer.element.style.opacity=0;
|
||||
e.renderer.$cursorLayer.element.style.opacity=0;
|
||||
return e;
|
||||
}
|
||||
|
||||
const codeAstOptimized = ace.edit("code-ast-optimized");
|
||||
codeAstOptimized.setShowPrintMargin(false);
|
||||
codeAstOptimized.setOptions({
|
||||
readOnly: true,
|
||||
highlightActiveLine: false,
|
||||
highlightGutterLine: false
|
||||
})
|
||||
codeAstOptimized.renderer.$cursorLayer.element.style.opacity=0;
|
||||
function setupEditorArea(id, lsKey) {
|
||||
const e = ace.edit(id);
|
||||
e.setShowPrintMargin(false);
|
||||
e.setValue(localStorage.getItem(lsKey) || '');
|
||||
e.moveCursorTo(0, 0);
|
||||
return e;
|
||||
}
|
||||
|
||||
const grammar = setupEditorArea("grammar-editor", "grammarText");
|
||||
const code = setupEditorArea("code-editor", "codeText");
|
||||
|
||||
const codeAst = setupInfoArea("code-ast");
|
||||
const codeAstOptimized = setupInfoArea("code-ast-optimized");
|
||||
const profile = setupInfoArea("profile");
|
||||
|
||||
$('#opt_mode').val(localStorage.getItem('optimazationMode') || 'all');
|
||||
|
||||
// Parse
|
||||
function escapeHtml(unsafe) {
|
||||
return unsafe
|
||||
.replace(/&/g, "&")
|
||||
@ -71,6 +71,7 @@ function parse() {
|
||||
$codeValidation.hide();
|
||||
codeAst.setValue('');
|
||||
codeAstOptimized.setValue('');
|
||||
profile.setValue('');
|
||||
|
||||
if (grammarText.length === 0) {
|
||||
return;
|
||||
@ -84,6 +85,7 @@ function parse() {
|
||||
|
||||
codeAst.insert(data.ast);
|
||||
codeAstOptimized.insert(data.astOptimized);
|
||||
profile.insert(data.profile);
|
||||
|
||||
if (data.source_valid) {
|
||||
$codeValidation.removeClass('editor-validation-invalid').text('Valid').show();
|
||||
|
@ -59,14 +59,21 @@ std::string lint(const std::string &grammarText, const std::string &codeText, bo
|
||||
std::string codeResult;
|
||||
std::string astResult;
|
||||
std::string astResultOptimized;
|
||||
std::string profileResult;
|
||||
|
||||
peg::parser peg;
|
||||
auto is_grammar_valid = parse_grammar(grammarText, peg, grammarResult);
|
||||
auto is_source_valid = false;
|
||||
|
||||
if (is_grammar_valid && peg) {
|
||||
std::stringstream ss;
|
||||
peg::enable_profiling(peg, ss);
|
||||
|
||||
std::shared_ptr<peg::Ast> ast;
|
||||
is_source_valid = parse_code(codeText, peg, codeResult, ast);
|
||||
|
||||
profileResult = escape_json(ss.str());
|
||||
|
||||
if (ast) {
|
||||
astResult = escape_json(peg::ast_to_s(ast));
|
||||
astResultOptimized = escape_json(
|
||||
@ -83,6 +90,7 @@ std::string lint(const std::string &grammarText, const std::string &codeText, bo
|
||||
json += ",\"code\":" + codeResult;
|
||||
json += ",\"ast\":\"" + astResult + "\"";
|
||||
json += ",\"astOptimized\":\"" + astResultOptimized + "\"";
|
||||
json += ",\"profile\":\"" + profileResult + "\"";
|
||||
}
|
||||
json += "}";
|
||||
|
||||
|
BIN
docs/native.wasm
BIN
docs/native.wasm
Binary file not shown.
@ -130,11 +130,11 @@ int main(int argc, const char **argv) {
|
||||
if (opt_packrat) { parser.enable_packrat_parsing(); }
|
||||
|
||||
if (opt_trace) {
|
||||
enable_tracing(parser);
|
||||
enable_tracing(parser, std::cout);
|
||||
}
|
||||
|
||||
if (opt_profile) {
|
||||
enable_profiling(parser);
|
||||
enable_profiling(parser, std::cout);
|
||||
}
|
||||
|
||||
parser.set_verbose_trace(opt_verbose);
|
||||
|
26
peglib.h
26
peglib.h
@ -4393,7 +4393,8 @@ public:
|
||||
}
|
||||
|
||||
void enable_trace(TracerEnter tracer_enter, TracerLeave tracer_leave,
|
||||
TracerStartOrEnd tracer_start, TracerStartOrEnd tracer_end) {
|
||||
TracerStartOrEnd tracer_start,
|
||||
TracerStartOrEnd tracer_end) {
|
||||
if (grammar_ != nullptr) {
|
||||
auto &rule = (*grammar_)[start_];
|
||||
rule.tracer_enter = tracer_enter;
|
||||
@ -4450,7 +4451,7 @@ private:
|
||||
* enable_tracing
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
inline void enable_tracing(parser &parser) {
|
||||
inline void enable_tracing(parser &parser, std::ostream &os) {
|
||||
size_t prev_pos = 0;
|
||||
parser.enable_trace(
|
||||
[&](auto &ope, auto s, auto, auto &, auto &c, auto &, auto &) {
|
||||
@ -4468,8 +4469,8 @@ inline void enable_tracing(parser &parser) {
|
||||
auto lit = dynamic_cast<const peg::LiteralString *>(&ope);
|
||||
if (lit) { name += " '" + peg::escape_characters(lit->lit_) + "'"; }
|
||||
}
|
||||
std::cout << "E " << pos << backtrack << "\t" << indent << "┌" << name
|
||||
<< " #" << c.trace_ids.back() << std::endl;
|
||||
os << "E " << pos << backtrack << "\t" << indent << "┌" << name << " #"
|
||||
<< c.trace_ids.back() << std::endl;
|
||||
prev_pos = static_cast<size_t>(pos);
|
||||
},
|
||||
[&](auto &ope, auto s, auto, auto &sv, auto &c, auto &, auto len,
|
||||
@ -4498,7 +4499,7 @@ inline void enable_tracing(parser &parser) {
|
||||
peg::TokenChecker::is_token(const_cast<peg::Ope &>(ope))) {
|
||||
matched = ", match '" + peg::escape_characters(s, len) + "'";
|
||||
}
|
||||
std::cout << "L " << pos << "\t" << indent << ret << name << " #"
|
||||
os << "L " << pos << "\t" << indent << ret << name << " #"
|
||||
<< c.trace_ids.back() << choice.str() << token << matched
|
||||
<< std::endl;
|
||||
},
|
||||
@ -4509,7 +4510,7 @@ inline void enable_tracing(parser &parser) {
|
||||
* enable_profiling
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
inline void enable_profiling(parser &parser) {
|
||||
inline void enable_profiling(parser &parser, std::ostream &os) {
|
||||
struct Stats {
|
||||
struct Item {
|
||||
std::string name;
|
||||
@ -4522,8 +4523,7 @@ inline void enable_profiling(parser &parser) {
|
||||
};
|
||||
|
||||
parser.enable_trace(
|
||||
[&](auto &ope, auto, auto, auto &, auto &, auto &,
|
||||
std::any &trace_data) {
|
||||
[&](auto &ope, auto, auto, auto &, auto &, auto &, std::any &trace_data) {
|
||||
if (auto holder = dynamic_cast<const peg::Holder *>(&ope)) {
|
||||
auto &stats = *std::any_cast<Stats *>(trace_data);
|
||||
|
||||
@ -4550,7 +4550,7 @@ inline void enable_profiling(parser &parser) {
|
||||
}
|
||||
if (index == 0) {
|
||||
size_t id = 0;
|
||||
std::cout << " id total % success fail "
|
||||
os << " id total % success fail "
|
||||
"definition"
|
||||
<< std::endl;
|
||||
size_t total_total, total_success = 0, total_fail = 0;
|
||||
@ -4562,18 +4562,18 @@ inline void enable_profiling(parser &parser) {
|
||||
auto ratio = total * 100.0 / stats.total;
|
||||
sprintf(buff, "%4zu %10lu %5.2f %10lu %10lu %s", id, total,
|
||||
ratio, success, fail, name.c_str());
|
||||
std::cout << buff << std::endl;
|
||||
os << buff << std::endl;
|
||||
id++;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
os << std::endl;
|
||||
total_total = total_success + total_fail;
|
||||
sprintf(buff, "%4s %10lu %5s %10lu %10lu %s", "", total_total,
|
||||
"", total_success, total_fail, "Total counters");
|
||||
std::cout << buff << std::endl;
|
||||
os << buff << std::endl;
|
||||
sprintf(buff, "%4s %10s %5s %10.2f %10.2f %s", "", "", "",
|
||||
total_success * 100.0 / total_total,
|
||||
total_fail * 100.0 / total_total, "% success/fail");
|
||||
std::cout << buff << std::endl;
|
||||
os << buff << std::endl;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user