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