peggyjs/peggy

Start and end index of matched rule in the source code.

alex-klimov opened this issue · 5 comments

I need to provide additional information in the editor, when user stands on the text matching particular rules.

Is there built-in way to return start and end index of the text matched with a rule?

Yes, location() or range(). Try to see this output:

start = "a" { return {
  offset: offset(),
  text: text(),
  range: range(),
  location: location(),
}; }

Thanks, this works brilliantly!

One quick question before closing the issue: range() seems to give exactly what I was asking for. In addition, it has property source which is undefined. Is this supposed to be exactly as what text() returns? Should I be concerned that range.source is undefined?

Just read the linked documentation. This is the value of options.grammarSource and it is intended to give location a filename.

Found it, thanks! Linking it here, so other people might make use of it in the future:
https://peggyjs.org/documentation.html#locations

One additional quick note. I sometimes use a function called "loc" when I'm applying locations to lots of AST nodes:

{
  function loc(node) {
    node.loc = location();
  }
}

foo = "foo" { return loc({type: "foo"}); }

location() knows about the current parse state, and the function is available within scope to all initializer functions inside of the initial {} (but NOT top-level initializers using {{}}). I particularly use this approach when I have to modify the location to be in a different form for a specific AST, like here in @peggyjs/eslint-parser