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    pub fn set_options(&mut self, options: Options) {
54        self.options = options;
55    }
56
57    pub fn parse(&mut self, source: &str) -> core::result::Result<crate::Module, ParseError> {
58        self.inner(source).map_err(|x| x.as_parse_error(source))
59    }
60
61    fn inner<'a>(&mut self, source: &'a str) -> Result<'a, crate::Module> {
62        let tu = self.parser.parse(source, &self.options)?;
63        let index = index::Index::generate(&tu)?;
64        let module = Lowerer::new(&index).lower(tu)?;
65
66        Ok(module)
67    }
68}
69
70/// <div class="warning">
71// NOTE: Keep this in sync with `wgpu::Device::create_shader_module`!
72// NOTE: Keep this in sync with `wgpu_core::Global::device_create_shader_module`!
73///
74/// This function may consume a lot of stack space. Compiler-enforced limits for parsing recursion
75/// exist; if shader compilation runs into them, it will return an error gracefully. However, on
76/// some build profiles and platforms, the default stack size for a thread may be exceeded before
77/// this limit is reached during parsing. Callers should ensure that there is enough stack space
78/// for this, particularly if calls to this method are exposed to user input.
79///
80/// </div>
81pub fn parse_str(source: &str) -> core::result::Result<crate::Module, ParseError> {
82    Frontend::new().parse(source)
83}
84
85#[cfg(test)]
86#[track_caller]
87pub fn assert_parse_err(input: &str, snapshot: &str) {
88    let output = parse_str(input)
89        .expect_err("expected parser error")
90        .emit_to_string(input);
91    if output != snapshot {
92        for diff in diff::lines(snapshot, &output) {
93            match diff {
94                diff::Result::Left(l) => println!("-{l}"),
95                diff::Result::Both(l, _) => println!(" {l}"),
96                diff::Result::Right(r) => println!("+{r}"),
97            }
98        }
99        panic!("Error snapshot failed");
100    }
101}