shader-parser.c 17 KB

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