Fix line-continuation bug.
[platform/upstream/glslang.git] / glslang / MachineIndependent / Scan.cpp
1 //
2 //Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 //Copyright (C) 2013 LunarG, Inc.
4 //
5 //All rights reserved.
6 //
7 //Redistribution and use in source and binary forms, with or without
8 //modification, are permitted provided that the following conditions
9 //are met:
10 //
11 //    Redistributions of source code must retain the above copyright
12 //    notice, this list of conditions and the following disclaimer.
13 //
14 //    Redistributions in binary form must reproduce the above
15 //    copyright notice, this list of conditions and the following
16 //    disclaimer in the documentation and/or other materials provided
17 //    with the distribution.
18 //
19 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 //    contributors may be used to endorse or promote products derived
21 //    from this software without specific prior written permission.
22 //
23 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 //POSSIBILITY OF SUCH DAMAGE.
35 //
36
37 //
38 // GLSL scanning, leveraging the scanning done by the preprocessor.
39 //
40
41 #include <string.h>
42
43 #include "../Include/Types.h"
44 #include "SymbolTable.h"
45 #include "glslang_tab.cpp.h"
46 #include "ParseHelper.h"
47 #include "ScanContext.h"
48 #include "Scan.h"
49
50 // preprocessor includes
51 #include "preprocessor/PpContext.h"
52 #include "preprocessor/PpTokens.h"
53
54 namespace glslang {
55     
56 // read past any white space
57 void TInputScanner::consumeWhiteSpace(bool& foundNonSpaceTab)
58 {
59     char c = peek();  // don't accidentally consume anything other than whitespace
60     while (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
61         if (c == '\r' || c == '\n')
62             foundNonSpaceTab = true;
63         get();
64         c = peek();
65     }
66 }
67
68 // return true if a comment was actually consumed
69 bool TInputScanner::consumeComment()
70 {
71     if (peek() != '/')
72         return false;
73
74     get();  // consume the '/'
75     char c = peek();
76     if (c == '/') {
77
78         // a '//' style comment
79         get();  // consume the second '/'
80         c = get();
81         do {
82             while (c > 0 && c != '\\' && c != '\r' && c != '\n')
83                 c = get();
84
85             if (c <= 0 || c == '\r' || c == '\n') {
86                 while (c == '\r' || c == '\n')
87                     c = get();
88
89                 // we reached the end of the comment
90                 break;
91             } else {
92                 // it's a '\', so we need to keep going, after skipping what's escaped
93
94                 // read the skipped character
95                 c = get();
96
97                 // if it's a two-character newline, skip both characters
98                 if (c == '\r' && peek() == '\n')
99                     get();
100                 c = get();
101             }
102         } while (true);
103
104         // put back the last non-comment character
105         if (c > 0)
106             unget();
107
108         return true;
109     } else if (c == '*') {
110
111         // a '/*' style comment
112         get();  // consume the '*'
113         c = get();
114         do {
115             while (c > 0 && c != '*')
116                 c = get();
117             if (c == '*') {
118                 c = get();
119                 if (c == '/')
120                     break;  // end of comment
121                 // not end of comment
122             } else // end of input
123                 break;
124         } while (true);
125
126         return true;
127     } else {
128         // it's not a comment, put the '/' back
129         unget();
130
131         return false;
132     }
133 }
134
135 // skip whitespace, then skip a comment, rinse, repeat
136 void TInputScanner::consumeWhitespaceComment(bool& foundNonSpaceTab)
137 {
138     do {
139         consumeWhiteSpace(foundNonSpaceTab);
140  
141         // if not starting a comment now, then done
142         char c = peek();
143         if (c != '/' || c < 0)
144             return;
145
146         // skip potential comment 
147         foundNonSpaceTab = true;
148         if (! consumeComment())
149             return;
150
151     } while (true);
152 }
153
154 // Returns true if there was non-white space (e.g., a comment, newline) before the #version
155 // or no #version was found; otherwise, returns false.  There is no error case, it always
156 // succeeds, but will leave version == 0 if no #version was found.
157 //
158 // N.B. does not attempt to leave input in any particular known state.  The assumption
159 // is that scanning will start anew, following the rules for the chosen version/profile,
160 // and with a corresponding parsing context.
161 //
162 bool TInputScanner::scanVersion(int& version, EProfile& profile)
163 {
164     // This function doesn't have to get all the semantics correct,
165     // just find the #version if there is a correct one present.
166     // The preprocessor will have the responsibility of getting all the semantics right.
167
168     version = 0;  // means not found
169     profile = ENoProfile;
170
171     bool foundNonSpaceTab = false;
172     consumeWhitespaceComment(foundNonSpaceTab);
173
174     // #
175     if (get() != '#')
176         return true;
177
178     // whitespace
179     char c;
180     do {
181         c = get();
182     } while (c == ' ' || c == '\t');
183
184     if (    c != 'v' ||
185         get() != 'e' ||
186         get() != 'r' ||
187         get() != 's' ||
188         get() != 'i' ||
189         get() != 'o' ||
190         get() != 'n')
191         return true;
192
193     // whitespace
194     do {
195         c = get();
196     } while (c == ' ' || c == '\t');
197
198     // version number
199     while (c >= '0' && c <= '9') {
200         version = 10 * version + (c - '0');
201         c = get();
202     }
203     if (version == 0)
204         return true;
205     
206     // whitespace
207     while (c == ' ' || c == '\t')
208         c = get();
209
210     // profile
211     const int maxProfileLength = 13;  // not including any 0
212     char profileString[maxProfileLength];
213     int profileLength;
214     for (profileLength = 0; profileLength < maxProfileLength; ++profileLength) {
215         if (c < 0 || c == ' ' || c == '\t' || c == '\n' || c == '\r')
216             break;
217         profileString[profileLength] = c;
218         c = get();
219     }
220     if (c > 0 && c != ' ' && c != '\t' && c != '\n' && c != '\r')
221         return true;
222
223     if (profileLength == 2 && strncmp(profileString, "es", profileLength) == 0)
224         profile = EEsProfile;
225     else if (profileLength == 4 && strncmp(profileString, "core", profileLength) == 0)
226         profile = ECoreProfile;
227     else if (profileLength == 13 && strncmp(profileString, "compatibility", profileLength) == 0)
228         profile = ECompatibilityProfile;
229
230     return foundNonSpaceTab;
231 }
232
233 // Fill this in when doing glslang-level scanning, to hand back to the parser.
234 class TParserToken {
235 public:
236     explicit TParserToken(YYSTYPE& b) : sType(b) { }
237
238     YYSTYPE& sType;
239 };
240
241 } // end namespace glslang
242
243 // This is the function the glslang parser (i.e., bison) calls to get its next token
244 int yylex(YYSTYPE* glslangTokenDesc, glslang::TParseContext& parseContext)
245 {
246     glslang::TParserToken token(*glslangTokenDesc);
247
248     return parseContext.getScanContext()->tokenize(parseContext.getPpContext(), token);
249 }
250
251 namespace {
252
253 // A single global usable by all threads, by all versions, by all languages.
254 // After a single process-level initialization, this is read only and thread safe
255 std::map<std::string, int>* KeywordMap = 0;
256 std::set<std::string>* ReservedSet = 0;
257
258 };
259
260 namespace glslang {
261
262 void TScanContext::fillInKeywordMap()
263 {
264     if (KeywordMap != 0) {
265         // this is really an error, as this should called only once per process
266         // but, the only risk is if two threads called simultaneously
267         return;
268     }
269     KeywordMap = new std::map<std::string, int>;
270
271     (*KeywordMap)["const"] =                   CONST;
272     (*KeywordMap)["uniform"] =                 UNIFORM;
273     (*KeywordMap)["in"] =                      IN;
274     (*KeywordMap)["out"] =                     OUT;
275     (*KeywordMap)["inout"] =                   INOUT;
276     (*KeywordMap)["struct"] =                  STRUCT;
277     (*KeywordMap)["break"] =                   BREAK;
278     (*KeywordMap)["continue"] =                CONTINUE;
279     (*KeywordMap)["do"] =                      DO;
280     (*KeywordMap)["for"] =                     FOR;
281     (*KeywordMap)["while"] =                   WHILE;
282     (*KeywordMap)["switch"] =                  SWITCH;
283     (*KeywordMap)["case"] =                    CASE;
284     (*KeywordMap)["default"] =                 DEFAULT;
285     (*KeywordMap)["if"] =                      IF;
286     (*KeywordMap)["else"] =                    ELSE;
287     (*KeywordMap)["discard"] =                 DISCARD;
288     (*KeywordMap)["return"] =                  RETURN;
289     (*KeywordMap)["void"] =                    VOID;
290     (*KeywordMap)["bool"] =                    BOOL;
291     (*KeywordMap)["float"] =                   FLOAT;
292     (*KeywordMap)["int"] =                     INT;
293     (*KeywordMap)["bvec2"] =                   BVEC2;
294     (*KeywordMap)["bvec3"] =                   BVEC3;
295     (*KeywordMap)["bvec4"] =                   BVEC4;
296     (*KeywordMap)["vec2"] =                    VEC2;
297     (*KeywordMap)["vec3"] =                    VEC3;
298     (*KeywordMap)["vec4"] =                    VEC4;
299     (*KeywordMap)["ivec2"] =                   IVEC2;
300     (*KeywordMap)["ivec3"] =                   IVEC3;
301     (*KeywordMap)["ivec4"] =                   IVEC4;
302     (*KeywordMap)["mat2"] =                    MAT2;
303     (*KeywordMap)["mat3"] =                    MAT3;
304     (*KeywordMap)["mat4"] =                    MAT4;
305     (*KeywordMap)["sampler2D"] =               SAMPLER2D;
306     (*KeywordMap)["samplerCube"] =             SAMPLERCUBE;
307     (*KeywordMap)["true"] =                    BOOLCONSTANT;
308     (*KeywordMap)["false"] =                   BOOLCONSTANT;
309     (*KeywordMap)["attribute"] =               ATTRIBUTE;
310     (*KeywordMap)["varying"] =                 VARYING;
311     (*KeywordMap)["buffer"] =                  BUFFER;
312     (*KeywordMap)["coherent"] =                COHERENT;
313     (*KeywordMap)["restrict"] =                RESTRICT;
314     (*KeywordMap)["readonly"] =                READONLY;
315     (*KeywordMap)["writeonly"] =               WRITEONLY;
316     (*KeywordMap)["atomic_uint"] =             ATOMIC_UINT;
317     (*KeywordMap)["volatile"] =                VOLATILE;
318     (*KeywordMap)["layout"] =                  LAYOUT;
319     (*KeywordMap)["shared"] =                  SHARED;
320     (*KeywordMap)["patch"] =                   PATCH;
321     (*KeywordMap)["sample"] =                  SAMPLE;
322     (*KeywordMap)["subroutine"] =              SUBROUTINE;
323     (*KeywordMap)["highp"] =                   HIGH_PRECISION;
324     (*KeywordMap)["mediump"] =                 MEDIUM_PRECISION;
325     (*KeywordMap)["lowp"] =                    LOW_PRECISION;
326     (*KeywordMap)["precision"] =               PRECISION;
327     (*KeywordMap)["mat2x2"] =                  MAT2X2;
328     (*KeywordMap)["mat2x3"] =                  MAT2X3;
329     (*KeywordMap)["mat2x4"] =                  MAT2X4;
330     (*KeywordMap)["mat3x2"] =                  MAT3X2;
331     (*KeywordMap)["mat3x3"] =                  MAT3X3;
332     (*KeywordMap)["mat3x4"] =                  MAT3X4;
333     (*KeywordMap)["mat4x2"] =                  MAT4X2;
334     (*KeywordMap)["mat4x3"] =                  MAT4X3;
335     (*KeywordMap)["mat4x4"] =                  MAT4X4;
336     (*KeywordMap)["dmat2"] =                   DMAT2;
337     (*KeywordMap)["dmat3"] =                   DMAT3;
338     (*KeywordMap)["dmat4"] =                   DMAT4;
339     (*KeywordMap)["dmat2x2"] =                 DMAT2X2;
340     (*KeywordMap)["dmat2x3"] =                 DMAT2X3;
341     (*KeywordMap)["dmat2x4"] =                 DMAT2X4;
342     (*KeywordMap)["dmat3x2"] =                 DMAT3X2;
343     (*KeywordMap)["dmat3x3"] =                 DMAT3X3;
344     (*KeywordMap)["dmat3x4"] =                 DMAT3X4;
345     (*KeywordMap)["dmat4x2"] =                 DMAT4X2;
346     (*KeywordMap)["dmat4x3"] =                 DMAT4X3;
347     (*KeywordMap)["dmat4x4"] =                 DMAT4X4;
348     (*KeywordMap)["image1D"] =                 IMAGE1D;
349     (*KeywordMap)["iimage1D"] =                IIMAGE1D;
350     (*KeywordMap)["uimage1D"] =                UIMAGE1D;
351     (*KeywordMap)["image2D"] =                 IMAGE2D;
352     (*KeywordMap)["iimage2D"] =                IIMAGE2D;
353     (*KeywordMap)["uimage2D"] =                UIMAGE2D;
354     (*KeywordMap)["image3D"] =                 IMAGE3D;
355     (*KeywordMap)["iimage3D"] =                IIMAGE3D;
356     (*KeywordMap)["uimage3D"] =                UIMAGE3D;
357     (*KeywordMap)["image2DRect"] =             IMAGE2DRECT;
358     (*KeywordMap)["iimage2DRect"] =            IIMAGE2DRECT;
359     (*KeywordMap)["uimage2DRect"] =            UIMAGE2DRECT;
360     (*KeywordMap)["imageCube"] =               IMAGECUBE;
361     (*KeywordMap)["iimageCube"] =              IIMAGECUBE;
362     (*KeywordMap)["uimageCube"] =              UIMAGECUBE;
363     (*KeywordMap)["imageBuffer"] =             IMAGEBUFFER;
364     (*KeywordMap)["iimageBuffer"] =            IIMAGEBUFFER;
365     (*KeywordMap)["uimageBuffer"] =            UIMAGEBUFFER;
366     (*KeywordMap)["image1DArray"] =            IMAGE1DARRAY;
367     (*KeywordMap)["iimage1DArray"] =           IIMAGE1DARRAY;
368     (*KeywordMap)["uimage1DArray"] =           UIMAGE1DARRAY;
369     (*KeywordMap)["image2DArray"] =            IMAGE2DARRAY;
370     (*KeywordMap)["iimage2DArray"] =           IIMAGE2DARRAY;
371     (*KeywordMap)["uimage2DArray"] =           UIMAGE2DARRAY;
372     (*KeywordMap)["imageCubeArray"] =          IMAGECUBEARRAY;
373     (*KeywordMap)["iimageCubeArray"] =         IIMAGECUBEARRAY;
374     (*KeywordMap)["uimageCubeArray"] =         UIMAGECUBEARRAY;
375     (*KeywordMap)["image2DMS"] =               IMAGE2DMS;
376     (*KeywordMap)["iimage2DMS"] =              IIMAGE2DMS;
377     (*KeywordMap)["uimage2DMS"] =              UIMAGE2DMS;
378     (*KeywordMap)["image2DMSArray"] =          IMAGE2DMSARRAY;
379     (*KeywordMap)["iimage2DMSArray"] =         IIMAGE2DMSARRAY;
380     (*KeywordMap)["uimage2DMSArray"] =         UIMAGE2DMSARRAY;
381     (*KeywordMap)["double"] =                  DOUBLE;
382     (*KeywordMap)["dvec2"] =                   DVEC2;
383     (*KeywordMap)["dvec3"] =                   DVEC3;
384     (*KeywordMap)["dvec4"] =                   DVEC4;
385     (*KeywordMap)["samplerCubeArray"] =        SAMPLERCUBEARRAY;
386     (*KeywordMap)["samplerCubeArrayShadow"] =  SAMPLERCUBEARRAYSHADOW;
387     (*KeywordMap)["isamplerCubeArray"] =       ISAMPLERCUBEARRAY;
388     (*KeywordMap)["usamplerCubeArray"] =       USAMPLERCUBEARRAY;
389     (*KeywordMap)["sampler1DArrayShadow"] =    SAMPLER1DARRAYSHADOW;
390     (*KeywordMap)["isampler1DArray"] =         ISAMPLER1DARRAY;
391     (*KeywordMap)["usampler1D"] =              USAMPLER1D;
392     (*KeywordMap)["isampler1D"] =              ISAMPLER1D;
393     (*KeywordMap)["usampler1DArray"] =         USAMPLER1DARRAY;
394     (*KeywordMap)["samplerBuffer"] =           SAMPLERBUFFER;
395     (*KeywordMap)["uint"] =                    UINT;
396     (*KeywordMap)["uvec2"] =                   UVEC2;
397     (*KeywordMap)["uvec3"] =                   UVEC3;
398     (*KeywordMap)["uvec4"] =                   UVEC4;
399     (*KeywordMap)["samplerCubeShadow"] =       SAMPLERCUBESHADOW;
400     (*KeywordMap)["sampler2DArray"] =          SAMPLER2DARRAY;
401     (*KeywordMap)["sampler2DArrayShadow"] =    SAMPLER2DARRAYSHADOW;
402     (*KeywordMap)["isampler2D"] =              ISAMPLER2D;
403     (*KeywordMap)["isampler3D"] =              ISAMPLER3D;
404     (*KeywordMap)["isamplerCube"] =            ISAMPLERCUBE;
405     (*KeywordMap)["isampler2DArray"] =         ISAMPLER2DARRAY;
406     (*KeywordMap)["usampler2D"] =              USAMPLER2D;
407     (*KeywordMap)["usampler3D"] =              USAMPLER3D;
408     (*KeywordMap)["usamplerCube"] =            USAMPLERCUBE;
409     (*KeywordMap)["usampler2DArray"] =         USAMPLER2DARRAY;
410     (*KeywordMap)["isampler2DRect"] =          ISAMPLER2DRECT;
411     (*KeywordMap)["usampler2DRect"] =          USAMPLER2DRECT;
412     (*KeywordMap)["isamplerBuffer"] =          ISAMPLERBUFFER;
413     (*KeywordMap)["usamplerBuffer"] =          USAMPLERBUFFER;
414     (*KeywordMap)["sampler2DMS"] =             SAMPLER2DMS;
415     (*KeywordMap)["isampler2DMS"] =            ISAMPLER2DMS;
416     (*KeywordMap)["usampler2DMS"] =            USAMPLER2DMS;
417     (*KeywordMap)["sampler2DMSArray"] =        SAMPLER2DMSARRAY;
418     (*KeywordMap)["isampler2DMSArray"] =       ISAMPLER2DMSARRAY;
419     (*KeywordMap)["usampler2DMSArray"] =       USAMPLER2DMSARRAY;
420     (*KeywordMap)["sampler1D"] =               SAMPLER1D;
421     (*KeywordMap)["sampler1DShadow"] =         SAMPLER1DSHADOW;
422     (*KeywordMap)["sampler3D"] =               SAMPLER3D;
423     (*KeywordMap)["sampler2DShadow"] =         SAMPLER2DSHADOW;
424     (*KeywordMap)["sampler2DRect"] =           SAMPLER2DRECT;
425     (*KeywordMap)["sampler2DRectShadow"] =     SAMPLER2DRECTSHADOW;
426     (*KeywordMap)["sampler1DArray"] =          SAMPLER1DARRAY;
427     (*KeywordMap)["samplerExternalOES"] =      SAMPLEREXTERNALOES; // GL_OES_EGL_image_external
428     (*KeywordMap)["noperspective"] =           NOPERSPECTIVE;
429     (*KeywordMap)["smooth"] =                  SMOOTH;
430     (*KeywordMap)["flat"] =                    FLAT;
431     (*KeywordMap)["centroid"] =                CENTROID;
432     (*KeywordMap)["precise"] =                 PRECISE;
433     (*KeywordMap)["invariant"] =               INVARIANT;
434     (*KeywordMap)["packed"] =                  PACKED;
435     (*KeywordMap)["resource"] =                RESOURCE;
436     (*KeywordMap)["superp"] =                  SUPERP;
437
438     ReservedSet = new std::set<std::string>;
439     
440     ReservedSet->insert("common");
441     ReservedSet->insert("partition");
442     ReservedSet->insert("active");
443     ReservedSet->insert("asm");
444     ReservedSet->insert("class");
445     ReservedSet->insert("union");
446     ReservedSet->insert("enum");
447     ReservedSet->insert("typedef");
448     ReservedSet->insert("template");
449     ReservedSet->insert("this");
450     ReservedSet->insert("goto");
451     ReservedSet->insert("inline");
452     ReservedSet->insert("noinline");
453     ReservedSet->insert("public");
454     ReservedSet->insert("static");
455     ReservedSet->insert("extern");
456     ReservedSet->insert("external");
457     ReservedSet->insert("interface");
458     ReservedSet->insert("long");
459     ReservedSet->insert("short");
460     ReservedSet->insert("half");
461     ReservedSet->insert("fixed");
462     ReservedSet->insert("unsigned");
463     ReservedSet->insert("input");
464     ReservedSet->insert("output");
465     ReservedSet->insert("hvec2");
466     ReservedSet->insert("hvec3");
467     ReservedSet->insert("hvec4");
468     ReservedSet->insert("fvec2");
469     ReservedSet->insert("fvec3");
470     ReservedSet->insert("fvec4");
471     ReservedSet->insert("sampler3DRect");
472     ReservedSet->insert("filter");
473     ReservedSet->insert("sizeof");
474     ReservedSet->insert("cast");
475     ReservedSet->insert("namespace");
476     ReservedSet->insert("using");
477 }
478
479 int TScanContext::tokenize(TPpContext* pp, TParserToken& token)
480 {
481     parserToken = &token;
482     TPpToken ppToken;
483     tokenText = pp->tokenize(&ppToken);
484     if (tokenText == 0)
485         return 0;
486
487     loc = ppToken.loc;
488     parserToken->sType.lex.loc = loc;
489     switch (ppToken.token) {
490     case ';':  afterType = false;   return SEMICOLON;
491     case ',':  afterType = false;   return COMMA;
492     case ':':                       return COLON;
493     case '=':  afterType = false;   return EQUAL;
494     case '(':  afterType = false;   return LEFT_PAREN;
495     case ')':  afterType = false;   return RIGHT_PAREN;
496     case '.':  field = true;        return DOT;
497     case '!':                       return BANG;
498     case '-':                       return DASH;
499     case '~':                       return TILDE;
500     case '+':                       return PLUS;
501     case '*':                       return STAR;
502     case '/':                       return SLASH;
503     case '%':                       return PERCENT;
504     case '<':                       return LEFT_ANGLE;
505     case '>':                       return RIGHT_ANGLE;
506     case '|':                       return VERTICAL_BAR;
507     case '^':                       return CARET;
508     case '&':                       return AMPERSAND;
509     case '?':                       return QUESTION;
510     case '[':                       return LEFT_BRACKET;
511     case ']':                       return RIGHT_BRACKET;
512     case '{':                       return LEFT_BRACE;
513     case '}':                       return RIGHT_BRACE;
514
515     case CPP_AND_OP:                return AND_OP;
516     case CPP_SUB_ASSIGN:            return SUB_ASSIGN;
517     case CPP_MOD_ASSIGN:            return MOD_ASSIGN;
518     case CPP_ADD_ASSIGN:            return ADD_ASSIGN;
519     case CPP_DIV_ASSIGN:            return DIV_ASSIGN;
520     case CPP_MUL_ASSIGN:            return MUL_ASSIGN;
521     case CPP_EQ_OP:                 return EQ_OP;
522     case CPP_XOR_OP:                return XOR_OP;
523     case CPP_GE_OP:                 return GE_OP;
524     case CPP_RIGHT_OP:              return RIGHT_OP;
525     case CPP_LE_OP:                 return LE_OP;
526     case CPP_LEFT_OP:               return LEFT_OP;
527     case CPP_DEC_OP:                return DEC_OP;
528     case CPP_NE_OP:                 return NE_OP;
529     case CPP_OR_OP:                 return OR_OP;
530     case CPP_INC_OP:                return INC_OP;
531     case CPP_RIGHT_ASSIGN:          return RIGHT_ASSIGN;
532     case CPP_LEFT_ASSIGN:           return LEFT_ASSIGN;
533     case CPP_AND_ASSIGN:            return AND_ASSIGN;
534     case CPP_OR_ASSIGN:             return OR_ASSIGN;
535     case CPP_XOR_ASSIGN:            return XOR_ASSIGN;
536                                    
537     case CPP_INTCONSTANT:           parserToken->sType.lex.i = ppToken.ival;       return INTCONSTANT;
538     case CPP_UINTCONSTANT:          parserToken->sType.lex.i = ppToken.ival;       return UINTCONSTANT;
539     case CPP_FLOATCONSTANT:         parserToken->sType.lex.d = ppToken.dval;       return FLOATCONSTANT;
540     case CPP_DOUBLECONSTANT:        parserToken->sType.lex.d = ppToken.dval;       return DOUBLECONSTANT;
541     case CPP_IDENTIFIER:            return tokenizeIdentifier();
542
543     case EOF:                       return 0;
544                                    
545     default:
546         parseContext.infoSink.info.message(EPrefixInternalError, "Unknown PP token", loc);
547         return 0;
548     }
549 }
550
551 int TScanContext::tokenizeIdentifier()
552 {
553     if (ReservedSet->find(tokenText) != ReservedSet->end())
554         return reservedWord();
555
556     std::map<std::string, int>::const_iterator it = KeywordMap->find(tokenText);
557     if (it == KeywordMap->end()) {
558         // Should have an identifier of some sort
559         return identifierOrType();
560     }
561     keyword = it->second;
562     field = false;
563
564     switch (keyword) {
565     case CONST:
566     case UNIFORM:
567     case IN:
568     case OUT:
569     case INOUT:
570     case STRUCT:
571     case BREAK:
572     case CONTINUE:
573     case DO:
574     case FOR:
575     case WHILE:
576     case IF:
577     case ELSE:
578     case DISCARD:
579     case RETURN:
580     case CASE:
581         return keyword;
582
583     case SWITCH:
584     case DEFAULT:
585         if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
586             (parseContext.profile != EEsProfile && parseContext.version < 130))
587             reservedWord();
588         return keyword;
589
590     case VOID:
591     case BOOL:
592     case FLOAT:
593     case INT:
594     case BVEC2:
595     case BVEC3:
596     case BVEC4:
597     case VEC2:
598     case VEC3:
599     case VEC4:
600     case IVEC2:
601     case IVEC3:
602     case IVEC4:
603     case MAT2:
604     case MAT3:
605     case MAT4:
606     case SAMPLER2D:
607     case SAMPLERCUBE:
608         afterType = true;
609         return keyword;
610
611     case BOOLCONSTANT:
612         if (strcmp("true", tokenText) == 0)
613             parserToken->sType.lex.b = true;
614         else
615             parserToken->sType.lex.b = false;
616         return keyword;
617
618     case ATTRIBUTE:
619     case VARYING:
620         if (parseContext.profile == EEsProfile && parseContext.version >= 300)
621             reservedWord();
622         return keyword;
623
624     case BUFFER:
625         if (parseContext.version < 430)
626             return identifierOrType();
627         return keyword;
628
629     case COHERENT:
630     case RESTRICT:
631     case READONLY:
632     case WRITEONLY:
633     case ATOMIC_UINT:
634         return es30ReservedFromGLSL(420);
635
636     case VOLATILE:
637         if (parseContext.profile == EEsProfile || parseContext.version < 420)
638             reservedWord();
639         return keyword;
640
641     case LAYOUT:
642     case SHARED:
643         if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
644             (parseContext.profile != EEsProfile && parseContext.version < 140))
645             return identifierOrType();
646         return keyword;
647
648     case PATCH:
649     case SAMPLE:
650     case SUBROUTINE:
651         return es30ReservedFromGLSL(400);
652
653     case HIGH_PRECISION:
654     case MEDIUM_PRECISION:
655     case LOW_PRECISION:
656     case PRECISION:
657         return precisionKeyword();
658
659     case MAT2X2:
660     case MAT2X3:
661     case MAT2X4:
662     case MAT3X2:
663     case MAT3X3:
664     case MAT3X4:
665     case MAT4X2:
666     case MAT4X3:
667     case MAT4X4:        
668         return matNxM();
669
670     case DMAT2:
671     case DMAT3:
672     case DMAT4:
673     case DMAT2X2:
674     case DMAT2X3:
675     case DMAT2X4:
676     case DMAT3X2:
677     case DMAT3X3:
678     case DMAT3X4:
679     case DMAT4X2:
680     case DMAT4X3:
681     case DMAT4X4:
682         return dMat();
683
684     case IMAGE1D:
685     case IIMAGE1D:
686     case UIMAGE1D:
687     case IMAGE2D:
688     case IIMAGE2D:
689     case UIMAGE2D:
690     case IMAGE3D:
691     case IIMAGE3D:
692     case UIMAGE3D:
693     case IMAGE2DRECT:
694     case IIMAGE2DRECT:
695     case UIMAGE2DRECT:
696     case IMAGECUBE:
697     case IIMAGECUBE:
698     case UIMAGECUBE:
699     case IMAGEBUFFER:
700     case IIMAGEBUFFER:
701     case UIMAGEBUFFER:
702     case IMAGE1DARRAY:
703     case IIMAGE1DARRAY:
704     case UIMAGE1DARRAY:
705     case IMAGE2DARRAY:
706     case IIMAGE2DARRAY:
707     case UIMAGE2DARRAY:
708         return firstGenerationImage();
709
710     case IMAGECUBEARRAY:
711     case IIMAGECUBEARRAY:
712     case UIMAGECUBEARRAY:
713     case IMAGE2DMS:
714     case IIMAGE2DMS:
715     case UIMAGE2DMS:
716     case IMAGE2DMSARRAY:
717     case IIMAGE2DMSARRAY:
718     case UIMAGE2DMSARRAY:
719         return secondGenerationImage();
720
721     case DOUBLE:
722     case DVEC2:
723     case DVEC3:
724     case DVEC4:
725     case SAMPLERCUBEARRAY:
726     case SAMPLERCUBEARRAYSHADOW:
727     case ISAMPLERCUBEARRAY:
728     case USAMPLERCUBEARRAY:
729         afterType = true;
730         if (parseContext.profile == EEsProfile || parseContext.version < 400)
731             reservedWord();
732         return keyword;
733
734     case ISAMPLER1D:
735     case ISAMPLER1DARRAY:
736     case SAMPLER1DARRAYSHADOW:
737     case USAMPLER1D:
738     case USAMPLER1DARRAY:
739     case SAMPLERBUFFER:
740         afterType = true;
741         return es30ReservedFromGLSL(130);
742
743     case UINT:
744     case UVEC2:
745     case UVEC3:
746     case UVEC4:
747     case SAMPLERCUBESHADOW:
748     case SAMPLER2DARRAY:
749     case SAMPLER2DARRAYSHADOW:
750     case ISAMPLER2D:
751     case ISAMPLER3D:
752     case ISAMPLERCUBE:
753     case ISAMPLER2DARRAY:
754     case USAMPLER2D:
755     case USAMPLER3D:
756     case USAMPLERCUBE:
757     case USAMPLER2DARRAY:
758         afterType = true;
759         return nonreservedKeyword(300, 130);
760         
761     case ISAMPLER2DRECT:
762     case USAMPLER2DRECT:
763     case ISAMPLERBUFFER:
764     case USAMPLERBUFFER:
765         afterType = true;
766         return es30ReservedFromGLSL(140);
767         
768     case SAMPLER2DMS:
769     case ISAMPLER2DMS:
770     case USAMPLER2DMS:
771     case SAMPLER2DMSARRAY:
772     case ISAMPLER2DMSARRAY:
773     case USAMPLER2DMSARRAY:
774         afterType = true;
775         return es30ReservedFromGLSL(150);
776
777     case SAMPLER1D:
778     case SAMPLER1DSHADOW:
779         afterType = true;
780         if (parseContext.profile == EEsProfile)
781             reservedWord();
782         return keyword;
783
784     case SAMPLER3D:
785         afterType = true;
786         if (parseContext.profile == EEsProfile && parseContext.version < 300) {
787             if (! parseContext.extensionsTurnedOn(1, &GL_OES_texture_3D))
788                 reservedWord();
789         }
790         return keyword;
791
792     case SAMPLER2DSHADOW:
793         afterType = true;
794         if (parseContext.profile == EEsProfile && parseContext.version < 300)
795             reservedWord();
796         return keyword;
797
798     case SAMPLER2DRECT:
799     case SAMPLER2DRECTSHADOW:
800         afterType = true;
801         if (parseContext.profile == EEsProfile ||
802             (parseContext.profile != EEsProfile && parseContext.version < 140))
803             reservedWord();
804         return keyword;
805
806     case SAMPLER1DARRAY:
807         afterType = true;
808         if (parseContext.profile == EEsProfile && parseContext.version == 300)
809             reservedWord();
810         else if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
811                  (parseContext.profile != EEsProfile && parseContext.version < 130))
812             return identifierOrType();
813         return keyword;
814
815     case SAMPLEREXTERNALOES:
816         afterType = true;
817         if (parseContext.symbolTable.atBuiltInLevel() || parseContext.extensionsTurnedOn(1, &GL_OES_EGL_image_external))
818             return keyword;
819         return identifierOrType();
820
821     case NOPERSPECTIVE:
822         return es30ReservedFromGLSL(130);
823         
824     case SMOOTH:
825         if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
826             (parseContext.profile != EEsProfile && parseContext.version < 130))
827             return identifierOrType();
828         return keyword;
829
830     case FLAT:
831         if (parseContext.profile == EEsProfile && parseContext.version < 300)
832             reservedWord();
833         else if (parseContext.profile != EEsProfile && parseContext.version < 130)
834             return identifierOrType();
835         return keyword;
836
837     case CENTROID:
838         if (parseContext.version < 120)
839             return identifierOrType();
840         return keyword;
841
842     case PRECISE:
843         if (parseContext.profile == EEsProfile ||
844             (parseContext.profile != EEsProfile && parseContext.version < 400))
845             return identifierOrType();
846         return keyword;
847
848     case INVARIANT:
849         if (parseContext.profile != EEsProfile && parseContext.version < 120)
850             return identifierOrType();
851         return keyword;
852
853     case PACKED:
854         if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
855             (parseContext.profile != EEsProfile && parseContext.version < 330))
856             return reservedWord();
857         return identifierOrType();
858
859     case RESOURCE:
860     {
861         bool reserved = (parseContext.profile == EEsProfile && parseContext.version >= 300) ||
862                         (parseContext.profile != EEsProfile && parseContext.version >= 420);
863         return identifierOrReserved(reserved);
864     }
865     case SUPERP:
866     {
867         bool reserved = parseContext.profile == EEsProfile || parseContext.version >= 130;
868         return identifierOrReserved(reserved);
869     }
870     
871     default:
872         parseContext.infoSink.info.message(EPrefixInternalError, "Unknown glslang keyword", loc);
873         return 0;
874     }
875 }
876
877 int TScanContext::identifierOrType()
878 {
879     parserToken->sType.lex.string = NewPoolTString(tokenText);
880     if (field) {
881         field = false;
882  
883         return FIELD_SELECTION;
884     }
885
886     parserToken->sType.lex.symbol = parseContext.symbolTable.find(*parserToken->sType.lex.string);
887     if (afterType == false && parserToken->sType.lex.symbol) {
888         if (const TVariable* variable = parserToken->sType.lex.symbol->getAsVariable()) {
889             if (variable->isUserType()) {
890                 afterType = true;
891
892                 return TYPE_NAME;
893             }
894         }
895     }
896
897     return IDENTIFIER;
898 }
899
900 // Give an error for use of a reserved symbol.
901 // However, allow built-in declarations to use reserved words, to allow
902 // extension support before the extension is enabled.
903 int TScanContext::reservedWord()
904 {
905     if (! parseContext.symbolTable.atBuiltInLevel())
906         parseContext.error(loc, "Reserved word.", tokenText, "", "");
907
908     return 0;
909 }
910
911 int TScanContext::identifierOrReserved(bool reserved)
912 {
913     if (reserved) {
914         reservedWord();
915
916         return 0;
917     }
918
919     if (parseContext.forwardCompatible)
920         parseContext.warn(loc, "using future reserved keyword", tokenText, "");
921
922     return identifierOrType();
923 }
924
925 // For keywords that suddenly showed up on non-ES (not previously reserved)
926 // but then got reserved by ES 3.0.
927 int TScanContext::es30ReservedFromGLSL(int version)
928 {
929     if ((parseContext.profile == EEsProfile && parseContext.version < 300) ||
930         (parseContext.profile != EEsProfile && parseContext.version < version)) {
931             if (parseContext.forwardCompatible)
932                 parseContext.warn(loc, "future reserved word in ES 300 and keyword in GLSL", tokenText, "");
933
934             return identifierOrType();
935     } else if (parseContext.profile == EEsProfile && parseContext.version >= 300)
936         reservedWord();
937
938     return keyword;
939 }
940
941 // For a keyword that was never reserved, until it suddenly
942 // showed up, both in an es version and a non-ES version.
943 int TScanContext::nonreservedKeyword(int esVersion, int nonEsVersion)
944 {
945     if ((parseContext.profile == EEsProfile && parseContext.version < esVersion) ||
946         (parseContext.profile != EEsProfile && parseContext.version < nonEsVersion)) {
947         if (parseContext.forwardCompatible)
948             parseContext.warn(loc, "using future keyword", tokenText, "");
949
950         return identifierOrType();
951     }
952
953     return keyword;
954 }
955
956 int TScanContext::precisionKeyword()
957 {
958     if (parseContext.profile == EEsProfile || parseContext.version >= 130)
959         return keyword;
960
961     if (parseContext.forwardCompatible)
962         parseContext.warn(loc, "using ES precision qualifier keyword", tokenText, "");
963
964     return identifierOrType();
965 }
966
967 int TScanContext::matNxM()
968 {
969     afterType = true;
970
971     if (parseContext.version > 110)
972         return keyword;
973
974     if (parseContext.forwardCompatible)
975         parseContext.warn(loc, "using future non-square matrix type keyword", tokenText, "");
976
977     return identifierOrType();
978 }
979
980 int TScanContext::dMat()
981 {
982     afterType = true;
983
984     if (parseContext.profile == EEsProfile && parseContext.version >= 300) {
985         reservedWord();
986
987         return keyword;
988     }
989
990     if (parseContext.profile != EEsProfile && parseContext.version >= 400)
991         return keyword;
992
993     if (parseContext.forwardCompatible)
994         parseContext.warn(loc, "using future type keyword", tokenText, "");
995
996     return identifierOrType();
997 }
998
999 int TScanContext::firstGenerationImage()
1000 {
1001     afterType = true;
1002
1003     if (parseContext.profile != EEsProfile && parseContext.version >= 420)
1004         return keyword;
1005
1006     if ((parseContext.profile == EEsProfile && parseContext.version >= 300) ||
1007         (parseContext.profile != EEsProfile && parseContext.version >= 130)) {
1008         reservedWord();
1009
1010         return keyword;
1011     }
1012
1013     if (parseContext.forwardCompatible)
1014         parseContext.warn(loc, "using future type keyword", tokenText, "");
1015
1016     return identifierOrType();
1017 }
1018
1019 int TScanContext::secondGenerationImage()
1020 {
1021     afterType = true;
1022
1023     if (parseContext.profile != EEsProfile && parseContext.version >= 420)
1024         return keyword;
1025
1026     if (parseContext.forwardCompatible)
1027         parseContext.warn(loc, "using future type keyword", tokenText, "");
1028
1029     return identifierOrType();
1030 }
1031
1032 } // end namespace glslang