wgsl-parser 0.5.0

A zero-copy recursive-descent parser for WebGPU shading language
Documentation
use snapshot::{begin_snapshots, snapshot, snapshot_test};

use crate::{expr::Expr, stmt::Stmt, utils::WithTokens};

begin_snapshots!();

#[snapshot_test]
fn unary_pre_expr() {
	snapshot!(Expr(WithTokens) "-foo");
	snapshot!(Expr(WithTokens) "!foo");
	snapshot!(Expr(WithTokens) "~foo");
	snapshot!(Expr(WithTokens) "*foo");
	snapshot!(Expr(WithTokens) "&foo");
}

#[snapshot_test]
fn unary_post_expr() {
	snapshot!(Expr(WithTokens) "foo++");
	snapshot!(Expr(WithTokens) "foo--");
}

#[snapshot_test]
fn binary_expr() {
	snapshot!(Expr(WithTokens) "2 + 2");
	snapshot!(Expr(WithTokens) "2 + 2 * 4");
	snapshot!(Expr(WithTokens) "2 * 2 + 4");
	snapshot!(Expr(WithTokens) "idx % 15 == 0");
}

#[snapshot_test]
fn type_ctor_expr() {
	snapshot!(Expr(WithTokens) "vec3<f32>(1.0, 1.0, 1.0)");
}

#[snapshot_test]
fn group_expr() {
	snapshot!(Expr(WithTokens) "(2 + 2) * 4");
}

#[snapshot_test]
fn bitcast_expr() {
	snapshot!(Expr(WithTokens) "bitcast<f32>(foo)");
}

#[snapshot_test]
fn literal_expr() {
	snapshot!(Expr(WithTokens) "42.42");
	snapshot!(Expr(WithTokens) "0x42.42");
	snapshot!(Expr(WithTokens) "0");
	snapshot!(Expr(WithTokens) "42u");
	snapshot!(Expr(WithTokens) "true");
}

#[snapshot_test]
fn ident_expr() {
	snapshot!(Expr(WithTokens) "foo");
	snapshot!(Expr(WithTokens) "foo::bar");
	snapshot!(Expr(WithTokens) "foo::bar::baz");
}

#[snapshot_test]
fn assignment_expr() {
	snapshot!(Expr(WithTokens) "foo = 2");
	snapshot!(Expr(WithTokens) "foo.bar = 2");
	snapshot!(Expr(WithTokens) "2 = 2");
}

#[snapshot_test]
fn compound_assignment_expr() {
	snapshot!(Expr(WithTokens) "foo += 2");
	snapshot!(Expr(WithTokens) "foo -= 2");
	snapshot!(Expr(WithTokens) "foo *= 2");
	snapshot!(Expr(WithTokens) "foo /= 2");
	snapshot!(Expr(WithTokens) "foo %= 2");
	snapshot!(Expr(WithTokens) "foo &= 2");
	snapshot!(Expr(WithTokens) "foo |= 2");
	snapshot!(Expr(WithTokens) "foo ^= 2");
	snapshot!(Expr(WithTokens) "foo >>= 2");
	snapshot!(Expr(WithTokens) "foo <<= 2");
}

#[snapshot_test]
fn fn_call_expr() {
	snapshot!(Expr(WithTokens) "max(4, 2)");
	snapshot!(Expr(WithTokens) "some::imported_fn(4, 2)");
}

#[snapshot_test]
fn primary_expr_postfixes() {
	snapshot!(Expr(WithTokens) "foo.bar.baz");
	snapshot!(Expr(WithTokens) "foo.bar[baz]");
	snapshot!(Expr(WithTokens) "foo.bar(4).baz(2)[0]");
	snapshot!(Expr(WithTokens) "foo.bar[0].xyz");
	// Regression test: The postfix expression following `light.` should only
	// encompass `color`, instead of consuming `color * ambient_strength` as a
	// binary expression
	snapshot!(Expr(WithTokens) "light.color * ambient_strength");
}

#[snapshot_test]
fn chained_method_calls() {
	snapshot!(Stmt(WithTokens) r#"
		let specular_strength = in
			.world_normal
			.dot(half_dir)
			.max(0.0)
			.pow(32.0);
	"#);
}