wasm_wat_sexp (wasm v0.1.0)

View Source

Tokens to a tree, and the thing that tells you where your parenthesis is missing.

The text format is s-expressions, so this is small: a list becomes a list and everything else stays as the lexer produced it. What it adds is balance.

An unclosed parenthesis in a fifty-thousand-line .wast file reports the position of the parenthesis that was never closed, not the end of the file. That is the difference between a usable error and "unexpected end of input".

Summary

Types

A tree: a list of forms, or a token from the lexer.

Functions

As read_all/1, but raising rather than answering.

Read one form, answering it and whatever tokens follow.

Read every top-level form in a source file.

Types

sexp()

-nominal sexp() :: {list, non_neg_integer(), [sexp()]} | wasm_wat_lex:token().

A tree: a list of forms, or a token from the lexer.

Functions

forms_of(Source)

-spec forms_of(binary()) -> [sexp()].

As read_all/1, but raising rather than answering.

For a caller already inside wasm_error:capture/1. Matching {ok, _} on the answering form is how a malformed file becomes an internal error with a badmatch in it, which happened twice here before this existed.

read(Tokens)

-spec read([wasm_wat_lex:token()]) -> {sexp(), [wasm_wat_lex:token()]}.

Read one form, answering it and whatever tokens follow.

read_all(Source)

-spec read_all(binary()) -> {ok, [sexp()]} | {error, wasm_error:error()}.

Read every top-level form in a source file.