Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glsl_lexer.lpp 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. %{
  2. /*
  3. * Copyright © 2008, 2009 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. #include <ctype.h>
  25. #include "ast.h"
  26. #include "glsl_parser_extras.h"
  27. #include "glsl_parser.h"
  28. #define YY_USER_ACTION \
  29. do { \
  30. yylloc->source = 0; \
  31. yylloc->first_column = yycolumn + 1; \
  32. yylloc->first_line = yylineno + 1; \
  33. yycolumn += yyleng; \
  34. } while(0);
  35. %}
  36. %option bison-bridge bison-locations reentrant noyywrap
  37. %option nounput noyy_top_state
  38. %option never-interactive
  39. %option prefix="_mesa_glsl_"
  40. %option extra-type="struct _mesa_glsl_parse_state *"
  41. %x PP
  42. DEC_INT [1-9][0-9]*
  43. HEX_INT 0[xX][0-9a-fA-F]+
  44. OCT_INT 0[0-7]*
  45. INT ({DEC_INT}|{HEX_INT}|{OCT_INT})
  46. SPC [ \t]*
  47. SPCP [ \t]+
  48. HASH ^{SPC}#{SPC}
  49. %%
  50. [ \r\t]+ ;
  51. /* Preprocessor tokens. */
  52. ^[ \t]*#[ \t]*$ ;
  53. ^[ \t]*#[ \t]*version { BEGIN PP; return VERSION; }
  54. ^[ \t]*#[ \t]*extension { BEGIN PP; return EXTENSION; }
  55. {HASH}line{SPCP}{INT}{SPCP}{INT}{SPC}$ {
  56. /* Eat characters until the first digit is
  57. * encountered
  58. */
  59. char *ptr = yytext;
  60. while (!isdigit(*ptr))
  61. ptr++;
  62. /* Subtract one from the line number because
  63. * yylineno is zero-based instead of
  64. * one-based.
  65. */
  66. yylineno = strtol(ptr, &ptr, 0) - 1;
  67. yylloc->source = strtol(ptr, NULL, 0);
  68. }
  69. {HASH}line{SPCP}{INT}{SPC}$ {
  70. /* Eat characters until the first digit is
  71. * encountered
  72. */
  73. char *ptr = yytext;
  74. while (!isdigit(*ptr))
  75. ptr++;
  76. /* Subtract one from the line number because
  77. * yylineno is zero-based instead of
  78. * one-based.
  79. */
  80. yylineno = strtol(ptr, &ptr, 0) - 1;
  81. }
  82. ^[ \t]*#[ \t]*pragma { BEGIN PP; return PRAGMA; }
  83. <PP>\/\/[^\n]* { }
  84. <PP>[ \t\r]* { }
  85. <PP>: return COLON;
  86. <PP>[_a-zA-Z][_a-zA-Z0-9]* {
  87. yylval->identifier = strdup(yytext);
  88. return IDENTIFIER;
  89. }
  90. <PP>[1-9][0-9]* {
  91. yylval->n = strtol(yytext, NULL, 10);
  92. return INTCONSTANT;
  93. }
  94. <PP>\n { BEGIN 0; yylineno++; yycolumn = 0; return EOL; }
  95. \n { yylineno++; yycolumn = 0; }
  96. attribute return ATTRIBUTE;
  97. const return CONST;
  98. bool return BOOL;
  99. float return FLOAT;
  100. int return INT;
  101. break return BREAK;
  102. continue return CONTINUE;
  103. do return DO;
  104. while return WHILE;
  105. else return ELSE;
  106. for return FOR;
  107. if return IF;
  108. discard return DISCARD;
  109. return return RETURN;
  110. bvec2 return BVEC2;
  111. bvec3 return BVEC3;
  112. bvec4 return BVEC4;
  113. ivec2 return IVEC2;
  114. ivec3 return IVEC3;
  115. ivec4 return IVEC4;
  116. vec2 return VEC2;
  117. vec3 return VEC3;
  118. vec4 return VEC4;
  119. mat2 return MAT2;
  120. mat3 return MAT3;
  121. mat4 return MAT4;
  122. mat2x2 return MAT2X2;
  123. mat2x3 return MAT2X3;
  124. mat2x4 return MAT2X4;
  125. mat3x2 return MAT3X2;
  126. mat3x3 return MAT3X3;
  127. mat3x4 return MAT3X4;
  128. mat4x2 return MAT4X2;
  129. mat4x3 return MAT4X3;
  130. mat4x4 return MAT4X4;
  131. in return IN;
  132. out return OUT;
  133. inout return INOUT;
  134. uniform return UNIFORM;
  135. varying return VARYING;
  136. centroid {
  137. if (yyextra->language_version >= 120) {
  138. return CENTROID;
  139. } else {
  140. yylval->identifier = strdup(yytext);
  141. return IDENTIFIER;
  142. }
  143. }
  144. invariant {
  145. if (yyextra->language_version >= 120) {
  146. return INVARIANT;
  147. } else {
  148. yylval->identifier = strdup(yytext);
  149. return IDENTIFIER;
  150. }
  151. }
  152. flat {
  153. if (yyextra->language_version >= 130) {
  154. return FLAT;
  155. } else {
  156. yylval->identifier = strdup(yytext);
  157. return IDENTIFIER;
  158. }
  159. }
  160. smooth {
  161. if (yyextra->language_version >= 130) {
  162. return SMOOTH;
  163. } else {
  164. yylval->identifier = strdup(yytext);
  165. return IDENTIFIER;
  166. }
  167. }
  168. noperspective {
  169. if (yyextra->language_version >= 130) {
  170. return NOPERSPECTIVE;
  171. } else {
  172. yylval->identifier = strdup(yytext);
  173. return IDENTIFIER;
  174. }
  175. }
  176. sampler1D return SAMPLER1D;
  177. sampler2D return SAMPLER2D;
  178. sampler3D return SAMPLER3D;
  179. samplerCube return SAMPLERCUBE;
  180. sampler1DShadow return SAMPLER1DSHADOW;
  181. sampler2DShadow return SAMPLER2DSHADOW;
  182. struct return STRUCT;
  183. void return VOID;
  184. \+\+ return INC_OP;
  185. -- return DEC_OP;
  186. \<= return LE_OP;
  187. >= return GE_OP;
  188. == return EQ_OP;
  189. != return NE_OP;
  190. && return AND_OP;
  191. \|\| return OR_OP;
  192. "^^" return XOR_OP;
  193. \*= return MUL_ASSIGN;
  194. \/= return DIV_ASSIGN;
  195. \+= return ADD_ASSIGN;
  196. \%= return MOD_ASSIGN;
  197. \<\<= return LEFT_ASSIGN;
  198. >>= return RIGHT_ASSIGN;
  199. &= return AND_ASSIGN;
  200. ^= return XOR_ASSIGN;
  201. \|= return OR_ASSIGN;
  202. -= return SUB_ASSIGN;
  203. [1-9][0-9]* {
  204. yylval->n = strtol(yytext, NULL, 10);
  205. return INTCONSTANT;
  206. }
  207. 0[xX][0-9a-fA-F]+ {
  208. yylval->n = strtol(yytext + 2, NULL, 16);
  209. return INTCONSTANT;
  210. }
  211. 0[0-7]* {
  212. yylval->n = strtol(yytext + 2, NULL, 8);
  213. return INTCONSTANT;
  214. }
  215. [0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
  216. yylval->real = strtod(yytext, NULL);
  217. return FLOATCONSTANT;
  218. }
  219. \.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
  220. yylval->real = strtod(yytext, NULL);
  221. return FLOATCONSTANT;
  222. }
  223. [0-9]+\.([eE][+-]?[0-9]+)?[fF]? {
  224. yylval->real = strtod(yytext, NULL);
  225. return FLOATCONSTANT;
  226. }
  227. [0-9]+[eE][+-]?[0-9]+[fF]? {
  228. yylval->real = strtod(yytext, NULL);
  229. return FLOATCONSTANT;
  230. }
  231. true {
  232. yylval->n = 1;
  233. return BOOLCONSTANT;
  234. }
  235. false {
  236. yylval->n = 0;
  237. return BOOLCONSTANT;
  238. }
  239. /* Reserved words in GLSL 1.10. */
  240. asm return ASM;
  241. class return CLASS;
  242. union return UNION;
  243. enum return ENUM;
  244. typedef return TYPEDEF;
  245. template return TEMPLATE;
  246. this return THIS;
  247. packed return PACKED;
  248. goto return GOTO;
  249. switch return SWITCH;
  250. default return DEFAULT;
  251. inline return INLINE;
  252. noinline return NOINLINE;
  253. volatile return VOLATILE;
  254. public return PUBLIC;
  255. static return STATIC;
  256. extern return EXTERN;
  257. external return EXTERNAL;
  258. interface return INTERFACE;
  259. long return LONG;
  260. short return SHORT;
  261. double return DOUBLE;
  262. half return HALF;
  263. fixed return FIXED;
  264. unsigned return UNSIGNED;
  265. input return INPUT;
  266. output return OUTPUT;
  267. hvec2 return HVEC2;
  268. hvec3 return HVEC3;
  269. hvec4 return HVEC4;
  270. dvec2 return DVEC2;
  271. dvec3 return DVEC3;
  272. dvec4 return DVEC4;
  273. fvec2 return FVEC2;
  274. fvec3 return FVEC3;
  275. fvec4 return FVEC4;
  276. sampler2DRect return SAMPLER2DRECT;
  277. sampler3DRect return SAMPLER3DRECT;
  278. sampler2DRectShadow return SAMPLER2DRECTSHADOW;
  279. sizeof return SIZEOF;
  280. cast return CAST;
  281. namespace return NAMESPACE;
  282. using return USING;
  283. /* Additional reserved words in GLSL 1.20. */
  284. lowp return LOWP;
  285. mediump return MEDIUMP;
  286. highp return HIGHP;
  287. precision return PRECISION;
  288. [_a-zA-Z][_a-zA-Z0-9]* {
  289. yylval->identifier = strdup(yytext);
  290. return IDENTIFIER;
  291. }
  292. . { return yytext[0]; }
  293. %%
  294. void
  295. _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state, const char *string)
  296. {
  297. yylex_init_extra(state, & state->scanner);
  298. yy_scan_string(string, state->scanner);
  299. }
  300. void
  301. _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state)
  302. {
  303. yylex_destroy(state->scanner);
  304. }