Highlight things
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

248 líneas
5.6KB

  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. //char * new_word = strdup(word);
  78. //size_t word_length = strlen(word);
  79. //char * new_word = (char*)malloc(word_length + 4 + 1);
  80. //memcpy(new_word, "\\<", 2);
  81. //memcpy(new_word + 2, word, word_length);
  82. //strcpy(new_word + 2 + word_length, "\\>");
  83. token_t * mt = (token_t*)malloc(sizeof(token_t));
  84. mt->hl = g;
  85. mt->t = KEYWORD;
  86. //mt->syntax = regex_compile(new_word);
  87. mt->syntax = regex_compile(word);
  88. append_token(mt);
  89. return mt;
  90. }
  91. int new_keyword_tokens(const char * const * words,
  92. hl_group_t * const g) {
  93. int i = 0;
  94. while (*words) {
  95. if(new_keyword_token(*words, g)) {
  96. ++i;
  97. }
  98. ++words;
  99. }
  100. return i;
  101. }
  102. token_t * new_region_token(const char * start,
  103. const char * end,
  104. hl_group_t * g) {
  105. char buffer[100];
  106. buffer[0] = '\0';
  107. strcat(buffer, start);
  108. strcat(buffer, "[\\d\\D]*");
  109. strcat(buffer, end);
  110. token_t * mt = (token_t*)malloc(sizeof(token_t));
  111. mt->hl = g;
  112. mt->t = KEYSYMBOL;
  113. mt->syntax = regex_compile(buffer);
  114. append_token(mt);
  115. return mt;
  116. }
  117. token_t * new_token(const char * const word,
  118. const token_type_t t,
  119. hl_group_t * const g) {
  120. switch (t) {
  121. case KEYSYMBOL: {
  122. return new_symbol_token(word, g);
  123. }
  124. case KEYWORD: {
  125. return new_keyword_token(word, g);
  126. }
  127. case MATCH: {
  128. token_t * mt = (token_t*)malloc(sizeof(token_t));
  129. mt->hl = g;
  130. mt->t = MATCH;
  131. mt->syntax = regex_compile(word);
  132. append_token(mt);
  133. } break;
  134. case REGION: {
  135. } break;
  136. }
  137. return NULL;
  138. }
  139. // --------------------
  140. // ### Highlighting ###
  141. // --------------------
  142. int token_fits(const token_t * const token,
  143. const char * const to,
  144. const int string_offset,
  145. const bool is_start_of_line,
  146. int * match_offset) {
  147. UNUSED(match_offset);
  148. //return regex_match(pattern, to, string_offset, match_offset);
  149. return regex_match(token->syntax, to, is_start_of_line, string_offset);
  150. }
  151. void render_string(const char * const string,
  152. const char * const mode) {
  153. for (const char * s = string; *s != '\00';) {
  154. int f = 0;
  155. size_t token_index = 0;
  156. int offset = 0;
  157. for (; token_index < token_table.element_count; token_index++) {
  158. token_t * t = *(token_t**)vector_get(&token_table,
  159. token_index);
  160. const bool is_start_of_line = (s == string) || (*s == '\n');
  161. f = token_fits(t, string, (int)(s - string), is_start_of_line, &offset);
  162. if (f) {
  163. break;
  164. }
  165. }
  166. //
  167. display_t * display;
  168. HASH_FIND_STR(display_table,
  169. mode,
  170. display);
  171. //
  172. if (f) {
  173. for (int i = 0; i < offset; i++) {
  174. token_t * t = *(token_t**)vector_get(&token_table,
  175. token_index);
  176. display->callback(s + i,
  177. 0,
  178. t->hl->attributes);
  179. }
  180. token_t * t = *(token_t**)vector_get(&token_table,
  181. token_index);
  182. display->callback(s + offset,
  183. f,
  184. t->hl->attributes);
  185. s += f + offset;
  186. } else {
  187. display->callback(s,
  188. 0,
  189. NULL);
  190. ++s;
  191. }
  192. }
  193. }
  194. int hl_init(void) {
  195. return 0;
  196. }
  197. int hl_deinit(void) {
  198. for (size_t i = 0; i < token_table.element_count; i++) {
  199. free_token(*(token_t**)vector_get(&token_table, i));
  200. }
  201. return 0;
  202. }