cpp-peglib/docs/native.cpp

115 lines
3.3 KiB
C++
Raw Normal View History

2019-02-09 06:00:30 +00:00
#include "../peglib.h"
#include <cstdio>
#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
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-07-05 23:34:08 +00:00
std::function<void(size_t, size_t, const std::string &, const std::string &)>
makeJSONFormatter(peg::parser &peg, std::string &json, bool &init) {
init = true;
2022-07-05 23:34:08 +00:00
return [&](size_t ln, size_t col, const std::string &msg, const std::string &rule) mutable {
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"(")";
2022-07-05 23:34:08 +00:00
if (!rule.empty()) {
auto it = peg.get_grammar().find(rule);
if (it != peg.get_grammar().end()) {
auto [gln, gcol] = it->second.line_;
json += ",";
json += R"("gln":)" + std::to_string(gln) + ",";
json += R"("gcol":)" + std::to_string(gcol);
}
}
2019-02-09 06:00:30 +00:00
json += "}";
init = false;
2019-02-09 06:00:30 +00:00
};
}
bool parse_grammar(const std::string &text, peg::parser &peg,
std::string &json) {
bool init;
2022-07-05 23:34:08 +00:00
peg.set_logger(makeJSONFormatter(peg, json, init));
2019-02-09 06:00:30 +00:00
json += "[";
auto ret = peg.load_grammar(text.data(), text.size());
json += "]";
return ret;
}
bool parse_code(const std::string &text, peg::parser &peg, std::string &json,
std::shared_ptr<peg::Ast> &ast) {
2019-02-09 06:00:30 +00:00
peg.enable_ast();
bool init;
2022-07-05 23:34:08 +00:00
peg.set_logger(makeJSONFormatter(peg, json, init));
2019-02-09 06:00:30 +00:00
json += "[";
auto ret = peg.parse_n(text.data(), text.size(), ast);
json += "]";
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;
std::string profileResult;
2019-02-09 06:00:30 +00:00
peg::parser peg;
auto is_grammar_valid = parse_grammar(grammarText, peg, grammarResult);
auto is_source_valid = false;
2019-02-09 06:00:30 +00:00
if (is_grammar_valid && peg) {
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;
is_source_valid = parse_code(codeText, peg, codeResult, ast);
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 + "\"";
json += ",\"profile\":\"" + profileResult + "\"";
2019-02-09 06:00:30 +00:00
}
json += "}";
return json;
}
EMSCRIPTEN_BINDINGS(native) { emscripten::function("lint", &lint); }