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