naga/front/wgsl/
mod.rs

1/*!
2Frontend for [WGSL][wgsl] (WebGPU Shading Language).
3
4[wgsl]: https://gpuweb.github.io/gpuweb/wgsl.html
5*/
6
7mod error;
8mod index;
9mod lower;
10mod parse;
11#[cfg(test)]
12mod tests;
13mod to_wgsl;
14
15use crate::front::wgsl::error::Error;
16use crate::front::wgsl::parse::Parser;
17use thiserror::Error;
18
19pub use crate::front::wgsl::error::ParseError;
20use crate::front::wgsl::lower::Lowerer;
21use crate::Scalar;
22
23pub use crate::front::wgsl::parse::directive::language_extension::{
24    ImplementedLanguageExtension, LanguageExtension, UnimplementedLanguageExtension,
25};
26
27pub struct Frontend {
28    parser: Parser,
29}
30
31impl Frontend {
32    pub const fn new() -> Self {
33        Self {
34            parser: Parser::new(),
35        }
36    }
37
38    pub fn parse(&mut self, source: &str) -> Result<crate::Module, ParseError> {
39        self.inner(source).map_err(|x| x.as_parse_error(source))
40    }
41
42    fn inner<'a>(&mut self, source: &'a str) -> Result<crate::Module, Error<'a>> {
43        let tu = self.parser.parse(source)?;
44        let index = index::Index::generate(&tu)?;
45        let module = Lowerer::new(&index).lower(&tu)?;
46
47        Ok(module)
48    }
49}
50
51/// <div class="warning">
52// NOTE: Keep this in sync with `wgpu::Device::create_shader_module`!
53// NOTE: Keep this in sync with `wgpu_core::Global::device_create_shader_module`!
54///
55/// This function may consume a lot of stack space. Compiler-enforced limits for parsing recursion
56/// exist; if shader compilation runs into them, it will return an error gracefully. However, on
57/// some build profiles and platforms, the default stack size for a thread may be exceeded before
58/// this limit is reached during parsing. Callers should ensure that there is enough stack space
59/// for this, particularly if calls to this method are exposed to user input.
60///
61/// </div>
62pub fn parse_str(source: &str) -> Result<crate::Module, ParseError> {
63    Frontend::new().parse(source)
64}
65
66#[cfg(test)]
67#[track_caller]
68pub fn assert_parse_err(input: &str, snapshot: &str) {
69    let output = parse_str(input)
70        .expect_err("expected parser error")
71        .emit_to_string(input);
72    if output != snapshot {
73        for diff in diff::lines(snapshot, &output) {
74            match diff {
75                diff::Result::Left(l) => println!("-{l}"),
76                diff::Result::Both(l, _) => println!(" {l}"),
77                diff::Result::Right(r) => println!("+{r}"),
78            }
79        }
80        panic!("Error snapshot failed");
81    }
82}