summaryrefslogtreecommitdiff
path: root/GLESv2/yagl_glsl_lexer.l
blob: 65badd092e76fed6ceec7d8afc7100f1b9797a92 (plain)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
%{
#include <GL/gl.h>
#include "yagl_glsl_state.h"
#include "yagl_malloc.h"

#define YY_NO_INPUT

#define YY_INPUT(buf, result, max_size) \
    result = string_input(buf, max_size, yyscanner);

static yy_size_t string_input(char *buf, yy_size_t max_size, yyscan_t yyscanner);
%}

%option bison-bridge reentrant noyywrap
%option nounput
%option never-interactive
%option prefix="yagl_glsl_lexer_"
%option extra-type="struct yagl_glsl_state *"

%x COMMENT PP

WS [ \r\t\v\f]
PRECISION "lowp"|"mediump"|"highp"|"precision"[^;\n]+;?
CONTROL [()\[\]{},;?:/%*&|^!+\-=<>\.]
STRING [^ \r\t\v\f\n()\[\]{},;?:/%*&|^!+\-=<>\.]+

%%

"//"[^\n]* {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_comment(state, strlen(yytext));
}

"/*" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    BEGIN(COMMENT);
    yagl_glsl_state_new_comment(state, strlen(yytext));
}

^{WS}*#{WS}*version {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    BEGIN(PP);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_VERSION;
}

^{WS}*#{WS}*define {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    BEGIN(PP);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_DEFINE;
}

^{WS}*#{WS}*extension[^\n]* {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_EXTENSION;
}

[\n]+ {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_pending(state, yytext);
}

{WS}+ {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_pending(state, yytext);
}

"gl_MaxVertexUniformVectors" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_MAXVERTEXUNIFORMVECTORS;
}

"gl_MaxFragmentUniformVectors" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_MAXFRAGMENTUNIFORMVECTORS;
}

"gl_MaxVaryingVectors" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_MAXVARYINGVECTORS;
}

"gl_MaxVaryingFloats" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_MAXVARYINGFLOATS;
}

"attribute" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_ATTRIBUTE;
}

"varying" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_VARYING;
}

"texture1DProjLod" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE1DPROJLOD;
}

"texture1DProj" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE1DPROJ;
}

"texture1DLod" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE1DLOD;
}

"texture1D" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE1D;
}

"texture2DProjLod" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE2DPROJLOD;
}

"texture2DLod" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE2DLOD;
}

"texture2DProj" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE2DPROJ;
}

"texture2D" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE2D;
}

<PP>"texture2D" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE2D;
}

"texture3DProjLod" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE3DPROJLOD;
}

"texture3DLod" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE3DLOD;
}

"texture3DProj" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE3DPROJ;
}

"texture3D" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURE3D;
}

"textureCubeLod" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURECUBELOD;
}

"textureCube" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_TEXTURECUBE;
}

"gl_FragColor" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_GLFRAGCOLOR;
}

{PRECISION} {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_PRECISION;
}

{CONTROL} {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_pending(state, yytext);
}

{STRING} {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_pending(state, yytext);
}

<PP>\n {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    BEGIN(INITIAL);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_EOL;
}

<PP>{WS}+ {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_pending(state, yytext);
}

<PP>[1-9][0-9]* {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_integer_token(state, yylval, strtol(yytext, NULL, 10));
    return TOK_INTEGER;
}

<PP>"es" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_ES;
}

<PP>{PRECISION} {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_PRECISION;
}

<PP>{CONTROL} {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_STRING;
}

<PP>{STRING} {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_str_token(state, yylval, yytext);
    return TOK_STRING;
}

<COMMENT>[^*\n]* {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_comment(state, strlen(yytext));
}

<COMMENT>[^*\n]*\n {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_comment(state, strlen(yytext) - 1);
    yagl_glsl_state_new_pending(state, "\n");
}

<COMMENT>"*"+[^*/\n]* {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_comment(state, strlen(yytext));
}

<COMMENT>"*"+[^*/\n]*\n {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    yagl_glsl_state_new_comment(state, strlen(yytext) - 1);
    yagl_glsl_state_new_pending(state, "\n");
}

<COMMENT>"*"+"/" {
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    BEGIN(INITIAL);
    yagl_glsl_state_new_comment(state, strlen(yytext));
}

%%

static yy_size_t string_input(char *buf, yy_size_t max_size, yyscan_t yyscanner)
{
    struct yagl_glsl_state *state = yagl_glsl_lexer_get_extra(yyscanner);
    int rem = state->source_len - state->source_pos;
    int num_read = (rem < max_size) ? rem : max_size;

    memcpy(buf, state->source + state->source_pos, num_read);

    state->source_pos += num_read;

    return num_read;
}

//extern int yagl_glsl_debug;

int yagl_glsl_state_init(struct yagl_glsl_state *state,
                         GLenum shader_type,
                         const char *source,
                         int source_len,
                         int es3_supported)
{
    yyscan_t scanner = NULL;

    memset(state, 0, sizeof(*state));

    //yagl_glsl_debug = 1;

    if (yagl_glsl_lexer_lex_init_extra(state, &scanner)) {
        return 0;
    }

    state->shader_type = shader_type;
    state->source = source;
    state->source_len = source_len;
    state->es3_supported = es3_supported;
    state->scanner = scanner;
    state->token_index = 1;

    yagl_glsl_state_set_version(state, 120, 0);

    yagl_vector_init(&state->header, sizeof(char), 0);
    yagl_vector_init(&state->output, sizeof(char), 0);
    yagl_list_init(&state->pending_list);
    yagl_vector_init(&state->pending, sizeof(char), 0);
    yagl_vector_init(&state->strings, sizeof(char*), 0);

    return 1;
}

void yagl_glsl_state_cleanup(struct yagl_glsl_state *state)
{
    yyscan_t scanner = state->scanner;
    int i;
    char **tmp;
    struct yagl_glsl_pending *it, *tmp_it;

    tmp = yagl_vector_data(&state->strings);

    for (i = 0; i < yagl_vector_size(&state->strings); ++i) {
        free(*(tmp + i));
    }

    yagl_vector_cleanup(&state->strings);

    yagl_vector_cleanup(&state->pending);

    yagl_list_for_each_safe(struct yagl_glsl_pending,
                            it,
                            tmp_it,
                            &state->pending_list, list) {
        yagl_list_remove(&it->list);
        yagl_free(it);
    }

    yagl_vector_cleanup(&state->output);

    yagl_vector_cleanup(&state->header);

    yagl_glsl_lexer_lex_destroy(scanner);
}