Fixed missing comma problem in JSON output from PEG playground parser

This commit is contained in:
yhirose 2019-03-07 17:51:22 -05:00
parent bbeadfa937
commit 51b13a4291
3 changed files with 10 additions and 8 deletions

View File

@ -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 += "]";

File diff suppressed because one or more lines are too long

Binary file not shown.