shader-parser.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "../util/platform.h"
  15. #include "shader-parser.h"
  16. enum gs_shader_param_type get_shader_param_type(const char *type)
  17. {
  18. if (strcmp(type, "float") == 0)
  19. return GS_SHADER_PARAM_FLOAT;
  20. else if (strcmp(type, "float2") == 0)
  21. return GS_SHADER_PARAM_VEC2;
  22. else if (strcmp(type, "float3") == 0)
  23. return GS_SHADER_PARAM_VEC3;
  24. else if (strcmp(type, "float4") == 0)
  25. return GS_SHADER_PARAM_VEC4;
  26. else if (strcmp(type, "int2") == 0)
  27. return GS_SHADER_PARAM_INT2;
  28. else if (strcmp(type, "int3") == 0)
  29. return GS_SHADER_PARAM_INT3;
  30. else if (strcmp(type, "int4") == 0)
  31. return GS_SHADER_PARAM_INT4;
  32. else if (astrcmp_n(type, "texture", 7) == 0)
  33. return GS_SHADER_PARAM_TEXTURE;
  34. else if (strcmp(type, "float4x4") == 0)
  35. return GS_SHADER_PARAM_MATRIX4X4;
  36. else if (strcmp(type, "bool") == 0)
  37. return GS_SHADER_PARAM_BOOL;
  38. else if (strcmp(type, "int") == 0)
  39. return GS_SHADER_PARAM_INT;
  40. else if (strcmp(type, "string") == 0)
  41. return GS_SHADER_PARAM_STRING;
  42. return GS_SHADER_PARAM_UNKNOWN;
  43. }
  44. enum gs_sample_filter get_sample_filter(const char *filter)
  45. {
  46. if (astrcmpi(filter, "Anisotropy") == 0)
  47. return GS_FILTER_ANISOTROPIC;
  48. else if (astrcmpi(filter, "Point") == 0 ||
  49. strcmp(filter, "MIN_MAG_MIP_POINT") == 0)
  50. return GS_FILTER_POINT;
  51. else if (astrcmpi(filter, "Linear") == 0 ||
  52. strcmp(filter, "MIN_MAG_MIP_LINEAR") == 0)
  53. return GS_FILTER_LINEAR;
  54. else if (strcmp(filter, "MIN_MAG_POINT_MIP_LINEAR") == 0)
  55. return GS_FILTER_MIN_MAG_POINT_MIP_LINEAR;
  56. else if (strcmp(filter, "MIN_POINT_MAG_LINEAR_MIP_POINT") == 0)
  57. return GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;
  58. else if (strcmp(filter, "MIN_POINT_MAG_MIP_LINEAR") == 0)
  59. return GS_FILTER_MIN_POINT_MAG_MIP_LINEAR;
  60. else if (strcmp(filter, "MIN_LINEAR_MAG_MIP_POINT") == 0)
  61. return GS_FILTER_MIN_LINEAR_MAG_MIP_POINT;
  62. else if (strcmp(filter, "MIN_LINEAR_MAG_POINT_MIP_LINEAR") == 0)
  63. return GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
  64. else if (strcmp(filter, "MIN_MAG_LINEAR_MIP_POINT") == 0)
  65. return GS_FILTER_MIN_MAG_LINEAR_MIP_POINT;
  66. return GS_FILTER_LINEAR;
  67. }
  68. extern enum gs_address_mode get_address_mode(const char *mode)
  69. {
  70. if (astrcmpi(mode, "Wrap") == 0 || astrcmpi(mode, "Repeat") == 0)
  71. return GS_ADDRESS_WRAP;
  72. else if (astrcmpi(mode, "Clamp") == 0 || astrcmpi(mode, "None") == 0)
  73. return GS_ADDRESS_CLAMP;
  74. else if (astrcmpi(mode, "Mirror") == 0)
  75. return GS_ADDRESS_MIRROR;
  76. else if (astrcmpi(mode, "Border") == 0)
  77. return GS_ADDRESS_BORDER;
  78. else if (astrcmpi(mode, "MirrorOnce") == 0)
  79. return GS_ADDRESS_MIRRORONCE;
  80. return GS_ADDRESS_CLAMP;
  81. }
  82. void shader_sampler_convert(struct shader_sampler *ss,
  83. struct gs_sampler_info *info)
  84. {
  85. size_t i;
  86. memset(info, 0, sizeof(struct gs_sampler_info));
  87. for (i = 0; i < ss->states.num; i++) {
  88. const char *state = ss->states.array[i];
  89. const char *value = ss->values.array[i];
  90. if (astrcmpi(state, "Filter") == 0)
  91. info->filter = get_sample_filter(value);
  92. else if (astrcmpi(state, "AddressU") == 0)
  93. info->address_u = get_address_mode(value);
  94. else if (astrcmpi(state, "AddressV") == 0)
  95. info->address_v = get_address_mode(value);
  96. else if (astrcmpi(state, "AddressW") == 0)
  97. info->address_w = get_address_mode(value);
  98. else if (astrcmpi(state, "MaxAnisotropy") == 0)
  99. info->max_anisotropy = (int)strtol(value, NULL, 10);
  100. else if (astrcmpi(state, "BorderColor") == 0)
  101. info->border_color = strtol(value + 1, NULL, 16);
  102. }
  103. }
  104. /* ------------------------------------------------------------------------- */
  105. static int sp_parse_sampler_state_item(struct shader_parser *sp,
  106. struct shader_sampler *ss)
  107. {
  108. int ret;
  109. char *state = NULL, *value = NULL;
  110. ret = cf_next_name(&sp->cfp, &state, "state name", ";");
  111. if (ret != PARSE_SUCCESS) goto fail;
  112. ret = cf_next_token_should_be(&sp->cfp, "=", ";", NULL);
  113. if (ret != PARSE_SUCCESS) goto fail;
  114. ret = cf_next_token_copy(&sp->cfp, &value);
  115. if (ret != PARSE_SUCCESS) goto fail;
  116. ret = cf_next_token_should_be(&sp->cfp, ";", ";", NULL);
  117. if (ret != PARSE_SUCCESS) goto fail;
  118. da_push_back(ss->states, &state);
  119. da_push_back(ss->values, &value);
  120. return ret;
  121. fail:
  122. bfree(state);
  123. bfree(value);
  124. return ret;
  125. }
  126. static void sp_parse_sampler_state(struct shader_parser *sp)
  127. {
  128. struct shader_sampler ss;
  129. struct cf_token peek;
  130. shader_sampler_init(&ss);
  131. if (cf_next_name(&sp->cfp, &ss.name, "name", ";") != PARSE_SUCCESS)
  132. goto error;
  133. if (cf_next_token_should_be(&sp->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  134. goto error;
  135. if (!cf_peek_valid_token(&sp->cfp, &peek))
  136. goto error;
  137. while (strref_cmp(&peek.str, "}") != 0) {
  138. int ret = sp_parse_sampler_state_item(sp, &ss);
  139. if (ret == PARSE_EOF)
  140. goto error;
  141. if (!cf_peek_valid_token(&sp->cfp, &peek))
  142. goto error;
  143. }
  144. if (cf_next_token_should_be(&sp->cfp, "}", ";", NULL) != PARSE_SUCCESS)
  145. goto error;
  146. if (cf_next_token_should_be(&sp->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  147. goto error;
  148. da_push_back(sp->samplers, &ss);
  149. return;
  150. error:
  151. shader_sampler_free(&ss);
  152. }
  153. static inline int sp_parse_struct_var(struct shader_parser *sp,
  154. struct shader_var *var)
  155. {
  156. int code;
  157. /* -------------------------------------- */
  158. /* variable type */
  159. if (!cf_next_valid_token(&sp->cfp)) return PARSE_EOF;
  160. if (cf_token_is(&sp->cfp, ";")) return PARSE_CONTINUE;
  161. if (cf_token_is(&sp->cfp, "}")) return PARSE_BREAK;
  162. code = cf_token_is_type(&sp->cfp, CFTOKEN_NAME, "type name", ";");
  163. if (code != PARSE_SUCCESS)
  164. return code;
  165. cf_copy_token(&sp->cfp, &var->type);
  166. /* -------------------------------------- */
  167. /* variable name */
  168. if (!cf_next_valid_token(&sp->cfp)) return PARSE_EOF;
  169. if (cf_token_is(&sp->cfp, ";")) return PARSE_UNEXPECTED_CONTINUE;
  170. if (cf_token_is(&sp->cfp, "}")) return PARSE_UNEXPECTED_BREAK;
  171. code = cf_token_is_type(&sp->cfp, CFTOKEN_NAME, "variable name",
  172. ";");
  173. if (code != PARSE_SUCCESS)
  174. return code;
  175. cf_copy_token(&sp->cfp, &var->name);
  176. /* -------------------------------------- */
  177. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  178. if (!cf_next_valid_token(&sp->cfp)) return PARSE_EOF;
  179. if (cf_token_is(&sp->cfp, ":")) {
  180. if (!cf_next_valid_token(&sp->cfp)) return PARSE_EOF;
  181. if (cf_token_is(&sp->cfp, ";"))
  182. return PARSE_UNEXPECTED_CONTINUE;
  183. if (cf_token_is(&sp->cfp, "}"))
  184. return PARSE_UNEXPECTED_BREAK;
  185. code = cf_token_is_type(&sp->cfp, CFTOKEN_NAME,
  186. "mapping name", ";");
  187. if (code != PARSE_SUCCESS)
  188. return code;
  189. cf_copy_token(&sp->cfp, &var->mapping);
  190. if (!cf_next_valid_token(&sp->cfp)) return PARSE_EOF;
  191. }
  192. /* -------------------------------------- */
  193. if (!cf_token_is(&sp->cfp, ";")) {
  194. if (!cf_go_to_valid_token(&sp->cfp, ";", "}"))
  195. return PARSE_EOF;
  196. return PARSE_CONTINUE;
  197. }
  198. return PARSE_SUCCESS;
  199. }
  200. static void sp_parse_struct(struct shader_parser *sp)
  201. {
  202. struct shader_struct ss;
  203. shader_struct_init(&ss);
  204. if (cf_next_name(&sp->cfp, &ss.name, "name", ";") != PARSE_SUCCESS)
  205. goto error;
  206. if (cf_next_token_should_be(&sp->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  207. goto error;
  208. /* get structure variables */
  209. while (true) {
  210. bool do_break = false;
  211. struct shader_var var;
  212. shader_var_init(&var);
  213. switch (sp_parse_struct_var(sp, &var)) {
  214. case PARSE_UNEXPECTED_CONTINUE:
  215. cf_adderror_syntax_error(&sp->cfp);
  216. /* Falls through. */
  217. case PARSE_CONTINUE:
  218. shader_var_free(&var);
  219. continue;
  220. case PARSE_UNEXPECTED_BREAK:
  221. cf_adderror_syntax_error(&sp->cfp);
  222. /* Falls through. */
  223. case PARSE_BREAK:
  224. shader_var_free(&var);
  225. do_break = true;
  226. break;
  227. case PARSE_EOF:
  228. shader_var_free(&var);
  229. goto error;
  230. }
  231. if (do_break)
  232. break;
  233. da_push_back(ss.vars, &var);
  234. }
  235. if (cf_next_token_should_be(&sp->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  236. goto error;
  237. da_push_back(sp->structs, &ss);
  238. return;
  239. error:
  240. shader_struct_free(&ss);
  241. }
  242. static inline int sp_check_for_keyword(struct shader_parser *sp,
  243. const char *keyword, bool *val)
  244. {
  245. bool new_val = cf_token_is(&sp->cfp, keyword);
  246. if (new_val) {
  247. if (!cf_next_valid_token(&sp->cfp))
  248. return PARSE_EOF;
  249. if (new_val && *val)
  250. cf_adderror(&sp->cfp, "'$1' keyword already specified",
  251. LEX_WARNING, keyword, NULL, NULL);
  252. *val = new_val;
  253. return PARSE_CONTINUE;
  254. }
  255. return PARSE_SUCCESS;
  256. }
  257. static inline int sp_parse_func_param(struct shader_parser *sp,
  258. struct shader_var *var)
  259. {
  260. int code;
  261. bool var_type_keyword = false;
  262. if (!cf_next_valid_token(&sp->cfp))
  263. return PARSE_EOF;
  264. code = sp_check_for_keyword(sp, "in", &var_type_keyword);
  265. if (code == PARSE_EOF)
  266. return PARSE_EOF;
  267. else if (var_type_keyword)
  268. var->var_type = SHADER_VAR_IN;
  269. if (!var_type_keyword) {
  270. code = sp_check_for_keyword(sp, "inout", &var_type_keyword);
  271. if (code == PARSE_EOF)
  272. return PARSE_EOF;
  273. else if (var_type_keyword)
  274. var->var_type = SHADER_VAR_INOUT;
  275. }
  276. if (!var_type_keyword) {
  277. code = sp_check_for_keyword(sp, "out", &var_type_keyword);
  278. if (code == PARSE_EOF)
  279. return PARSE_EOF;
  280. else if (var_type_keyword)
  281. var->var_type = SHADER_VAR_OUT;
  282. }
  283. if (!var_type_keyword) {
  284. code = sp_check_for_keyword(sp, "uniform", &var_type_keyword);
  285. if (code == PARSE_EOF)
  286. return PARSE_EOF;
  287. else if (var_type_keyword)
  288. var->var_type = SHADER_VAR_UNIFORM;
  289. }
  290. code = cf_get_name(&sp->cfp, &var->type, "type", ")");
  291. if (code != PARSE_SUCCESS)
  292. return code;
  293. code = cf_next_name(&sp->cfp, &var->name, "name", ")");
  294. if (code != PARSE_SUCCESS)
  295. return code;
  296. if (!cf_next_valid_token(&sp->cfp))
  297. return PARSE_EOF;
  298. if (cf_token_is(&sp->cfp, ":")) {
  299. code = cf_next_name(&sp->cfp, &var->mapping,
  300. "mapping specifier", ")");
  301. if (code != PARSE_SUCCESS)
  302. return code;
  303. if (!cf_next_valid_token(&sp->cfp))
  304. return PARSE_EOF;
  305. }
  306. return PARSE_SUCCESS;
  307. }
  308. static bool sp_parse_func_params(struct shader_parser *sp,
  309. struct shader_func *func)
  310. {
  311. struct cf_token peek;
  312. int code;
  313. cf_token_clear(&peek);
  314. if (!cf_peek_valid_token(&sp->cfp, &peek))
  315. return false;
  316. if (*peek.str.array == ')') {
  317. cf_next_token(&sp->cfp);
  318. goto exit;
  319. }
  320. do {
  321. struct shader_var var;
  322. shader_var_init(&var);
  323. if (!cf_token_is(&sp->cfp, "(") && !cf_token_is(&sp->cfp, ","))
  324. cf_adderror_syntax_error(&sp->cfp);
  325. code = sp_parse_func_param(sp, &var);
  326. if (code != PARSE_SUCCESS) {
  327. shader_var_free(&var);
  328. if (code == PARSE_CONTINUE)
  329. goto exit;
  330. else if (code == PARSE_EOF)
  331. return false;
  332. }
  333. da_push_back(func->params, &var);
  334. } while (!cf_token_is(&sp->cfp, ")"));
  335. exit:
  336. return true;
  337. }
  338. static void sp_parse_function(struct shader_parser *sp, char *type, char *name)
  339. {
  340. struct shader_func func;
  341. shader_func_init(&func, type, name);
  342. if (!sp_parse_func_params(sp, &func))
  343. goto error;
  344. if (!cf_next_valid_token(&sp->cfp))
  345. goto error;
  346. /* if function is mapped to something, for example COLOR */
  347. if (cf_token_is(&sp->cfp, ":")) {
  348. char *mapping = NULL;
  349. int errorcode = cf_next_name(&sp->cfp, &mapping, "mapping",
  350. "{");
  351. if (errorcode != PARSE_SUCCESS)
  352. goto error;
  353. func.mapping = mapping;
  354. if (!cf_next_valid_token(&sp->cfp))
  355. goto error;
  356. }
  357. if (!cf_token_is(&sp->cfp, "{")) {
  358. cf_adderror_expecting(&sp->cfp, "{");
  359. goto error;
  360. }
  361. func.start = sp->cfp.cur_token;
  362. if (!cf_pass_pair(&sp->cfp, '{', '}'))
  363. goto error;
  364. /* it is established that the current token is '}' if we reach this */
  365. cf_next_token(&sp->cfp);
  366. func.end = sp->cfp.cur_token;
  367. da_push_back(sp->funcs, &func);
  368. return;
  369. error:
  370. shader_func_free(&func);
  371. }
  372. /* parses "array[count]" */
  373. static bool sp_parse_param_array(struct shader_parser *sp,
  374. struct shader_var *param)
  375. {
  376. if (!cf_next_valid_token(&sp->cfp))
  377. return false;
  378. if (sp->cfp.cur_token->type != CFTOKEN_NUM ||
  379. !valid_int_str(sp->cfp.cur_token->str.array,
  380. sp->cfp.cur_token->str.len))
  381. return false;
  382. param->array_count =(int)strtol(sp->cfp.cur_token->str.array, NULL, 10);
  383. if (cf_next_token_should_be(&sp->cfp, "]", ";", NULL) == PARSE_EOF)
  384. return false;
  385. if (!cf_next_valid_token(&sp->cfp))
  386. return false;
  387. return true;
  388. }
  389. static inline int sp_parse_param_assign_intfloat(struct shader_parser *sp,
  390. struct shader_var *param, bool is_float)
  391. {
  392. int code;
  393. bool is_negative = false;
  394. if (!cf_next_valid_token(&sp->cfp))
  395. return PARSE_EOF;
  396. if (cf_token_is(&sp->cfp, "-")) {
  397. is_negative = true;
  398. if (!cf_next_token(&sp->cfp))
  399. return PARSE_EOF;
  400. }
  401. code = cf_token_is_type(&sp->cfp, CFTOKEN_NUM, "numeric value", ";");
  402. if (code != PARSE_SUCCESS)
  403. return code;
  404. if (is_float) {
  405. float f = (float)os_strtod(sp->cfp.cur_token->str.array);
  406. if (is_negative) f = -f;
  407. da_push_back_array(param->default_val, &f, sizeof(float));
  408. } else {
  409. long l = strtol(sp->cfp.cur_token->str.array, NULL, 10);
  410. if (is_negative) l = -l;
  411. da_push_back_array(param->default_val, &l, sizeof(long));
  412. }
  413. return PARSE_SUCCESS;
  414. }
  415. /*
  416. * parses assignment for float1, float2, float3, float4, and any combination
  417. * for float3x3, float4x4, etc
  418. */
  419. static inline int sp_parse_param_assign_float_array(struct shader_parser *sp,
  420. struct shader_var *param)
  421. {
  422. const char *float_type = param->type+5;
  423. int float_count = 0, code, i;
  424. /* -------------------------------------------- */
  425. if (float_type[0] < '1' || float_type[0] > '4')
  426. cf_adderror(&sp->cfp, "Invalid row count", LEX_ERROR,
  427. NULL, NULL, NULL);
  428. float_count = float_type[0]-'0';
  429. if (float_type[1] == 'x') {
  430. if (float_type[2] < '1' || float_type[2] > '4')
  431. cf_adderror(&sp->cfp, "Invalid column count",
  432. LEX_ERROR, NULL, NULL, NULL);
  433. float_count *= float_type[2]-'0';
  434. }
  435. /* -------------------------------------------- */
  436. code = cf_next_token_should_be(&sp->cfp, "{", ";", NULL);
  437. if (code != PARSE_SUCCESS) return code;
  438. for (i = 0; i < float_count; i++) {
  439. char *next = ((i+1) < float_count) ? "," : "}";
  440. code = sp_parse_param_assign_intfloat(sp, param, true);
  441. if (code != PARSE_SUCCESS) return code;
  442. code = cf_next_token_should_be(&sp->cfp, next, ";", NULL);
  443. if (code != PARSE_SUCCESS) return code;
  444. }
  445. return PARSE_SUCCESS;
  446. }
  447. static int sp_parse_param_assignment_val(struct shader_parser *sp,
  448. struct shader_var *param)
  449. {
  450. if (strcmp(param->type, "int") == 0)
  451. return sp_parse_param_assign_intfloat(sp, param, false);
  452. else if (strcmp(param->type, "float") == 0)
  453. return sp_parse_param_assign_intfloat(sp, param, true);
  454. else if (astrcmp_n(param->type, "float", 5) == 0)
  455. return sp_parse_param_assign_float_array(sp, param);
  456. cf_adderror(&sp->cfp, "Invalid type '$1' used for assignment",
  457. LEX_ERROR, param->type, NULL, NULL);
  458. return PARSE_CONTINUE;
  459. }
  460. static inline bool sp_parse_param_assign(struct shader_parser *sp,
  461. struct shader_var *param)
  462. {
  463. if (sp_parse_param_assignment_val(sp, param) != PARSE_SUCCESS)
  464. return false;
  465. if (!cf_next_valid_token(&sp->cfp))
  466. return false;
  467. return true;
  468. }
  469. static void sp_parse_param(struct shader_parser *sp,
  470. char *type, char *name, bool is_const, bool is_uniform)
  471. {
  472. struct shader_var param;
  473. shader_var_init_param(&param, type, name, is_uniform, is_const);
  474. if (cf_token_is(&sp->cfp, ";"))
  475. goto complete;
  476. if (cf_token_is(&sp->cfp, "[") && !sp_parse_param_array(sp, &param))
  477. goto error;
  478. if (cf_token_is(&sp->cfp, "=") && !sp_parse_param_assign(sp, &param))
  479. goto error;
  480. if (!cf_token_is(&sp->cfp, ";"))
  481. goto error;
  482. complete:
  483. da_push_back(sp->params, &param);
  484. return;
  485. error:
  486. shader_var_free(&param);
  487. }
  488. static bool sp_get_var_specifiers(struct shader_parser *sp,
  489. bool *is_const, bool *is_uniform)
  490. {
  491. while(true) {
  492. int code = sp_check_for_keyword(sp, "const", is_const);
  493. if (code == PARSE_EOF)
  494. return false;
  495. else if (code == PARSE_CONTINUE)
  496. continue;
  497. code = sp_check_for_keyword(sp, "uniform", is_uniform);
  498. if (code == PARSE_EOF)
  499. return false;
  500. else if (code == PARSE_CONTINUE)
  501. continue;
  502. break;
  503. }
  504. return true;
  505. }
  506. static inline void report_invalid_func_keyword(struct shader_parser *sp,
  507. const char *name, bool val)
  508. {
  509. if (val)
  510. cf_adderror(&sp->cfp, "'$1' keyword cannot be used with a "
  511. "function", LEX_ERROR,
  512. name, NULL, NULL);
  513. }
  514. static void sp_parse_other(struct shader_parser *sp)
  515. {
  516. bool is_const = false, is_uniform = false;
  517. char *type = NULL, *name = NULL;
  518. if (!sp_get_var_specifiers(sp, &is_const, &is_uniform))
  519. goto error;
  520. if (cf_get_name(&sp->cfp, &type, "type", ";") != PARSE_SUCCESS)
  521. goto error;
  522. if (cf_next_name(&sp->cfp, &name, "name", ";") != PARSE_SUCCESS)
  523. goto error;
  524. if (!cf_next_valid_token(&sp->cfp))
  525. goto error;
  526. if (cf_token_is(&sp->cfp, "(")) {
  527. report_invalid_func_keyword(sp, "const", is_const);
  528. report_invalid_func_keyword(sp, "uniform", is_uniform);
  529. sp_parse_function(sp, type, name);
  530. return;
  531. } else {
  532. sp_parse_param(sp, type, name, is_const, is_uniform);
  533. return;
  534. }
  535. error:
  536. bfree(type);
  537. bfree(name);
  538. }
  539. bool shader_parse(struct shader_parser *sp, const char *shader,
  540. const char *file)
  541. {
  542. if (!cf_parser_parse(&sp->cfp, shader, file))
  543. return false;
  544. while (sp->cfp.cur_token && sp->cfp.cur_token->type != CFTOKEN_NONE) {
  545. if (cf_token_is(&sp->cfp, ";") ||
  546. is_whitespace(*sp->cfp.cur_token->str.array)) {
  547. sp->cfp.cur_token++;
  548. } else if (cf_token_is(&sp->cfp, "struct")) {
  549. sp_parse_struct(sp);
  550. } else if (cf_token_is(&sp->cfp, "sampler_state")) {
  551. sp_parse_sampler_state(sp);
  552. } else if (cf_token_is(&sp->cfp, "{")) {
  553. cf_adderror(&sp->cfp, "Unexpected code segment",
  554. LEX_ERROR, NULL, NULL, NULL);
  555. cf_pass_pair(&sp->cfp, '{', '}');
  556. } else {
  557. /* parameters and functions */
  558. sp_parse_other(sp);
  559. }
  560. }
  561. return !error_data_has_errors(&sp->cfp.error_list);
  562. }