diff --git a/README.md b/README.md index d7f51d8..ae51fd0 100644 --- a/README.md +++ b/README.md @@ -435,6 +435,7 @@ import { renameBindings, renameFunctions, renameTypes } from '@shaderfrog/glsl-p // ... parse an ast... // Suffix top level variables with _x +// TODO UPDATE THIS renameBindings(ast.scopes[0], (name, node) => `${name}_x`); // Suffix function names with _x renameFunctions(ast.scopes[0], (name, node) => `${name}_x`); diff --git a/package.json b/package.json index dd7985e..eb4665a 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "engines": { "node": ">=16" }, - "version": "5.0.0-beta.2", + "version": "5.0.0-beta.3", "type": "module", "description": "A GLSL ES 1.0 and 3.0 parser and preprocessor that can preserve whitespace and comments", "scripts": { diff --git a/src/parser/scope.test.ts b/src/parser/scope.test.ts index 4ae2e5c..6499050 100644 --- a/src/parser/scope.test.ts +++ b/src/parser/scope.test.ts @@ -361,27 +361,6 @@ StructName_x main(in StructName_x x, StructName_x[3] y) { expect(Object.keys(ast.scopes[2].types)).toEqual(['StructName']); }); -test('shangus', () => { - const ast = c.parseSrc(` -struct MyStruct { float y; }; -attribute vec3 position; -vec3 func() {}`); - - ast.scopes[0].bindings = renameBindings( - ast.scopes[0].bindings, - (name) => `${name}_x` - ); - ast.scopes[0].functions = renameFunctions( - ast.scopes[0].functions, - (name) => `${name}_y` - ); - ast.scopes[0].types = renameTypes(ast.scopes[0].types, (name) => `${name}_z`); - - expect(Object.keys(ast.scopes[0].bindings)).toEqual(['position_x']); - expect(Object.keys(ast.scopes[0].functions)).toEqual(['func_y']); - expect(Object.keys(ast.scopes[0].types)).toEqual(['MyStruct_z']); -}); - test('fn args shadowing global scope identified as separate bindings', () => { const ast = c.parseSrc(` attribute vec3 position; diff --git a/src/parser/test-helpers.ts b/src/parser/test-helpers.ts index a3e2309..5a8615a 100644 --- a/src/parser/test-helpers.ts +++ b/src/parser/test-helpers.ts @@ -67,37 +67,6 @@ export const buildParser = () => { // } // }; -export const debugEntry = (bindings: ScopeIndex) => { - return Object.entries(bindings).map( - ([k, v]) => - `${k}: (${v.references.length} references, ${ - v.declaration ? '' : 'un' - }declared): ${v.references.map((r) => r.type).join(', ')}` - ); -}; -export const debugFunctionEntry = (bindings: FunctionScopeIndex) => - Object.entries(bindings).flatMap(([name, overloads]) => - Object.entries(overloads).map( - ([signature, overload]) => - `${name} (${signature}): (${overload.references.length} references, ${ - overload.declaration ? '' : 'un' - }declared): ${overload.references.map((r) => r.type).join(', ')}` - ) - ); - -export const debugScopes = (astOrScopes: Program | Scope[]) => - console.log( - 'Scopes:', - 'scopes' in astOrScopes - ? astOrScopes.scopes - : astOrScopes.map((s) => ({ - name: s.name, - types: debugEntry(s.types), - bindings: debugEntry(s.bindings), - functions: debugFunctionEntry(s.functions), - })) - ); - const middle = /\/\* start \*\/((.|[\r\n])+)(\/\* end \*\/)?/m; type ParseSrc = (src: string, options?: ParserOptions) => Program; diff --git a/src/parser/utils.ts b/src/parser/utils.ts index 98d4699..86a9c62 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -1,6 +1,8 @@ +import { Program } from '../ast/ast-types.js'; import { FunctionOverloadIndex, FunctionScopeIndex, + Scope, ScopeEntry, ScopeIndex, TypeScopeEntry, @@ -123,3 +125,34 @@ export const renameFunctions = ( ); export const xor = (a: any, b: any): boolean => (a || b) && !(a && b); + +export const debugEntry = (bindings: ScopeIndex) => { + return Object.entries(bindings).map( + ([k, v]) => + `${k}: (${v.references.length} references, ${ + v.declaration ? '' : 'un' + }declared): ${v.references.map((r) => r.type).join(', ')}` + ); +}; +export const debugFunctionEntry = (bindings: FunctionScopeIndex) => + Object.entries(bindings).flatMap(([name, overloads]) => + Object.entries(overloads).map( + ([signature, overload]) => + `${name} (${signature}): (${overload.references.length} references, ${ + overload.declaration ? '' : 'un' + }declared): ${overload.references.map((r) => r.type).join(', ')}` + ) + ); + +export const debugScopes = (astOrScopes: Program | Scope[]) => + console.log( + 'Scopes:', + 'scopes' in astOrScopes + ? astOrScopes.scopes + : astOrScopes.map((s) => ({ + name: s.name, + types: debugEntry(s.types), + bindings: debugEntry(s.bindings), + functions: debugFunctionEntry(s.functions), + })) + );