yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
880 stars 112 forks source link

Is it possible to also jump to definition on input error in the playground ? #226

Closed mingodad closed 2 years ago

mingodad commented 2 years ago

Trying to develop/debug grammars using the playground would be easier if when there is an error in the input/source and we click the error message it also jumped to the definition/rule in the grammar. If the parser knows the rule that failed somehow this info could be encoded in the error message to allow jump to it.

Attached is a grammar and input file that actually gives this error message on input:

39:29 syntax error, unexpected ';', expecting 'construct', 'static', 'destruct', 'set', 'operator'.

It seems clear that the failing rule is known due to the expecting list provided, so if that rule could also be shown on the grammar editor when clicking the error message that would be nice.

Maybe something like this pseudo code:

function generateErrorListHTML(errors) {
  let html = '<ul>';

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

  html += '<ul>';

  return html;
}
...
// Event handing in the info area
function makeOnClickInInfo(editor) {
  return function () {
    const el = $(this);
    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')) {
      grammar.navigateTo(el.data('gln') - 1, el.data('gcol') - 1);
      grammar.scrollToLine(el.data('gln') - 1, true, false, null);
    }
  }
};

jancy.peg.zip

yhirose commented 2 years ago

@mingodad, I think it's possible to make links for definition rules in expecting lists. (By the way, in your examples, 'construct', 'static', ... are not rules, but tokens. So they cannot be links.)

mingodad commented 2 years ago

Yes I know that they are tokens, but to show then probably the parser used the known failed rule.

yhirose commented 2 years ago

There is no way to report line no and column offset if a parser isn't made from a grammar, but constructed with parser combinators. I'll close it for now since I am not planning to implement it.

mingodad commented 2 years ago

Just for an working example of this feature on a related project (base on your playground) see https://mingodad.github.io/CocoR-Typescript/playground , for example select the Json parser (using the select at the upper middle screen) and edit the Input example to make it invalid the click parse to see the error message and when clicking see that both grammar/input editor show the the offending rule/input.