1
0

shader-parser.c 17 KB

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