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