pmh_styleparser.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /* PEG Markdown Highlight
  2. * Copyright 2011-2016 Ali Rantakari -- http://hasseg.org
  3. * Licensed under the GPL2+ and MIT licenses (see LICENSE for more info).
  4. *
  5. * styleparser.c
  6. *
  7. * Parser for custom syntax highlighting stylesheets.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13. #include <ctype.h>
  14. #include "pmh_styleparser.h"
  15. #include "pmh_parser.h"
  16. #if pmh_DEBUG_OUTPUT
  17. #define pmhsp_PRINTF(x, ...) fprintf(stderr, x, ##__VA_ARGS__)
  18. #else
  19. #define pmhsp_PRINTF(x, ...)
  20. #endif
  21. // vasprintf is not in the C standard nor in POSIX so we provide our own
  22. static int our_vasprintf(char **strptr, const char *fmt, va_list argptr)
  23. {
  24. int ret;
  25. va_list argptr2;
  26. *strptr = NULL;
  27. va_copy(argptr2, argptr);
  28. ret = vsnprintf(NULL, 0, fmt, argptr2);
  29. if (ret <= 0)
  30. return ret;
  31. *strptr = (char *)malloc(ret+1);
  32. if (*strptr == NULL)
  33. return -1;
  34. va_copy(argptr2, argptr);
  35. ret = vsnprintf(*strptr, ret+1, fmt, argptr2);
  36. return ret;
  37. }
  38. // Parsing context data
  39. typedef struct
  40. {
  41. char *input;
  42. void (*error_callback)(char*,int,void*);
  43. void *error_callback_context;
  44. int styles_pos;
  45. pmh_style_collection *styles;
  46. } style_parser_data;
  47. typedef struct raw_attribute
  48. {
  49. char *name;
  50. char *value;
  51. int line_number;
  52. struct raw_attribute *next;
  53. } raw_attribute;
  54. static raw_attribute *new_raw_attribute(char *name, char *value,
  55. int line_number)
  56. {
  57. raw_attribute *v = (raw_attribute *)malloc(sizeof(raw_attribute));
  58. v->name = name;
  59. v->value = value;
  60. v->line_number = line_number;
  61. v->next = NULL;
  62. return v;
  63. }
  64. static void free_raw_attributes(raw_attribute *list)
  65. {
  66. raw_attribute *cur = list;
  67. while (cur != NULL)
  68. {
  69. raw_attribute *pattr = NULL;
  70. if (cur->name != NULL) free(cur->name);
  71. if (cur->value != NULL) free(cur->value);
  72. pattr = cur;
  73. cur = cur->next;
  74. free(pattr);
  75. }
  76. }
  77. static void report_error(style_parser_data *p_data,
  78. int line_number, char *str, ...)
  79. {
  80. va_list argptr;
  81. if (p_data->error_callback == NULL)
  82. return;
  83. va_start(argptr, str);
  84. char *errmsg;
  85. our_vasprintf(&errmsg, str, argptr);
  86. va_end(argptr);
  87. p_data->error_callback(errmsg, line_number,
  88. p_data->error_callback_context);
  89. free(errmsg);
  90. }
  91. static char *trim_str(char *str)
  92. {
  93. while (isspace(*str))
  94. str++;
  95. if (*str == '\0')
  96. return str;
  97. char *end = str + strlen(str) - 1;
  98. while (end > str && isspace(*end))
  99. end--;
  100. *(end+1) = '\0';
  101. return str;
  102. }
  103. static char *trim_str_dup(char *str)
  104. {
  105. size_t start = 0;
  106. while (isspace(*(str + start)))
  107. start++;
  108. size_t end = strlen(str) - 1;
  109. while (start < end && isspace(*(str + end)))
  110. end--;
  111. size_t len = end - start + 1;
  112. char *ret = (char *)malloc(sizeof(char)*len + 1);
  113. *ret = '\0';
  114. strncat(ret, (str + start), len);
  115. return ret;
  116. }
  117. static char *strcpy_lower(char *str)
  118. {
  119. char *low = strdup(str);
  120. int i;
  121. int len = strlen(str);
  122. for (i = 0; i < len; i++)
  123. *(low+i) = tolower(*(low+i));
  124. return low;
  125. }
  126. static char *standardize_str(char *str)
  127. {
  128. return strcpy_lower(trim_str(str));
  129. }
  130. static pmh_attr_argb_color *new_argb_color(int r, int g, int b, int a)
  131. {
  132. pmh_attr_argb_color *c = (pmh_attr_argb_color *)
  133. malloc(sizeof(pmh_attr_argb_color));
  134. c->red = r; c->green = g; c->blue = b; c->alpha = a;
  135. return c;
  136. }
  137. static pmh_attr_argb_color *new_argb_from_hex(long long hex, bool has_alpha)
  138. {
  139. // 0xaarrggbb
  140. int a = has_alpha ? ((hex >> 24) & 0xFF) : 255;
  141. int r = ((hex >> 16) & 0xFF);
  142. int g = ((hex >> 8) & 0xFF);
  143. int b = (hex & 0xFF);
  144. return new_argb_color(r,g,b,a);
  145. }
  146. static pmh_attr_argb_color *new_argb_from_hex_str(style_parser_data *p_data,
  147. int attr_line_number,
  148. char *str)
  149. {
  150. // "aarrggbb"
  151. int len = strlen(str);
  152. if (len != 6 && len != 8) {
  153. report_error(p_data, attr_line_number,
  154. "Value '%s' is not a valid color value: it should be a "
  155. "hexadecimal number, 6 or 8 characters long.",
  156. str);
  157. return NULL;
  158. }
  159. char *endptr = NULL;
  160. long long num = strtoll(str, &endptr, 16);
  161. if (*endptr != '\0') {
  162. report_error(p_data, attr_line_number,
  163. "Value '%s' is not a valid color value: the character "
  164. "'%c' is invalid. The color value should be a hexadecimal "
  165. "number, 6 or 8 characters long.",
  166. str, *endptr);
  167. return NULL;
  168. }
  169. return new_argb_from_hex(num, (len == 8));
  170. }
  171. static pmh_attr_value *new_attr_value()
  172. {
  173. return (pmh_attr_value *)malloc(sizeof(pmh_attr_value));
  174. }
  175. static pmh_attr_font_styles *new_font_styles()
  176. {
  177. pmh_attr_font_styles *ret = (pmh_attr_font_styles *)
  178. malloc(sizeof(pmh_attr_font_styles));
  179. ret->italic = false;
  180. ret->bold = false;
  181. ret->underlined = false;
  182. return ret;
  183. }
  184. static pmh_attr_font_size *new_font_size()
  185. {
  186. pmh_attr_font_size *ret = (pmh_attr_font_size *)
  187. malloc(sizeof(pmh_attr_font_size));
  188. ret->is_relative = false;
  189. ret->size_pt = 0;
  190. return ret;
  191. }
  192. static pmh_style_attribute *new_attr(char *name, pmh_attr_type type)
  193. {
  194. pmh_style_attribute *attr = (pmh_style_attribute *)malloc(sizeof(pmh_style_attribute));
  195. attr->name = strdup(name);
  196. attr->type = type;
  197. attr->next = NULL;
  198. return attr;
  199. }
  200. static void free_style_attributes(pmh_style_attribute *list)
  201. {
  202. pmh_style_attribute *cur = list;
  203. while (cur != NULL)
  204. {
  205. if (cur->name != NULL)
  206. free(cur->name);
  207. if (cur->value != NULL)
  208. {
  209. if (cur->type == pmh_attr_type_foreground_color
  210. || cur->type == pmh_attr_type_background_color
  211. || cur->type == pmh_attr_type_caret_color
  212. || cur->type == pmh_attr_type_strike_color)
  213. free(cur->value->argb_color);
  214. else if (cur->type == pmh_attr_type_font_family)
  215. free(cur->value->font_family);
  216. else if (cur->type == pmh_attr_type_font_style)
  217. free(cur->value->font_styles);
  218. else if (cur->type == pmh_attr_type_font_size_pt)
  219. free(cur->value->font_size);
  220. else if (cur->type == pmh_attr_type_other)
  221. free(cur->value->string);
  222. free(cur->value);
  223. }
  224. pmh_style_attribute *pattr = cur;
  225. cur = cur->next;
  226. free(pattr);
  227. }
  228. }
  229. #define IF_ATTR_NAME(x) if (strcmp(x, name) == 0)
  230. pmh_attr_type pmh_attr_type_from_name(char *name)
  231. {
  232. IF_ATTR_NAME("color") return pmh_attr_type_foreground_color;
  233. else IF_ATTR_NAME("foreground") return pmh_attr_type_foreground_color;
  234. else IF_ATTR_NAME("foreground-color") return pmh_attr_type_foreground_color;
  235. else IF_ATTR_NAME("background") return pmh_attr_type_background_color;
  236. else IF_ATTR_NAME("background-color") return pmh_attr_type_background_color;
  237. else IF_ATTR_NAME("caret") return pmh_attr_type_caret_color;
  238. else IF_ATTR_NAME("caret-color") return pmh_attr_type_caret_color;
  239. else IF_ATTR_NAME("strike") return pmh_attr_type_strike_color;
  240. else IF_ATTR_NAME("strike-color") return pmh_attr_type_strike_color;
  241. else IF_ATTR_NAME("font-size") return pmh_attr_type_font_size_pt;
  242. else IF_ATTR_NAME("font-family") return pmh_attr_type_font_family;
  243. else IF_ATTR_NAME("font-style") return pmh_attr_type_font_style;
  244. return pmh_attr_type_other;
  245. }
  246. char *pmh_attr_name_from_type(pmh_attr_type type)
  247. {
  248. switch (type)
  249. {
  250. case pmh_attr_type_foreground_color:
  251. return "foreground-color"; break;
  252. case pmh_attr_type_background_color:
  253. return "background-color"; break;
  254. case pmh_attr_type_caret_color:
  255. return "caret-color"; break;
  256. case pmh_attr_type_strike_color:
  257. return "strike-color"; break;
  258. case pmh_attr_type_font_size_pt:
  259. return "font-size"; break;
  260. case pmh_attr_type_font_family:
  261. return "font-family"; break;
  262. case pmh_attr_type_font_style:
  263. return "font-style"; break;
  264. default:
  265. return "unknown";
  266. }
  267. }
  268. typedef struct multi_value
  269. {
  270. char *value;
  271. size_t length;
  272. int line_number;
  273. struct multi_value *next;
  274. } multi_value;
  275. static multi_value *split_multi_value(char *input, char separator)
  276. {
  277. multi_value *head = NULL;
  278. multi_value *tail = NULL;
  279. char *c = input;
  280. while (*c != '\0')
  281. {
  282. size_t i;
  283. for (i = 0; (*(c+i) != '\0' && *(c+i) != separator); i++);
  284. multi_value *mv = (multi_value *)malloc(sizeof(multi_value));
  285. mv->value = (char *)malloc(sizeof(char)*i + 1);
  286. mv->length = i;
  287. mv->line_number = 0;
  288. mv->next = NULL;
  289. *mv->value = '\0';
  290. strncat(mv->value, c, i);
  291. if (head == NULL) {
  292. head = mv;
  293. tail = mv;
  294. } else {
  295. tail->next = mv;
  296. tail = mv;
  297. }
  298. if (*(c+i) == separator)
  299. i++;
  300. c += i;
  301. }
  302. return head;
  303. }
  304. static void free_multi_value(multi_value *val)
  305. {
  306. multi_value *cur = val;
  307. while (cur != NULL)
  308. {
  309. multi_value *pvalue = cur;
  310. multi_value *next_cur = cur->next;
  311. free(pvalue->value);
  312. free(pvalue);
  313. cur = next_cur;
  314. }
  315. }
  316. #define EQUALS(a,b) (strcmp(a, b) == 0)
  317. static pmh_style_attribute *interpret_attributes(style_parser_data *p_data,
  318. pmh_element_type lang_element_type,
  319. raw_attribute *raw_attributes)
  320. {
  321. pmh_style_attribute *attrs = NULL;
  322. raw_attribute *cur = raw_attributes;
  323. while (cur != NULL)
  324. {
  325. pmh_attr_type atype = pmh_attr_type_from_name(cur->name);
  326. pmh_style_attribute *attr = new_attr(cur->name, atype);
  327. attr->lang_element_type = lang_element_type;
  328. attr->value = new_attr_value();
  329. if (atype == pmh_attr_type_foreground_color
  330. || atype == pmh_attr_type_background_color
  331. || atype == pmh_attr_type_caret_color
  332. || atype == pmh_attr_type_strike_color)
  333. {
  334. char *hexstr = trim_str(cur->value);
  335. // new_argb_from_hex_str() reports conversion errors
  336. attr->value->argb_color =
  337. new_argb_from_hex_str(p_data, cur->line_number, hexstr);
  338. if (attr->value->argb_color == NULL) {
  339. free_style_attributes(attr);
  340. attr = NULL;
  341. }
  342. }
  343. else if (atype == pmh_attr_type_font_size_pt)
  344. {
  345. pmh_attr_font_size *fs = new_font_size();
  346. attr->value->font_size = fs;
  347. char *trimmed_value = trim_str_dup(cur->value);
  348. fs->is_relative = (*trimmed_value == '+' || *trimmed_value == '-');
  349. char *endptr = NULL;
  350. fs->size_pt = (int)strtol(cur->value, &endptr, 10);
  351. if (endptr == cur->value) {
  352. report_error(p_data, cur->line_number,
  353. "Value '%s' is invalid for attribute '%s'",
  354. cur->value, cur->name);
  355. free_style_attributes(attr);
  356. attr = NULL;
  357. }
  358. free(trimmed_value);
  359. }
  360. else if (atype == pmh_attr_type_font_family)
  361. {
  362. attr->value->font_family = trim_str_dup(cur->value);
  363. }
  364. else if (atype == pmh_attr_type_font_style)
  365. {
  366. attr->value->font_styles = new_font_styles();
  367. multi_value *values = split_multi_value(cur->value, ',');
  368. multi_value *value_cur = values;
  369. while (value_cur != NULL)
  370. {
  371. char *standardized_value = standardize_str(value_cur->value);
  372. if (EQUALS(standardized_value, "italic"))
  373. attr->value->font_styles->italic = true;
  374. else if (EQUALS(standardized_value, "bold"))
  375. attr->value->font_styles->bold = true;
  376. else if (EQUALS(standardized_value, "underlined"))
  377. attr->value->font_styles->underlined = true;
  378. else {
  379. report_error(p_data, cur->line_number,
  380. "Value '%s' is invalid for attribute '%s'",
  381. standardized_value, cur->name);
  382. }
  383. free(standardized_value);
  384. value_cur = value_cur->next;
  385. }
  386. free_multi_value(values);
  387. }
  388. else if (atype == pmh_attr_type_other)
  389. {
  390. attr->value->string = trim_str_dup(cur->value);
  391. }
  392. if (attr != NULL) {
  393. // add to linked list
  394. attr->next = attrs;
  395. attrs = attr;
  396. }
  397. cur = cur->next;
  398. }
  399. return attrs;
  400. }
  401. static void interpret_and_add_style(style_parser_data *p_data,
  402. char *style_rule_name,
  403. int style_rule_line_number,
  404. raw_attribute *raw_attributes)
  405. {
  406. bool isEditorType = false;
  407. bool isCurrentLineType = false;
  408. bool isSelectionType = false;
  409. pmh_element_type type = pmh_element_type_from_name(style_rule_name);
  410. if (type == pmh_NO_TYPE)
  411. {
  412. if (EQUALS(style_rule_name, "editor"))
  413. isEditorType = true, type = pmh_NO_TYPE;
  414. else if (EQUALS(style_rule_name, "editor-current-line"))
  415. isCurrentLineType = true, type = pmh_NO_TYPE;
  416. else if (EQUALS(style_rule_name, "editor-selection"))
  417. isSelectionType = true, type = pmh_NO_TYPE;
  418. else {
  419. report_error(p_data, style_rule_line_number,
  420. "Style rule '%s' is not a language element type name or "
  421. "one of the following: 'editor', 'editor-current-line', "
  422. "'editor-selection'",
  423. style_rule_name);
  424. return;
  425. }
  426. }
  427. pmh_style_attribute *attrs = interpret_attributes(p_data, type, raw_attributes);
  428. if (isEditorType)
  429. p_data->styles->editor_styles = attrs;
  430. else if (isCurrentLineType)
  431. p_data->styles->editor_current_line_styles = attrs;
  432. else if (isSelectionType)
  433. p_data->styles->editor_selection_styles = attrs;
  434. else
  435. p_data->styles->element_styles[(p_data->styles_pos)++] = attrs;
  436. }
  437. static bool char_is_whitespace(char c)
  438. {
  439. return (c == ' ' || c == '\t');
  440. }
  441. static bool char_begins_linecomment(char c)
  442. {
  443. return (c == '#');
  444. }
  445. static bool line_is_comment(multi_value *line)
  446. {
  447. char *c;
  448. for (c = line->value; *c != '\0'; c++)
  449. {
  450. if (!char_is_whitespace(*c))
  451. return char_begins_linecomment(*c);
  452. }
  453. return false;
  454. }
  455. static bool line_is_empty(multi_value *line)
  456. {
  457. char *c;
  458. for (c = line->value; *c != '\0'; c++)
  459. {
  460. if (!char_is_whitespace(*c))
  461. return false;
  462. }
  463. return true;
  464. }
  465. typedef struct block
  466. {
  467. multi_value *lines;
  468. struct block *next;
  469. } block;
  470. static block *new_block()
  471. {
  472. block *ret = (block *)malloc(sizeof(block));
  473. ret->next = NULL;
  474. ret->lines = NULL;
  475. return ret;
  476. }
  477. static void free_blocks(block *val)
  478. {
  479. block *cur = val;
  480. while (cur != NULL)
  481. {
  482. block *pblock = cur;
  483. block *next = pblock->next;
  484. free_multi_value(pblock->lines);
  485. free(pblock);
  486. cur = next;
  487. }
  488. }
  489. static block *get_blocks(char *input)
  490. {
  491. block *head = NULL;
  492. block *tail = NULL;
  493. block *current_block = NULL;
  494. multi_value *discarded_lines = NULL;
  495. int line_number_counter = 1;
  496. multi_value *lines = split_multi_value(input, '\n');
  497. multi_value *previous_line = NULL;
  498. multi_value *line_cur = lines;
  499. while (line_cur != NULL)
  500. {
  501. bool discard_line = false;
  502. line_cur->line_number = line_number_counter++;
  503. if (line_is_empty(line_cur))
  504. {
  505. discard_line = true;
  506. if (current_block != NULL)
  507. {
  508. // terminate block
  509. if (tail != current_block)
  510. tail->next = current_block;
  511. tail = current_block;
  512. current_block = NULL;
  513. previous_line->next = NULL;
  514. }
  515. }
  516. else if (line_is_comment(line_cur))
  517. {
  518. // Do not discard (i.e. free()) comment lines within blocks:
  519. if (current_block == NULL)
  520. discard_line = true;
  521. }
  522. else
  523. {
  524. if (current_block == NULL)
  525. {
  526. // start block
  527. current_block = new_block();
  528. current_block->lines = line_cur;
  529. if (previous_line != NULL)
  530. previous_line->next = NULL;
  531. }
  532. if (head == NULL) {
  533. head = current_block;
  534. tail = current_block;
  535. }
  536. }
  537. multi_value *next_cur = line_cur->next;
  538. previous_line = (discard_line) ? NULL : line_cur;
  539. if (discard_line) {
  540. line_cur->next = discarded_lines;
  541. discarded_lines = line_cur;
  542. }
  543. line_cur = next_cur;
  544. }
  545. if (current_block != NULL && tail != current_block)
  546. tail->next = current_block;
  547. free_multi_value(discarded_lines);
  548. return head;
  549. }
  550. #define ASSIGNMENT_OP_UITEXT "':' or '='"
  551. #define IS_ASSIGNMENT_OP(c) ((c) == ':' || (c) == '=')
  552. #define IS_STYLE_RULE_NAME_CHAR(c) \
  553. ( (c) != '\0' && !isspace(c) \
  554. && !char_begins_linecomment(c) && !IS_ASSIGNMENT_OP(c) )
  555. #define IS_ATTRIBUTE_NAME_CHAR(c) \
  556. ( (c) != '\0' && !char_begins_linecomment(c) && !IS_ASSIGNMENT_OP(c) )
  557. #define IS_ATTRIBUTE_VALUE_CHAR(c) \
  558. ( (c) != '\0' && !char_begins_linecomment(c) )
  559. static char *get_style_rule_name(multi_value *line)
  560. {
  561. char *str = line->value;
  562. // Scan past leading whitespace:
  563. size_t start_index;
  564. for (start_index = 0;
  565. (*(str+start_index) != '\0' && isspace(*(str+start_index)));
  566. start_index++);
  567. // Scan until style rule name characters end:
  568. size_t value_end_index;
  569. for (value_end_index = start_index;
  570. IS_STYLE_RULE_NAME_CHAR(*(str + value_end_index));
  571. value_end_index++);
  572. // Copy style rule name:
  573. size_t value_len = value_end_index - start_index;
  574. char *value = (char *)malloc(sizeof(char)*value_len + 1);
  575. *value = '\0';
  576. strncat(value, (str + start_index), value_len);
  577. return value;
  578. }
  579. static bool parse_attribute_line(style_parser_data *p_data, multi_value *line,
  580. char **out_attr_name, char **out_attr_value)
  581. {
  582. char *str = line->value;
  583. // Scan past leading whitespace:
  584. size_t name_start_index;
  585. for (name_start_index = 0;
  586. ( *(str+name_start_index) != '\0' &&
  587. isspace(*(str+name_start_index)) );
  588. name_start_index++);
  589. // Scan until attribute name characters end:
  590. size_t name_end_index;
  591. for (name_end_index = name_start_index;
  592. IS_ATTRIBUTE_NAME_CHAR(*(str + name_end_index));
  593. name_end_index++);
  594. // Scan backwards to trim trailing whitespace off:
  595. while (name_start_index < name_end_index
  596. && isspace(*(str + name_end_index - 1)))
  597. name_end_index--;
  598. // Scan until just after the first assignment operator:
  599. size_t assignment_end_index;
  600. for (assignment_end_index = name_end_index;
  601. ( *(str + assignment_end_index) != '\0' &&
  602. !IS_ASSIGNMENT_OP(*(str + assignment_end_index)) );
  603. assignment_end_index++);
  604. // Scan over the found assignment operator, or report error:
  605. if (IS_ASSIGNMENT_OP(*(str + assignment_end_index)))
  606. assignment_end_index++;
  607. else
  608. {
  609. report_error(p_data, line->line_number,
  610. "Invalid attribute definition: str does not contain "
  611. "an assignment operator (%s): '%s'",
  612. ASSIGNMENT_OP_UITEXT, str);
  613. return false;
  614. }
  615. size_t value_start_index = assignment_end_index;
  616. // Scan until attribute value characters end:
  617. size_t value_end_index;
  618. for (value_end_index = value_start_index;
  619. IS_ATTRIBUTE_VALUE_CHAR(*(str + value_end_index));
  620. value_end_index++);
  621. // Copy attribute name:
  622. size_t name_len = name_end_index - name_start_index;
  623. char *attr_name = (char *)malloc(sizeof(char)*name_len + 1);
  624. *attr_name = '\0';
  625. strncat(attr_name, (str + name_start_index), name_len);
  626. *out_attr_name = attr_name;
  627. // Copy attribute value:
  628. size_t attr_value_len = value_end_index - assignment_end_index;
  629. char *attr_value_str = (char *)malloc(sizeof(char)*attr_value_len + 1);
  630. *attr_value_str = '\0';
  631. strncat(attr_value_str, (str + assignment_end_index), attr_value_len);
  632. *out_attr_value = attr_value_str;
  633. return true;
  634. }
  635. #define HAS_UTF8_BOM(x) ( ((*x & 0xFF) == 0xEF)\
  636. && ((*(x+1) & 0xFF) == 0xBB)\
  637. && ((*(x+2) & 0xFF) == 0xBF) )
  638. // - Removes UTF-8 BOM
  639. // - Standardizes line endings to \n
  640. static char *strcpy_preformat_style(char *str)
  641. {
  642. char *new_str = (char *)malloc(sizeof(char) * strlen(str) + 1);
  643. char *c = str;
  644. int i = 0;
  645. if (HAS_UTF8_BOM(c))
  646. c += 3;
  647. while (*c != '\0')
  648. {
  649. if (*c == '\r' && *(c+1) == '\n')
  650. {
  651. *(new_str+i) = '\n';
  652. i++;
  653. c += 2;
  654. }
  655. else if (*c == '\r')
  656. {
  657. *(new_str+i) = '\n';
  658. i++;
  659. c++;
  660. }
  661. else
  662. {
  663. *(new_str+i) = *c;
  664. i++;
  665. c++;
  666. }
  667. }
  668. *(new_str+i) = '\0';
  669. return new_str;
  670. }
  671. static void _sty_parse(style_parser_data *p_data)
  672. {
  673. // We don't have to worry about leaking the original p_data->input;
  674. // the user of the library is responsible for that:
  675. p_data->input = strcpy_preformat_style(p_data->input);
  676. block *blocks = get_blocks(p_data->input);
  677. block *block_cur = blocks;
  678. while (block_cur != NULL)
  679. {
  680. raw_attribute *attributes_head = NULL;
  681. raw_attribute *attributes_tail = NULL;
  682. pmhsp_PRINTF("Block:\n");
  683. multi_value *header_line = block_cur->lines;
  684. if (header_line == NULL) {
  685. block_cur = block_cur->next;
  686. continue;
  687. }
  688. pmhsp_PRINTF(" Head line (len %ld): '%s'\n",
  689. header_line->length, header_line->value);
  690. char *style_rule_name = get_style_rule_name(header_line);
  691. pmhsp_PRINTF(" Style rule name: '%s'\n", style_rule_name);
  692. multi_value *attr_line_cur = header_line->next;
  693. if (attr_line_cur == NULL)
  694. report_error(p_data, header_line->line_number,
  695. "No style attributes defined for style rule '%s'",
  696. style_rule_name);
  697. while (attr_line_cur != NULL)
  698. {
  699. if (line_is_comment(attr_line_cur))
  700. {
  701. attr_line_cur = attr_line_cur->next;
  702. continue;
  703. }
  704. pmhsp_PRINTF(" Attr line (len %ld): '%s'\n",
  705. attr_line_cur->length, attr_line_cur->value);
  706. char *attr_name_str;
  707. char *attr_value_str;
  708. bool success = parse_attribute_line(p_data,
  709. attr_line_cur,
  710. &attr_name_str,
  711. &attr_value_str);
  712. if (success)
  713. {
  714. pmhsp_PRINTF(" Attr: '%s' Value: '%s'\n",
  715. attr_name_str, attr_value_str);
  716. raw_attribute *attribute =
  717. new_raw_attribute(attr_name_str, attr_value_str,
  718. attr_line_cur->line_number);
  719. if (attributes_head == NULL) {
  720. attributes_head = attribute;
  721. attributes_tail = attribute;
  722. } else {
  723. attributes_tail->next = attribute;
  724. attributes_tail = attribute;
  725. }
  726. }
  727. attr_line_cur = attr_line_cur->next;
  728. }
  729. if (attributes_head != NULL)
  730. {
  731. interpret_and_add_style(p_data, style_rule_name,
  732. header_line->line_number, attributes_head);
  733. free_raw_attributes(attributes_head);
  734. }
  735. free(style_rule_name);
  736. block_cur = block_cur->next;
  737. }
  738. free_blocks(blocks);
  739. free(p_data->input);
  740. }
  741. static pmh_style_collection *new_style_collection()
  742. {
  743. pmh_style_collection *sc = (pmh_style_collection *)
  744. malloc(sizeof(pmh_style_collection));
  745. sc->element_styles = (pmh_style_attribute**)
  746. malloc(sizeof(pmh_style_attribute*)
  747. * pmh_NUM_LANG_TYPES);
  748. int i;
  749. for (i = 0; i < pmh_NUM_LANG_TYPES; i++)
  750. sc->element_styles[i] = NULL;
  751. sc->editor_styles = NULL;
  752. sc->editor_current_line_styles = NULL;
  753. sc->editor_selection_styles = NULL;
  754. return sc;
  755. }
  756. void pmh_free_style_collection(pmh_style_collection *coll)
  757. {
  758. free_style_attributes(coll->editor_styles);
  759. free_style_attributes(coll->editor_current_line_styles);
  760. free_style_attributes(coll->editor_selection_styles);
  761. int i;
  762. for (i = 0; i < pmh_NUM_LANG_TYPES; i++)
  763. free_style_attributes(coll->element_styles[i]);
  764. free(coll->element_styles);
  765. free(coll);
  766. }
  767. static style_parser_data *new_style_parser_data(char *input)
  768. {
  769. style_parser_data *p_data = (style_parser_data*)
  770. malloc(sizeof(style_parser_data));
  771. p_data->input = input;
  772. p_data->styles_pos = 0;
  773. p_data->styles = new_style_collection();
  774. return p_data;
  775. }
  776. pmh_style_collection *pmh_parse_styles(char *input,
  777. void(*error_callback)(char*,int,void*),
  778. void *error_callback_context)
  779. {
  780. style_parser_data *p_data = new_style_parser_data(input);
  781. p_data->error_callback = error_callback;
  782. p_data->error_callback_context = error_callback_context;
  783. _sty_parse(p_data);
  784. pmh_style_collection *ret = p_data->styles;
  785. free(p_data);
  786. return ret;
  787. }