ysfx_parse.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // Copyright 2021 Jean Pierre Cimalando
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #include "ysfx_parse.hpp"
  18. #include "ysfx_utils.hpp"
  19. #include <cstdlib>
  20. #include <cstring>
  21. #include <algorithm>
  22. #include <vector>
  23. #include <cctype>
  24. #include <unordered_set>
  25. ysfx_section_t* new_or_append(ysfx_section_u &section, uint32_t line_no)
  26. {
  27. if (!section) {
  28. section.reset(new ysfx_section_t);
  29. section->line_offset = line_no + 1;
  30. } else {
  31. // We insert blank lines to ensure that the line numbers provided will be correct
  32. std::string::difference_type num_lines = std::count(section->text.begin(), section->text.end(), '\n');
  33. section->text.append(line_no - section->line_offset - num_lines + 1, '\n');
  34. }
  35. return section.get();
  36. }
  37. bool ysfx_parse_toplevel(ysfx::text_reader &reader, ysfx_toplevel_t &toplevel, ysfx_parse_error *error, bool onlyHeader=false)
  38. {
  39. toplevel = ysfx_toplevel_t{};
  40. ysfx_section_t *current = new ysfx_section_t;
  41. toplevel.header.reset(current);
  42. std::string line;
  43. uint32_t lineno = 0;
  44. line.reserve(256);
  45. while (reader.read_next_line(line)) {
  46. const char *linep = line.c_str();
  47. if (linep[0] == '@') {
  48. if (onlyHeader) return true;
  49. // a new section starts
  50. ysfx::string_list tokens = ysfx::split_strings_noempty(linep, &ysfx::ascii_isspace);
  51. if (tokens[0] == "@init")
  52. current = new_or_append(toplevel.init, lineno);
  53. else if (tokens[0] == "@slider")
  54. current = new_or_append(toplevel.slider, lineno);
  55. else if (tokens[0] == "@block")
  56. current = new_or_append(toplevel.block, lineno);
  57. else if (tokens[0] == "@sample")
  58. current = new_or_append(toplevel.sample, lineno);
  59. else if (tokens[0] == "@serialize")
  60. current = new_or_append(toplevel.serialize, lineno);
  61. else if (tokens[0] == "@gfx") {
  62. current = new_or_append(toplevel.gfx, lineno);
  63. long gfx_w = 0;
  64. long gfx_h = 0;
  65. if (tokens.size() > 1)
  66. gfx_w = (long)ysfx::dot_atof(tokens[1].c_str());
  67. if (tokens.size() > 2)
  68. gfx_h = (long)ysfx::dot_atof(tokens[2].c_str());
  69. toplevel.gfx_w = (gfx_w > 0) ? (uint32_t)gfx_w : 0;
  70. toplevel.gfx_h = (gfx_h > 0) ? (uint32_t)gfx_h : 0;
  71. }
  72. else {
  73. if (error) {
  74. error->line = lineno;
  75. error->message = std::string("Invalid section: ") + line;
  76. }
  77. return false;
  78. }
  79. }
  80. else {
  81. current->text.append(line);
  82. current->text.push_back('\n');
  83. }
  84. ++lineno;
  85. }
  86. return true;
  87. }
  88. ysfx_config_item ysfx_parse_config_line(const char *rest)
  89. {
  90. ysfx_config_item item;
  91. // Whitespace before the identifier is ignored
  92. auto cur = rest;
  93. while (*cur && ysfx::ascii_isspace(*cur)) ++cur;
  94. // Identifier
  95. auto pos = cur;
  96. while (*pos && !ysfx::ascii_isspace(*pos)) ++pos;
  97. item.identifier = std::string(cur, pos - cur);
  98. // Skip whitespace
  99. while (*pos && ysfx::ascii_isspace(*pos)) ++pos;
  100. if (!*pos) return item;
  101. cur = pos;
  102. auto closing_item = ((*pos == '"') || (*pos == '\'')) ? *pos : ' ';
  103. pos += 1;
  104. if (!*pos) return item;
  105. while (*pos && *pos != closing_item) ++pos;
  106. if (closing_item == '"') {
  107. item.name = std::string(cur + 1, pos - cur - 1);
  108. } else {
  109. item.name = std::string(cur, pos - cur);
  110. }
  111. cur = pos + 1;
  112. while (*cur && ysfx::ascii_isspace(*cur)) ++cur; // Skip whitespace
  113. // Read double
  114. ysfx_real value = (ysfx_real)ysfx::dot_strtod(cur, (char **)&pos);
  115. item.default_value = value;
  116. if (cur == pos) return item; // TODO: emit error
  117. auto key = std::string(cur, pos - cur);
  118. cur = pos + 1;
  119. while (*cur) {
  120. while (*cur && ysfx::ascii_isspace(*cur)) ++cur; // Skip whitespace
  121. // Read double
  122. ysfx_real value = (ysfx_real)ysfx::dot_strtod(cur, (char **)&pos);
  123. if (cur == pos) return item; // TODO: emit error
  124. auto key = std::string(cur, pos - cur);
  125. cur = pos;
  126. while (*cur && ysfx::ascii_isspace(*cur)) ++cur; // Skip whitespace
  127. if (*cur == '=') {
  128. cur++;
  129. while (*cur && ysfx::ascii_isspace(*cur)) ++cur; // Skip whitespace
  130. if (*cur) {
  131. closing_item = ((*cur == '"') || (*cur == '\'')) ? *cur : ' ';
  132. pos = cur + 1;
  133. while (*pos && (*pos != closing_item)) ++pos;
  134. switch(closing_item)
  135. {
  136. case '"':
  137. key = std::string(cur + 1, pos - cur - 1);
  138. break;
  139. case ' ':
  140. key = std::string(cur, pos - cur);
  141. break;
  142. default:
  143. key = std::string(cur, pos - cur + (*pos ? 1 : 0)); // blegh
  144. }
  145. cur = pos + (*pos ? 1 : 0); // blegh x2
  146. }
  147. }
  148. item.var_names.push_back(key);
  149. item.var_values.push_back(value);
  150. }
  151. return item;
  152. }
  153. bool ysfx_config_item_is_valid(const ysfx_config_item& item) {
  154. if (item.identifier.size() < 2) return false;
  155. if (item.name.size() < 2) return false;
  156. if (item.var_names.size() < 2) return false;
  157. if (item.var_values.size() < 2) return false;
  158. if (item.var_names.size() != item.var_names.size()) return false;
  159. for (auto v : item.var_names) {
  160. if (v.empty()) return false;
  161. }
  162. return true;
  163. }
  164. bool ysfx_parse_header(ysfx_section_t *section, ysfx_header_t &header, ysfx_parse_error *error)
  165. {
  166. header = ysfx_header_t{};
  167. ysfx::string_text_reader reader(section->text.c_str());
  168. std::string line;
  169. uint32_t lineno = section->line_offset;
  170. line.reserve(256);
  171. ///
  172. auto unprefix = [](const char *text, const char **restp, const char *prefix) -> bool {
  173. size_t len = strlen(prefix);
  174. if (strncmp(text, prefix, len))
  175. return false;
  176. if (restp)
  177. *restp = text + len;
  178. return true;
  179. };
  180. //--------------------------------------------------------------------------
  181. // pass 1: regular metadata
  182. std::unordered_set<std::string> config_identifiers{};
  183. while (reader.read_next_line(line)) {
  184. const char *linep = line.c_str();
  185. const char *rest = nullptr;
  186. ysfx_slider_t slider;
  187. ysfx_parsed_filename_t filename;
  188. ///
  189. if (unprefix(linep, &rest, "desc:")) {
  190. if (header.desc.empty())
  191. header.desc = ysfx::trim(rest, &ysfx::ascii_isspace);
  192. }
  193. else if (unprefix(linep, &rest, "author:")) {
  194. if (header.author.empty())
  195. header.author = ysfx::trim(rest, &ysfx::ascii_isspace);
  196. }
  197. else if (unprefix(linep, &rest, "tags:")) {
  198. if (header.tags.empty()) {
  199. for (const std::string &tag : ysfx::split_strings_noempty(rest, &ysfx::ascii_isspace))
  200. header.tags.push_back(tag);
  201. }
  202. }
  203. else if (unprefix(linep, &rest, "in_pin:")) {
  204. header.explicit_pins = true;
  205. header.in_pins.push_back(ysfx::trim(rest, &ysfx::ascii_isspace));
  206. }
  207. else if (unprefix(linep, &rest, "out_pin:")) {
  208. header.explicit_pins = true;
  209. header.out_pins.push_back(ysfx::trim(rest, &ysfx::ascii_isspace));
  210. }
  211. else if (unprefix(linep, &rest, "config:")) {
  212. auto item = ysfx_parse_config_line(rest);
  213. if (ysfx_config_item_is_valid(item)) {
  214. std::string identifier{item.identifier};
  215. std::transform(identifier.begin(), identifier.end(), identifier.begin(), ysfx::ascii_tolower);
  216. if (config_identifiers.find(identifier) != config_identifiers.end()) {
  217. if (error) {
  218. error->line = lineno;
  219. error->message = std::string("Duplicate config variable: ") + item.identifier;
  220. }
  221. return false;
  222. }
  223. config_identifiers.insert(identifier);
  224. header.config_items.push_back(item);
  225. }
  226. }
  227. else if (unprefix(linep, &rest, "options:")) {
  228. auto option_line = ysfx::trim_spaces_around_equals(rest);
  229. for (const std::string &opt : ysfx::split_strings_noempty(option_line.c_str(), &ysfx::ascii_isspace)) {
  230. size_t pos = opt.find('=');
  231. std::string name = (pos == opt.npos) ? opt : opt.substr(0, pos);
  232. std::string value = (pos == opt.npos) ? std::string{} : opt.substr(pos + 1);
  233. if (name == "gmem")
  234. header.options.gmem = value;
  235. else if (name == "maxmem") {
  236. int32_t maxmem = (int32_t)ysfx::dot_atof(value.c_str());
  237. header.options.maxmem = (maxmem < 0) ? 0 : (uint32_t)maxmem;
  238. }
  239. else if (name == "prealloc") {
  240. int32_t prealloc = (value.compare("*") == 0) ? -1 : (int32_t) ysfx::dot_atof(value.c_str());
  241. header.options.prealloc = prealloc;
  242. }
  243. else if (name == "want_all_kb") {
  244. header.options.want_all_kb = true;
  245. } else if (name == "no_meter") {
  246. header.options.no_meter = true;
  247. } else if (name == "gfx_hz") {
  248. int32_t gfx_hz = (int32_t) ysfx::dot_atof(value.c_str());
  249. if (gfx_hz > 0 && gfx_hz < 2000) {
  250. header.options.gfx_hz = static_cast<uint32_t>(gfx_hz);
  251. }
  252. }
  253. }
  254. }
  255. else if (unprefix(linep, &rest, "import") && ysfx::ascii_isspace(rest[0]))
  256. header.imports.push_back(ysfx::trim(rest + 1, &ysfx::ascii_isspace));
  257. else if (ysfx_parse_slider(linep, slider)) {
  258. if (slider.id >= ysfx_max_sliders)
  259. continue;
  260. slider.exists = true;
  261. header.sliders[slider.id] = slider;
  262. }
  263. else if (ysfx_parse_filename(linep, filename)) {
  264. if (filename.index != header.filenames.size())
  265. continue;
  266. header.filenames.push_back(std::move(filename.filename));
  267. }
  268. ++lineno;
  269. }
  270. //--------------------------------------------------------------------------
  271. // pass 2: comments
  272. reader = ysfx::string_text_reader{section->text.c_str()};
  273. while (reader.read_next_line(line)) {
  274. const char *linep = line.c_str();
  275. const char *rest = nullptr;
  276. // some files contain metadata in the form of comments
  277. // this is not part of spec, but we'll take this info regardless
  278. if (unprefix(linep, &rest, "//author:")) {
  279. if (header.author.empty())
  280. header.author = ysfx::trim(rest, &ysfx::ascii_isspace);
  281. }
  282. else if (unprefix(linep, &rest, "//tags:")) {
  283. if (header.tags.empty()) {
  284. for (const std::string &tag : ysfx::split_strings_noempty(rest, &ysfx::ascii_isspace))
  285. header.tags.push_back(tag);
  286. }
  287. }
  288. }
  289. //--------------------------------------------------------------------------
  290. if (header.in_pins.size() == 1 && !ysfx::ascii_casecmp(header.in_pins.front().c_str(), "none"))
  291. header.in_pins.clear();
  292. if (header.out_pins.size() == 1 && !ysfx::ascii_casecmp(header.out_pins.front().c_str(), "none"))
  293. header.out_pins.clear();
  294. if (header.in_pins.size() > ysfx_max_channels)
  295. header.in_pins.resize(ysfx_max_channels);
  296. if (header.out_pins.size() > ysfx_max_channels)
  297. header.out_pins.resize(ysfx_max_channels);
  298. return true;
  299. }
  300. bool ysfx_parse_slider(const char *line, ysfx_slider_t &slider)
  301. {
  302. // NOTE this parser is intentionally very permissive,
  303. // in order to match the reference behavior
  304. slider = ysfx_slider_t{};
  305. #define PARSE_FAIL do { \
  306. /*fprintf(stderr, "parse error (line %d): `%s`\n", __LINE__, line);*/ \
  307. return false; \
  308. } while (0)
  309. const char *cur = line;
  310. if (strnicmp("slider", cur, 6)) {
  311. PARSE_FAIL;
  312. }
  313. cur += 6;
  314. // parse ID (1-index)
  315. unsigned long id = strtoul(cur, (char **)&cur, 10);
  316. if (id < 1 || id > ysfx_max_sliders)
  317. PARSE_FAIL;
  318. slider.id = (uint32_t)--id;
  319. // semicolon
  320. if (*cur++ != ':')
  321. PARSE_FAIL;
  322. // Whitespace before the identifier is ignored
  323. while (*cur && ysfx::ascii_isspace(*cur)) ++cur;
  324. // search if there is an '=' sign prior to any '<' or ','
  325. // if there is, it's a custom variable
  326. {
  327. const char *pos = cur;
  328. for (char c; pos && (c = *pos) && c != '='; )
  329. pos = (c == '<' || c == ',') ? nullptr : (pos + 1);
  330. if (pos && *pos) {
  331. slider.var.assign(cur, pos);
  332. cur = pos + 1;
  333. }
  334. else
  335. slider.var = "slider" + std::to_string(id + 1);
  336. }
  337. // a regular slider
  338. if (*cur != '/') {
  339. slider.def = (ysfx_real)ysfx::dot_strtod(cur, (char **)&cur);
  340. while (*cur && *cur != ',' && *cur != '<') ++cur;
  341. if (!*cur) PARSE_FAIL;
  342. // no range specification
  343. if (*cur == ',')
  344. ++cur;
  345. // range specification
  346. else if (*cur == '<') {
  347. ++cur;
  348. // min
  349. slider.min = (ysfx_real)ysfx::dot_strtod(cur, (char **)&cur);
  350. while (*cur && *cur != ',' && *cur != '>') ++cur;
  351. if (!*cur) PARSE_FAIL;
  352. // max
  353. if (*cur == ',') {
  354. ++cur;
  355. slider.max = (ysfx_real)ysfx::dot_strtod(cur, (char **)&cur);
  356. while (*cur && *cur != ',' && *cur != '>') ++cur;
  357. if (!*cur) PARSE_FAIL;
  358. }
  359. // inc
  360. if (*cur == ',') {
  361. ++cur;
  362. slider.inc = (ysfx_real)ysfx::dot_strtod(cur, (char **)&cur);
  363. while (*cur && *cur != '{' && *cur != '>' && *cur != ':') ++cur;
  364. if (!*cur) PARSE_FAIL;
  365. // enumeration values
  366. if (*cur == '{') {
  367. const char *pos = ++cur;
  368. while (*cur && *cur != '}' && *cur != '>') ++cur;
  369. if (!*cur) PARSE_FAIL;
  370. slider.is_enum = true;
  371. slider.enum_names = ysfx::split_strings_noempty(
  372. std::string(pos, cur).c_str(),
  373. [](char c) -> bool { return c == ','; });
  374. for (std::string &name : slider.enum_names)
  375. name = ysfx::trim(name.c_str(), &ysfx::ascii_isspace);
  376. }
  377. // shaping
  378. if (*cur == ':') {
  379. ++cur;
  380. if (strnicmp("log", cur, 3) == 0) {
  381. slider.shape = 1;
  382. cur += 3;
  383. } else if (strnicmp("sqr", cur, 3) == 0) {
  384. slider.shape = 2;
  385. slider.shape_modifier = 2;
  386. cur += 3;
  387. }
  388. // Do we have a modifier on this shape?
  389. if (*cur == '=') {
  390. ++cur;
  391. slider.shape_modifier = (ysfx_real)ysfx::dot_strtod(cur, (char **)&cur);
  392. // Do some checking on the modifiers for validity
  393. if (std::abs(slider.shape_modifier) < 0.0001) {
  394. if (slider.shape == 2) {
  395. // A power law slider with power zero is invalid.
  396. slider.shape = 0;
  397. };
  398. } else {
  399. if (std::abs(slider.shape_modifier - slider.min) < 0.0000001) {
  400. slider.shape = 0;
  401. }
  402. }
  403. if (std::abs(slider.max - slider.min) < 1e-12) {
  404. slider.shape = 0;
  405. }
  406. while (*cur && *cur != '>') ++cur;
  407. if (!*cur) PARSE_FAIL;
  408. }
  409. }
  410. }
  411. while (*cur && *cur != '>') ++cur;
  412. if (!*cur) PARSE_FAIL;
  413. ++cur;
  414. }
  415. else
  416. PARSE_FAIL;
  417. // NOTE: skip ',' and whitespace. not sure why, it's how it is
  418. while (*cur && (*cur == ',' || ysfx::ascii_isspace(*cur))) ++cur;
  419. if (!*cur) PARSE_FAIL;
  420. }
  421. // a path slider
  422. else
  423. {
  424. const char *pos = cur;
  425. while (*cur && *cur != ':') ++cur;
  426. if (!*cur) PARSE_FAIL;
  427. slider.path.assign(pos, cur);
  428. ++cur;
  429. slider.def = (ysfx_real)ysfx::dot_strtod(cur, (char **)&cur);
  430. slider.inc = 1;
  431. slider.is_enum = true;
  432. while (*cur && *cur != ':') ++cur;
  433. if (!*cur) PARSE_FAIL;
  434. ++cur;
  435. }
  436. // description and visibility
  437. while (ysfx::ascii_isspace(*cur))
  438. ++cur;
  439. slider.initially_visible = true;
  440. if (*cur == '-') {
  441. ++cur;
  442. slider.initially_visible = false;
  443. }
  444. slider.desc = ysfx::trim(cur, &ysfx::ascii_isspace);
  445. if (slider.desc.empty())
  446. PARSE_FAIL;
  447. //
  448. #undef PARSE_FAIL
  449. return true;
  450. }
  451. bool ysfx_parse_filename(const char *line, ysfx_parsed_filename_t &filename)
  452. {
  453. filename = ysfx_parsed_filename_t{};
  454. const char *cur = line;
  455. for (const char *p = "filename:"; *p; ++p) {
  456. if (*cur++ != *p)
  457. return false;
  458. }
  459. int64_t index = (int64_t)ysfx::dot_strtod(cur, (char **)&cur);
  460. if (index < 0 || index > ~(uint32_t)0)
  461. return false;
  462. while (*cur && *cur != ',') ++cur;
  463. if (!*cur) return false;;
  464. ++cur;
  465. filename.index = (uint32_t)index;
  466. filename.filename.assign(cur);
  467. return true;
  468. }