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 8.2KB

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