Skip to content

Commit

Permalink
Fix #226
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Sep 4, 2022
1 parent 1958bad commit afd3acb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
11 changes: 10 additions & 1 deletion docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ function generateErrorListHTML(errors) {
let html = '<ul>';

html += $.map(errors, function (x) {
return '<li data-ln="' + x.ln + '" data-col="' + x.col + '"><span>' + x.ln + ':' + x.col + '</span> <span>' + escapeHtml(x.msg) + '</span></li>';
if (x.gln && x.gcol) {
return `<li data-ln="${x.ln}" data-col="${x.col}" data-gln="${x.gln}" data-gcol="${x.gcol}"><span>${x.ln}:${x.col}</span> <span>${escapeHtml(x.msg)}</span></li>`;
} else {
return `<li data-ln="${x.ln}" data-col="${x.col}"><span>${x.ln}:${x.col}</span> <span>${escapeHtml(x.msg)}</span></li>`;
}
}).join('');

html += '<ul>';
Expand Down Expand Up @@ -149,6 +153,11 @@ function makeOnClickInInfo(editor) {
editor.navigateTo(el.data('ln') - 1, el.data('col') - 1);
editor.scrollToLine(el.data('ln') - 1, true, false, null);
editor.focus();

if(el.data('gln') && el.data('gcol')) {
grammar.navigateTo(el.data('gln') - 1, el.data('gcol') - 1);
grammar.scrollToLine(el.data('gln') - 1, true, false, null);
}
}
};
$('#grammar-info').on('click', 'li', makeOnClickInInfo(grammar));
Expand Down
19 changes: 14 additions & 5 deletions docs/native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@ 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, bool &init) {
std::function<void(size_t, size_t, const std::string &, const std::string &)>
makeJSONFormatter(peg::parser &peg, std::string &json, bool &init) {
init = true;
return [&](size_t ln, size_t col, const std::string &msg) mutable {
return [&](size_t ln, size_t col, const std::string &msg, const std::string &rule) mutable {
if (!init) { json += ","; }
json += "{";
json += R"("ln":)" + std::to_string(ln) + ",";
json += R"("col":)" + std::to_string(col) + ",";
json += R"("msg":")" + escape_json(msg) + R"(")";
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);
}
}
json += "}";

init = false;
Expand All @@ -36,7 +45,7 @@ makeJSONFormatter(std::string &json, bool &init) {
bool parse_grammar(const std::string &text, peg::parser &peg,
std::string &json) {
bool init;
peg.set_logger(makeJSONFormatter(json, init));
peg.set_logger(makeJSONFormatter(peg, json, init));
json += "[";
auto ret = peg.load_grammar(text.data(), text.size());
json += "]";
Expand All @@ -47,7 +56,7 @@ bool parse_code(const std::string &text, peg::parser &peg, std::string &json,
std::shared_ptr<peg::Ast> &ast) {
peg.enable_ast();
bool init;
peg.set_logger(makeJSONFormatter(json, init));
peg.set_logger(makeJSONFormatter(peg, json, init));
json += "[";
auto ret = peg.parse_n(text.data(), text.size(), ast);
json += "]";
Expand Down
Binary file modified docs/native.wasm
Binary file not shown.
27 changes: 12 additions & 15 deletions peglib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,7 @@ class Definition {

std::string name;
const char *s_ = nullptr;
std::pair<size_t, size_t> line_ = {1, 1};

std::function<bool(const SemanticValues &vs, const std::any &dt,
std::string &msg)>
Expand Down Expand Up @@ -2557,6 +2558,9 @@ inline void ErrorInfo::output_log(const Log &log, const char *s, size_t n) {
msg += "'";
} else {
msg += "<" + error_rule->name + ">";
if (label.empty()) {
label = error_rule->name;
}
}
first_item = false;
}
Expand Down Expand Up @@ -2937,12 +2941,10 @@ inline size_t Recovery::parse_core(const char *s, size_t n,
// Custom error message
if (c.log) {
auto label = dynamic_cast<Reference *>(rule.args_[0].get());
if (label) {
if (!label->rule_->error_message.empty()) {
c.error_info.message_pos = s;
c.error_info.message = label->rule_->error_message;
c.error_info.label = label->rule_->name;
}
if (label && !label->rule_->error_message.empty()) {
c.error_info.message_pos = s;
c.error_info.message = label->rule_->error_message;
c.error_info.label = label->rule_->name;
}
}

Expand Down Expand Up @@ -3481,13 +3483,14 @@ class ParserGenerator {
rule <= ope;
rule.name = name;
rule.s_ = vs.sv().data();
rule.line_ = line_info(vs.ss, rule.s_);
rule.ignoreSemanticValue = ignore;
rule.is_macro = is_macro;
rule.params = params;

if (data.start.empty()) {
data.start = name;
data.start_pos = vs.sv().data();
data.start = rule.name;
data.start_pos = rule.s_;
}
} else {
data.duplicates_of_definition.emplace_back(name, vs.sv().data());
Expand Down Expand Up @@ -4549,13 +4552,7 @@ class parser {

const Definition &operator[](const char *s) const { return (*grammar_)[s]; }

std::vector<std::string> get_rule_names() const {
std::vector<std::string> rules;
for (auto &[name, _] : *grammar_) {
rules.push_back(name);
}
return rules;
}
const Grammar &get_grammar() const { return *grammar_; }

void disable_eoi_check() {
if (grammar_ != nullptr) {
Expand Down
3 changes: 2 additions & 1 deletion test/test1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,8 @@ TEST(GeneralTest, Semantic_values_test) {
x <- 'x'
)");

for (const auto &rule : parser.get_rule_names()) {
for (const auto &item : parser.get_grammar()) {
const auto &rule = item.first;
parser[rule.data()] = [rule](const SemanticValues &vs, std::any &) {
if (rule == "term") {
EXPECT_EQ("a at 0", std::any_cast<std::string>(vs[0]));
Expand Down

0 comments on commit afd3acb

Please sign in to comment.