Expand description
A parser for WGSL files, written directly from the specification with lalrpop.
§Parsing a source file
let source = "@fragment fn frag_main() -> @location(0) vec4f { return vec4(1); }";
let parsed = wgsl_parse::Parser::parse_str(source).unwrap();
let compare = TranslationUnit {
global_directives: vec![],
global_declarations: vec![GlobalDeclaration::Function(Function {
attributes: vec![Attribute {
name: "fragment".to_string(),
arguments: None
}],
name: "frag_main".to_string(),
parameters: vec![],
return_attributes: vec![Attribute {
name: "location".to_string(),
arguments: Some(vec![Expression::Literal(LiteralExpression::AbstractInt(0))])
}],
return_type: Some(TypeExpression {
name: "vec4f".to_string(),
template_args: None
}),
body: CompoundStatement {
attributes: vec![],
statements: vec![Statement::Return(Some(Expression::FunctionCall(
FunctionCallExpression {
name: "vec4".to_string(),
template_args: None,
arguments: vec![Expression::Literal(LiteralExpression::AbstractInt(1))]
}
)))]
}
})]
};
assert_eq!(parsed, compare);§Syntax tree
See syntax tree.
Modifying the syntax tree:
let source = "const hello = 0u;";
let mut module = wgsl_parse::Parser::parse_str(source).unwrap();
// modify the module as needed...
let decl = &mut module
.global_declarations
.iter_mut()
.find_map(|decl| match decl {
wgsl_parse::syntax::GlobalDeclaration::Declaration(decl) => Some(decl),
_ => None,
})
.unwrap();
decl.name = "world".to_string();
assert_eq!(format!("{module}").trim(), "const world = 0u;");§Stringification
The syntax tree elements implement 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 = wgsl_parse::Parser::parse_str(source).unwrap();
// modify the module as needed...
println!("{module}");Re-exports§
Modules§
- error
- A
SpannedErroris the error type returned byParser::parse*functions. - lexer
- Prefer using
Parser::parse_str. You shouldn’t need to manipulate the lexer. - parser
- The
Parsertakes WGSL source code and returns a syntax tree. - span
- syntax
- A syntax tree for WGSL files. The root of the tree is a
TranslationUnit. - syntax_
spanned - A spanned syntax tree for WGSL files. The root of the tree is a
TranslationUnit.