shader-parser.c 17 KB

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