Mere fmt — written in Mere, running in the browser
Paste a Mere expression or a full file on the left and press
Format. The Wasm module on this page tokenizes,
parses, and pretty-prints your input — all written in Mere itself,
no server round-trip. Top-level let X = … ; /
let rec … ; / type T = … ; declarations
and a trailing main expression both work.
Source:
lexer.mere,
parser.mere,
fmt.mere,
bridged by
selfhost-fmt.mere.
The shared AST lives in
contrib/parser/ast.mere.
(click Format to see the formatted output)
Pipeline
Three contrib libraries combine into the wasm running on this page:
lexer.mere(~336 lines, Stage 50a) — source string → tokensparser.mere(~1010 lines, Stages 50b–50e) — tokens → ASTfmt.mere(~480 lines, Stage 49b) — AST → formatted source
They share the same expr / pattern /
ty definitions through ast.mere
(Stage 50f-1's refactor), which is what lets a single browser
bridge file import both ends and pipe values through with no
translation layer.
What this page formats
Single expressions and full files both work. A full file
looks like a sequence of let X = e ; /
let rec X = e ; / type T = … ;
declarations followed by a main expression — the same shape Mere
itself uses. The output is byte-identical to running
mere fmt on the OCaml side for the slice of syntax
both formatters cover (see "Limitations" below for what's still
out of scope).
Things to try: arithmetic with precedence, let /
let rec bindings, if / match
control flow, curried lambdas (the formatter reverses the chain
back into \x y -> body shorthand), constructor
patterns, list literals, ranges, variant type declarations,
annotated lambdas like fn (n: int) -> n + 1.
Limitations
-
Records (
type T = { f: ty };/T { f = e }/e.f/T { … with f = e }) — the AST declares them but the parser doesn't accept them yet. -
'astyle type variables in declarations / annotations. -
extern fn/extern type/module M { … }/import/open/view/drop/signature— full OCaml-side syntax not yet ported to the self-host parser. -
Phase 36 sugar operators (
|>/<|/<</>>/@@/?/?!/<-) — same. - Comments — the lexer discards them.
Why this matters
Self-hosting both the parser and the formatter closes the §S1 plan (paper trial):
- Stage 49a–b — Mere AST + pretty-printer in Mere
- Stage 49d — early browser demo with hand-coded ASTs
- Stage 50a–e — self-host parser, end-to-end
- Stage 50f-1 — lift AST into shared
ast.mere - Stage 50f-2 — this page: live in-browser
mere fmt