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 optional_directive .and .loop source_element .and '\0' .emit ESCAPE_TOKEN .emit TOKEN_END;
86 c_style_comment_block .or cpp_style_comment_block .or new_line_directive .or source_token;
89 '/' .and '*' .and c_style_comment_rest .and .true .emit ' ';
92 .loop c_style_comment_body .and c_style_comment_end;
95 c_style_comment_char_nostar .or c_style_comment_char_star_noslashstar;
97 c_style_comment_char_nostar
98 new_line .or '\x2B'-'\xFF' .or '\x01'-'\x29';
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;
105 c_style_comment_char_noslashstar
106 new_line .or '\x30'-'\xFF' .or '\x01'-'\x29' .or '\x2B'-'\x2E';
109 '*' .and .loop c_style_comment_char_star .and '/';
111 c_style_comment_char_star
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;
123 cpp_style_comment_char
124 '\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
127 new_line .and optional_directive;
130 generic_new_line .emit '\n';
133 carriage_return_line_feed .or line_feed_carriage_return .or '\n' .or '\r';
135 carriage_return_line_feed
138 line_feed_carriage_return
142 directive .emit ESCAPE_TOKEN .or .true;
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'
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;
159 optional_space .and '#' .and optional_space .and "define" .and symbol .and opt_parameters .and
163 optional_space .and '#' .and optional_space .and "undef" .and symbol;
166 optional_space .and '#' .and optional_space .and "if" .and expression;
169 optional_space .and '#' .and optional_space .and "ifdef" .and symbol;
172 optional_space .and '#' .and optional_space .and "ifndef" .and symbol;
175 optional_space .and '#' .and optional_space .and "else";
178 optional_space .and '#' .and optional_space .and "elif" .and expression;
181 optional_space .and '#' .and optional_space .and "endif";
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;
188 optional_space .and '#' .and optional_space .and "line" .and expression;
191 space .and symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
194 parameters .or .true .emit PARAM_END;
197 '(' .and parameters_1 .and optional_space .and ')' .emit PARAM_END;
199 parameters_2 .or .true;
201 parameter .emit PARAM_PARAMETER .and .loop parameters_3;
203 optional_space .and ',' .and parameter .emit PARAM_PARAMETER;
206 optional_space .and symbol_character .emit * .and .loop symbol_character2 .emit * .and
210 .loop definition_character .emit * .and .true .emit '\0';
213 '\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
216 expression_element .and .loop expression_element .and .true .emit '\0';
219 expression_character .emit *;
222 '\x0E'-'\xFF' .or '\x01'-'\x09' .or '\x0B'-'\x0C';
225 symbol_character .emit * .and .loop symbol_character2 .emit * .and .true .emit '\0';
228 "require" .emit BEHAVIOR_REQUIRE .or
229 "enable" .emit BEHAVIOR_ENABLE .or
230 "warn" .emit BEHAVIOR_WARN .or
231 "disable" .emit BEHAVIOR_DISABLE;
237 single_space .and .loop single_space;
243 space .emit ' ' .or complex_token .or source_token_1;
245 simple_token .emit ' ' .and .true .emit ' ';
248 * All possible tokens.
252 identifier .or number;
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;
259 identifier_char1 .emit * .and .loop identifier_char2 .emit *;
261 'a'-'z' .or 'A'-'Z' .or '_';
263 'a'-'z' .or 'A'-'Z' .or '0'-'9' .or '_';
275 '0'-'9' .or 'A'-'F' .or 'a'-'f';
280 float_fractional_constant .and float_optional_exponent_part;
282 float_digit_sequence .and float_exponent_part;
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;
293 float_optional_exponent_part
294 float_exponent_part .or .true;
297 digit_dec .emit * .and .loop digit_dec .emit *;
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;
307 '+' .emit '+' .or '-' .emit '-' .or .true;
310 integer_hex .or integer_oct .or integer_dec;
313 '0' .emit '0' .and integer_hex_1 .emit * .and digit_hex .emit * .and
314 .loop digit_hex .emit *;
319 '0' .emit '0' .and .loop digit_oct .emit *;
322 digit_dec .emit * .and .loop digit_dec .emit *;
325 '+' .emit * .and '+' .emit *;
328 '-' .emit * .and '-' .emit *;
331 '<' .emit * .and '=' .emit *;
334 '>' .emit * .and '=' .emit *;
337 '=' .emit * .and '=' .emit *;
340 '!' .emit * .and '=' .emit *;
343 '&' .emit * .and '&' .emit *;
346 '^' .emit * .and '^' .emit *;
349 '|' .emit * .and '|' .emit *;
352 '+' .emit * .and '=' .emit *;
355 '-' .emit * .and '=' .emit *;
358 '*' .emit * .and '=' .emit *;
361 '/' .emit * .and '=' .emit *;
364 * All characters except '\0' and '#'.
367 '\x24'-'\xFF' .emit * .or '\x01'-'\x22' .emit *;
370 'A'-'Z' .or 'a'-'z' .or '_';
373 'A'-'Z' .or 'a'-'z' .or '0'-'9' .or '_';
375 .string string_lexer;
378 lex_first_identifier_character .and .loop lex_next_identifier_character;
380 lex_first_identifier_character
381 'a'-'z' .or 'A'-'Z' .or '_';
383 lex_next_identifier_character
384 'a'-'z' .or 'A'-'Z' .or '0'-'9' .or '_';