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