Tizen 2.0 Release
[profile/ivi/osmesa.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 #ifdef _MSC_VER
38 #define YY_NO_UNISTD_H
39 #endif
40
41 #define YY_NO_INPUT
42
43 #define YY_USER_ACTION                                          \
44    do {                                                         \
45       yylloc->first_column = yycolumn + 1;                      \
46       yylloc->first_line = yylineno;                            \
47       yycolumn += yyleng;                                       \
48    } while(0);
49
50 #define YY_USER_INIT                    \
51         do {                            \
52                 yylineno = 1;           \
53                 yycolumn = 1;           \
54                 yylloc->source = 0;     \
55         } while(0)
56 %}
57
58 %option bison-bridge bison-locations reentrant noyywrap
59 %option extra-type="glcpp_parser_t *"
60 %option prefix="glcpp_"
61 %option stack
62 %option never-interactive
63
64 %x DONE COMMENT UNREACHABLE SKIP
65
66 SPACE           [[:space:]]
67 NONSPACE        [^[:space:]]
68 NEWLINE         [\n]
69 HSPACE          [ \t]
70 HASH            ^{HSPACE}*#{HSPACE}*
71 IDENTIFIER      [_a-zA-Z][_a-zA-Z0-9]*
72 PUNCTUATION     [][(){}.&*~!/%<>^|;,=+-]
73 OTHER           [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
74
75 DIGITS                  [0-9][0-9]*
76 DECIMAL_INTEGER         [1-9][0-9]*[uU]?
77 OCTAL_INTEGER           0[0-7]*[uU]?
78 HEXADECIMAL_INTEGER     0[xX][0-9a-fA-F]+[uU]?
79
80 %%
81         /* Implicitly switch between SKIP and INITIAL (non-skipping);
82          * don't switch if some other state was explicitly set.
83          */
84         glcpp_parser_t *parser = yyextra;
85         if (YY_START == 0 || YY_START == SKIP) {
86                 if (parser->lexing_if || parser->skip_stack == NULL || parser->skip_stack->type == SKIP_NO_SKIP) {
87                         BEGIN 0;
88                 } else {
89                         BEGIN SKIP;
90                 }
91         }
92
93         /* Single-line comments */
94 "//"[^\n]* {
95 }
96
97         /* Multi-line comments */
98 "/*"                    { yy_push_state(COMMENT, yyscanner); }
99 <COMMENT>[^*\n]*
100 <COMMENT>[^*\n]*\n      { yylineno++; yycolumn = 0; return NEWLINE; }
101 <COMMENT>"*"+[^*/\n]*
102 <COMMENT>"*"+[^*/\n]*\n { yylineno++; yycolumn = 0; return NEWLINE; }
103 <COMMENT>"*"+"/"        {
104         yy_pop_state(yyscanner);
105         if (yyextra->space_tokens)
106                 return SPACE;
107 }
108
109 {HASH}version {
110         yylval->str = ralloc_strdup (yyextra, yytext);
111         yyextra->space_tokens = 0;
112         return HASH_VERSION;
113 }
114
115         /* glcpp doesn't handle #extension, #version, or #pragma directives.
116          * Simply pass them through to the main compiler's lexer/parser. */
117 {HASH}(extension|pragma)[^\n]+ {
118         yylval->str = ralloc_strdup (yyextra, yytext);
119         yylineno++;
120         yycolumn = 0;
121         return OTHER;
122 }
123
124 {HASH}line{HSPACE}+{DIGITS}{HSPACE}+{DIGITS}{HSPACE}*$ {
125         /* Eat characters until the first digit is
126          * encountered
127          */
128         char *ptr = yytext;
129         while (!isdigit(*ptr))
130                 ptr++;
131
132         /* Subtract one from the line number because
133          * yylineno is zero-based instead of
134          * one-based.
135          */
136         yylineno = strtol(ptr, &ptr, 0) - 1;
137         yylloc->source = strtol(ptr, NULL, 0);
138 }
139
140 {HASH}line{HSPACE}+{DIGITS}{HSPACE}*$ {
141         /* Eat characters until the first digit is
142          * encountered
143          */
144         char *ptr = yytext;
145         while (!isdigit(*ptr))
146                 ptr++;
147
148         /* Subtract one from the line number because
149          * yylineno is zero-based instead of
150          * one-based.
151          */
152         yylineno = strtol(ptr, &ptr, 0) - 1;
153 }
154
155 <SKIP,INITIAL>{
156 {HASH}ifdef {
157         yyextra->lexing_if = 1;
158         yyextra->space_tokens = 0;
159         return HASH_IFDEF;
160 }
161
162 {HASH}ifndef {
163         yyextra->lexing_if = 1;
164         yyextra->space_tokens = 0;
165         return HASH_IFNDEF;
166 }
167
168 {HASH}if/[^_a-zA-Z0-9] {
169         yyextra->lexing_if = 1;
170         yyextra->space_tokens = 0;
171         return HASH_IF;
172 }
173
174 {HASH}elif {
175         yyextra->lexing_if = 1;
176         yyextra->space_tokens = 0;
177         return HASH_ELIF;
178 }
179
180 {HASH}else {
181         yyextra->space_tokens = 0;
182         return HASH_ELSE;
183 }
184
185 {HASH}endif {
186         yyextra->space_tokens = 0;
187         return HASH_ENDIF;
188 }
189 }
190
191 <SKIP>[^\n] ;
192
193 {HASH}error.* {
194         char *p;
195         for (p = yytext; !isalpha(p[0]); p++); /* skip "  #   " */
196         p += 5; /* skip "error" */
197         glcpp_error(yylloc, yyextra, "#error%s", p);
198 }
199
200 {HASH}define{HSPACE}+/{IDENTIFIER}"(" {
201         yyextra->space_tokens = 0;
202         return HASH_DEFINE_FUNC;
203 }
204
205 {HASH}define {
206         yyextra->space_tokens = 0;
207         return HASH_DEFINE_OBJ;
208 }
209
210 {HASH}undef {
211         yyextra->space_tokens = 0;
212         return HASH_UNDEF;
213 }
214
215 {HASH} {
216         yyextra->space_tokens = 0;
217         return HASH;
218 }
219
220 {DECIMAL_INTEGER} {
221         yylval->str = ralloc_strdup (yyextra, yytext);
222         return INTEGER_STRING;
223 }
224
225 {OCTAL_INTEGER} {
226         yylval->str = ralloc_strdup (yyextra, yytext);
227         return INTEGER_STRING;
228 }
229
230 {HEXADECIMAL_INTEGER} {
231         yylval->str = ralloc_strdup (yyextra, yytext);
232         return INTEGER_STRING;
233 }
234
235 "<<"  {
236         return LEFT_SHIFT;
237 }
238
239 ">>" {
240         return RIGHT_SHIFT;
241 }
242
243 "<=" {
244         return LESS_OR_EQUAL;
245 }
246
247 ">=" {
248         return GREATER_OR_EQUAL;
249 }
250
251 "==" {
252         return EQUAL;
253 }
254
255 "!=" {
256         return NOT_EQUAL;
257 }
258
259 "&&" {
260         return AND;
261 }
262
263 "||" {
264         return OR;
265 }
266
267 "##" {
268         return PASTE;
269 }
270
271 "defined" {
272         return DEFINED;
273 }
274
275 {IDENTIFIER} {
276         yylval->str = ralloc_strdup (yyextra, yytext);
277         return IDENTIFIER;
278 }
279
280 {PUNCTUATION} {
281         return yytext[0];
282 }
283
284 {OTHER}+ {
285         yylval->str = ralloc_strdup (yyextra, yytext);
286         return OTHER;
287 }
288
289 {HSPACE}+ {
290         if (yyextra->space_tokens) {
291                 return SPACE;
292         }
293 }
294
295 <SKIP,INITIAL>\n {
296         yyextra->lexing_if = 0;
297         yylineno++;
298         yycolumn = 0;
299         return NEWLINE;
300 }
301
302         /* Handle missing newline at EOF. */
303 <INITIAL><<EOF>> {
304         BEGIN DONE; /* Don't keep matching this rule forever. */
305         yyextra->lexing_if = 0;
306         return NEWLINE;
307 }
308
309         /* We don't actually use the UNREACHABLE start condition. We
310         only have this action here so that we can pretend to call some
311         generated functions, (to avoid "defined but not used"
312         warnings. */
313 <UNREACHABLE>. {
314         unput('.');
315         yy_top_state(yyextra);
316 }
317
318 %%
319
320 void
321 glcpp_lex_set_source_string(glcpp_parser_t *parser, const char *shader)
322 {
323         yy_scan_string(shader, parser->scanner);
324 }