|
|
|
@ -18,10 +18,9 @@ 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) { |
|
|
|
|
auto init = std::make_shared<bool>(true); |
|
|
|
|
|
|
|
|
|
return [&json, init](size_t ln, size_t col, const std::string& msg) mutable { |
|
|
|
|
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 += ","; |
|
|
|
|
} |
|
|
|
@ -30,12 +29,14 @@ std::function<void(size_t, size_t, const std::string&)> makeJSONFormatter(std::s |
|
|
|
|
json += R"("col":)" + std::to_string(col) + ","; |
|
|
|
|
json += R"("msg":")" + escape_json(msg) + R"(")"; |
|
|
|
|
json += "}"; |
|
|
|
|
*init = false; |
|
|
|
|
|
|
|
|
|
init = false; |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool parse_grammar(const std::string& text, peg::parser& peg, std::string& json) { |
|
|
|
|
peg.log = makeJSONFormatter(json); |
|
|
|
|
bool init; |
|
|
|
|
peg.log = makeJSONFormatter(json, init); |
|
|
|
|
json += "["; |
|
|
|
|
auto ret = peg.load_grammar(text.data(), text.size()); |
|
|
|
|
json += "]"; |
|
|
|
@ -45,7 +46,8 @@ bool parse_grammar(const std::string& text, peg::parser& peg, std::string& json) |
|
|
|
|
bool parse_code(const std::string& text, peg::parser& peg, std::string& json, |
|
|
|
|
std::shared_ptr<peg::Ast>& ast) { |
|
|
|
|
peg.enable_ast(); |
|
|
|
|
peg.log = makeJSONFormatter(json); |
|
|
|
|
bool init; |
|
|
|
|
peg.log = makeJSONFormatter(json, init); |
|
|
|
|
json += "["; |
|
|
|
|
auto ret = peg.parse_n(text.data(), text.size(), ast); |
|
|
|
|
json += "]"; |
|
|
|
|