#syntax #javascript-parser

rslint_parser

An extremely fast ECMAScript parser made for the rslint project

8 releases

0.3.1 Oct 6, 2021
0.3.0 Sep 16, 2021
0.2.1 Oct 21, 2020
0.1.3 Oct 6, 2020

#1744 in Web programming

Download history 116/week @ 2024-07-20 171/week @ 2024-07-27 83/week @ 2024-08-03 55/week @ 2024-08-10 40/week @ 2024-08-17 62/week @ 2024-08-24 110/week @ 2024-08-31 65/week @ 2024-09-07 74/week @ 2024-09-14 100/week @ 2024-09-21 70/week @ 2024-09-28 49/week @ 2024-10-05 39/week @ 2024-10-12 31/week @ 2024-10-19 43/week @ 2024-10-26 56/week @ 2024-11-02

175 downloads per month
Used in 6 crates (5 directly)

MIT license

1MB
24K SLoC

Extremely fast, lossless, and error tolerant JavaScript Parser.

The parser uses an abstraction over non-whitespace tokens. This allows us to losslessly or lossly parse code without requiring explicit handling of whitespace. The parser yields events, not an AST, the events are resolved into untyped syntax nodes, which can then be casted into a typed AST.

The parser is able to produce a valid AST from any source code. Erroneous productions are wrapped into ERROR syntax nodes, the original source code is completely represented in the final syntax nodes.

You probably do not want to use the parser struct, unless you want to parse fragments of Js source code or make your own productions. Instead use functions such as parse_text and parse_text_lossy which offer abstracted versions for parsing.

Notable features of the parser are:

  • Extremely fast parsing and lexing through the extremely fast rslint_lexer.
  • Ability to do Lossy or Lossless parsing on demand without explicit whitespace handling.
  • Customizable, able to parse any fragments of JS code at your discretion.
  • Completely error tolerant, able to produce an AST from any source code.
  • Zero cost for converting untyped nodes to a typed AST.
  • Ability to go from AST to SyntaxNodes to SyntaxTokens to source code and back very easily with nearly zero cost.
  • Very easy tree traversal through SyntaxNode.
  • Descriptive errors with multiple labels and notes.
  • Very cheap cloning, cloning an ast node or syntax node is the cost of adding a reference to an Rc.
  • Cheap incremental reparsing of changed text.

The crate further includes utilities such as:

It is inspired by the rust analyzer parser but adapted for JavaScript.

Syntax Nodes vs AST Nodes

The crate relies on a concept of untyped SyntaxNodes vs typed AstNodes. Syntax nodes represent the syntax tree in an untyped way. They represent a location in an immutable tree with two pointers. The syntax tree is composed of SyntaxNodes and SyntaxTokens in a nested tree structure. Each node can have parents, siblings, children, descendants, etc.

AstNodes represent a typed version of a syntax node. They have the same exact representation as syntax nodes therefore a conversion between either has zero runtime cost. Every piece of data of an ast node is optional, this is due to the fact that the parser is completely error tolerant.

Each representation has its advantages:

SyntaxNodes

  • Very simple traversing of the syntax tree through functions on them.
  • Easily able to convert to underlying text, range, or tokens.
  • Contain all whitespace bound to the underlying production (in the case of lossless parsing).
  • Can be easily converted into its typed representation with zero cost.
  • Can be turned into a pretty representation with fmt debug.

AST Nodes

  • Easy access to properties of the underlying production.
  • Zero cost conversion to a syntax node.

In conclusion, the use of both representations means we are not constrained to acting through typed nodes. Which makes traversal hard and you often have to resort to autogenerated visitor patterns. AST nodes are simply a way to easily access subproperties of a syntax node.event;

Dependencies

~3–10MB
~98K SLoC