Xighlight C source code...
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
11KB

  1. /*
  2. Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. Xighlight 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. #include <xolatile/xtandard.c>
  8. #include <xolatile/xyntax.c>
  9. static int highlighted = 0;
  10. static void echo_version (void) {
  11. echo ("xighlight: Terminal syntax highlighter (version 144000)\n");
  12. }
  13. static void echo_license (void) {
  14. echo ("xighlight: Terminal syntax highlighter (GNU general public license version 3)\n");
  15. }
  16. static void highlight_common (void) { /* Should be changed to support basic GCC, Clang, Valgrind and Splint output... */
  17. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  18. if (highlighted != 0) {
  19. syntax_delete ();
  20. }
  21. syntax_define_separators (separators);
  22. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  23. syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  24. syntax_define_range ("`", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  25. syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
  26. (void) syntax_insert (1, 1, "abcdefghijklmnopqrstuvwxyz", syntax_separator, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  27. (void) syntax_insert (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", syntax_separator, '\0', COLOUR_WHITE, EFFECT_BOLD);
  28. (void) syntax_insert (1, 1, "_", syntax_separator, '\0', COLOUR_PINK, EFFECT_BOLD);
  29. highlighted = 1;
  30. }
  31. static void highlight_c (void) {
  32. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  33. char * keywords [32] = {
  34. "register", "volatile", "auto", "const", "static", "extern", "if", "else",
  35. "do", "while", "for", "continue", "switch", "case", "default", "break",
  36. "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
  37. "char", "short", "int", "long", "signed", "unsigned", "float", "double"
  38. };
  39. if (highlighted != 0) {
  40. syntax_delete ();
  41. }
  42. syntax_define_separators (separators);
  43. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  44. syntax_define_range ("/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
  45. syntax_define_range ("//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  46. syntax_define_range ("#", "\n", '\\', COLOUR_PINK, EFFECT_NORMAL);
  47. syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  48. syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
  49. syntax_define_words (1, keywords, 32, COLOUR_BLUE, EFFECT_NORMAL);
  50. highlighted = 1;
  51. }
  52. static void highlight_ada (void) {
  53. char * separators = ".,:;<=>+-*/&|()\" \t\r\n";
  54. char * keywords [73] = {
  55. "abort", "else", "new", "return", "abs", "elsif", "not", "reverse",
  56. "abstract", "end", "null", "accept", "entry", "select", "access", "of",
  57. "separate", "aliased", "exit", "or", "some", "all", "others", "subtype",
  58. "and", "for", "out", "array", "function", "at", "tagged", "generic",
  59. "package", "task", "begin", "goto", "pragma", "body", "private", "then",
  60. "type", "case", "in", "constant", "until", "is", "raise", "use",
  61. "if", "declare", "range", "delay", "limited", "record", "when", "delta",
  62. "loop", "rem", "while", "digits", "renames", "with", "do", "mod",
  63. "requeue", "xor", "procedure", "protected", "interface", "synchronized", "exception", "overriding",
  64. "terminate"
  65. };
  66. if (highlighted != 0) {
  67. syntax_delete ();
  68. }
  69. syntax_define_separators (separators);
  70. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  71. syntax_define_range ("--", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  72. syntax_define_range ("'", "'", '\0', COLOUR_PINK, EFFECT_BOLD);
  73. syntax_define_operators (".,:;<=>+-*/&|()'", COLOUR_BLUE, EFFECT_BOLD);
  74. syntax_define_words (0, keywords, 73, COLOUR_BLUE, EFFECT_NORMAL);
  75. highlighted = 1;
  76. }
  77. static void highlight_cpp (void) {
  78. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  79. char * keywords [102] = {
  80. "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept",
  81. "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char",
  82. "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const", "consteval",
  83. "constexpr", "constinit", "const_cast", "continue", "co_await", "co_return", "co_yield", "decltype",
  84. "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit",
  85. "export", "extern", "false", "float", "for", "friend", "goto", "if",
  86. "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not",
  87. "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public",
  88. "reflexpr", "register", "reinterpret_cast", "requires", "return", "short", "signed", "sizeof",
  89. "static", "static_assert", "static_cast", "struct", "switch", "synchronized", "template", "this",
  90. "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union",
  91. "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor",
  92. "xor_eq", "final", "override", "import", "module", "transaction_safe"
  93. };
  94. if (highlighted != 0) {
  95. syntax_delete ();
  96. }
  97. syntax_define_separators (separators);
  98. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  99. syntax_define_range ("/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
  100. syntax_define_range ("//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  101. syntax_define_range ("#", "\n", '\\', COLOUR_PINK, EFFECT_NORMAL);
  102. syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  103. syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
  104. syntax_define_words (1, keywords, 102, COLOUR_BLUE, EFFECT_NORMAL);
  105. highlighted = 1;
  106. }
  107. /*
  108. static void highlight_python (void) {
  109. char * separators = ".,:<=>+*-/%!&~^?|()[]'\" \t\r\n";
  110. char * keywords [35] = {
  111. "False", "await", "else", "import", "pass", "None", "break", "except",
  112. "in", "raise", "True", "class", "finally", "is", "return", "and",
  113. "continue", "for", "lambda", "try", "as", "def", "from", "nonlocal",
  114. "while", "assert", "del", "global", "not", "with", "async", "elif",
  115. "if", "or", "yield"
  116. };
  117. if (highlighted != 0) {
  118. syntax_delete ();
  119. }
  120. syntax_define_separators (separators);
  121. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  122. syntax_define_range ("#", "\n", '\\', COLOUR_GREY, EFFECT_BOLD);
  123. syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  124. syntax_define_operators (".,:<=>+*-/%!&~^?|()[]", COLOUR_BLUE, EFFECT_BOLD);
  125. syntax_define_words (0, keywords, 35, COLOUR_BLUE, EFFECT_NORMAL);
  126. highlighted = 1;
  127. }
  128. */
  129. static void highlight_valgrind (void) {
  130. char * separators = "./-=?() \t\r\n";
  131. char * keywords [1] = {
  132. "Invalid"
  133. };
  134. if (highlighted != 0) {
  135. syntax_delete ();
  136. }
  137. syntax_define_separators (separators);
  138. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  139. syntax_define_range ("==", "==", '\0', COLOUR_GREY, EFFECT_BOLD);
  140. syntax_define_range ("(", ")", '\0', COLOUR_PINK, EFFECT_NORMAL);
  141. syntax_define_range ("???", "\n", '\0', COLOUR_RED, EFFECT_BOLD);
  142. syntax_define_operators ("./-:", COLOUR_BLUE, EFFECT_BOLD);
  143. syntax_define_words (0, keywords, 1, COLOUR_BLUE, EFFECT_NORMAL);
  144. highlighted = 1;
  145. }
  146. int main (int argc, char * * argv) {
  147. int offset = 0;
  148. int select = 0;
  149. int length = 0;
  150. char * buffer = NULL;
  151. argument_define ("-v", "--version", echo_version);
  152. argument_define ("-l", "--license", echo_license);
  153. argument_define ("-c", "--c", highlight_c);
  154. argument_define ("-a", "--ada", highlight_ada);
  155. argument_define ("-C", "--c++", highlight_cpp);
  156. /*argument_define ("-p", "--python", highlight_python);*/
  157. argument_define ("-V", "--valgrind", highlight_valgrind);
  158. if (argc != 1) {
  159. argument_select (argc, argv);
  160. }
  161. if (buffer == NULL) {
  162. if (argument_input == NULL) {
  163. buffer = record ();
  164. } else {
  165. select = file_type (argument_input);
  166. buffer = file_import (argument_input);
  167. }
  168. }
  169. if (highlighted == 0) {
  170. if ((select == FILE_TYPE_C_SOURCE) || (select == FILE_TYPE_C_HEADER)) {
  171. highlight_c ();
  172. } else if ((select == FILE_TYPE_ADA_BODY) || (select == FILE_TYPE_ADA_SPECIFICATION)) {
  173. highlight_ada ();
  174. } else if ((select == FILE_TYPE_CPP_SOURCE) || (select == FILE_TYPE_CPP_HEADER)) {
  175. highlight_cpp ();
  176. } else {
  177. highlight_common ();
  178. }
  179. }
  180. for (offset = 0; buffer [offset] != '\0'; offset += length) {
  181. select = syntax_select (& buffer [offset], & length);
  182. if (select >= syntax_count) {
  183. terminal_colour (COLOUR_RED, EFFECT_REVERSE);
  184. } else {
  185. terminal_colour (syntax_colour [select], syntax_effect [select]);
  186. }
  187. out (& buffer [offset], length);
  188. terminal_cancel ();
  189. }
  190. buffer = deallocate (buffer);
  191. syntax_delete ();
  192. argument_delete ();
  193. return (EXIT_SUCCESS);
  194. }