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::{stmt::Stmt, utils::WithTokens, SyntaxTree};

begin_snapshots!();

#[snapshot_test]
fn if_stmt() {
	snapshot!(Stmt(WithTokens) r#"
		if true {
			discard;
		} else {}
	"#);

	snapshot!(Stmt(WithTokens) r#"
		if 0 != 1 {}
	"#);

	snapshot!(Stmt(WithTokens) r#"
		if false {
			return;
		} else if true {
			return;
		} else if 2 + 2 == 5 {
			return;
		} else {}
	"#);
}

#[snapshot_test]
fn switch_stmt() {
	snapshot!(Stmt(WithTokens) r#"
		switch 3 {
			case 0, 1: {
				pos = 0.0;
			}
			case 2: {
				pos = 1.0;
				fallthrough;
			}
			case 3: {}
			default: {
				pos = 3.0;
			}
		}
	"#);

	snapshot!(SyntaxTree(WithTokens) r#"
		fn main() {
			var a: i32;
			let x: i32 = generateValue();
			switch x {
				case 0: {
					a = 1;
				}
				default {
					a = 2;
				}
				case 1, 2, {
					a = 3;
				}
				case 3, {
					a = 4;
				}
				case 4 {
					a = 5;
				}
			}
		}
	"#);

	snapshot!(SyntaxTree(WithTokens) r#"
		fn main() {
			const c = 2;
			var a: i32;
			let x: i32 = generateValue();
			switch x {
				case 0: {
					a = 1;
				}
				case 1, c {
					a = 3;
				}
				case 3, default {
					a = 4;
				}
			}
		}
	"#);
}

#[snapshot_test]
fn loop_stmt() {
	snapshot!(SyntaxTree(WithTokens) r#"
		fn main() {
			var a: i32 = 2;
			var i: i32 = 0;
			loop {
				if i >= 4 { break; }

				a *= 2;
				i++;
			}
		}
	"#);

	// With `continue`
	snapshot!(SyntaxTree(WithTokens) r#"
		fn main() {
			var a: i32 = 2;
			var i: i32 = 0;
			loop {
				if i >= 4 { break; }

				let step: i32 = 1;
				i += step;
				if i % 2 == 0 { continue; }

				a *= 2;
			}
		}
	"#);

	// With `continue` and `continuing`
	snapshot!(SyntaxTree(WithTokens) r#"
		fn main() {
			var a: i32 = 2;
			var i: i32 = 0;
			loop {
				if i >= 4 { break; }

				let step: i32 = 1;

				if i % 2 == 0 { continue; }

				a *= 2;

				continuing {
					i += step;
				}
			}
		}
	"#);
}

#[snapshot_test]
fn for_stmt() {
	snapshot!(Stmt(WithTokens) r#"
		for (var i: i32 = 0; i < 4; i++) {
			a += 2;
		}
	"#);

	snapshot!(Stmt(WithTokens) r#"
		for (var i: i32 = 0; i < 4; i += 1) {
			a = a + 2;
		}
	"#);

	snapshot!(Stmt(WithTokens) r#"
		for (var i: i32 = 0; i < 4; i = i + 1) {
			a = a + 2;
		}
	"#);

	snapshot!(Stmt(WithTokens) r#"
		for (; foo.iter < 4; foo.iter++) {
			foo.value += 2;
		}
	"#);

	snapshot!(Stmt(WithTokens) r#"
		for(;;) {
			break;
		}
	"#);
}

#[snapshot_test]
fn var_stmt() {
	snapshot!(Stmt(WithTokens) "var i: i32 = 0;");
}