Xighlight C source code...
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

236 lignes
10KB

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