Skip to content

Conversation

@AndrewRayCode
Copy link
Collaborator

@AndrewRayCode AndrewRayCode commented Apr 27, 2025

This change adds a new API to access the parse function directly rather than import the parser. The README has been updated in this branch to reflect the change.

Old way to parse:

import { parser } from '@shaderfrog/glsl-parser';
import { parser as preprocessorParser } from '@shaderfrog/glsl-parser/preprocessor';
const ast = parser.parse(
  preprocessorParser.parse('float a = 1.0')
);

New way to parse:

import { parse } from '@shaderfrog/glsl-parser';
import { parse as preprocessorParse } from '@shaderfrog/glsl-parser/preprocessor';
const ast = parse(
  preprocessorParse('float a = 1.0')
);

parser.parse() exposes a GlslSyntaxError which is an alias for peggy.SyntaxError - the raw error from the Peggy parser. This error has a painful, confusing format() method that should not be exposed to end users.

When using parse() - it wraps the error in one that automatically formats it, hiding the grammarSource

When using parse() instead of parser.parse(), if you're checking the error type in a parse catch block. Old code:

console.log(error instanceof parser.SyntaxError);

New code:

import { GlslSyntaxError } from '@shaderfrog/glsl-parser';
console.log(error instanceof GlslSyntaxError)

Also if you were importing the error type, you need to update your code to import the class instance

import { type GlslSyntaxError } from '@shaderfrog/glsl-parser';

New code:

import { GlslSyntaxError } from '@shaderfrog/glsl-parser';

@AndrewRayCode AndrewRayCode force-pushed the error-format branch 14 times, most recently from 442919b to 84181ee Compare April 27, 2025 22:33
@AndrewRayCode AndrewRayCode changed the title Updating GlslSyntaxError to always show formatted error Breaking change: parse() and updating GlslSyntaxError to always show formatted error Apr 27, 2025
@AndrewRayCode AndrewRayCode changed the title Breaking change: parse() and updating GlslSyntaxError to always show formatted error Breaking change: parser.parse() -> parse() and with better GlslSyntaxError Apr 27, 2025
@AndrewRayCode AndrewRayCode changed the title Breaking change: parser.parse() -> parse() and with better GlslSyntaxError 6.0.0 Breaking change: parser.parse() -> parse() and with better GlslSyntaxError Apr 27, 2025
@AndrewRayCode AndrewRayCode force-pushed the error-format branch 4 times, most recently from d66effb to b835b6c Compare April 28, 2025 00:43
@AndrewRayCode AndrewRayCode changed the title 6.0.0 Breaking change: parser.parse() -> parse() and with better GlslSyntaxError 6.0.0 Breaking change: parser.parse() -> parse() with better error message Apr 28, 2025
@AndrewRayCode AndrewRayCode merged commit ac5e57d into main Apr 28, 2025
1 check passed
@AndrewRayCode AndrewRayCode deleted the error-format branch April 28, 2025 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants