1
0

shader-parser.c 17 KB

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