All Projects → samthor → Gumnut

samthor / Gumnut

Licence: apache-2.0
JS parser in Web Assembly / C

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Gumnut

Webassemblyjs
Toolchain for WebAssembly
Stars: ✭ 566 (+304.29%)
Mutual labels:  parser, wasm
Markdown Wasm
Markdown parser and HTML generator implemented in WebAssembly, based on md4c
Stars: ✭ 833 (+495%)
Mutual labels:  parser, wasm
Go Wasm
WebAssembly binary file parser written in go
Stars: ✭ 121 (-13.57%)
Mutual labels:  parser, wasm
Antlr4 C3
A grammar agnostic code completion engine for ANTLR4 based parsers
Stars: ✭ 135 (-3.57%)
Mutual labels:  parser
Harser
Easy way for HTML parsing and building XPath
Stars: ✭ 135 (-3.57%)
Mutual labels:  parser
Secretnetwork
𝕊 The Secret Network
Stars: ✭ 138 (-1.43%)
Mutual labels:  wasm
Dtp Stat
Карта ДТП
Stars: ✭ 141 (+0.71%)
Mutual labels:  parser
V86
x86 virtualization in your browser, recompiling x86 to wasm on the fly
Stars: ✭ 12,765 (+9017.86%)
Mutual labels:  wasm
Pygdbmi
A library to parse gdb mi output and interact with gdb subprocesses
Stars: ✭ 139 (-0.71%)
Mutual labels:  parser
Mocha1995
☕️ The world's first JavaScript engine written in 1995 by Brendan Eich, now compiled back to JS and WASM!
Stars: ✭ 136 (-2.86%)
Mutual labels:  wasm
Prance
Resolving Swagger/OpenAPI 2.0 and 3.0 Parser
Stars: ✭ 133 (-5%)
Mutual labels:  parser
Wasmex
Execute WebAssembly / WASM from Elixir
Stars: ✭ 136 (-2.86%)
Mutual labels:  wasm
As Wasi
An AssemblyScript API layer for WASI system calls.
Stars: ✭ 135 (-3.57%)
Mutual labels:  wasm
Entrypoint
Composable CLI Argument Parser for all modern .Net platforms.
Stars: ✭ 136 (-2.86%)
Mutual labels:  parser
Json Autotype
Automatic Haskell type inference from JSON input
Stars: ✭ 139 (-0.71%)
Mutual labels:  parser
Filament
Filament is a real-time physically based rendering engine for Android, iOS, Windows, Linux, macOS, and WebGL2
Stars: ✭ 13,215 (+9339.29%)
Mutual labels:  wasm
Awesome Wasm Tools
😎 A curated list of awesome, language-agnostic WebAssembly tools
Stars: ✭ 139 (-0.71%)
Mutual labels:  wasm
Blazormaterial
Blazor components implementing Google's Material components for web - https://material.io/components/web
Stars: ✭ 136 (-2.86%)
Mutual labels:  wasm
To Milliseconds
Convert an object of time properties to milliseconds: `{seconds: 2}` → `2000`
Stars: ✭ 136 (-2.86%)
Mutual labels:  parser
Evmc
EVMC – Ethereum Client-VM Connector API
Stars: ✭ 137 (-2.14%)
Mutual labels:  wasm

Tests

A permissive JavaScript tokenizer and parser in C. See a demo syntax highlighter.

Supports ESM code only (i.e., type="module", which is implicitly strict). Supports all language features in the draft specification (as of January 2021).

This is compiled via Web Assembly to run on the web or inside Node without native bindings. It's not reentrant, so you can't parse another file from within its callbacks. It does not generate an AST (although does emit enough data to do so in JS), does not modify the input, and does not use malloc or free.

Usage

Import and install via your favourite package manager. This requires Node v13.10.0 or higher.

The parser works by invoking callbacks on every token as well as open/close announcements for a 'stack', which roughly maps to something you might make an AST node out of.

import {buildHarness} from 'gumnut';

const harness = await buildHarness();  // WebAssembly instantiation is async

const buffer = new TextEncoder().encode('console.info("hello");');
const memory = harness.prepare(buffer.length);
memory.set(buffer);

harness.handle({
  callback() {
    const type = harness.token.type();
    console.info('token', harness.token.type(), harness.token.string());
  },
  open(stackType) { /* open stack type, return false to skip contents */ },
  close(stackType) { /* close stack type */ },
});

harness.run();

This is fairly low-level and designed to be used by other tools.

Module Imports Rewriter

This provides a rewriter for unresolved ESM imports (i.e., those pointing to "node_modules"), which could be used as part of an ESM dev server. Usage:

import buildImportsRewriter from 'gumnut/imports';
import buildResolver from 'esm-resolve';

// WebAssembly instantiation is async
const run = await buildImportsRewriter(buildResolver);
run('./source.js', (part) => process.stdout.write(part));

This example uses esm-resolve, which implements an ESM resolver in pure JS.

Coverage

This correctly parses all 'pass-explicit' tests from test262-parser-tests, except those which rely on non-strict mode behavior (e.g., use variable names like static and let).

Note

JavaScript has a single open-ended 'after-the-fact' ambiguity for keywords, as async is not always a keyword—even in strict mode. Here's an example:

// this is a function call of a method named "async"
async(/* anything can go here */) {}

// this is an async arrow function
async(/* anything can go here */) => {}

// this calls the async method on foo, and is _not_ ambiguous
foo.async() {}

This parser has to walk over code like this at most twice to resolve whether async is a keyword before continuing. See arrow functions break JavaScript parsers for more details.

It also needs to walk over non-async functions at most twice—like (a, b) =>—to correctly label the argument as either creating new variables in scope, or just using them (like a function call or simple parens).

History

Since engineers like to rewrite everything all the time, see the 2020 branch of this code.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].