Skip to content

Commit

Permalink
Cleanup and prep for release
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRayCode committed Jul 28, 2024
1 parent 3613d34 commit e61bd62
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ type ParserOptions = {
// like undefined functions and variables. If `failOnWarn` is set to true,
// warnings will still cause the parser to raise an error. Defaults to false.
quiet: boolean;

// An optional string representing the origin of the GLSL, for debugging and
// error messages. For example, "main.js". If the parser raises an error, the
// grammarSource shows up in the error.source field. If you format the error
// (see the errors section), the grammarSource shows up in the formatted error
// string. Defaults to undefined.
grammarSource: string;

// If true, sets location information on each AST node, in the form of
// { column: number, line: number, offset: number }. Defaults to false.
includeLocation: boolean;

// If true, causes the parser to raise an error instead of log a warning.
// The parser does limited type checking, and things like undeclared variables
// are treated as warnings. Defaults to false.
Expand Down Expand Up @@ -431,36 +434,39 @@ visitors.
You can rename bindings (aka variables), functions, and types (aka structs) with `renameBindings`, `renameFunctions`, and `renameTypes` respectively.
The signature for these methods generally looks like:
The signature for these methods:
```ts
const renameBindings = (
// The scope to rename the bindings in. ast.scopes[0] is the global scope.
// Passing this ast.scopes[0] renames all global variables
bindings: ScopeIndex,

// The rename function. This is called once per scope entry with the original
// name in the scope, to generate the renamed variable.
mangle: (name: string) => string
// The renameX() functions do two things:
// 1. Each function *mutates* the AST to rename identifiers in place.
// 2. It *returns* an *immutable* new ScopeIndex where the scope referneces
// themselves are renamed.
// If you want your ast.scopes array to stay in sync with your AST, you need to
// re-assign it to the output of the functions! See examples below.
): ScopeIndex
```
These scope renaming functions, `renameBindings`, `renameFunctions`, and `renameTypes`, do two things:
1. Each function *mutates* the AST to rename identifiers in place.
2. They *return* an *immutable* new ScopeIndex where the scope references
themselves are renamed.
If you want your ast.scopes array to stay in sync with your AST, you need to
re-assign it to the output of the functions! Examples:
```typescript
import { renameBindings, renameFunctions, renameTypes } from '@shaderfrog/glsl-parser/utils';

// Suffix top level variables with _x, and update the scope
ast.scopes[0] = renameBindings(ast.scopes[0], (name) => `${name}_x`);
ast.scopes[0].bindings = renameBindings(ast.scopes[0].bindings, (name) => `${name}_x`);

// Suffix function names with _x
ast.scopes[0] = renameFunctions(ast.scopes[0], (name) => `${name}_x`);
ast.scopes[0].functions = renameFunctions(ast.scopes[0].functions, (name) => `${name}_x`);

// Suffix struct names and usages (including constructors) with _x
ast.scopes[0] = renameTypes(ast.scopes[0], (name) => `${name}_x`);
ast.scopes[0].types = renameTypes(ast.scopes[0].types, (name) => `${name}_x`);
```
There are also functions to rename only one variable/identifier in a given
Expand All @@ -469,7 +475,8 @@ scope. Use these if you know specifically which variable you want to rename.
```typescript
import { renameBinding, renameFunction, renameType } from '@shaderfrog/glsl-parser/utils';

// Replace all instances of "oldVar" with "newVar" (in the global scope)
// Replace all instances of "oldVar" with "newVar" in the global scope, and
// creates a new global scope entry named "newVar"
ast.scopes[0].bindings.newVar = renameBinding(
ast.scopes[0].bindings.oldVar,
'newVar',
Expand All @@ -495,7 +502,7 @@ delete ast.scopes[0].functions.oldType;
#### Debugging utility functions
The parser also exports some debugging functions, useful for logging information
The parser also exports a debugging function, useful for logging information
about the AST.
```ts
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"engines": {
"node": ">=16"
},
"version": "5.0.0-beta.4",
"version": "5.0.0",
"type": "module",
"description": "A GLSL ES 1.0 and 3.0 parser and preprocessor that can preserve whitespace and comments",
"scripts": {
Expand Down

0 comments on commit e61bd62

Please sign in to comment.