@@ -47,10 +47,11 @@ type ParserOptions = {
4747 // like undefined functions and variables. If `failOnWarn` is set to true,
4848 // warnings will still cause the parser to raise an error. Defaults to false.
4949 quiet: boolean ;
50- // The origin of the GLSL, for debugging. For example, "main.js", If the
51- // parser raises an error (specifically a GrammarError), and you call
52- // error.format([]) on it, the error shows { source: 'main.js', ... }.
53- // Defaults to null.
50+ // An optional string reprsenting the origin of the GLSL, for debugging and
51+ // error messages. For example, "main.js". If the parser raises an error, the
52+ // grammarSource shows up in the error.source field. If you format the error
53+ // (see the errors section), the grammarSource shows up in the formatted error
54+ // string. Defaults to undefined.
5455 grammarSource: string ;
5556 // If true, sets location information on each AST node, in the form of
5657 // { column: number, line: number, offset: number }. Defaults to false.
@@ -207,6 +208,65 @@ matches based on the name and arity of the functions.
207208
208209See also [ #Utility-Functions] for renaming scope references.
209210
211+ ## Errors
212+
213+ If you have invalid GLSL, the parser throws a ` GlslSyntaxError ` , which is a type
214+ alias for ` peggy.SyntaxError ` .
215+
216+ ``` ts
217+ import { parser , GlslSyntaxError } from ' @shaderfrog/glsl-parser' ;
218+
219+ let error: GlslSyntaxError | undefined ;
220+ try {
221+ // Line without a semicolon
222+ c .parse (` float a ` );
223+ } catch (e ) {
224+ error = e as GlslSyntaxError ;
225+ }
226+ ```
227+
228+ The error class lives on the parser object itself:
229+ ``` ts
230+ console .log (error instanceof parser .SyntaxError )
231+ // true
232+ ```
233+
234+ The error message is automatically generated by Peggy:
235+ ``` ts
236+ console .log (error .message )
237+ // 'Expected ",", ";", "=", or array specifier but end of input found'
238+ ```
239+
240+ It includes the location of the error. Note ` source ` is the ` grammarSource `
241+ string provided to the parser options, which is ` undefined ` by default.
242+ ``` ts
243+ console .log (error .location )
244+ /*
245+ {
246+ source: undefined,
247+ start: { offset: 7, line: 1, column: 8 },
248+ end: { offset: 7, line: 1, column: 8 }
249+ }
250+ */
251+ ```
252+
253+ The standard Peggy error object also has a fairly confusing ` format() ` method,
254+ which produces an ASCII formatted string with arrows and underlines. The
255+ ` source ` option passed to ` .format() ` ** must** match your ` grammarSource ` in
256+ parser options (which is ` undefined ` by default). This API is awkward and I
257+ might override it in future versions of the parser.
258+
259+ ``` ts
260+ console .log (error .format ([{ text , source: undefined }])
261+ /*
262+ Error: Expected ",", ";", "=", or array specifier but "f" found.
263+ --> undefined:2:1
264+ |
265+ 2 | float b
266+ | ^
267+ */
268+ ` ` `
269+
210270## Manipulating and Searching ASTs
211271
212272### Visitors
0 commit comments