Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/KFlash/seafox

A blazing fast 100% spec compliant, self-hosted javascript parser written in Typescript
https://github.com/KFlash/seafox

acorn ecmascript es2020 estree javascript parser parsing tc39 typescript

Last synced: 8 days ago
JSON representation

A blazing fast 100% spec compliant, self-hosted javascript parser written in Typescript

Lists

README

        

Seafox

A blazing fast 100% spec compliant, self-hosted javascript parser written in Typescript.



Seafox NPM
GitHub license
Total alerts
Circle
License

## Features

* Conforms to the standard ECMAScript® 2021 (ECMA-262 11th Edition) language specification
* Support for additional ECMAScript features for Web Browsers
* Optionally track syntactic node locations
* Emits an ESTree-compatible abstract syntax tree
* Lexical analysis
* No backtracking
* Low memory usage
* Insane performance both on desktop computers and handheld devices
* Twice as fast as other Javascript parsers
* Very well tested (~33 000 unit tests with full code coverage)
* Lightweight - ~84 KB minified

## Installation

```sh
npm install seafox --save-dev
```

## API

Seafox generates `AST` according to [ESTree AST format](https://github.com/estree/estree), and can be used to perform [syntactic analysis](https://en.wikipedia.org/wiki/Parsing) (parsing) or [lexical analysis](https://en.wikipedia.org/wiki/Lexical_analysis) (tokenization) of a JavaScript program, and with `ES2015` and later a JavaScript program can be either [a script or a module](https://tc39.github.io/ecma262/index.html#sec-ecmascript-language-scripts-and-modules).

The `parse` method exposed by `Seafox` takes an optional `options` object which allows you to specify whether to parse in [`script`](https://tc39.github.io/ecma262/#sec-parse-script) mode (the default) or in [`module`](https://tc39.github.io/ecma262/#sec-parsemodule) mode.

This is the available options:

```js
{
// Allow parsing using Module as the goal symbol
module?: boolean;

// The flag to enable start and end offsets and line/column location information to each node
loc: false;

// Disable web compatibility
disableWebCompat: false;

// The flag to attach raw property to each literal and identifier node
raw: false;

// Enabled directives
directives: false;

// The flag to allow return in the global scope
globalReturn: false;

// The flag to enable implied strict mode
impliedStrict: false;

// Enable non-standard parenthesized expression node
preserveParens: false;

// Allows token extraction. Accepts only a function
onToken: function() {}
}
```

Example usage:

```ts

import { parseScript, parseModule, parse } from './seafox';

parseScript('({x: [y] = 0} = 1)');

parseModule('({x: [y] = 0} = 1)', { directives: true, raw: true });

parse('({x: [y] = 0} = 1)', { module: true });

parse('({x: [y] = 0} = 1)');

```

# Lexical analysis

Lexical analysis can only be done during parsing and accepts only a function type as the option

```ts
parseScript('foo = bar', { onToken: () => {}});
```
The callback function have 4 arguments.

| Arguments | Description |
| ----------- | ------------------------------------------------------------ |
| `token` | The token to be extracted |
| `value` | Value of the extracted token |
| `start` | Start position of the extracted token |
| `end` | End position of the extracted token |

The `loc` option needs to be enabled for `start` and `end`. Otherwise this values will be set to `undefined`

# Performance

Seafox is developed for performance and low memory usage, and the parser is about 2x - 4x faster than all other javascript parsers.