Updated playground

This commit is contained in:
yhirose 2022-09-02 23:58:38 -04:00
parent c9090b6661
commit b83c7be298
2 changed files with 12 additions and 10 deletions

View File

@ -18,10 +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 &)> std::function<void(size_t, size_t, const std::string &, const std::string &)>
makeJSONFormatter(std::string &json, bool &init) { 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,
const std::string &) mutable {
if (!init) { json += ","; } if (!init) { json += ","; }
json += "{"; json += "{";
json += R"("ln":)" + std::to_string(ln) + ","; json += R"("ln":)" + std::to_string(ln) + ",";
@ -54,7 +55,8 @@ 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, bool opt_mode, bool packrat) { std::string lint(const std::string &grammarText, const std::string &codeText,
bool opt_mode, bool packrat) {
std::string grammarResult; std::string grammarResult;
std::string codeResult; std::string codeResult;
std::string astResult; std::string astResult;
@ -69,9 +71,7 @@ std::string lint(const std::string &grammarText, const std::string &codeText, bo
std::stringstream ss; std::stringstream ss;
peg::enable_profiling(peg, ss); peg::enable_profiling(peg, ss);
if (packrat) { if (packrat) { peg.enable_packrat_parsing(); }
peg.enable_packrat_parsing();
}
std::shared_ptr<peg::Ast> ast; std::shared_ptr<peg::Ast> ast;
is_source_valid = parse_code(codeText, peg, codeResult, ast); is_source_valid = parse_code(codeText, peg, codeResult, ast);
@ -80,16 +80,18 @@ std::string lint(const std::string &grammarText, const std::string &codeText, bo
if (ast) { if (ast) {
astResult = escape_json(peg::ast_to_s(ast)); astResult = escape_json(peg::ast_to_s(ast));
astResultOptimized = escape_json( astResultOptimized =
peg::ast_to_s(peg.optimize_ast(ast, opt_mode))); escape_json(peg::ast_to_s(peg.optimize_ast(ast, opt_mode)));
} }
} }
std::string json; std::string json;
json += "{"; json += "{";
json += std::string("\"grammar_valid\":") + (is_grammar_valid ? "true" : "false"); json +=
std::string("\"grammar_valid\":") + (is_grammar_valid ? "true" : "false");
json += ",\"grammar\":" + grammarResult; json += ",\"grammar\":" + grammarResult;
json += std::string(",\"source_valid\":") + (is_source_valid ? "true" : "false"); json +=
std::string(",\"source_valid\":") + (is_source_valid ? "true" : "false");
if (!codeResult.empty()) { if (!codeResult.empty()) {
json += ",\"code\":" + codeResult; json += ",\"code\":" + codeResult;
json += ",\"ast\":\"" + astResult + "\""; json += ",\"ast\":\"" + astResult + "\"";

Binary file not shown.