📜 ⬆️ ⬇️

Esprima - javascript parser written in javascript

The development of the Esprima parser appeared on the network and on github , positioned for parsing in general and for javascript in particular. It is written in Javascript and translates the fed text into a JSON structure, which can then be analyzed, for example, for preprocessing codes, creating sugar wrappers for JS, automated error search without running code, converting languages ​​(cross-compiling), converting server-side JS into client-side or on the contrary, to minify the code or vice versa, to parse the obfuscated. But these are all ideas for the future. An archive appeared quite recently (in the middle of November, the first commit with 2000 lines of code), but it has already acquired its investigators, judging by the forks. The parsing format is compatible with the Mozilla SpiderMonkey Parser API .

You can check the operation of the parser in the current state without even downloading the library from the repository - there is a demo page on the site. We enter a small piece of the script in its input field (so that the result is visible) - and we look at the resulting hash tree. (What to do next with this tree is another question.) We see that the syntactic constructs of the language are marked and recognized in the tree. By the way, the input field uses the online editing environment CodeMirror with syntax highlighting and supports working with tabs, group adding spaces to lines for highlighting, indents when entering brackets, so it’s quite convenient to type the code directly in the browser. Parsing is performed in real time and immediately responds to errors in the code, at least when there is little code in the window. However, it works very briskly, both clean parsing and display on the page. For example, redrawing the entire jQuery source code parsing (250 KB) takes about a second (Fx8 \ Linux \ i7-2600 CPU, embed. Video intel), and pure parsing, according to the author of the program, is less than 0.1 s. (In this article there are a lot of interesting data about the speed of parsing on different JS engines.)

At least the following browsers are supported: IE 8+, Firefox 3.5+, Safari 4+, Chrome 7+ and Opera 10.5+ and nodeJS + npm.

The developer (Ariya Hidayat) on his website has launched an online program for testing various parsers that runs directly on your client computer. 4 source libraries and 4 parsers participate in the races: Esprima, Narcissus, parse-js, ZeParser. As the developer emphasizes, all the numbers should not be taken too seriously (because the data is displayed in different formats and each has ways of optimization), but only to make sure that the new parser is on a par with others and does not give too bad results , modestly silent about the fact that he bypasses all the others in tests. That's what happened with me:

')
Here are the visualized test results in comparison with one of the best parsers obtained by the developer :


JS parsers written in JS: Narcissus (used in the Mozilla JS engine), parse-js (used in the UglifyJS minicator), ZeParser. Several keywords and links can be found on stackoverflow , search and developer sites.

The developer, again, online, supports unit and other testing of his code. A good move, because he will be able to receive messages from various users about the results of tests conducted in various environments.

At some stage of implementation, Esprima began to support the selection of comments as a separate array of comments [] at the end of the tree, with line numbers, which may also be used to automatically create documentation.

The developer’s plans are to support features of the language such as 8-riches numbers, more complete support for IE, improving your own website and presenting tests on it.

Such projects are also interesting because using only JS, you can support the development of the frontend and nodeJS at a higher level than by rewriting by adapting the code for the server manually. But really convenient systems for such an approach will appear in the near future, and there is a chance to make a contribution and take a place in the sun right now, looking at the development of server-side JS-technologies.

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


All Articles