wart_rs
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

wart_rs

WebAssembly Text Format (WAT) lexer from the wast crate, compiled to WebAssembly for use in JavaScript.

Frontend usage

Run in the browser:

import init, { lexer } from './node_modules/wart_rs/wart_rs.js'

await init()

lexer('(module (func))', (kind, offset, len, text) => {
    if (kind instanceof Error)
        return console.error(kind)
    // offset and len are in bytes
    console.log('%o %o %o %o', kind, offset, len, text)
})

Backend usage

Works in Bun!

import { readFile } from 'node:fs/promises'
import init, { lexer } from 'wart_rs/wart_rs.js'

const wasm = new URL(import.meta.resolve('wart_rs/wart_rs_bg.wasm'))
await init(readFile(wasm))

lexer('(module (func))', (kind, offset, len, text) => {
    if (kind instanceof Error)
        return console.error(kind)
    // offset and len are in bytes
    console.log('%o %o %o %o', kind, offset, len, text)
})

Backend usage (CommonJS modules)

Since 2009.

const { readFile } = require('node:fs/promises')

import('wart_rs/wart_rs.js').then(async ({ default: init, lexer }) => {
    const wasm = require.resolve('wart_rs/wart_rs_bg.wasm')
    await init(readFile(wasm))

    lexer('(module (func))', (kind, offset, len, text) => {
        if (kind instanceof Error)
            return console.error(kind)
        // offset and len are in bytes
        console.log('%o %o %o %o', kind, offset, len, text)
    })
})

Tokens

The lexer yields tokens of the following types:

  • LineComment
  • BlockComment (can be nested)
  • Whitespace
  • LParen
  • RParen
  • String
  • Id (start with $)
  • Keyword
  • Reserved (unexpected token?)
  • Integer
  • Float

Package Sidebar

Install

npm i wart_rs

Weekly Downloads

7

Version

0.0.3

License

MIT

Unpacked Size

57.5 kB

Total Files

6

Last publish

Collaborators

  • mvasilkov