@@ -39,18 +39,18 @@ npm install --save @shaderfrog/glsl-parser
39
39
## Parsing
40
40
41
41
``` typescript
42
- import { parser , generate } from ' @shaderfrog/glsl-parser' ;
42
+ import { parse , generate } from ' @shaderfrog/glsl-parser' ;
43
43
44
44
// To parse a GLSL program's source code into an AST:
45
- const program = parser . parse (' float a = 1.0;' );
45
+ const program = parse (' float a = 1.0;' );
46
46
47
47
// To turn a parsed AST back into a source program
48
48
const transpiled = generate (program );
49
49
```
50
50
51
51
The parser accepts an optional second ` options ` argument:
52
52
``` js
53
- parser . parse (' float a = 1.0;' , options);
53
+ parse (' float a = 1.0;' , options);
54
54
```
55
55
56
56
Where ` options ` is:
@@ -72,28 +72,26 @@ type ParserOptions = {
72
72
// Hide warnings. If set to false or not set, then the parser logs warnings
73
73
// like undefined functions and variables. If `failOnWarn` is set to true,
74
74
// warnings will still cause the parser to raise an error. Defaults to false.
75
- quiet: boolean ;
76
-
77
- // An optional string representing the origin of the GLSL, for debugging and
78
- // error messages. For example, "main.js". If the parser raises an error, the
79
- // grammarSource shows up in the error.source field. If you format the error
80
- // (see the errors section), the grammarSource shows up in the formatted error
81
- // string. Defaults to undefined.
82
- grammarSource: string ;
75
+ quiet? : boolean ;
83
76
84
77
// If true, sets location information on each AST node, in the form of
85
78
// { column: number, line: number, offset: number }. Defaults to false.
86
- includeLocation: boolean ;
79
+ includeLocation? : boolean ;
87
80
88
81
// If true, causes the parser to raise an error instead of log a warning.
89
82
// The parser does limited type checking, and things like undeclared variables
90
83
// are treated as warnings. Defaults to false.
91
- failOnWarn: boolean ;
84
+ failOnWarn? : boolean ;
85
+
86
+ // An optional string representing the file name of your GLSL For example,
87
+ // you can pass "main.glsl" here. If an error is raised, the error message
88
+ // will show "main.glsl:row:column". This is only used in the error message.
89
+ grammarSource? : string ;
92
90
}
93
91
` ` `
94
92
95
93
## The program type
96
- ` parser . parse ()` returns a ` Program ` , which is a special AST Node:
94
+ ` parse ()` returns a ` Program ` , which is a special AST Node:
97
95
98
96
` ` ` typescript
99
97
interface Program {
@@ -194,27 +192,23 @@ See also [Utility-Functions](#Utility-Functions) for renaming scope references.
194
192
195
193
## Errors
196
194
197
- If you have invalid GLSL, the parser throws a ` GlslSyntaxError ` . The parser uses
198
- Peggy under the hood, so ` GlslSyntaxError ` is a convenience type alias for a
199
- ` peggy.SyntaxError ` .
195
+ If you have invalid GLSL, the parser throws a ` GlslSyntaxError ` .
200
196
201
197
``` ts
202
- import { parser , GlslSyntaxError } from ' @shaderfrog/glsl-parser' ;
198
+ import { parse , GlslSyntaxError } from ' @shaderfrog/glsl-parser' ;
203
199
204
200
let error: GlslSyntaxError | undefined ;
205
201
try {
206
202
// Line without a semicolon
207
- c . parse (` float a ` );
203
+ parse (` float a ` );
208
204
} catch (e ) {
209
205
error = e as GlslSyntaxError ;
210
206
}
211
207
```
212
208
213
- If you want to check if a caught error is an ` instanceof ` a ` GlslSyntaxError ` ,
214
- then you need to use the Peggy ` SyntaxError ` error object, which lives on the
215
- parser:
209
+ If you want to check if a caught error is an ` instanceof ` a ` GlslSyntaxError ` :
216
210
``` ts
217
- console .log (error instanceof parser . SyntaxError )
211
+ console .log (error instanceof GlslSyntaxError )
218
212
// true
219
213
```
220
214
@@ -224,7 +218,13 @@ should be safe to cast it to a `GlslSyntaxError` with `as` in Typescript.
224
218
The error message string is automatically generated by Peggy:
225
219
``` ts
226
220
console .log (error .message )
227
- // 'Expected ",", ";", "=", or array specifier but end of input found'
221
+ /*
222
+ Error: Expected ",", ";", "=", or array specifier but "f" found.
223
+ --> undefined:2:1
224
+ |
225
+ 2 | float b
226
+ | ^
227
+ */
228
228
```
229
229
230
230
The error object includes the location of the error. It is not printed in the
@@ -241,26 +241,10 @@ console.log(error.location)
241
241
```
242
242
Note the ` source ` field on the error object is the ` grammarSource ` string
243
243
provided to the parser options, which is ` undefined ` by default. If you pass in
244
- a ` grammarSource ` to ` parser. parse()` , it shows up in the error object. This is
244
+ a ` grammarSource ` to ` parse() ` , it shows up in the error object. This is
245
245
meant to help you track which source file you're parsing, for example you could
246
- enter ` "myfile.glsl" ` as an argument to ` parser. parse()` so that the error
246
+ enter ` "myfile.glsl" ` as an argument to ` parse() ` so that the error
247
247
includes that your source GLSL came from your application's ` myfile.glsl ` file.
248
-
249
- The error object also has a fairly confusing ` format() ` method, which comes
250
- from the underlying Peggy error object. It produces an ASCII formatted string
251
- with arrows and underlines. The ` source ` option passed to ` .format() ` ** must**
252
- match your ` grammarSource ` in parser options (which is ` undefined ` by default).
253
- This API is awkward and I might override it in future versions of the parser.
254
-
255
- ``` ts
256
- console .log (error .format ([{ text , source: undefined }])
257
- /*
258
- Error: Expected ",", ";", "=", or array specifier but "f" found.
259
- --> undefined:2:1
260
- |
261
- 2 | float b
262
- | ^
263
- */
264
248
```
265
249
266
250
## Preprocessing
@@ -311,14 +295,14 @@ import {
311
295
preprocessAst ,
312
296
preprocessComments ,
313
297
generate ,
314
- parser ,
298
+ parse ,
315
299
} from ' @shaderfrog/glsl-parser/preprocessor' ;
316
300
317
301
// Remove comments before preprocessing
318
302
const commentsRemoved = preprocessComments (` float a = 1.0; ` );
319
303
320
304
// Parse the source text into an AST
321
- const ast = parser . parse (commentsRemoved );
305
+ const ast = parse (commentsRemoved );
322
306
323
307
// Then preprocess it, expanding #defines, evaluating #ifs, etc
324
308
preprocessAst (ast );
@@ -328,6 +312,23 @@ preprocessAst(ast);
328
312
const preprocessed = generate (ast );
329
313
```
330
314
315
+ ## Accessing the raw Peggy parser
316
+ If you need to access the Peggy compiled parser directly, import the ` parser ` .
317
+ You can manually call ` parser.parse() ` if you prefer. Note if there are errors,
318
+ rather than returning a ` GlslSyntaxError ` , it will return the underlying
319
+ ` peg$syntaxError ` .
320
+
321
+ ``` typescript
322
+ import { parser } from ' @shaderfrog/glsl-parser' ;
323
+ import { parser as preprocessorParser } from ' @shaderfrog/glsl-parser/preprocessor' ;
324
+
325
+ try {
326
+ const ast = parser .parse (' float f = 1.0;' );
327
+ } catch (e ) {
328
+ console .log (e instanceof parser .SyntaxError ); // true
329
+ }
330
+ ```
331
+
331
332
## Manipulating and Searching ASTs
332
333
333
334
### Visitors
@@ -409,12 +410,12 @@ follow the same convention outlined above.
409
410
410
411
``` typescript
411
412
import {
412
- parser ,
413
+ parse ,
413
414
visitPreprocessedAst ,
414
415
} from ' @shaderfrog/glsl-parser/preprocessor' ;
415
416
416
417
// Parse the source text into an AST
417
- const ast = parser . parse (` float a = 1.0; ` );
418
+ const ast = parse (` float a = 1.0; ` );
418
419
visitPreprocessedAst (ast , visitors );
419
420
```
420
421
0 commit comments