1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
/*
* Mesa 3-D graphics library
* Version: 6.6
*
* Copyright (C) 2006 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* \file slang_pp_directives.syn
* slang preprocessor directives parser
* \author Michal Krol
*/
.syntax source;
/*
* This syntax script preprocesses a GLSL shader.
* It is assumed, that the #version directive has been parsed. Separate pass for parsing
* version gives better control on behavior depending on the version number given.
*
* The output is a source string with comments and directives removed. White spaces and comments
* are replaced with on or more spaces. All new-lines are preserved and converted to Linux format.
* Directives are escaped with a null character. The end of the source string is marked by
* two consecutive null characters. The consumer is responsible for executing the escaped
* directives, removing dead portions of code and expanding macros.
*/
.emtcode ESCAPE_TOKEN 0
/*
* The TOKEN_* symbols follow the ESCAPE_TOKEN.
*
* NOTE:
* There is no TOKEN_IFDEF and neither is TOKEN_IFNDEF. They are handled with TOKEN_IF and
* operator defined.
* The "#ifdef SYMBOL" is replaced with "#if defined SYMBOL"
* The "#ifndef SYMBOL" is replaced with "#if !defined SYMBOL"
*/
.emtcode TOKEN_END 0
.emtcode TOKEN_DEFINE 1
.emtcode TOKEN_UNDEF 2
.emtcode TOKEN_IF 3
.emtcode TOKEN_ELSE 4
.emtcode TOKEN_ELIF 5
.emtcode TOKEN_ENDIF 6
.emtcode TOKEN_ERROR 7
.emtcode TOKEN_PRAGMA 8
.emtcode TOKEN_EXTENSION 9
.emtcode TOKEN_LINE 10
/*
* The PARAM_* symbols follow the TOKEN_DEFINE.
*/
.emtcode PARAM_END 0
.emtcode PARAM_PARAMETER 1
/*
* The BEHAVIOR_* symbols follow the TOKEN_EXTENSION.
*/
.emtcode BEHAVIOR_REQUIRE 1
.emtcode BEHAVIOR_ENABLE 2
.emtcode BEHAVIOR_WARN 3
.emtcode BEHAVIOR_DISABLE 4
/*
* The PRAGMA_* symbols follow TOKEN_PRAGMA
*/
.emtcode PRAGMA_NO_PARAM 0
.emtcode PRAGMA_PARAM 1
source
optional_directive .and .loop source_element .and '\0' .emit ESCAPE_TOKEN .emit TOKEN_END;
source_element
c_style_comment_block .or cpp_style_comment_block .or new_line_directive .or source_token;
c_style_comment_block
'/' .and '*' .and c_style_comment_rest .and .true .emit ' ';
c_style_comment_rest
.loop c_style_comment_body .and c_style_comment_end;
c_style_comment_body
c_style_comment_char_nostar .or c_style_comment_char_star_noslashstar;
c_style_comment_char_nostar
new_line .or '\x2B'-'\xFF' .or '\x01'-'\x29';
c_style_comment_char_star_noslashstar
'*' .and c_style_comment_char_star_noslashstar_1;
c_style_comment_char_star_noslashstar_1
c_style_comment_char_noslashstar .or c_style_comment_char_star_noslashstar;
c_style_comment_char_noslashstar
new_line .or '\x30'-'\xFF' .or '\x01'-'\x29' .or '\x2B'-'\x2E';
c_style_comment_end
'*' .and .loop c_style_comment_char_star .and '/';
c_style_comment_char_star
'*';
cpp_style_comment_block
'/' .and '/' .and cpp_style_comment_block_1;
cpp_style_comment_block_1
cpp_style_comment_block_2 .or cpp_style_comment_block_3;
cpp_style_comment_block_2
.loop cpp_style_comment_char .and new_line_directive;
cpp_style_comment_block_3
.loop cpp_style_comment_char;
cpp_style_comment_char
'\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
new_line_directive
new_line .and optional_directive;
new_line
generic_new_line .emit '\n';
generic_new_line
carriage_return_line_feed .or line_feed_carriage_return .or '\n' .or '\r';
carriage_return_line_feed
'\r' .and '\n';
line_feed_carriage_return
'\n' .and '\r';
optional_directive
directive .emit ESCAPE_TOKEN .or .true;
directive
dir_define .emit TOKEN_DEFINE .or
dir_undef .emit TOKEN_UNDEF .or
dir_if .emit TOKEN_IF .or
dir_ifdef .emit TOKEN_IF .emit 'd' .emit 'e' .emit 'f' .emit 'i' .emit 'n' .emit 'e' .emit 'd'
.emit ' ' .or
dir_ifndef .emit TOKEN_IF .emit '!' .emit 'd' .emit 'e' .emit 'f' .emit 'i' .emit 'n' .emit 'e'
.emit 'd' .emit ' ' .or
dir_else .emit TOKEN_ELSE .or
dir_elif .emit TOKEN_ELIF .or
dir_endif .emit TOKEN_ENDIF .or
dir_ext .emit TOKEN_EXTENSION .or
dir_pragma .emit TOKEN_PRAGMA .or
dir_line .emit TOKEN_LINE;
dir_define
optional_space .and '#' .and optional_space .and "define" .and symbol .and opt_parameters .and
definition;
dir_undef
optional_space .and '#' .and optional_space .and "undef" .and symbol;
dir_if
optional_space .and '#' .and optional_space .and "if" .and expression;
dir_ifdef
optional_space .and '#' .and optional_space .and "ifdef" .and symbol;
dir_ifndef
optional_space .and '#' .and optional_space .and "ifndef" .and symbol;
dir_else
optional_space .and '#' .and optional_space .and "else";
dir_elif
optional_space .and '#' .and optional_space .and "elif" .and expression;
dir_endif
optional_space .and '#' .and optional_space .and "endif";
dir_ext
optional_space .and '#' .and optional_space .and "extension" .and space .and extension_name .and
optional_space .and ':' .and optional_space .and extension_behavior;
dir_line
optional_space .and '#' .and optional_space .and "line" .and expression;
dir_pragma
optional_space .and '#' .and optional_space .and "pragma" .and symbol .and opt_pragma_param;
opt_pragma_param
pragma_param .or .true .emit PRAGMA_NO_PARAM;
pragma_param
optional_space .and '(' .emit PRAGMA_PARAM .and optional_space .and symbol_no_space .and optional_space .and ')';
symbol_no_space
symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
symbol
space .and symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
opt_parameters
parameters .or .true .emit PARAM_END;
parameters
'(' .and parameters_1 .and optional_space .and ')' .emit PARAM_END;
parameters_1
parameters_2 .or .true;
parameters_2
parameter .emit PARAM_PARAMETER .and .loop parameters_3;
parameters_3
optional_space .and ',' .and parameter .emit PARAM_PARAMETER;
parameter
optional_space .and symbol_character .emit * .and .loop symbol_character2 .emit * .and
.true .emit '\0';
definition
.loop definition_character .emit * .and .true .emit '\0';
definition_character
'\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
expression
expression_element .and .loop expression_element .and .true .emit '\0';
expression_element
expression_character .emit *;
expression_character
'\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
extension_name
symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
extension_behavior
"require" .emit BEHAVIOR_REQUIRE .or
"enable" .emit BEHAVIOR_ENABLE .or
"warn" .emit BEHAVIOR_WARN .or
"disable" .emit BEHAVIOR_DISABLE;
optional_space
.loop single_space;
space
single_space .and .loop single_space;
single_space
' ' .or '\t';
source_token
space .emit ' ' .or complex_token .or source_token_1;
source_token_1
simple_token .emit ' ' .and .true .emit ' ';
/*
* All possible tokens.
*/
complex_token
identifier .or number;
simple_token
increment .or decrement .or lequal .or gequal .or equal .or nequal .or and .or xor .or or .or
addto .or subtractfrom .or multiplyto .or divideto .or other;
identifier
identifier_char1 .emit * .and .loop identifier_char2 .emit *;
identifier_char1
'a'-'z' .or 'A'-'Z' .or '_';
identifier_char2
'a'-'z' .or 'A'-'Z' .or '0'-'9' .or '_';
number
float .or integer;
digit_oct
'0'-'7';
digit_dec
'0'-'9';
digit_hex
'0'-'9' .or 'A'-'F' .or 'a'-'f';
float
float_1 .or float_2;
float_1
float_fractional_constant .and float_optional_exponent_part;
float_2
float_digit_sequence .and float_exponent_part;
float_fractional_constant
float_fractional_constant_1 .or float_fractional_constant_2 .or float_fractional_constant_3;
float_fractional_constant_1
float_digit_sequence .and '.' .emit '.' .and float_digit_sequence;
float_fractional_constant_2
float_digit_sequence .and '.' .emit '.';
float_fractional_constant_3
'.' .emit '.' .and float_digit_sequence;
float_optional_exponent_part
float_exponent_part .or .true;
float_digit_sequence
digit_dec .emit * .and .loop digit_dec .emit *;
float_exponent_part
float_exponent_part_1 .or float_exponent_part_2;
float_exponent_part_1
'e' .emit 'e' .and float_optional_sign .and float_digit_sequence;
float_exponent_part_2
'E' .emit 'E' .and float_optional_sign .and float_digit_sequence;
float_optional_sign
'+' .emit '+' .or '-' .emit '-' .or .true;
integer
integer_hex .or integer_oct .or integer_dec;
integer_hex
'0' .emit '0' .and integer_hex_1 .emit * .and digit_hex .emit * .and
.loop digit_hex .emit *;
integer_hex_1
'x' .or 'X';
integer_oct
'0' .emit '0' .and .loop digit_oct .emit *;
integer_dec
digit_dec .emit * .and .loop digit_dec .emit *;
increment
'+' .emit * .and '+' .emit *;
decrement
'-' .emit * .and '-' .emit *;
lequal
'<' .emit * .and '=' .emit *;
gequal
'>' .emit * .and '=' .emit *;
equal
'=' .emit * .and '=' .emit *;
nequal
'!' .emit * .and '=' .emit *;
and
'&' .emit * .and '&' .emit *;
xor
'^' .emit * .and '^' .emit *;
or
'|' .emit * .and '|' .emit *;
addto
'+' .emit * .and '=' .emit *;
subtractfrom
'-' .emit * .and '=' .emit *;
multiplyto
'*' .emit * .and '=' .emit *;
divideto
'/' .emit * .and '=' .emit *;
/*
* All characters except '\0' and '#'.
*/
other
'\x24'-'\xFF' .emit * .or '\x01'-'\x22' .emit *;
symbol_character
'A'-'Z' .or 'a'-'z' .or '_';
symbol_character2
'A'-'Z' .or 'a'-'z' .or '0'-'9' .or '_';
.string string_lexer;
string_lexer
lex_first_identifier_character .and .loop lex_next_identifier_character;
lex_first_identifier_character
'a'-'z' .or 'A'-'Z' .or '_';
lex_next_identifier_character
'a'-'z' .or 'A'-'Z' .or '0'-'9' .or '_';
|