How to imperatively call Peg.js rule during parsing

27 views
Skip to first unread message

Tomáš Ehrlich

unread,
Jul 9, 2019, 3:22:22 AM7/9/19
to PEG.js: Parser Generator for JavaScript
Hey everyone,
I'm writing parser for reStructuredText. One important feature of reStructuredText is extensibility - you can define your own inline roles and block directives which extend parser. I looked at few directives implemented in Python and they mostly expose method for parsing inline or block elements.

For example, in this source:

.. my-directive:: argument


   directive content

The directive receives "argument" and ["directive content"] and can decide how to process it. It can take the literal value or run parseInline/parseBlock to resume parsing into tokens.

In my grammar I already have `Inline` and `BlockElement` rules. I was hoping I could simply call `peg$parseInline` and `peg$parseBlockElement`, but that doesn't work because these functions doesn't taky any arguments. Instead they use the internal cursor location when processing another chunk of source.

Another option would be split the grammar into three pieces - core.pegjs, inline.pegjs, block.pegjs and create three parsers. I assume this should work, but the performance will be suboptimal for built-in formatting,
which will need to parse source twice. For example:

This is regular paragraph with *inline* **formatting**.


.. my-directive:: *inline* **formatting** as well


In this case, `my-directive` receives `*inline* **formatting** as well` and parses it with inline parses (if the directive requires it).

On the other hand, the inline markup inside paragraph will always be parsed with inline parser, but now it needs to parse source into raw tokens and then
call second parser to get inline tokens.


Is it possible to somehow call subset of parser on a specific string? E.g. call peg$parseInline with a value?

Any ideas for a better solution?


Thanks in advance
   Tom

Reply all
Reply to author
Forward
0 new messages