shader-parser.c 18 KB

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