Skip to content

Commit 51b13a4

Browse files
committed
Fixed missing comma problem in JSON output from PEG playground parser
1 parent bbeadfa commit 51b13a4

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

docs/native.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ std::string escape_json(const std::string& s) {
1818
return o.str();
1919
}
2020

21-
std::function<void(size_t, size_t, const std::string&)> makeJSONFormatter(std::string& json) {
22-
auto init = std::make_shared<bool>(true);
23-
24-
return [&json, init](size_t ln, size_t col, const std::string& msg) mutable {
21+
std::function<void(size_t, size_t, const std::string&)> makeJSONFormatter(std::string& json, bool& init) {
22+
init = true;
23+
return [&](size_t ln, size_t col, const std::string& msg) mutable {
2524
if (!init) {
2625
json += ",";
2726
}
@@ -30,12 +29,14 @@ std::function<void(size_t, size_t, const std::string&)> makeJSONFormatter(std::s
3029
json += R"("col":)" + std::to_string(col) + ",";
3130
json += R"("msg":")" + escape_json(msg) + R"(")";
3231
json += "}";
33-
*init = false;
32+
33+
init = false;
3434
};
3535
}
3636

3737
bool parse_grammar(const std::string& text, peg::parser& peg, std::string& json) {
38-
peg.log = makeJSONFormatter(json);
38+
bool init;
39+
peg.log = makeJSONFormatter(json, init);
3940
json += "[";
4041
auto ret = peg.load_grammar(text.data(), text.size());
4142
json += "]";
@@ -45,7 +46,8 @@ bool parse_grammar(const std::string& text, peg::parser& peg, std::string& json)
4546
bool parse_code(const std::string& text, peg::parser& peg, std::string& json,
4647
std::shared_ptr<peg::Ast>& ast) {
4748
peg.enable_ast();
48-
peg.log = makeJSONFormatter(json);
49+
bool init;
50+
peg.log = makeJSONFormatter(json, init);
4951
json += "[";
5052
auto ret = peg.parse_n(text.data(), text.size(), ast);
5153
json += "]";

docs/native.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/native.wasm

91.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)