1 #include "../peglib.h"
2 #include <cstdio>
3 #include <emscripten/bind.h>
4 #include <functional>
5 #include <iomanip>
6 #include <sstream>
7 
8 // https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784
escape_json(const std::string & s)9 std::string escape_json(const std::string &s) {
10   std::ostringstream o;
11   for (auto c : s) {
12     if (c == '"' || c == '\\' || ('\x00' <= c && c <= '\x1f')) {
13       o << "\\u" << std::hex << std::setw(4) << std::setfill('0') << (int)c;
14     } else {
15       o << c;
16     }
17   }
18   return o.str();
19 }
20 
21 std::function<void(size_t, size_t, const std::string &)>
makeJSONFormatter(std::string & json,bool & init)22 makeJSONFormatter(std::string &json, bool &init) {
23   init = true;
24   return [&](size_t ln, size_t col, const std::string &msg) mutable {
25     if (!init) { json += ","; }
26     json += "{";
27     json += R"("ln":)" + std::to_string(ln) + ",";
28     json += R"("col":)" + std::to_string(col) + ",";
29     json += R"("msg":")" + escape_json(msg) + R"(")";
30     json += "}";
31 
32     init = false;
33   };
34 }
35 
parse_grammar(const std::string & text,peg::parser & peg,std::string & json)36 bool parse_grammar(const std::string &text, peg::parser &peg,
37                    std::string &json) {
38   bool init;
39   peg.log = makeJSONFormatter(json, init);
40   json += "[";
41   auto ret = peg.load_grammar(text.data(), text.size());
42   json += "]";
43   return ret;
44 }
45 
parse_code(const std::string & text,peg::parser & peg,std::string & json,std::shared_ptr<peg::Ast> & ast)46 void parse_code(const std::string &text, peg::parser &peg, std::string &json,
47                 std::shared_ptr<peg::Ast> &ast) {
48   peg.enable_ast();
49   bool init;
50   peg.log = makeJSONFormatter(json, init);
51   json += "[";
52   auto ret = peg.parse_n(text.data(), text.size(), ast);
53   json += "]";
54 }
55 
lint(const std::string & grammarText,const std::string & codeText,bool opt_mode)56 std::string lint(const std::string &grammarText, const std::string &codeText, bool opt_mode) {
57   std::string grammarResult;
58   std::string codeResult;
59   std::string astResult;
60   std::string astResultOptimized;
61 
62   peg::parser peg;
63   auto ret = parse_grammar(grammarText, peg, grammarResult);
64 
65   if (ret && peg) {
66     std::shared_ptr<peg::Ast> ast;
67     parse_code(codeText, peg, codeResult, ast);
68     if (ast) {
69       astResult = escape_json(peg::ast_to_s(ast));
70       astResultOptimized = escape_json(
71           peg::ast_to_s(peg.optimize_ast(ast, opt_mode)));
72     }
73   }
74 
75   std::string json;
76   json += "{";
77   json += "\"grammar\":" + grammarResult;
78   if (!codeResult.empty()) {
79     json += ",\"code\":" + codeResult;
80     json += ",\"ast\":\"" + astResult + "\"";
81     json += ",\"astOptimized\":\"" + astResultOptimized + "\"";
82   }
83   json += "}";
84 
85   return json;
86 }
87 
EMSCRIPTEN_BINDINGS(native)88 EMSCRIPTEN_BINDINGS(native) { emscripten::function("lint", &lint); }
89