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