More to come...
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

277 satır
12KB

  1. /*
  2. Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. Xhartae is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
  4. And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
  5. It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
  6. */
  7. #ifndef CHAPTER_4_SOURCE
  8. #define CHAPTER_4_SOURCE
  9. #include "chapter_4.h"
  10. /*
  11. Of course, we could just write something like 'preview_unhighlighted_text_file' function (name is obviously a joke), but this would stylize (apply colour and effect character
  12. attributes) to our entire text file. When we're writing programs, syntax highlighting makes a lot of difference to readability, the same way the code formatting does, and initial
  13. program design structure. If you use a lot of external variables everywhere, the entire programs starts to be messy or difficult to maintain, write and debug (or both). However,
  14. if you use a lot of variables, and you pass each of them separately into functions, or if you have one huge monolithic structure (this time literal 'struct), you aren't doing
  15. much better, except the compiler will have easier time to optimize your code, even tho that kind of code becomes pain to write. So, having few external functions, that do one
  16. thing well, and having few external variables, that won't be edited outside of that file is the best in my opinion.
  17. Your function calls won't be long, if you don't want to make those external ("global") variables visible to some other file, just move them to C source file instead of C header
  18. file, and redeclare them as 'static', making them internal variables. Keep in mind, you have to use brain more in that case, and think about what you're modifying, where and why.
  19. Well, if you want to write programs, and not to think about them, just close this and read Jin Ping Mei instead or something. There's no cheatsheet for making good programs, you
  20. choose your constraints, your program design structure, and you start working on it.
  21. @C
  22. void preview_unhighlighted_text_file (char * text_file, int x, int y) {
  23. char * text_data;
  24. text_data = file_record (text_file);
  25. for (curses_active = 1; curses_active != 0; ) {
  26. curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
  27. curses_render_string (text_data, COLOUR_WHITE, EFFECT_NORMAL, x, x);
  28. curses_synchronize ();
  29. }
  30. text_data = deallocate (text_data);
  31. }
  32. @
  33. So, lets write very basic C programming language syntax highlighting, explain how can we easily do it in little more than 100 lines of (scarily verbose and nicely aligned) code
  34. and why we don't need regular expressions for it. You can use these 'syntax_*' functions to tokenize some source code, highlight the syntax of it or something else that I didn't
  35. even think about if you're creative. Of course, we can use it to highlight syntax of some other programming language, not only C, and we'll use it later to highlight assembly,
  36. Ada, C++, and maybe few more programming languages.
  37. */
  38. static int syntax_count = 0; // Number of previously defined syntax rules.
  39. static int syntax_active = FALSE; // Syntax "library" or subprogram was initialized if this value is TRUE.
  40. static int * syntax_enrange = NULL; // Syntax rule can start with any character from 'syntax_begin' if this value is TRUE.
  41. static int * syntax_derange = NULL; // Syntax rule can start with any character from 'syntax_end' if this value is TRUE.
  42. static char * * syntax_begin = NULL; // Strings containing valid character (sub)sequence for begining the scan.
  43. static char * * syntax_end = NULL; // Strings containing valid character (sub)sequence for ending the scan.
  44. static char * syntax_escape = NULL; // Escape sequence for the rule, useful for line-breaks in C macros and line-based languages.
  45. static int * syntax_colour = NULL; // Colour for our token, these two could be completely independent, but I like to keep them here.
  46. static int * syntax_effect = NULL; // Effect for our token.
  47. static void syntax_delete (void) {
  48. int offset;
  49. if (syntax_active == FALSE) {
  50. return;
  51. }
  52. // We could reverse-loop through this without a local variable 'offset' using this approach, but I consider this bad for readability.
  53. // --syntax_count;
  54. // do {
  55. // syntax_begin [syntax_count] = deallocate (syntax_begin [syntax_count]);
  56. // syntax_end [syntax_count] = deallocate (syntax_end [syntax_count]);
  57. // } while (--syntax_count != -1);
  58. for (offset = 0; offset < syntax_count; ++offset) {
  59. syntax_begin [offset] = deallocate (syntax_begin [offset]);
  60. syntax_end [offset] = deallocate (syntax_end [offset]);
  61. }
  62. syntax_enrange = deallocate (syntax_enrange);
  63. syntax_derange = deallocate (syntax_derange);
  64. syntax_begin = deallocate (syntax_begin);
  65. syntax_end = deallocate (syntax_end);
  66. syntax_escape = deallocate (syntax_escape);
  67. syntax_colour = deallocate (syntax_colour);
  68. syntax_effect = deallocate (syntax_effect);
  69. syntax_active = FALSE;
  70. syntax_count = 0;
  71. }
  72. static int syntax_define (int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
  73. if (syntax_active == FALSE) {
  74. syntax_active = TRUE;
  75. atexit (syntax_delete);
  76. }
  77. fatal_failure (begin == NULL, "syntax_define: Begin string is null pointer.");
  78. fatal_failure (end == NULL, "syntax_define: End string is null pointer.");
  79. ++syntax_count;
  80. syntax_enrange = reallocate (syntax_enrange, syntax_count * (int) sizeof (* syntax_enrange));
  81. syntax_derange = reallocate (syntax_derange, syntax_count * (int) sizeof (* syntax_derange));
  82. syntax_begin = reallocate (syntax_begin, syntax_count * (int) sizeof (* syntax_begin));
  83. syntax_end = reallocate (syntax_end, syntax_count * (int) sizeof (* syntax_end));
  84. syntax_escape = reallocate (syntax_escape, syntax_count * (int) sizeof (* syntax_escape));
  85. syntax_colour = reallocate (syntax_colour, syntax_count * (int) sizeof (* syntax_colour));
  86. syntax_effect = reallocate (syntax_effect, syntax_count * (int) sizeof (* syntax_effect));
  87. syntax_enrange [syntax_count - 1] = enrange;
  88. syntax_derange [syntax_count - 1] = derange;
  89. syntax_escape [syntax_count - 1] = escape;
  90. syntax_colour [syntax_count - 1] = colour;
  91. syntax_effect [syntax_count - 1] = effect;
  92. syntax_begin [syntax_count - 1] = allocate ((string_length (begin) + 1) * (int) sizeof (* * syntax_begin));
  93. syntax_end [syntax_count - 1] = allocate ((string_length (end) + 1) * (int) sizeof (* * syntax_end));
  94. string_copy (syntax_begin [syntax_count - 1], begin);
  95. string_copy (syntax_end [syntax_count - 1], end);
  96. return (syntax_count - 1);
  97. }
  98. static int syntax_select (char * string, int * length) {
  99. int offset, subset, select;
  100. fatal_failure (syntax_active == FALSE, "syntax_select: Syntax is not active.");
  101. fatal_failure (string == NULL, "syntax_select: String is null.");
  102. fatal_failure (length == NULL, "syntax_select: Length is null.");
  103. for (select = offset = 0; select != syntax_count; ++select) {
  104. if (syntax_enrange [select] == FALSE) {
  105. if (string_compare_limit (string, syntax_begin [select], string_length (syntax_begin [select])) == TRUE) {
  106. break; // We need to limit our string comparisson function.
  107. } else {
  108. continue;
  109. }
  110. } else {
  111. for (subset = 0; subset != string_length (syntax_begin [select]); ++subset) {
  112. if (string [offset] == syntax_begin [select] [subset]) {
  113. goto selected; // We can't use 'break' here, because it will exit only one loop, not both of them.
  114. } else {
  115. continue;
  116. }
  117. }
  118. }
  119. }
  120. selected:
  121. if (select >= syntax_count) {
  122. * length = 1;
  123. return (syntax_count);
  124. }
  125. for (offset = 1; string [offset - 1] != '\0'; ++offset) {
  126. if (string [offset] == syntax_escape [select]) {
  127. ++offset;
  128. continue;
  129. }
  130. if (syntax_derange [select] == FALSE) {
  131. if (string_compare_limit (& string [offset], syntax_end [select], string_length (syntax_end [select])) == TRUE) {
  132. * length = offset + string_length (syntax_end [select]);
  133. return (select);
  134. }
  135. } else {
  136. subset = 0;
  137. if (string_compare (syntax_end [select], "") == TRUE) {
  138. break;
  139. } do {
  140. if (string [offset] == syntax_end [select] [subset]) {
  141. * length = offset;
  142. goto finished;
  143. }
  144. } while (++subset != string_length (syntax_end [select]));
  145. }
  146. }
  147. finished:
  148. return (select);
  149. }
  150. static void syntax_highlight_c (void) {
  151. char * separators = ".,:;<=>+*-/%!&~^?|()[]{}'\" \t\r\n";
  152. char * keywords [] = {
  153. "register", "volatile", "auto", "const", "static", "extern", "if", "else",
  154. "do", "while", "for", "continue", "switch", "case", "default", "break",
  155. "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
  156. "char", "short", "int", "long", "signed", "unsigned", "float", "double"
  157. };
  158. int word;
  159. syntax_define (FALSE, FALSE, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
  160. syntax_define (FALSE, FALSE, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  161. syntax_define (FALSE, FALSE, "#", "\n", '\\', COLOUR_YELLOW, EFFECT_ITALIC);
  162. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  163. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  164. for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
  165. syntax_define (FALSE, TRUE, keywords [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  166. }
  167. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  168. syntax_define (TRUE, FALSE, ".,:;<=>+*-/%!&~^?|", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  169. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  170. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  171. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  172. syntax_define (TRUE, TRUE, "_", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
  173. }
  174. void preview_c_file (char * text_file, int width, int height, int x, int y) {
  175. char * text_data;
  176. int reset_x = x;
  177. int reset_y = y;
  178. (void) width;
  179. (void) height;
  180. syntax_highlight_c ();
  181. text_data = file_record (text_file);
  182. for (curses_active = 1; curses_active != 0; ) {
  183. int offset, select, length;
  184. curses_render_background (' ', COLOUR_WHITE, EFFECT_NORMAL);
  185. x = reset_x;
  186. y = reset_y;
  187. select = syntax_count;
  188. length = 0;
  189. for (offset = 0; offset < string_length (text_data); offset += length) {
  190. int suboffset, colour, effect;
  191. select = syntax_select (& text_data [offset], & length);
  192. if (select >= syntax_count) {
  193. colour = COLOUR_WHITE;
  194. effect = EFFECT_NORMAL;
  195. } else {
  196. colour = syntax_colour [select];
  197. effect = syntax_effect [select];
  198. }
  199. for (suboffset = 0; suboffset < length; ++suboffset) {
  200. if (text_data [offset + suboffset] == CHARACTER_LINE_FEED) {
  201. x = reset_x;
  202. y += 1;
  203. } else if (text_data [offset + suboffset] == CHARACTER_TAB_HORIZONTAL) {
  204. x += 8;
  205. } else {
  206. curses_render_character (text_data [offset + suboffset], colour, effect, x, y);
  207. x += 1;
  208. }
  209. }
  210. // curses_render_string_limit (& text_data [offset], length, syntax_colour [select], syntax_effect [select], x, y);
  211. }
  212. curses_synchronize ();
  213. }
  214. text_data = deallocate (text_data);
  215. }
  216. #endif