Merge branch 'origin' into glsl-compiler-1
[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 source
83 optional_directive .and .loop source_element .and '\0' .emit ESCAPE_TOKEN .emit TOKEN_END;
84
85 source_element
86 c_style_comment_block .or cpp_style_comment_block .or new_line_directive .or source_token;
87
88 c_style_comment_block
89 '/' .and '*' .and c_style_comment_rest .and .true .emit ' ';
90
91 c_style_comment_rest
92 .loop c_style_comment_body .and c_style_comment_end;
93
94 c_style_comment_body
95 c_style_comment_char_nostar .or c_style_comment_char_star_noslashstar;
96
97 c_style_comment_char_nostar
98 new_line .or '\x2B'-'\xFF' .or '\x01'-'\x29';
99
100 c_style_comment_char_star_noslashstar
101 '*' .and c_style_comment_char_star_noslashstar_1;
102 c_style_comment_char_star_noslashstar_1
103 c_style_comment_char_noslashstar .or c_style_comment_char_star_noslashstar;
104
105 c_style_comment_char_noslashstar
106 new_line .or '\x30'-'\xFF' .or '\x01'-'\x29' .or '\x2B'-'\x2E';
107
108 c_style_comment_end
109 '*' .and .loop c_style_comment_char_star .and '/';
110
111 c_style_comment_char_star
112 '*';
113
114 cpp_style_comment_block
115 '/' .and '/' .and cpp_style_comment_block_1;
116 cpp_style_comment_block_1
117 cpp_style_comment_block_2 .or cpp_style_comment_block_3;
118 cpp_style_comment_block_2
119 .loop cpp_style_comment_char .and new_line_directive;
120 cpp_style_comment_block_3
121 .loop cpp_style_comment_char;
122
123 cpp_style_comment_char
124 '\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
125
126 new_line_directive
127 new_line .and optional_directive;
128
129 new_line
130 generic_new_line .emit '\n';
131
132 generic_new_line
133 carriage_return_line_feed .or line_feed_carriage_return .or '\n' .or '\r';
134
135 carriage_return_line_feed
136 '\r' .and '\n';
137
138 line_feed_carriage_return
139 '\n' .and '\r';
140
141 optional_directive
142 directive .emit ESCAPE_TOKEN .or .true;
143
144 directive
145 dir_define .emit TOKEN_DEFINE .or
146 dir_undef .emit TOKEN_UNDEF .or
147 dir_if .emit TOKEN_IF .or
148 dir_ifdef .emit TOKEN_IF .emit 'd' .emit 'e' .emit 'f' .emit 'i' .emit 'n' .emit 'e' .emit 'd'
149 .emit ' ' .or
150 dir_ifndef .emit TOKEN_IF .emit '!' .emit 'd' .emit 'e' .emit 'f' .emit 'i' .emit 'n' .emit 'e'
151 .emit 'd' .emit ' ' .or
152 dir_else .emit TOKEN_ELSE .or
153 dir_elif .emit TOKEN_ELIF .or
154 dir_endif .emit TOKEN_ENDIF .or
155 dir_ext .emit TOKEN_EXTENSION .or
156 dir_line .emit TOKEN_LINE;
157
158 dir_define
159 optional_space .and '#' .and optional_space .and "define" .and symbol .and opt_parameters .and
160 definition;
161
162 dir_undef
163 optional_space .and '#' .and optional_space .and "undef" .and symbol;
164
165 dir_if
166 optional_space .and '#' .and optional_space .and "if" .and expression;
167
168 dir_ifdef
169 optional_space .and '#' .and optional_space .and "ifdef" .and symbol;
170
171 dir_ifndef
172 optional_space .and '#' .and optional_space .and "ifndef" .and symbol;
173
174 dir_else
175 optional_space .and '#' .and optional_space .and "else";
176
177 dir_elif
178 optional_space .and '#' .and optional_space .and "elif" .and expression;
179
180 dir_endif
181 optional_space .and '#' .and optional_space .and "endif";
182
183 dir_ext
184 optional_space .and '#' .and optional_space .and "extension" .and space .and extension_name .and
185 optional_space .and ':' .and optional_space .and extension_behavior;
186
187 dir_line
188 optional_space .and '#' .and optional_space .and "line" .and expression;
189
190 symbol
191 space .and symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
192
193 opt_parameters
194 parameters .or .true .emit PARAM_END;
195
196 parameters
197 '(' .and parameters_1 .and optional_space .and ')' .emit PARAM_END;
198 parameters_1
199 parameters_2 .or .true;
200 parameters_2
201 parameter .emit PARAM_PARAMETER .and .loop parameters_3;
202 parameters_3
203 optional_space .and ',' .and parameter .emit PARAM_PARAMETER;
204
205 parameter
206 optional_space .and symbol_character .emit * .and .loop symbol_character2 .emit * .and
207 .true .emit '\0';
208
209 definition
210 .loop definition_character .emit * .and .true .emit '\0';
211
212 definition_character
213 '\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
214
215 expression
216 expression_element .and .loop expression_element .and .true .emit '\0';
217
218 expression_element
219 expression_character .emit *;
220
221 expression_character
222 '\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
223
224 extension_name
225 symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
226
227 extension_behavior
228 "require" .emit BEHAVIOR_REQUIRE .or
229 "enable" .emit BEHAVIOR_ENABLE .or
230 "warn" .emit BEHAVIOR_WARN .or
231 "disable" .emit BEHAVIOR_DISABLE;
232
233 optional_space
234 .loop single_space;
235
236 space
237 single_space .and .loop single_space;
238
239 single_space
240 ' ' .or '\t';
241
242 source_token
243 space .emit ' ' .or complex_token .or source_token_1;
244 source_token_1
245 simple_token .emit ' ' .and .true .emit ' ';
246
247 /*
248 * All possible tokens.
249 */
250
251 complex_token
252 identifier .or number;
253
254 simple_token
255 increment .or decrement .or lequal .or gequal .or equal .or nequal .or and .or xor .or or .or
256 addto .or subtractfrom .or multiplyto .or divideto .or other;
257
258 identifier
259 identifier_char1 .emit * .and .loop identifier_char2 .emit *;
260 identifier_char1
261 'a'-'z' .or 'A'-'Z' .or '_';
262 identifier_char2
263 'a'-'z' .or 'A'-'Z' .or '0'-'9' .or '_';
264
265 number
266 float .or integer;
267
268 digit_oct
269 '0'-'7';
270
271 digit_dec
272 '0'-'9';
273
274 digit_hex
275 '0'-'9' .or 'A'-'F' .or 'a'-'f';
276
277 float
278 float_1 .or float_2;
279 float_1
280 float_fractional_constant .and float_optional_exponent_part;
281 float_2
282 float_digit_sequence .and float_exponent_part;
283
284 float_fractional_constant
285 float_fractional_constant_1 .or float_fractional_constant_2 .or float_fractional_constant_3;
286 float_fractional_constant_1
287 float_digit_sequence .and '.' .emit '.' .and float_digit_sequence;
288 float_fractional_constant_2
289 float_digit_sequence .and '.' .emit '.';
290 float_fractional_constant_3
291 '.' .emit '.' .and float_digit_sequence;
292
293 float_optional_exponent_part
294 float_exponent_part .or .true;
295
296 float_digit_sequence
297 digit_dec .emit * .and .loop digit_dec .emit *;
298
299 float_exponent_part
300 float_exponent_part_1 .or float_exponent_part_2;
301 float_exponent_part_1
302 'e' .emit 'e' .and float_optional_sign .and float_digit_sequence;
303 float_exponent_part_2
304 'E' .emit 'E' .and float_optional_sign .and float_digit_sequence;
305
306 float_optional_sign
307 '+' .emit '+' .or '-' .emit '-' .or .true;
308
309 integer
310 integer_hex .or integer_oct .or integer_dec;
311
312 integer_hex
313 '0' .emit '0' .and integer_hex_1 .emit * .and digit_hex .emit * .and
314 .loop digit_hex .emit *;
315 integer_hex_1
316 'x' .or 'X';
317
318 integer_oct
319 '0' .emit '0' .and .loop digit_oct .emit *;
320
321 integer_dec
322 digit_dec .emit * .and .loop digit_dec .emit *;
323
324 increment
325 '+' .emit * .and '+' .emit *;
326
327 decrement
328 '-' .emit * .and '-' .emit *;
329
330 lequal
331 '<' .emit * .and '=' .emit *;
332
333 gequal
334 '>' .emit * .and '=' .emit *;
335
336 equal
337 '=' .emit * .and '=' .emit *;
338
339 nequal
340 '!' .emit * .and '=' .emit *;
341
342 and
343 '&' .emit * .and '&' .emit *;
344
345 xor
346 '^' .emit * .and '^' .emit *;
347
348 or
349 '|' .emit * .and '|' .emit *;
350
351 addto
352 '+' .emit * .and '=' .emit *;
353
354 subtractfrom
355 '-' .emit * .and '=' .emit *;
356
357 multiplyto
358 '*' .emit * .and '=' .emit *;
359
360 divideto
361 '/' .emit * .and '=' .emit *;
362
363 /*
364 * All characters except '\0' and '#'.
365 */
366 other
367 '\x24'-'\xFF' .emit * .or '\x01'-'\x22' .emit *;
368
369 symbol_character
370 'A'-'Z' .or 'a'-'z' .or '_';
371
372 symbol_character2
373 'A'-'Z' .or 'a'-'z' .or '0'-'9' .or '_';
374
375 .string string_lexer;
376
377 string_lexer
378 lex_first_identifier_character .and .loop lex_next_identifier_character;
379
380 lex_first_identifier_character
381 'a'-'z' .or 'A'-'Z' .or '_';
382
383 lex_next_identifier_character
384 'a'-'z' .or 'A'-'Z' .or '0'-'9' .or '_';
385