Crates.io | wat_parser |
lib.rs | wat_parser |
version | 0.1.0 |
source | src |
created_at | 2024-11-30 04:17:25.046248 |
updated_at | 2024-11-30 04:17:25.046248 |
description | WebAssembly Text Format concrete syntax tree parser. |
homepage | |
repository | https://github.com/g-plane/wasm-language-tools |
max_upload_size | |
id | 1466289 |
size | 88,922 |
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.
You need to create a parser instance with source code. Once created, the source code can't be changed. So if you want to parse different source code, you need to create a new parser instance.
use wat_parser::Parser;
use wat_syntax::SyntaxKind;
let input = "(module)";
let mut parser = Parser::new(input);
let tree = parser.parse();
assert_eq!(tree.kind(), SyntaxKind::ROOT);
Any syntax errors won't prevent the parser from parsing the rest of the input, however you can retrieve the errors like this:
use wat_parser::Parser;
use wat_syntax::SyntaxKind;
let input = "(module";
let mut parser = Parser::new(input);
let tree = parser.parse();
let errors = parser.errors();
assert_eq!(errors[0].start, 7);
assert!(errors[0].message.to_string().contains("expected `)`"));