A parser for WGSL files, written directly from the specification with lalrpop.
Parsing a source file
# use *;
let source = "@fragment fn frag_main() -> @location(0) vec4f { return vec4(1); }";
let parsed = parse_str.unwrap;
let compare = TranslationUnit ;
assert_eq!;
Syntax tree
See syntax tree.
Modifying the syntax tree:
let source = "const hello = 0u;";
let mut module = parse_str.unwrap;
// modify the module as needed...
let decl = &mut module
.global_declarations
.iter_mut
.find_map
.unwrap;
decl.name = "world".to_string;
assert_eq!;
Stringification
The syntax tree elements implement [Display][std::fmt::Display].
The display is always pretty-printed.
TODO: implement :# for pretty vs. inline formatting.
let source = "@fragment fn frag_main() -> @location(0) vec4f { return vec4(1); }";
let mut module = parse_str.unwrap;
// modify the module as needed...
println!;