UNPKG

wgsl-plus

Version:

A WGSL preprocessor, prettifier, minifier, obfuscator, and compiler with C-style macros, conditional compilation, file linking, and multi-format output for WebGPU shaders.

44 lines (43 loc) 1.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = collectStructNames; const next_name_1 = __importDefault(require("./next-name")); const swizzles_1 = __importDefault(require("./swizzles")); /** * Checks if an identifier could be a vector swizzle. * @param name The identifier to check. * @returns True if it consists only of 'x', 'y', 'z', 'w' and is 1-4 characters long. */ function isPotentialSwizzle(name) { if (name.length < 1 || name.length > 4) return false; for (let char of name) { if (!swizzles_1.default.has(char)) return false; } return true; } function collectStructNames(tokens) { const structMemberMap = new Map(); // For struct member names for (let i = 0; i < tokens.length; i++) { const token = tokens[i]; if (token.type === "keyword" && token.value === "struct") { i += 2; while (i < tokens.length && tokens[i].value !== "}") { if (tokens[i].type === "identifier" && i + 1 < tokens.length && tokens[i + 1].value === ":") { const memberName = tokens[i].value; if (!isPotentialSwizzle(memberName) && !structMemberMap.has(memberName)) { structMemberMap.set(memberName, (0, next_name_1.default)()); } } i++; } } } return structMemberMap; }