wasm-language-tools/crates/parser
Pig Fang a15d9eedc9
Some checks are pending
test / test (push) Waiting to run
chore: v0.7.0
2025-11-15 18:20:52 +08:00
..
benches feat(parser): rename parse_to_green to parse and drop old parse 2025-09-20 10:01:56 +08:00
examples chore: update dependencies 2025-11-15 11:17:57 +08:00
src fix(parser): fix missing folded then block with trivias 2025-11-15 15:22:06 +08:00
tests fix(parser): fix missing folded then block with trivias 2025-11-15 15:22:06 +08:00
Cargo.toml chore: v0.7.0 2025-11-15 18:20:52 +08:00
README.md feat(parser): rename parse_to_green to parse and drop old parse 2025-09-20 10:01:56 +08:00

The parser for WebAssembly Text Format.

This parser is error-tolerant, which means it can parse even even if the input contains syntax errors.

This parser will produce concrete syntax tree (CST), but you can build AST from it with a bunch of helpers from wat_syntax::ast module.

Usage

Use the main [parse] function:

use wat_syntax::SyntaxKind;

let input = "(module)";
let (tree, errors) = wat_parser::parse(input);
assert_eq!(tree.kind(), SyntaxKind::ROOT.into());

Any syntax errors won't prevent the parser from parsing the rest of the input, so the [parse] function returns a tuple which contains the CST and syntax errors. You can access syntax errors like this:

use rowan::TextSize;
use wat_syntax::SyntaxKind;

let input = "(module";
let (tree, errors) = wat_parser::parse(input);
assert_eq!(errors[0].range.start(), TextSize::from(7));
assert!(errors[0].message.to_string().contains("expected `)`"));