2 * Mesa 3-D graphics library
5 * Copyright (C) 2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * \file slang_pp_directives.syn
27 * slang preprocessor directives parser
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.
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.
45 .emtcode ESCAPE_TOKEN 0
48 * The TOKEN_* symbols follow the ESCAPE_TOKEN.
51 * There is no TOKEN_IFDEF and neither is TOKEN_IFNDEF. They are handled with TOKEN_IF and
53 * The "#ifdef SYMBOL" is replaced with "#if defined SYMBOL"
54 * The "#ifndef SYMBOL" is replaced with "#if !defined SYMBOL"
57 .emtcode TOKEN_DEFINE 1
58 .emtcode TOKEN_UNDEF 2
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
69 * The PARAM_* symbols follow the TOKEN_DEFINE.
72 .emtcode PARAM_PARAMETER 1
75 * The BEHAVIOR_* symbols follow the TOKEN_EXTENSION.
77 .emtcode BEHAVIOR_REQUIRE 1
78 .emtcode BEHAVIOR_ENABLE 2
79 .emtcode BEHAVIOR_WARN 3
80 .emtcode BEHAVIOR_DISABLE 4
83 * The PRAGMA_* symbols follow TOKEN_PRAGMA
85 .emtcode PRAGMA_NO_PARAM 0
86 .emtcode PRAGMA_PARAM 1
89 optional_directive .and .loop source_element .and '\0' .emit ESCAPE_TOKEN .emit TOKEN_END;
92 c_style_comment_block .or cpp_style_comment_block .or new_line_directive .or source_token;
95 '/' .and '*' .and c_style_comment_rest .and .true .emit ' ';
98 .loop c_style_comment_body .and c_style_comment_end;
101 c_style_comment_char_nostar .or c_style_comment_char_star_noslashstar;
103 c_style_comment_char_nostar
104 new_line .or '\x2B'-'\xFF' .or '\x01'-'\x29';
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;
111 c_style_comment_char_noslashstar
112 new_line .or '\x30'-'\xFF' .or '\x01'-'\x29' .or '\x2B'-'\x2E';
115 '*' .and .loop c_style_comment_char_star .and '/';
117 c_style_comment_char_star
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;
129 cpp_style_comment_char
130 '\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
133 new_line .and optional_directive;
136 generic_new_line .emit '\n';
139 carriage_return_line_feed .or line_feed_carriage_return .or '\n' .or '\r';
141 carriage_return_line_feed
144 line_feed_carriage_return
148 directive .emit ESCAPE_TOKEN .or .true;
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'
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;
166 optional_space .and '#' .and optional_space .and "define" .and symbol .and opt_parameters .and
170 optional_space .and '#' .and optional_space .and "undef" .and symbol;
173 optional_space .and '#' .and optional_space .and "if" .and expression;
176 optional_space .and '#' .and optional_space .and "ifdef" .and symbol;
179 optional_space .and '#' .and optional_space .and "ifndef" .and symbol;
182 optional_space .and '#' .and optional_space .and "else";
185 optional_space .and '#' .and optional_space .and "elif" .and expression;
188 optional_space .and '#' .and optional_space .and "endif";
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;
195 optional_space .and '#' .and optional_space .and "line" .and expression;
198 optional_space .and '#' .and optional_space .and "pragma" .and symbol .and opt_pragma_param;
202 pragma_param .or .true .emit PRAGMA_NO_PARAM;
205 optional_space .and '(' .emit PRAGMA_PARAM .and optional_space .and symbol_no_space .and optional_space .and ')';
208 symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
211 space .and symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
214 parameters .or .true .emit PARAM_END;
217 '(' .and parameters_1 .and optional_space .and ')' .emit PARAM_END;
219 parameters_2 .or .true;
221 parameter .emit PARAM_PARAMETER .and .loop parameters_3;
223 optional_space .and ',' .and parameter .emit PARAM_PARAMETER;
226 optional_space .and symbol_character .emit * .and .loop symbol_character2 .emit * .and
230 .loop definition_character .emit * .and .true .emit '\0';
233 '\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
236 expression_element .and .loop expression_element .and .true .emit '\0';
239 expression_character .emit *;
242 '\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
245 symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
248 "require" .emit BEHAVIOR_REQUIRE .or
249 "enable" .emit BEHAVIOR_ENABLE .or
250 "warn" .emit BEHAVIOR_WARN .or
251 "disable" .emit BEHAVIOR_DISABLE;
257 single_space .and .loop single_space;
263 space .emit ' ' .or complex_token .or source_token_1;
265 simple_token .emit ' ' .and .true .emit ' ';
268 * All possible tokens.
272 identifier .or number;
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;
279 identifier_char1 .emit * .and .loop identifier_char2 .emit *;
281 'a'-'z' .or 'A'-'Z' .or '_';
283 'a'-'z' .or 'A'-'Z' .or '0'-'9' .or '_';
295 '0'-'9' .or 'A'-'F' .or 'a'-'f';
300 float_fractional_constant .and float_optional_exponent_part;
302 float_digit_sequence .and float_exponent_part;
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;
313 float_optional_exponent_part
314 float_exponent_part .or .true;
317 digit_dec .emit * .and .loop digit_dec .emit *;
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;
327 '+' .emit '+' .or '-' .emit '-' .or .true;
330 integer_hex .or integer_oct .or integer_dec;
333 '0' .emit '0' .and integer_hex_1 .emit * .and digit_hex .emit * .and
334 .loop digit_hex .emit *;
339 '0' .emit '0' .and .loop digit_oct .emit *;
342 digit_dec .emit * .and .loop digit_dec .emit *;
345 '+' .emit * .and '+' .emit *;
348 '-' .emit * .and '-' .emit *;
351 '<' .emit * .and '=' .emit *;
354 '>' .emit * .and '=' .emit *;
357 '=' .emit * .and '=' .emit *;
360 '!' .emit * .and '=' .emit *;
363 '&' .emit * .and '&' .emit *;
366 '^' .emit * .and '^' .emit *;
369 '|' .emit * .and '|' .emit *;
372 '+' .emit * .and '=' .emit *;
375 '-' .emit * .and '=' .emit *;
378 '*' .emit * .and '=' .emit *;
381 '/' .emit * .and '=' .emit *;
384 * All characters except '\0' and '#'.
387 '\x24'-'\xFF' .emit * .or '\x01'-'\x22' .emit *;
390 'A'-'Z' .or 'a'-'z' .or '_';
393 'A'-'Z' .or 'a'-'z' .or '0'-'9' .or '_';
395 .string string_lexer;
398 lex_first_identifier_character .and .loop lex_next_identifier_character;
400 lex_first_identifier_character
401 'a'-'z' .or 'A'-'Z' .or '_';
403 lex_next_identifier_character
404 'a'-'z' .or 'A'-'Z' .or '0'-'9' .or '_';