shader-parser.c 18 KB

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