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