📜 ⬆️ ⬇️

Generator of functional parsers on JavaScript (with transducers)

Hello!

I saw that the article on transducers on JavaScript became quite popular and wanted to note that the generator of parsers on transistors ^ W transducers was available for a long time. At least, it is very similar. I have an article with a detailed description in English " Generating Functional Parsers " and, in fact, the source code .

There are no new rules here, there are no link posts already, they are asking to describe everything abundantly - so I will submit with a couple of words (diluted with necessary water), tell you what it is, otherwise I may not be right to be. Moreover, there is no Russian version of the article yet, and most likely it will not. One hope that this will pass.

The project according to the link above is not an idea to make a new parser generator, but rather an experiment, how perfect parser readability can be achieved without losing much of the parsing speed. In fact, the speed was lost impressively, I confess at once, but due to the banal - in addition to transducers, this version of generated parsers is based on the capture and suppression of exceptions (if an error occurs, an exception is thrown, which intercept operators turn off at will). While experimenting about try / catch leakiness in popular JS engines, it was sincerely forgotten. Perhaps you will have tips on working around this issue.
')
Here is a comparison of the code generated by peg.js in different versions (it was he who was taken as the basis and the generation core by AST was almost completely rewritten to create an alternative) and peg.js-fn , the “friendly version”. This comparison is intended to convince you of a successful outcome of the experiment, no matter what:

Compared code generated parsers

It is also important to note that peg.js-fn successfully passes all peg.js tests.

The resulting code abundantly speculates with the idea of ​​deferred functions (about which I wrote a lot on Habré before conscious samovipilivaniya), aka partial application, because of which parsing operators (such as and , or , sequence , choice ) can be combined into any sequences.

This allows the rule of PEG:

 start = . ('aa' / 'oo' / 'ee') . 

Get javascript parser with code:

 rules.start = seqnc(ch(), choice(match('aa'), match('oo'), match('ee')), ch()) 

Of course, all operators are lazily executed, which allows me to call them transducers.

In the article, the link describes the code of each of the operators, with examples.

PS All references to transducers in this post are used solely for attracting attention and as some self-irony. In fact, I have no good reason to associate the proposed approach with them, except that of the currently existing terms, for the described approach this one might be best suited.

Source: https://habr.com/ru/post/238007/


All Articles