scanner.c 24.9 KB
Newer Older
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
/****************************************************************************\
Copyright (c) 2002, NVIDIA Corporation.

NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms.  If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder. 

THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.

IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/
//
// scanner.c
//

48
#include <assert.h>
49
50
51
52
53
54
55
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 0
    #include <ieeefp.h>
56
#else
57
58
59
60
61
    #define isinff(x) (((*(int *)&(x) & 0x7f800000L)==0x7f800000L) && \
                       ((*(int *)&(x) & 0x007fffffL)==0000000000L))
#endif

#include "compiler/preprocessor/slglobals.h"
62
#include "compiler/util.h"
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

typedef struct StringInputSrc {
    InputSrc base;
    char *p;
} StringInputSrc;

static int eof_scan(InputSrc *is, yystypepp * yylvalpp)
{
    return EOF;
} // eof_scan

static void noop(InputSrc *in, int ch, yystypepp * yylvalpp) {}

static InputSrc eof_inputsrc = { 0, &eof_scan, &eof_scan, &noop };

static int byte_scan(InputSrc *, yystypepp * yylvalpp);

#define EOL_SY '\n'

#if defined(_MSC_VER)
    #define DBG_BREAKPOINT() __asm int 3
#elif defined(_M_AMD64)
    #define DBG_BREAKPOINT() assert(!"Dbg_Breakpoint");
#else
    #define DBG_BREAKPOINT()
#endif

#if defined(_MSC_VER) && !defined(_M_AMD64)
    __int64 RDTSC ( void ) {

        __int64 v;
    
        __asm __emit 0x0f
        __asm __emit 0x31
        __asm mov dword ptr v, eax
        __asm mov dword ptr v+4, edx
    
        return v;
    }
#endif


int InitScanner(CPPStruct *cpp)
{
    // Add various atoms needed by the CPP line scanner:
    if (!InitCPP())
        return 0;

    cpp->mostRecentToken = 0;
    cpp->tokenLoc = &cpp->ltokenLoc;

    cpp->ltokenLoc.file = 0;
    cpp->ltokenLoc.line = 0;

    cpp->currentInput = &eof_inputsrc;
    cpp->previous_token = '\n';
    cpp->pastFirstStatement = 0;

    return 1;
} // InitScanner

int FreeScanner(void)
{
    return (FreeCPP());
}

/*
 * str_getch()
 * takes care of reading from multiple strings.
 * returns the next-char from the input stream.
 * returns EOF when the complete shader is parsed.
 */
static int str_getch(StringInputSrc *in)
{
137
138
139
    for(;;){
       if (*in->p){
          if (*in->p == '\n') {
140
141
142
143
             in->base.line++;
             IncLineNumber();
          }
          return *in->p++;
144
145
146
147
148
149
150
151
152
153
154
155
       }
       if(++(cpp->PaWhichStr) < cpp->PaArgc){
          free(in);
          SetStringNumber(cpp->PaWhichStr);
          SetLineNumber(1);
          ScanFromString(cpp->PaArgv[cpp->PaWhichStr]);
          in=(StringInputSrc*)cpp->currentInput;
          continue;             
       }
       else{
          cpp->currentInput = in->base.prev;
          cpp->PaWhichStr=0;
156
157
158
          free(in);
          return EOF;
       }  
159
    }
160
161
162
163
} // str_getch

static void str_ungetch(StringInputSrc *in, int ch, yystypepp *type) {
    if (in->p[-1] == ch)in->p--;
164
165
166
167
168
    else {
        *(in->p)='\0'; //this would take care of shifting to the previous string.
        cpp->PaWhichStr--;
    }  
    if (ch == '\n') {
169
170
171
172
173
        in->base.line--;
        DecLineNumber();
    }
} // str_ungetch

174
int ScanFromString(const char *s)
175
176
{
    
177
    StringInputSrc *in = malloc(sizeof(StringInputSrc));
178
    memset(in, 0, sizeof(StringInputSrc));
179
    in->p = (char*) s;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    in->base.line = 1;
    in->base.scan = byte_scan;
    in->base.getch = (int (*)(InputSrc *, yystypepp *))str_getch;
    in->base.ungetch = (void (*)(InputSrc *, int, yystypepp *))str_ungetch;
    in->base.prev = cpp->currentInput;
    cpp->currentInput = &in->base;

    return 1;
} // ScanFromString;


///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Floating point constants: /////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

195
196
197
198
199
200
201
#define APPEND_CHAR_S(ch, str, len, max_len) \
      if (len < max_len) { \
          str[len++] = ch; \
      } else if (!alreadyComplained) { \
          CPPErrorToInfoLog("BUFFER OVERFLOW"); \
          alreadyComplained = 1; \
      }
202
203
204
205
206

/*
 * lFloatConst() - Scan a floating point constant.  Assumes that the scanner
 *         has seen at least one digit, followed by either a decimal '.' or the
 *         letter 'e'.
207
208
 * ch - '.' or 'e'
 * len - length of string already copied into yylvalpp->symbol_name.
209
210
 */

211
static int lFloatConst(int ch, int len, yystypepp * yylvalpp)
212
{
213
214
215
    int alreadyComplained = 0;
    assert((ch == '.') || (ch == 'e') || (ch == 'E'));

216
    if (ch == '.') {
217
218
219
220
        do {
            APPEND_CHAR_S(ch, yylvalpp->symbol_name, len, MAX_SYMBOL_NAME_LEN);
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
        } while (ch >= '0' && ch <= '9');
221
222
223
224
    }

    // Exponent:
    if (ch == 'e' || ch == 'E') {
225
        APPEND_CHAR_S(ch, yylvalpp->symbol_name, len, MAX_SYMBOL_NAME_LEN);
226
227
        ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
        if (ch == '+') {
228
229
            APPEND_CHAR_S(ch, yylvalpp->symbol_name, len, MAX_SYMBOL_NAME_LEN);
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
230
        } else if (ch == '-') {
231
            APPEND_CHAR_S(ch, yylvalpp->symbol_name, len, MAX_SYMBOL_NAME_LEN);
232
233
234
235
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
        }
        if (ch >= '0' && ch <= '9') {
            while (ch >= '0' && ch <= '9') {
236
                APPEND_CHAR_S(ch, yylvalpp->symbol_name, len, MAX_SYMBOL_NAME_LEN);
237
238
239
                ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            }
        } else {
240
            CPPErrorToInfoLog("EXPONENT INVALID");
241
242
        }
    }
243
244
245
246
247
248
249
    cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);

    assert(len <= MAX_SYMBOL_NAME_LEN);
    yylvalpp->symbol_name[len] = '\0';
    yylvalpp->sc_fval = (float) atof_dot(yylvalpp->symbol_name);
    if (isinff(yylvalpp->sc_fval)) {
        CPPErrorToInfoLog("FLOAT CONSTANT OVERFLOW");
250
251
252
253
254
255
256
257
258
259
260
    }
    return CPP_FLOATCONSTANT;
} // lFloatConst

///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////// Normal Scanner //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
    
static int byte_scan(InputSrc *in, yystypepp * yylvalpp)
{
    char string_val[MAX_STRING_LEN + 1];
261
    int alreadyComplained = 0;
262
263
264
265
266
    int len, ch, ii, ival = 0;

    for (;;) {
        yylvalpp->sc_int = 0;
        ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
267
 
268
269
270
271
        while (ch == ' ' || ch == '\t' || ch == '\r') {
            yylvalpp->sc_int = 1;
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
        }
272
        
273
274
        cpp->ltokenLoc.file = cpp->currentInput->name;
        cpp->ltokenLoc.line = cpp->currentInput->line;
275
        alreadyComplained = 0;
276
277
278
        len = 0;
        switch (ch) {
        default:
279
            return ch; // Single character token
280
281
        case EOF:
            return -1;
282
        case 'A': case 'B': case 'C': case 'D': case 'E':
283
284
285
286
287
288
289
290
291
292
293
294
        case 'F': case 'G': case 'H': case 'I': case 'J':
        case 'K': case 'L': case 'M': case 'N': case 'O':
        case 'P': case 'Q': case 'R': case 'S': case 'T':
        case 'U': case 'V': case 'W': case 'X': case 'Y':
        case 'Z': case '_':
        case 'a': case 'b': case 'c': case 'd': case 'e':
        case 'f': case 'g': case 'h': case 'i': case 'j':
        case 'k': case 'l': case 'm': case 'n': case 'o':
        case 'p': case 'q': case 'r': case 's': case 't':
        case 'u': case 'v': case 'w': case 'x': case 'y':
        case 'z':            
            do {
295
296
                APPEND_CHAR_S(ch, yylvalpp->symbol_name, len, MAX_SYMBOL_NAME_LEN);
                ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
297
298
299
300
            } while ((ch >= 'a' && ch <= 'z') ||
                     (ch >= 'A' && ch <= 'Z') ||
                     (ch >= '0' && ch <= '9') ||
                     ch == '_');
301
302
            assert(len <= MAX_SYMBOL_NAME_LEN);
            yylvalpp->symbol_name[len] = '\0';
303
            cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
304
            yylvalpp->sc_ident = LookUpAddString(atable, yylvalpp->symbol_name);
305
306
307
            return CPP_IDENTIFIER;
            break;
        case '0':
308
            APPEND_CHAR_S(ch, yylvalpp->symbol_name, len, MAX_SYMBOL_NAME_LEN);
309
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
310
311
            if (ch == 'x' || ch == 'X') {  // hexadecimal integer constants
                APPEND_CHAR_S(ch, yylvalpp->symbol_name, len, MAX_SYMBOL_NAME_LEN);
312
313
314
315
316
317
318
                ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
                if ((ch >= '0' && ch <= '9') ||
                    (ch >= 'A' && ch <= 'F') ||
                    (ch >= 'a' && ch <= 'f'))
                {
                    ival = 0;
                    do {
319
320
                        if ((ival <= 0x0fffffff) && (len < MAX_SYMBOL_NAME_LEN)) {
                            yylvalpp->symbol_name[len++] = ch;
321
322
323
324
325
326
327
328
                            if (ch >= '0' && ch <= '9') {
                                ii = ch - '0';
                            } else if (ch >= 'A' && ch <= 'F') {
                                ii = ch - 'A' + 10;
                            } else {
                                ii = ch - 'a' + 10;
                            }
                            ival = (ival << 4) | ii;
329
330
331
                        } else if (!alreadyComplained) {
                            CPPErrorToInfoLog("HEX CONSTANT OVERFLOW");
                            alreadyComplained = 1;
332
333
334
335
336
337
                        }
                        ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
                    } while ((ch >= '0' && ch <= '9') ||
                             (ch >= 'A' && ch <= 'F') ||
                             (ch >= 'a' && ch <= 'f'));
                } else {
338
                    CPPErrorToInfoLog("HEX CONSTANT INVALID");
339
                }
340
                assert(len <= MAX_SYMBOL_NAME_LEN);
341
                yylvalpp->symbol_name[len] = '\0';
342
343
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                yylvalpp->sc_int = ival;
344
345
346
347
                return CPP_INTCONSTANT;
            } else if (ch >= '0' && ch <= '7') { // octal integer constants
                ival = 0;
                do {
348
349
                    if ((ival <= 0x1fffffff) && (len < MAX_SYMBOL_NAME_LEN)) {
                        yylvalpp->symbol_name[len++] = ch;
350
351
                        ii = ch - '0';
                        ival = (ival << 3) | ii;
352
353
354
                    } else if (!alreadyComplained) {
                        CPPErrorToInfoLog("OCT CONSTANT OVERFLOW");
                        alreadyComplained = 1;
355
356
357
358
                    }
                    ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
                } while (ch >= '0' && ch <= '7');
                if (ch == '.' || ch == 'e' || ch == 'f' || ch == 'h' || ch == 'x'|| ch == 'E') 
359
360
                     return lFloatConst(ch, len, yylvalpp);
                assert(len <= MAX_SYMBOL_NAME_LEN);
361
                yylvalpp->symbol_name[len] = '\0';
362
363
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                yylvalpp->sc_int = ival;
364
365
                return CPP_INTCONSTANT;
            } else {
366
367
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                ch = '0';
368
369
370
371
372
            }
            // Fall through...
        case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
            do {
373
374
                APPEND_CHAR_S(ch, yylvalpp->symbol_name, len, MAX_SYMBOL_NAME_LEN);
                ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
375
376
            } while (ch >= '0' && ch <= '9');
            if (ch == '.' || ch == 'e' || ch == 'f' || ch == 'h' || ch == 'x'|| ch == 'E') {
377
                return lFloatConst(ch, len, yylvalpp);
378
            } else {
379
                assert(len <= MAX_SYMBOL_NAME_LEN);
380
                yylvalpp->symbol_name[len] = '\0';
381
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
382
383
384
                ival = 0;
                for (ii = 0; ii < len; ii++) {
                    ch = yylvalpp->symbol_name[ii] - '0';
385
                    ival = ival*10 + ch;
386
                    if ((ival > 214748364) || (ival == 214748364 && ch >= 8)) {
387
388
                        CPPErrorToInfoLog("INTEGER CONSTANT OVERFLOW");
                        break;
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
                    }
                }
                yylvalpp->sc_int = ival;
                if(ival==0)
                   strcpy(yylvalpp->symbol_name,"0");
                return CPP_INTCONSTANT;
            }
            break;
        case '-':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '-') {
                return CPP_DEC_OP;
            } else if (ch == '=') {
                return CPP_SUB_ASSIGN;
            } else {
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                return '-';
            }
        case '+':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '+') {
                return CPP_INC_OP;
            } else if (ch == '=') {
                return CPP_ADD_ASSIGN;
            } else {
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                return '+';
            }
        case '*':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '=') {
                return CPP_MUL_ASSIGN;
            } else {
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                return '*';
            }
        case '%':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '=') {
                return CPP_MOD_ASSIGN;
            } else if (ch == '>'){
                return CPP_RIGHT_BRACE;
            } else {
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                return '%';
            }
        case ':':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '>') {
                return CPP_RIGHT_BRACKET;
            } else {
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                return ':';
            }
        case '^':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '^') {
                return CPP_XOR_OP;
            } else {
                if (ch == '=')
                    return CPP_XOR_ASSIGN;
                else{
                  cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                  return '^';
                }
            }
        
        case '=':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '=') {
                return CPP_EQ_OP;
            } else {
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                return '=';
            }
        case '!':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '=') {
                return CPP_NE_OP;
            } else {
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                return '!';
            }
        case '|':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '|') {
                return CPP_OR_OP;
            } else {
                if (ch == '=')
                    return CPP_OR_ASSIGN;
                else{
                  cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                  return '|';
                }
            }
        case '&':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '&') {
                return CPP_AND_OP;
            } else {
                if (ch == '=')
                    return CPP_AND_ASSIGN;
                else{
                  cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                  return '&';
                }
            }
        case '<':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '<') {
                ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
                if(ch == '=')
                    return CPP_LEFT_ASSIGN;
                else{
                    cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                    return CPP_LEFT_OP;
                }
            } else {
                if (ch == '=') {
                    return CPP_LE_OP;
                } else {
                    if (ch == '%')
                        return CPP_LEFT_BRACE;
                    else if (ch == ':')
                        return CPP_LEFT_BRACKET;
                    else{
                        cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                        return '<';
                    }
                }
            }
        case '>':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '>') {
                ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
                if(ch == '=')
                    return CPP_RIGHT_ASSIGN;
                else{
                    cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                    return CPP_RIGHT_OP;
                }
            } else {
                if (ch == '=') {
                    return CPP_GE_OP;
                } else {
                    cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                    return '>';
                }
            }
        case '.':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch >= '0' && ch <= '9') {
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
542
                return lFloatConst('.', 0, yylvalpp);
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
            } else {
                if (ch == '.') {
                    return -1; // Special EOF hack
                } else {
                    cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                    return '.';
                }
            }
        case '/':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            if (ch == '/') {
                do {
                    ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
                } while (ch != '\n' && ch != EOF);
                if (ch == EOF)
                    return -1;
                return '\n';
            } else if (ch == '*') {
                int nlcount = 0;
                ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
                do {
                    while (ch != '*') {
                        if (ch == '\n') nlcount++;
                        if (ch == EOF) {
567
                            CPPErrorToInfoLog("EOF IN COMMENT");
568
569
570
571
572
573
                            return -1;
                        }
                        ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
                    }
                    ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
                    if (ch == EOF) {
574
                        CPPErrorToInfoLog("EOF IN COMMENT");
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
                        return -1;
                    }
                } while (ch != '/');
                if (nlcount) {
                    return '\n';
                }
                // Go try it again...
            } else if (ch == '=') {
                return CPP_DIV_ASSIGN;
            } else {
                cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
                return '/';
            }
            break;
        case '"':
            ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
            while (ch != '"' && ch != '\n' && ch != EOF) {
                if (ch == '\\') {
                    CPPErrorToInfoLog("The line continuation character (\\) is not part of the OpenGL ES Shading Language");
                    return -1;
                }
596
597
                APPEND_CHAR_S(ch, string_val, len, MAX_STRING_LEN);
                ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
598
            };
599
            assert(len <= MAX_STRING_LEN);
600
601
602
603
604
            string_val[len] = '\0';
            if (ch == '"') {
                yylvalpp->sc_ident = LookUpAddString(atable, string_val);
                return CPP_STRCONSTANT;
            } else {
605
                CPPErrorToInfoLog("EOL IN STRING");
606
607
                return ERROR_SY;
            }
608
            break;
609
610
611
612
613
614
        }
    }
} // byte_scan

int yylex_CPP(char* buf, int maxSize)
{    
615
    yystypepp yylvalpp;
616
617
618
619
620
621
    int token = '\n';   

    for(;;) {

        char* tokenString = 0;
        token = cpp->currentInput->scan(cpp->currentInput, &yylvalpp);
622
623
        if(check_EOF(token))
            return 0;
624
625
        if (token == '#') {
            if (cpp->previous_token == '\n'|| cpp->previous_token == 0) {
626
                token = readCPPline(&yylvalpp);
627
628
                if(check_EOF(token))
                    return 0;
629
                continue;
630
631
632
633
634
635
636
637
638
639
640
            } else {
                CPPErrorToInfoLog("preprocessor command must not be preceded by any other statement in that line");
                return 0;
            }
        }
        cpp->previous_token = token;
        // expand macros
        if (token == CPP_IDENTIFIER && MacroExpand(yylvalpp.sc_ident, &yylvalpp)) {
            cpp->pastFirstStatement = 1;
            continue;
        }
641

642
643
        if (token == '\n')
            continue;
644
645
646
        cpp->pastFirstStatement = 1;

        if (token == CPP_IDENTIFIER) {
647
            tokenString = GetStringOfAtom(atable,yylvalpp.sc_ident);
648
        } else if (token == CPP_FLOATCONSTANT || token == CPP_INTCONSTANT){
649
            tokenString = yylvalpp.symbol_name;
650
        } else {
651
            tokenString = GetStringOfAtom(atable,token);
652
        }
653
654

        if (tokenString) {
655
656
657
658
659
660
661
662
            int len = strlen(tokenString);
            cpp->tokensBeforeEOF = 1;
            if (len >= maxSize) {
                return maxSize;
            } else  if (len > 0) {
                strcpy(buf, tokenString);
                return len;
            }
663
664
665
666
667
668
669
670
671
672
673
674
675

            return 0;
        }
    }

    return 0;
} // yylex

//Checks if the token just read is EOF or not.
int check_EOF(int token)
{
   if(token==-1){
       if(cpp->ifdepth >0){
676
        CPPErrorToInfoLog("#endif missing!! Compilation stopped");
677
678
679
680
681
682
683
684
685
686
687
        cpp->CompileError=1;
       }
      return 1;
   }
   return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// End of scanner.c //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////