Highlight things
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

255 рядки
5.5KB

  1. #include "hl.h"
  2. #include <assert.h>
  3. vector_t token_table = {
  4. .data = NULL,
  5. .element_size = sizeof(token_t *),
  6. .element_count = 0UL
  7. };
  8. display_t * display_table = NULL;
  9. // -------------------------
  10. // ### Library Mangement ###
  11. // -------------------------
  12. hl_group_t * special_hl = NULL;
  13. hl_group_t * control_hl = NULL;
  14. hl_group_t * keyword_hl = NULL;
  15. hl_group_t * block_hl = NULL;
  16. hl_group_t * separator_hl = NULL;
  17. hl_group_t * operator_hl = NULL;
  18. hl_group_t * comment_hl = NULL;
  19. hl_group_t * string_literal_hl = NULL;
  20. // --------------------------------
  21. // ### Constructors/Destructors ###
  22. // --------------------------------
  23. void new_display_mode(display_t * mode) {
  24. HASH_ADD_STR(display_table,
  25. key,
  26. mode);
  27. }
  28. int free_token(token_t * token) {
  29. free(token->hl);
  30. regex_free(token->syntax);
  31. return 0;
  32. }
  33. int append_token(token_t * token) {
  34. vector_push(&token_table, &token);
  35. return 0;
  36. }
  37. token_t * new_symbol_token(const char * const c,
  38. hl_group_t * const g) {
  39. token_t * mt = (token_t*)malloc(sizeof(token_t));
  40. mt->hl = g;
  41. mt->t = KEYSYMBOL;
  42. mt->syntax = regex_compile(c);
  43. append_token(mt);
  44. return mt;
  45. }
  46. int new_symbol_tokens(const char * const * symbols,
  47. hl_group_t * const g) {
  48. int i = 0;
  49. while (*symbols) {
  50. if(new_symbol_token(*symbols, g)) {
  51. ++i;
  52. } else {
  53. assert(!(bool)"Kinda failed to new symbol token thing.");
  54. }
  55. ++symbols;
  56. }
  57. return i;
  58. }
  59. int new_char_tokens(const char * str,
  60. hl_group_t * const g) {
  61. int i = 0;
  62. char buffer[3];
  63. buffer[0] = '\\';
  64. buffer[2] = '\0';
  65. for(const char * s = str; *s != '\0'; s++) {
  66. buffer[1] = *s;
  67. if(new_symbol_token(is_magic(*s) ? buffer : buffer + 1, g)) {
  68. ++i;
  69. } else {
  70. assert(!(bool)"Kinda failed to new char token thing.");
  71. }
  72. }
  73. return i;
  74. }
  75. token_t * new_keyword_token(const char * const word,
  76. hl_group_t * const g) {
  77. size_t word_length = strlen(word);
  78. char * new_word = (char*)malloc(word_length + 4 + 1);
  79. memcpy(new_word, "\\<", 2);
  80. memcpy(new_word + 2, word, word_length);
  81. strcpy(new_word + 2 + word_length, "\\>");
  82. token_t * mt = (token_t*)malloc(sizeof(token_t));
  83. mt->hl = g;
  84. mt->t = KEYWORD;
  85. mt->syntax = regex_compile(new_word);
  86. append_token(mt);
  87. return mt;
  88. }
  89. int new_keyword_tokens(const char * const * words,
  90. hl_group_t * const g) {
  91. int i = 0;
  92. while (*words) {
  93. if(new_keyword_token(*words, g)) {
  94. ++i;
  95. }
  96. ++words;
  97. }
  98. return i;
  99. }
  100. token_t * new_region_token(const char * start,
  101. const char * end,
  102. hl_group_t * g) {
  103. char buffer[100];
  104. buffer[0] = '\0';
  105. strcat(buffer, start);
  106. strcat(buffer, "[\\d\\D]*");
  107. strcat(buffer, end);
  108. token_t * mt = (token_t*)malloc(sizeof(token_t));
  109. mt->hl = g;
  110. mt->t = KEYSYMBOL;
  111. mt->syntax = regex_compile(buffer);
  112. append_token(mt);
  113. return mt;
  114. }
  115. token_t * new_token(const char * const word,
  116. const token_type_t t,
  117. hl_group_t * const g) {
  118. switch (t) {
  119. case KEYSYMBOL: {
  120. return new_symbol_token(word, g);
  121. }
  122. case KEYWORD: {
  123. return new_keyword_token(word, g);
  124. }
  125. case MATCH: {
  126. token_t * mt = (token_t*)malloc(sizeof(token_t));
  127. mt->hl = g;
  128. mt->t = MATCH;
  129. mt->syntax = regex_compile(word);
  130. append_token(mt);
  131. } break;
  132. case REGION: {
  133. } break;
  134. }
  135. return NULL;
  136. }
  137. // --------------------
  138. // ### Highlighting ###
  139. // --------------------
  140. void render_string(const char * const string,
  141. const char * const mode) {
  142. display_t * display;
  143. HASH_FIND_STR(display_table,
  144. mode,
  145. display);
  146. typedef struct {
  147. const token_t * t;
  148. const match_t * m;
  149. int i;
  150. } result_t;
  151. result_t * const r = (result_t *)malloc(sizeof(result_t) * 1024); // XXX: dont
  152. int rrs = 0;
  153. for (size_t i = 0; i < token_table.element_count; i++) {
  154. token_t * t = *(token_t**)vector_get(&token_table,
  155. i);
  156. match_t * match = regex_match(t->syntax, string, true);
  157. if (is_sentinel(match)) {
  158. free(match);
  159. continue;
  160. }
  161. r[rrs++] = (result_t){
  162. .t = t,
  163. .m = match,
  164. .i = 0,
  165. };
  166. }
  167. for (const char * s = string; *s != '\00';) {
  168. const result_t sentinel = (result_t){NULL, &(match_t){ -1, -1}, -1};
  169. const result_t * max;
  170. max = &sentinel;
  171. for (int h = 0; h < rrs; h++) {
  172. result_t * const current_result = r + h;
  173. for (int j = 0; !is_sentinel(&(current_result->m[j])); j++) {
  174. if (current_result->m[j].position == (s - string)) {
  175. if (current_result->m[j].width > max->m->width) {
  176. current_result->i = j;
  177. max = current_result;
  178. }
  179. break;
  180. }
  181. }
  182. }
  183. if (max != &sentinel) {
  184. const match_t * mymatch = &(max->m[max->i]);
  185. const int padding = mymatch->position - (s - string);
  186. if (padding) {
  187. display->callback(s,
  188. padding,
  189. NULL);
  190. }
  191. display->callback(s + padding,
  192. mymatch->width,
  193. max->t->hl->attributes);
  194. s += padding + mymatch->width;
  195. } else {
  196. display->callback(s, 1, NULL);
  197. ++s;
  198. }
  199. }
  200. }
  201. int hl_init(void) {
  202. return 0;
  203. }
  204. int hl_deinit(void) {
  205. //for (size_t i = 0; i < token_table.element_count; i++) {
  206. // free_token(*(token_t**)vector_get(&token_table, i));
  207. //}
  208. return 0;
  209. }