开发者社区> 镜像站> NPM> wat-tokenizer
wat-tokenizer
Tokenize WebAssembly Text-format into list of lists
Last updated 3 years ago by emilbayes .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install wat-tokenizer 
SYNC missed versions from official npm registry.

wat-tokenizer

Tokenize WebAssembly Text-format into list of lists

Usage

var watTokenizer = require('wat-tokenizer')

var tokenizer = watTokenizer()

tokenizer.parse(Buffer.from('(say (lower "hello world"))'))

var ast = tokenizer.final(true)

The above S-Expression will have the following AST:

[
  [
    { [String: 'say'] col: 2, line: 1 },
    { [String: ' '] col: 5, line: 1 },
    [
      { [String: 'lower'] col: 7, line: 1 },
      { [String: ' '] col: 12, line: 1 },
      { [String: '"hello world"'] col: 13, line: 1 },
      col: 6, line: 1
    ],
    col: 1, line: 1
  ]
]

The syntax is tokens being String objects (rather than literals), meaning they behave like strings, but have extra properties, in this case .col and .line

API

var tokenizer = watTokenizer([prealloc = 2048])

Initialises a new tokenizer for WAT. Will naively tokenize anything that looks like S-expressions. prealloc determines the size of the Buffer used to parse each token. If you have very long strings in your source, you may wish to increase this. Default is 2048 bytes.

tokenizer.update(sourceBuf)

Update the tokenizer state with the source code contained in Buffer sourceBuf. Returns the tokenizer itself for easy chaining.

var listOfLists = tokenizer.final([assert = true])

Retrieve the parsed list of lists, optionally asserting that the internal state is not in an inconsistent state. This method may be called multiple times. Note that due to arrays being passed by reference, any mutations to this data structure will persist into the tokenizer, if you change the returned reference. Calling .update after calling .final is undefined behaviour.

Install

npm install wat-tokenizer

License

ISC

Current Tags

  • 0.3.0                                ...           latest (3 years ago)

2 Versions

  • 0.3.0                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 1
Last Month 2
Dependencies (1)
Dev Dependencies (1)
Dependents (0)
None