Merge remote branch 'origin/master' into glsl2
[mesa.git] / src / glsl / glcpp / glcpp-lex.l
1 %{
2 /*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "glcpp.h"
30 #include "glcpp-parse.h"
31
32 /* Flex annoyingly generates some functions without making them
33 * static. Let's declare them here. */
34 int glcpp_get_column (yyscan_t yyscanner);
35 void glcpp_set_column (int column_no , yyscan_t yyscanner);
36
37 #define YY_NO_INPUT
38
39 #define YY_USER_ACTION \
40 do { \
41 yylloc->source = 0; \
42 yylloc->first_column = yycolumn + 1; \
43 yylloc->first_line = yylineno; \
44 yycolumn += yyleng; \
45 } while(0);
46 #define YY_USER_INIT yylineno = 0; yycolumn = 0;
47 %}
48
49 %option bison-bridge bison-locations reentrant noyywrap
50 %option extra-type="glcpp_parser_t *"
51 %option prefix="glcpp_"
52 %option stack
53
54 %x DONE COMMENT UNREACHABLE
55
56 SPACE [[:space:]]
57 NONSPACE [^[:space:]]
58 NEWLINE [\n]
59 HSPACE [ \t]
60 HASH ^{HSPACE}*#{HSPACE}*
61 IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
62 PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
63 OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
64
65 DECIMAL_INTEGER [1-9][0-9]*[uU]?
66 OCTAL_INTEGER 0[0-7]*[uU]?
67 HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
68
69 %%
70
71 /* Single-line comments */
72 "//"[^\n]*\n {
73 yylineno++;
74 yycolumn = 0;
75 return NEWLINE;
76 }
77
78 /* Multi-line comments */
79 "/*" { yy_push_state(COMMENT, yyscanner); }
80 <COMMENT>[^*\n]*
81 <COMMENT>[^*\n]*\n { yylineno++; yycolumn = 0; }
82 <COMMENT>"*"+[^*/\n]*
83 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; }
84 <COMMENT>"*"+"/" {
85 yy_pop_state(yyscanner);
86 if (yyextra->space_tokens)
87 return SPACE;
88 }
89
90 /* glcpp doesn't handle #extension, #version, or #pragma directives.
91 * Simply pass them through to the main compiler's lexer/parser. */
92 {HASH}(extension|version|pragma)[^\n]+ {
93 yylval->str = xtalloc_strdup (yyextra, yytext);
94 yylineno++;
95 yycolumn = 0;
96 return OTHER;
97 }
98
99 {HASH}ifdef/.*\n {
100 yyextra->lexing_if = 1;
101 yyextra->space_tokens = 0;
102 return HASH_IFDEF;
103 }
104
105 {HASH}ifndef/.*\n {
106 yyextra->lexing_if = 1;
107 yyextra->space_tokens = 0;
108 return HASH_IFNDEF;
109 }
110
111 {HASH}if/[^_a-zA-Z0-9].*\n {
112 yyextra->lexing_if = 1;
113 yyextra->space_tokens = 0;
114 return HASH_IF;
115 }
116
117 {HASH}elif/.*\n {
118 yyextra->lexing_if = 1;
119 yyextra->space_tokens = 0;
120 return HASH_ELIF;
121 }
122
123 {HASH}else/.*\n {
124 yyextra->space_tokens = 0;
125 return HASH_ELSE;
126 }
127
128 {HASH}endif/.*\n {
129 yyextra->space_tokens = 0;
130 return HASH_ENDIF;
131 }
132
133 /* When skipping (due to an #if 0 or similar) consume anything
134 * up to a newline. We do this with less priority than any
135 * #if-related directive (#if, #elif, #else, #endif), but with
136 * more priority than any other directive or token to avoid
137 * any side-effects from skipped content.
138 *
139 * We use the lexing_if flag to avoid skipping any part of an
140 * if conditional expression. */
141 [^\n]+/\n {
142 /* Since this rule always matches, YY_USER_ACTION gets called for it,
143 * wrongly incrementing yycolumn. We undo that effect here. */
144 yycolumn -= yyleng;
145 if (yyextra->lexing_if ||
146 yyextra->skip_stack == NULL ||
147 yyextra->skip_stack->type == SKIP_NO_SKIP)
148 {
149 REJECT;
150 }
151 }
152
153 {HASH}error.* {
154 char *p;
155 for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
156 p += 5; /* skip "error" */
157 glcpp_error(yylloc, yyextra, "#error%s", p);
158 }
159
160 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
161 yyextra->space_tokens = 0;
162 return HASH_DEFINE_FUNC;
163 }
164
165 {HASH}define {
166 yyextra->space_tokens = 0;
167 return HASH_DEFINE_OBJ;
168 }
169
170 {HASH}undef {
171 yyextra->space_tokens = 0;
172 return HASH_UNDEF;
173 }
174
175 {HASH} {
176 yyextra->space_tokens = 0;
177 return HASH;
178 }
179
180 {DECIMAL_INTEGER} {
181 yylval->str = xtalloc_strdup (yyextra, yytext);
182 return INTEGER_STRING;
183 }
184
185 {OCTAL_INTEGER} {
186 yylval->str = xtalloc_strdup (yyextra, yytext);
187 return INTEGER_STRING;
188 }
189
190 {HEXADECIMAL_INTEGER} {
191 yylval->str = xtalloc_strdup (yyextra, yytext);
192 return INTEGER_STRING;
193 }
194
195 "<<" {
196 return LEFT_SHIFT;
197 }
198
199 ">>" {
200 return RIGHT_SHIFT;
201 }
202
203 "<=" {
204 return LESS_OR_EQUAL;
205 }
206
207 ">=" {
208 return GREATER_OR_EQUAL;
209 }
210
211 "==" {
212 return EQUAL;
213 }
214
215 "!=" {
216 return NOT_EQUAL;
217 }
218
219 "&&" {
220 return AND;
221 }
222
223 "||" {
224 return OR;
225 }
226
227 "##" {
228 return PASTE;
229 }
230
231 "defined" {
232 return DEFINED;
233 }
234
235 {IDENTIFIER} {
236 yylval->str = xtalloc_strdup (yyextra, yytext);
237 return IDENTIFIER;
238 }
239
240 {PUNCTUATION} {
241 return yytext[0];
242 }
243
244 {OTHER}+ {
245 yylval->str = xtalloc_strdup (yyextra, yytext);
246 return OTHER;
247 }
248
249 {HSPACE}+ {
250 if (yyextra->space_tokens) {
251 return SPACE;
252 }
253 }
254
255 \n {
256 yyextra->lexing_if = 0;
257 yylineno++;
258 yycolumn = 0;
259 return NEWLINE;
260 }
261
262 /* Handle missing newline at EOF. */
263 <INITIAL><<EOF>> {
264 BEGIN DONE; /* Don't keep matching this rule forever. */
265 yyextra->lexing_if = 0;
266 return NEWLINE;
267 }
268
269 /* We don't actually use the UNREACHABLE start condition. We
270 only have this action here so that we can pretend to call some
271 generated functions, (to avoid "defined but not used"
272 warnings. */
273 <UNREACHABLE>. {
274 unput('.');
275 yy_top_state(yyextra);
276 }
277
278 %%
279
280 void
281 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
282 {
283 yy_scan_string(shader, parser->scanner);
284 }