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