• Stars
    star
    509
  • Rank 86,772 (Top 2 %)
  • Language
    JavaScript
  • Created about 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An 100% spec compliant ES2021 JavaScript parser written in JS

Tenko

A "pixel perfect" 100% spec compliant JavaScript parser written in JavaScript, parsing ES6/ES2015 - ES2021.

REPL: https://pvdz.github.io/tenko/repl

  • Supports:
    • Anything stage 4 up to ES2021
    • Regex syntax (deep)
    • Parsing modes:
      • Sloppy / non-strict
      • Web compat / AnnexB
      • Strict
      • Module
  • AST
    • Is optional, enabled by default
    • Estree (default)
    • (Optional chaining AST works but AST spec seems still in flux)
    • Acorn
    • Babel (anything stage 4, except comments)
    • Supports location data (matching Acorn/Babel for reference)
  • Tests
    • 33k input syntax tests
    • Passes test262 suite (at least as per March 2020), without exception

Name

The name is short for "The Parser Formerly Known As ZeParser3".

It's also an anagram for "Token", perfectly fitting this project.

In Japanese it's a divine beast ("heavenly fox" or "celestial fox"), playing into my nicknames.

REPL

You can find the REPL in repl/index.html, github link: https://pvdz.github.io/tenko/repl

The REPL runs on dev master branch and needs a very new browser due to es module syntax.

Usage

import {Tenko, GOAL_MODULE, COLLECT_TOKENS_ALL} from 'src/index.mjs';
const {
  ast,                 // estree compatible AST
  tokens,              // array of numbers (see Lexer)
  tokenCountSolid,     // number of non-whitespace tokens
  tokenCountAny,       // number of tokens of any kind
} = Tenko(
  inputCode,           // string
  {
    // Parse with script or module goal (module allows import/export)
    goalMode = GOAL_MODULE, // GOAL_MODULE | GOAL_SCRIPT | "module" | "script"
    // Do you want to collect generated tokens at all?
    collectTokens = COLLECT_TOKENS_ALL, // COLLECT_TOKENS_ALL | COLLECT_TOKENS_SOLID | COLLECT_TOKENS_NONE | COLLECT_TOKENS_TYPES | "all" | "solid" | "none" | "types"
    // Apply Annex B rules? (Only works in sloppy mode)
    webCompat = true,
    // Start parsing as if in strict mode? (Works with script goal)
    strictMode = false,
    // Output a Babel compatible AST? Note: comment nodes are not properly mirrored
    babelCompat = false,
    // Add a loc (with `{start: {line, column}, stop: {line, column}}`) to each token?
    babelTokenCompat = false,
    // Pass on a reference that will be used as the AST root
    astRoot = null,
    // Should it normalize \r and \r\n to \n in the .raw of template nodes?
    // Estree spec but makes it hard to serialize template nodes losslessly
    templateNewlineNormalization = true,
    // Pass on a reference to store the tokens
    tokenStorage = [],
    // Callback to receive the lexer instance once its created
    getLexer = null, // getLexer(lexer)
    // You use this to parse `eval` code
    allowGlobalReturn = false,
    // Target a very specific ecmascript version (like, reject async). Number; 6 - 12, or 2015 - 2021, or Infinity.
    targetEsVersion = lastVersion, // (Last supported version is currently ES2021)
    // Leave built up scope information in the ASTs (good luck)
    exposeScopes = false,
    // Assign each node a unique incremental id
    astUids = false,
    // Do you want to print a code frame with error messages? (Part of the input around the point of error)
    errorCodeFrame = true,
    // For the code frame, do you want to always show the entire input, regardless of size? Or just a small context
    truncCodeFrame = true,
    // You can override the logging functions to catch or squash all output
    $log = console.log,
    $warn = console.warn,
    $error = console.error,
    // Value ot use for the `source` field of each `loc` object
    sourceField = '',
    // Generate a `range: {start: number, end: number}` property on all loc objects (does not require `locationTracking`)
    ranges = false,
    // Generate a `range: [start: number, end: number]` property on all nodes. `input.slice(range[0], range[1])` should get you the text for a node.
    nodeRange = false,
    // Do not populate loc properties on AST nodes (property will be undefined). Since v<unpublished>
    locationTracking = true,
  }
);

Development

There is a single entry point in the root project called t which calls tests/t.sh which calls out to various development related scripts.

ES modules

Note that the files use import and export declarations and import(), which requires node 10+ or a cutting edge browser.

At the time of writing node requires the experimental --experimental-modules flag.

It's a burden in some ways and nice in others. A prod build would not have any modules.

Test cases

All test cases are in "special" plain-text .md files. See tests/testcases/README.md for details on formatting those.

Entry point

Some interesting usages of ./t:

# Show help
./t --help

# Run and update (inline) all tests. 
# Use git diff to see changes. Will bail fast on unexpected or assertion errors.
# This tests four modes (sloppy, strict, module, and sloppy-web-compat)
# This also tests the printer on the first successful parse
./t u
# Run all tests step-by-step (same as above) and ask what to do for any changes
./t m

# Same as `./t u` but compare it against Babel or Acorn. Recorded changes should be discarded afterwards.
# Use this to test against AST differences. If there are any they will be printed explicitly.
# Acorn:
./t a
# Babel:
./t b

# Test a particular input from cli
./t i "some.input()"
# Test a particular test file
./t f "tests/testcases/regexes/foo.md"
# Use entire contents of given file as input
./t F "test262/test/annexB/built-ins/foo.js"

# Generate prod builds
# Generate a build. Strips ASSERT*, inline many constants
./t z
# Same as above but explicitly set `acornCompat` and `babelCompat` to `false`.
./t z --no-compat
# Generate pretty builds for debugging without asserts:
./t --pretty
# Minified build with Terser (will lower performance due to inlining)
./t --min

# Run test262 tests (requires cloning https://github.com/tc39/test262 into tenko/ignore/test262)
./t t

# Fuzz the parser
./t fuzz

# Regenerate all autogen test files. Regenerates files still need to be updated (`./t u`).
# All files, regardless:
./t g
# Only create new files:
./t G

# Find out which tests execute a particular code branch in the parser
# Add `HIT()` to any part of the code in src
# Reports (only) all inputs that trigger a `HIT()` call in Tenko
./t s

Some tooling that requires additional setup;

# Benchmarks (requires benchmark files in projroot/ignore/perf);
# Simply spawn new node process and run test:
./t p
# Run benchmarks repeatedly and report results
./t p6
# Configure machine to be as stable as possible (DANGEROUS, read the script before using it, requires root). All 
# changes should be reset after reboot. Then run the benchmarks in the shielded cpus at RT prio (also requires root).
./t stable
./t p6 --stabled
# Same as above but without running `./t stable` previously, and tries to undo certain (but not all) things afterwards
./t p6 --stable

# Investigate v8 perf regressions with deoptigate:
./t deoptigate

# Profile the parser in Chrome devtools (open the tab through `about://inspect`)
./t devtools

# Run a visual heatmap profiler for counts based investigation (private)
./t hf

There are many flags. Some are specific to an action, others are generic. Some examples:

--sloppy             Run in non-strict mode (but non-web compat!)
--strict             Run with script goal but consider the code strict
--module             Run with module goal (enabling strict mode by default)
--web                Run with script goal, non-strict, and enable web compat (AnnexB rules)
--annexb             Force enable AnnexB rules, regardless of mode

6                    Run as close to the rules as of ES6  / ES2015 as possible
7                    Run as close to the rules as of ES7  / ES2016 as possible
8                    Run as close to the rules as of ES8  / ES2017 as possible
9                    Run as close to the rules as of ES9  / ES2018 as possible
10                   Run as close to the rules as of ES10 / ES2019 as possible
11                   Run as close to the rules as of ES11 / ES2020 as possible
12                   Run as close to the rules as of ES11 / ES2021 as possible
2015
2016
2017
2018
2019
2020
2021

--min                Given a broken input, brute force minify the input while maintaining the same error message
--acorn              Output a Acorn compatible AST
--babel              Output a Babel compatible AST
--test-acorn         Compare the `--acorn` output to the actual output of Acorn on same input (./t a)
--test-babel         Compare the `--babel` output to the actual output of Babel on same input (./t b)
--test-node          Compile input in a `Function()` and report whether that throws when Tenko throws, for fuzzer
--build              Use a prod build (from standard output location), instead of dev sources, for all actions that support it
--nb                 Do not build (many actions will kick of a build before doing their thing, this prevents that)

And many more. For details, ./t --help should give you an up to date list of all actions and options.

Building

While the parser runs perfectly fine in dev mode it will be a bit slow. A build:

  • will remove non-assert dev artifacts
  • can remove inline asserts (lines that start with ASSERT)
  • can remove all the AST generation from the build (lines that start with AST)
  • inlines many constants used by the parser as enums or bitwise fields

To generate a build run this in the project root, flags can be combined:

./t z                      # Regular build with everything
./t z --no-ast             # Strip most AST related code from the build (~50% faster, but obviously no AST)
./t z --no-compat          # Strip acorn/babel compatibility
./t z --min                # Run Terser on result (will decrease performance due to inlining)
./t z --pretty             # Run Prettier on build result

Note that this (initially) uses my own printer to print the AST.

The build script writes and ESM and CJS file to ./build

Validation without AST

The no-AST build can validate JS almost as perfect as the regular build except for certain validation cases where it requires the AST:

  • Binary op after arrow with block body (()=>{}*x is illegal)
  • Regular expression on new line after arrow with block body (()=>{} \n /foo/g, prohibited by ASI rules and can't be a division)
  • Update operator anything that's writable but not a valid var or member expression (++[])
  • Delete with an ident that is wrapped in parenthesis (delete (foo) is illegal), trivial cases (delete foo;) should be fine

Testing

Each test is individually encapsulated in an .md file in tests/testcases/**. This file will contain the input code and the output as expected for sloppy mode, strict mode (script goal), module goal, and web compat mode (only works in sloppy mode, script goal).

If a run passes then the AST and types of tokens are printed in the output. This AST is also printed with src/tools/printer and its output is checked to produce the same AST.

If a run does not pass the error message and a pointer to where the error occurred are stored in the file.

The files can be auto-updated with ./t u or ./t m. This makes it easy to update something in the parser and use git to confirm whether anything changed, and if so what.

There are also autogen.md files, which generate a bunch of combinatorial tests (./t g or ./t G), similar to the other tests.

To create a new test simply add a new file, start it with @, a description, a line with only ### and the rest will be considered the test case verbatim. When you run the test runner this file will automatically be converted to a proper test case.

More Repositories

1

Zeon

A static visual JavaScript analyzer / editor
JavaScript
60
star
2

ZeParser

My JavaScript parser
JavaScript
54
star
3

heatfiler

A JS profiler and code coverage tool that displays the result in a (live updating) heatmap
JavaScript
37
star
4

sourcemapper

A source mapping tool that visualizes a source map for ya, what else
HTML
36
star
5

zeparser3

ZeParser3 was renamed to Tenko -> https://github.com/pvdz/tenko
JavaScript
31
star
6

zeparser2

Next version of my JS (ES5) parser
JavaScript
25
star
7

recompiler

Recompile JavaScript without restarting your app
JavaScript
23
star
8

preval

A wannabe JS compiler (still very much wip, not production ready)
JavaScript
12
star
9

gasher

The Gasher is a fuzzer, hasher, running as a genetic algorithm. It hashes a set if number inputs and tries to come up with a formula which hashes to a perfect minimal table. With webworkers.
JavaScript
9
star
10

gssparser

A css and gss (ccss) parser for thegrid.io
JavaScript
8
star
11

FuzzyGolf

An automatic code golfer for js
JavaScript
8
star
12

uglyfuzzer

This is a tentative fuzzer for trying to find bugs in UglifyJS
JavaScript
6
star
13

gonzales

The Gonzales project aims to parse JavaScript as fast as possible
JavaScript
5
star
14

drew

Apply a query to a language cut up in chunks, or tokens, so you can find certain structures with ease and analyze or rewrite them.
JavaScript
5
star
15

unyield

Transforms ES6 generator / yield syntax to ES5 compatible JavaScript source
JavaScript
5
star
16

SwfFuzzer

Generates random but structurally correct swf files
JavaScript
5
star
17

fdq

fork of finitedomain
JavaScript
4
star
18

yacge

Yet Another Chess Game Engine (Yacge) is a chess game web interface (vanilla html/css/js) that started out as a simple thing to validate my implementation of chess rules and which feature creeped into a fairly full engine and web UI
JavaScript
4
star
19

miners

Langton Ant meets Genetic Algorithm meets implicit emergent gameplay meets trying to code Rust
Rust
3
star
20

htmlday16

Slides and experiments for my iframes talk at htmlday / cssday 2016
HTML
3
star
21

SignificantWhitespace

A Whitespace compiler and interpreter in JavaScript for js1k
JavaScript
2
star
22

cssflexboxgui

A flexbox web gui for creating css flexlayouts and exporting them to html
HTML
2
star
23

js1kdemo2019

Xcode - a CoreWars engine for JS1k, all development sources etc
JavaScript
2
star
24

mope

A zero config type checker for a subset of JS
JavaScript
2
star
25

fdp

Finite Domain Preprocessor / Constraint Reduction System
JavaScript
2
star
26

hs_draw_sim

Hearthstone card draw simulator
JavaScript
1
star
27

Hutimility

Source code rewriter for custom HTML literal syntax in JS, using a custom version of ZeParser2
JavaScript
1
star
28

qfox.github.com

live demo!
1
star
29

logic-table-filter

Small tool to compare logic formula's to each other
JavaScript
1
star
30

test

test bed
1
star
31

js1k-2016-demo

See how I golfed down my js1k demo (http://js1k.com/2016-elemental/demo/2491) step-by-step (per commit). Tentative blog post at http://qfox.nl/weblog/372 . Start at the first commit and press next https://github.com/qfox/js1k-2016-demo/pull/1/commits/9e7f7176dd55d760509160e3edcd710fab370e19
HTML
1
star