2019-02-09 06:00:30 +00:00
|
|
|
#include "../peglib.h"
|
|
|
|
#include <cstdio>
|
2020-05-25 02:14:43 +00:00
|
|
|
#include <emscripten/bind.h>
|
2019-02-09 06:00:30 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784
|
2020-05-25 02:14:43 +00:00
|
|
|
std::string escape_json(const std::string &s) {
|
2019-02-09 06:00:30 +00:00
|
|
|
std::ostringstream o;
|
|
|
|
for (auto c : s) {
|
|
|
|
if (c == '"' || c == '\\' || ('\x00' <= c && c <= '\x1f')) {
|
|
|
|
o << "\\u" << std::hex << std::setw(4) << std::setfill('0') << (int)c;
|
|
|
|
} else {
|
|
|
|
o << c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o.str();
|
|
|
|
}
|
|
|
|
|
2022-09-03 12:12:12 +00:00
|
|
|
std::function<void(size_t, size_t, const std::string &)>
|
2020-05-25 02:14:43 +00:00
|
|
|
makeJSONFormatter(std::string &json, bool &init) {
|
2019-03-07 22:51:22 +00:00
|
|
|
init = true;
|
2022-09-03 12:12:12 +00:00
|
|
|
return [&](size_t ln, size_t col, const std::string &msg) mutable {
|
2020-05-25 02:14:43 +00:00
|
|
|
if (!init) { json += ","; }
|
2019-02-09 06:00:30 +00:00
|
|
|
json += "{";
|
|
|
|
json += R"("ln":)" + std::to_string(ln) + ",";
|
|
|
|
json += R"("col":)" + std::to_string(col) + ",";
|
|
|
|
json += R"("msg":")" + escape_json(msg) + R"(")";
|
|
|
|
json += "}";
|
2019-03-07 22:51:22 +00:00
|
|
|
|
|
|
|
init = false;
|
2019-02-09 06:00:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-25 02:14:43 +00:00
|
|
|
bool parse_grammar(const std::string &text, peg::parser &peg,
|
|
|
|
std::string &json) {
|
2019-03-07 22:51:22 +00:00
|
|
|
bool init;
|
2022-09-03 12:12:12 +00:00
|
|
|
peg.set_logger(makeJSONFormatter(json, init));
|
2019-02-09 06:00:30 +00:00
|
|
|
json += "[";
|
|
|
|
auto ret = peg.load_grammar(text.data(), text.size());
|
|
|
|
json += "]";
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-24 21:21:07 +00:00
|
|
|
bool parse_code(const std::string &text, peg::parser &peg, std::string &json,
|
2020-05-25 02:14:43 +00:00
|
|
|
std::shared_ptr<peg::Ast> &ast) {
|
2019-02-09 06:00:30 +00:00
|
|
|
peg.enable_ast();
|
2019-03-07 22:51:22 +00:00
|
|
|
bool init;
|
2022-09-03 12:12:12 +00:00
|
|
|
peg.set_logger(makeJSONFormatter(json, init));
|
2019-02-09 06:00:30 +00:00
|
|
|
json += "[";
|
|
|
|
auto ret = peg.parse_n(text.data(), text.size(), ast);
|
|
|
|
json += "]";
|
2021-04-24 21:21:07 +00:00
|
|
|
return ret;
|
2019-02-09 06:00:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 03:58:38 +00:00
|
|
|
std::string lint(const std::string &grammarText, const std::string &codeText,
|
|
|
|
bool opt_mode, bool packrat) {
|
2019-02-09 06:00:30 +00:00
|
|
|
std::string grammarResult;
|
|
|
|
std::string codeResult;
|
|
|
|
std::string astResult;
|
|
|
|
std::string astResultOptimized;
|
2022-06-03 23:25:49 +00:00
|
|
|
std::string profileResult;
|
2019-02-09 06:00:30 +00:00
|
|
|
|
|
|
|
peg::parser peg;
|
2021-04-24 21:21:07 +00:00
|
|
|
auto is_grammar_valid = parse_grammar(grammarText, peg, grammarResult);
|
|
|
|
auto is_source_valid = false;
|
2019-02-09 06:00:30 +00:00
|
|
|
|
2021-04-24 21:21:07 +00:00
|
|
|
if (is_grammar_valid && peg) {
|
2022-06-03 23:25:49 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
peg::enable_profiling(peg, ss);
|
|
|
|
|
2022-09-03 03:58:38 +00:00
|
|
|
if (packrat) { peg.enable_packrat_parsing(); }
|
2022-06-04 02:23:55 +00:00
|
|
|
|
2019-02-09 06:00:30 +00:00
|
|
|
std::shared_ptr<peg::Ast> ast;
|
2021-04-24 21:21:07 +00:00
|
|
|
is_source_valid = parse_code(codeText, peg, codeResult, ast);
|
2022-06-03 23:25:49 +00:00
|
|
|
|
|
|
|
profileResult = escape_json(ss.str());
|
|
|
|
|
2021-01-15 05:05:22 +00:00
|
|
|
if (ast) {
|
2019-02-09 06:00:30 +00:00
|
|
|
astResult = escape_json(peg::ast_to_s(ast));
|
2022-09-03 03:58:38 +00:00
|
|
|
astResultOptimized =
|
|
|
|
escape_json(peg::ast_to_s(peg.optimize_ast(ast, opt_mode)));
|
2019-02-09 06:00:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string json;
|
|
|
|
json += "{";
|
2022-09-03 03:58:38 +00:00
|
|
|
json +=
|
|
|
|
std::string("\"grammar_valid\":") + (is_grammar_valid ? "true" : "false");
|
2021-04-13 15:10:52 +00:00
|
|
|
json += ",\"grammar\":" + grammarResult;
|
2022-09-03 03:58:38 +00:00
|
|
|
json +=
|
|
|
|
std::string(",\"source_valid\":") + (is_source_valid ? "true" : "false");
|
2019-02-09 06:00:30 +00:00
|
|
|
if (!codeResult.empty()) {
|
|
|
|
json += ",\"code\":" + codeResult;
|
|
|
|
json += ",\"ast\":\"" + astResult + "\"";
|
|
|
|
json += ",\"astOptimized\":\"" + astResultOptimized + "\"";
|
2022-06-03 23:25:49 +00:00
|
|
|
json += ",\"profile\":\"" + profileResult + "\"";
|
2019-02-09 06:00:30 +00:00
|
|
|
}
|
|
|
|
json += "}";
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
EMSCRIPTEN_BINDINGS(native) { emscripten::function("lint", &lint); }
|