shader-parser.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. case PARSE_CONTINUE:
  217. shader_var_free(&var);
  218. continue;
  219. case PARSE_UNEXPECTED_BREAK:
  220. cf_adderror_syntax_error(&sp->cfp);
  221. case PARSE_BREAK:
  222. shader_var_free(&var);
  223. do_break = true;
  224. break;
  225. case PARSE_EOF:
  226. shader_var_free(&var);
  227. goto error;
  228. }
  229. if (do_break)
  230. break;
  231. da_push_back(ss.vars, &var);
  232. }
  233. if (cf_next_token_should_be(&sp->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  234. goto error;
  235. da_push_back(sp->structs, &ss);
  236. return;
  237. error:
  238. shader_struct_free(&ss);
  239. }
  240. static inline int sp_check_for_keyword(struct shader_parser *sp,
  241. const char *keyword, bool *val)
  242. {
  243. bool new_val = cf_token_is(&sp->cfp, keyword);
  244. if (new_val) {
  245. if (!cf_next_valid_token(&sp->cfp))
  246. return PARSE_EOF;
  247. if (new_val && *val)
  248. cf_adderror(&sp->cfp, "'$1' keyword already specified",
  249. LEX_WARNING, keyword, NULL, NULL);
  250. *val = new_val;
  251. return PARSE_CONTINUE;
  252. }
  253. return PARSE_SUCCESS;
  254. }
  255. static inline int sp_parse_func_param(struct shader_parser *sp,
  256. struct shader_var *var)
  257. {
  258. int code;
  259. bool is_uniform = false;
  260. if (!cf_next_valid_token(&sp->cfp))
  261. return PARSE_EOF;
  262. code = sp_check_for_keyword(sp, "uniform", &is_uniform);
  263. if (code == PARSE_EOF)
  264. return PARSE_EOF;
  265. var->var_type = is_uniform ? SHADER_VAR_UNIFORM : SHADER_VAR_NONE;
  266. code = cf_get_name(&sp->cfp, &var->type, "type", ")");
  267. if (code != PARSE_SUCCESS)
  268. return code;
  269. code = cf_next_name(&sp->cfp, &var->name, "name", ")");
  270. if (code != PARSE_SUCCESS)
  271. return code;
  272. if (!cf_next_valid_token(&sp->cfp))
  273. return PARSE_EOF;
  274. if (cf_token_is(&sp->cfp, ":")) {
  275. code = cf_next_name(&sp->cfp, &var->mapping,
  276. "mapping specifier", ")");
  277. if (code != PARSE_SUCCESS)
  278. return code;
  279. if (!cf_next_valid_token(&sp->cfp))
  280. return PARSE_EOF;
  281. }
  282. return PARSE_SUCCESS;
  283. }
  284. static bool sp_parse_func_params(struct shader_parser *sp,
  285. struct shader_func *func)
  286. {
  287. struct cf_token peek;
  288. int code;
  289. cf_token_clear(&peek);
  290. if (!cf_peek_valid_token(&sp->cfp, &peek))
  291. return false;
  292. if (*peek.str.array == ')') {
  293. cf_next_token(&sp->cfp);
  294. goto exit;
  295. }
  296. do {
  297. struct shader_var var;
  298. shader_var_init(&var);
  299. if (!cf_token_is(&sp->cfp, "(") && !cf_token_is(&sp->cfp, ","))
  300. cf_adderror_syntax_error(&sp->cfp);
  301. code = sp_parse_func_param(sp, &var);
  302. if (code != PARSE_SUCCESS) {
  303. shader_var_free(&var);
  304. if (code == PARSE_CONTINUE)
  305. goto exit;
  306. else if (code == PARSE_EOF)
  307. return false;
  308. }
  309. da_push_back(func->params, &var);
  310. } while (!cf_token_is(&sp->cfp, ")"));
  311. exit:
  312. return true;
  313. }
  314. static void sp_parse_function(struct shader_parser *sp, char *type, char *name)
  315. {
  316. struct shader_func func;
  317. shader_func_init(&func, type, name);
  318. if (!sp_parse_func_params(sp, &func))
  319. goto error;
  320. if (!cf_next_valid_token(&sp->cfp))
  321. goto error;
  322. /* if function is mapped to something, for example COLOR */
  323. if (cf_token_is(&sp->cfp, ":")) {
  324. char *mapping = NULL;
  325. int errorcode = cf_next_name(&sp->cfp, &mapping, "mapping",
  326. "{");
  327. if (errorcode != PARSE_SUCCESS)
  328. goto error;
  329. func.mapping = mapping;
  330. if (!cf_next_valid_token(&sp->cfp))
  331. goto error;
  332. }
  333. if (!cf_token_is(&sp->cfp, "{")) {
  334. cf_adderror_expecting(&sp->cfp, "{");
  335. goto error;
  336. }
  337. func.start = sp->cfp.cur_token;
  338. if (!cf_pass_pair(&sp->cfp, '{', '}'))
  339. goto error;
  340. /* it is established that the current token is '}' if we reach this */
  341. cf_next_token(&sp->cfp);
  342. func.end = sp->cfp.cur_token;
  343. da_push_back(sp->funcs, &func);
  344. return;
  345. error:
  346. shader_func_free(&func);
  347. }
  348. /* parses "array[count]" */
  349. static bool sp_parse_param_array(struct shader_parser *sp,
  350. struct shader_var *param)
  351. {
  352. if (!cf_next_valid_token(&sp->cfp))
  353. return false;
  354. if (sp->cfp.cur_token->type != CFTOKEN_NUM ||
  355. !valid_int_str(sp->cfp.cur_token->str.array,
  356. sp->cfp.cur_token->str.len))
  357. return false;
  358. param->array_count =(int)strtol(sp->cfp.cur_token->str.array, NULL, 10);
  359. if (cf_next_token_should_be(&sp->cfp, "]", ";", NULL) == PARSE_EOF)
  360. return false;
  361. if (!cf_next_valid_token(&sp->cfp))
  362. return false;
  363. return true;
  364. }
  365. static inline int sp_parse_param_assign_intfloat(struct shader_parser *sp,
  366. struct shader_var *param, bool is_float)
  367. {
  368. int code;
  369. bool is_negative = false;
  370. if (!cf_next_valid_token(&sp->cfp))
  371. return PARSE_EOF;
  372. if (cf_token_is(&sp->cfp, "-")) {
  373. is_negative = true;
  374. if (!cf_next_token(&sp->cfp))
  375. return PARSE_EOF;
  376. }
  377. code = cf_token_is_type(&sp->cfp, CFTOKEN_NUM, "numeric value", ";");
  378. if (code != PARSE_SUCCESS)
  379. return code;
  380. if (is_float) {
  381. float f = (float)os_strtod(sp->cfp.cur_token->str.array);
  382. if (is_negative) f = -f;
  383. da_push_back_array(param->default_val, &f, sizeof(float));
  384. } else {
  385. long l = strtol(sp->cfp.cur_token->str.array, NULL, 10);
  386. if (is_negative) l = -l;
  387. da_push_back_array(param->default_val, &l, sizeof(long));
  388. }
  389. return PARSE_SUCCESS;
  390. }
  391. /*
  392. * parses assignment for float1, float2, float3, float4, and any combination
  393. * for float3x3, float4x4, etc
  394. */
  395. static inline int sp_parse_param_assign_float_array(struct shader_parser *sp,
  396. struct shader_var *param)
  397. {
  398. const char *float_type = param->type+5;
  399. int float_count = 0, code, i;
  400. /* -------------------------------------------- */
  401. if (float_type[0] < '1' || float_type[0] > '4')
  402. cf_adderror(&sp->cfp, "Invalid row count", LEX_ERROR,
  403. NULL, NULL, NULL);
  404. float_count = float_type[0]-'0';
  405. if (float_type[1] == 'x') {
  406. if (float_type[2] < '1' || float_type[2] > '4')
  407. cf_adderror(&sp->cfp, "Invalid column count",
  408. LEX_ERROR, NULL, NULL, NULL);
  409. float_count *= float_type[2]-'0';
  410. }
  411. /* -------------------------------------------- */
  412. code = cf_next_token_should_be(&sp->cfp, "{", ";", NULL);
  413. if (code != PARSE_SUCCESS) return code;
  414. for (i = 0; i < float_count; i++) {
  415. char *next = ((i+1) < float_count) ? "," : "}";
  416. code = sp_parse_param_assign_intfloat(sp, param, true);
  417. if (code != PARSE_SUCCESS) return code;
  418. code = cf_next_token_should_be(&sp->cfp, next, ";", NULL);
  419. if (code != PARSE_SUCCESS) return code;
  420. }
  421. return PARSE_SUCCESS;
  422. }
  423. static int sp_parse_param_assignment_val(struct shader_parser *sp,
  424. struct shader_var *param)
  425. {
  426. if (strcmp(param->type, "int") == 0)
  427. return sp_parse_param_assign_intfloat(sp, param, false);
  428. else if (strcmp(param->type, "float") == 0)
  429. return sp_parse_param_assign_intfloat(sp, param, true);
  430. else if (astrcmp_n(param->type, "float", 5) == 0)
  431. return sp_parse_param_assign_float_array(sp, param);
  432. cf_adderror(&sp->cfp, "Invalid type '$1' used for assignment",
  433. LEX_ERROR, param->type, NULL, NULL);
  434. return PARSE_CONTINUE;
  435. }
  436. static inline bool sp_parse_param_assign(struct shader_parser *sp,
  437. struct shader_var *param)
  438. {
  439. if (sp_parse_param_assignment_val(sp, param) != PARSE_SUCCESS)
  440. return false;
  441. if (!cf_next_valid_token(&sp->cfp))
  442. return false;
  443. return true;
  444. }
  445. static void sp_parse_param(struct shader_parser *sp,
  446. char *type, char *name, bool is_const, bool is_uniform)
  447. {
  448. struct shader_var param;
  449. shader_var_init_param(&param, type, name, is_uniform, is_const);
  450. if (cf_token_is(&sp->cfp, ";"))
  451. goto complete;
  452. if (cf_token_is(&sp->cfp, "[") && !sp_parse_param_array(sp, &param))
  453. goto error;
  454. if (cf_token_is(&sp->cfp, "=") && !sp_parse_param_assign(sp, &param))
  455. goto error;
  456. if (!cf_token_is(&sp->cfp, ";"))
  457. goto error;
  458. complete:
  459. da_push_back(sp->params, &param);
  460. return;
  461. error:
  462. shader_var_free(&param);
  463. }
  464. static bool sp_get_var_specifiers(struct shader_parser *sp,
  465. bool *is_const, bool *is_uniform)
  466. {
  467. while(true) {
  468. int code = sp_check_for_keyword(sp, "const", is_const);
  469. if (code == PARSE_EOF)
  470. return false;
  471. else if (code == PARSE_CONTINUE)
  472. continue;
  473. code = sp_check_for_keyword(sp, "uniform", is_uniform);
  474. if (code == PARSE_EOF)
  475. return false;
  476. else if (code == PARSE_CONTINUE)
  477. continue;
  478. break;
  479. }
  480. return true;
  481. }
  482. static inline void report_invalid_func_keyword(struct shader_parser *sp,
  483. const char *name, bool val)
  484. {
  485. if (val)
  486. cf_adderror(&sp->cfp, "'$1' keyword cannot be used with a "
  487. "function", LEX_ERROR,
  488. name, NULL, NULL);
  489. }
  490. static void sp_parse_other(struct shader_parser *sp)
  491. {
  492. bool is_const = false, is_uniform = false;
  493. char *type = NULL, *name = NULL;
  494. if (!sp_get_var_specifiers(sp, &is_const, &is_uniform))
  495. goto error;
  496. if (cf_get_name(&sp->cfp, &type, "type", ";") != PARSE_SUCCESS)
  497. goto error;
  498. if (cf_next_name(&sp->cfp, &name, "name", ";") != PARSE_SUCCESS)
  499. goto error;
  500. if (!cf_next_valid_token(&sp->cfp))
  501. goto error;
  502. if (cf_token_is(&sp->cfp, "(")) {
  503. report_invalid_func_keyword(sp, "const", is_const);
  504. report_invalid_func_keyword(sp, "uniform", is_uniform);
  505. sp_parse_function(sp, type, name);
  506. return;
  507. } else {
  508. sp_parse_param(sp, type, name, is_const, is_uniform);
  509. return;
  510. }
  511. error:
  512. bfree(type);
  513. bfree(name);
  514. }
  515. bool shader_parse(struct shader_parser *sp, const char *shader,
  516. const char *file)
  517. {
  518. if (!cf_parser_parse(&sp->cfp, shader, file))
  519. return false;
  520. while (sp->cfp.cur_token && sp->cfp.cur_token->type != CFTOKEN_NONE) {
  521. if (cf_token_is(&sp->cfp, ";") ||
  522. is_whitespace(*sp->cfp.cur_token->str.array)) {
  523. sp->cfp.cur_token++;
  524. } else if (cf_token_is(&sp->cfp, "struct")) {
  525. sp_parse_struct(sp);
  526. } else if (cf_token_is(&sp->cfp, "sampler_state")) {
  527. sp_parse_sampler_state(sp);
  528. } else if (cf_token_is(&sp->cfp, "{")) {
  529. cf_adderror(&sp->cfp, "Unexpected code segment",
  530. LEX_ERROR, NULL, NULL, NULL);
  531. cf_pass_pair(&sp->cfp, '{', '}');
  532. } else {
  533. /* parameters and functions */
  534. sp_parse_other(sp);
  535. }
  536. }
  537. return !error_data_has_errors(&sp->cfp.error_list);
  538. }