Xighlight C source code...
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

177 行
8.1KB

  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 void highlight_c (void) {
  11. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  12. char * keywords [32] = {
  13. "register", "volatile", "auto", "const", "static", "extern", "if", "else",
  14. "do", "while", "for", "continue", "switch", "case", "default", "break",
  15. "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
  16. "char", "short", "int", "long", "signed", "unsigned", "float", "double"
  17. };
  18. program_mode = "C highlighting mode\n";
  19. syntax_define_separators (separators);
  20. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  21. syntax_define_range ("/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
  22. syntax_define_range ("//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  23. syntax_define_range ("#", "\n", '\\', COLOUR_PINK, EFFECT_NORMAL);
  24. syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  25. syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
  26. syntax_define_words (keywords, 32, COLOUR_BLUE, EFFECT_NORMAL);
  27. }
  28. static void highlight_ada (void) {
  29. char * separators = ".,:;<=>+-*/&|()\" \t\r\n";
  30. char * keywords [73] = {
  31. "abort", "else", "new", "return", "abs", "elsif", "not", "reverse",
  32. "abstract", "end", "null", "accept", "entry", "select", "access", "of",
  33. "separate", "aliased", "exit", "or", "some", "all", "others", "subtype",
  34. "and", "for", "out", "array", "function", "at", "tagged", "generic",
  35. "package", "task", "begin", "goto", "pragma", "body", "private", "then",
  36. "type", "case", "in", "constant", "until", "is", "raise", "use",
  37. "if", "declare", "range", "delay", "limited", "record", "when", "delta",
  38. "loop", "rem", "while", "digits", "renames", "with", "do", "mod",
  39. "requeue", "xor", "procedure", "protected", "interface", "synchronized", "exception", "overriding",
  40. "terminate"
  41. };
  42. program_mode = "Ada highlighting mode\n";
  43. syntax_define_separators (separators);
  44. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  45. syntax_define_range ("--", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  46. syntax_define_range ("'", "'", '\0', COLOUR_PINK, EFFECT_BOLD);
  47. syntax_define_operators (".,:;<=>+-*/&|()'", COLOUR_BLUE, EFFECT_BOLD);
  48. syntax_define_words (keywords, 73, COLOUR_BLUE, EFFECT_NORMAL);
  49. }
  50. static void highlight_cpp (void) {
  51. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  52. char * keywords [102] = {
  53. "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept",
  54. "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char",
  55. "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const", "consteval",
  56. "constexpr", "constinit", "const_cast", "continue", "co_await", "co_return", "co_yield", "decltype",
  57. "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit",
  58. "export", "extern", "false", "float", "for", "friend", "goto", "if",
  59. "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not",
  60. "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public",
  61. "reflexpr", "register", "reinterpret_cast", "requires", "return", "short", "signed", "sizeof",
  62. "static", "static_assert", "static_cast", "struct", "switch", "synchronized", "template", "this",
  63. "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union",
  64. "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor",
  65. "xor_eq", "final", "override", "import", "module", "transaction_safe"
  66. };
  67. program_mode = "C++ highlighting mode\n";
  68. syntax_define_separators (separators);
  69. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  70. syntax_define_range ("/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
  71. syntax_define_range ("//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  72. syntax_define_range ("#", "\n", '\\', COLOUR_PINK, EFFECT_NORMAL);
  73. syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  74. syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
  75. syntax_define_words (keywords, 102, COLOUR_BLUE, EFFECT_NORMAL);
  76. }
  77. /*
  78. static void highlight_ (void) {
  79. char * separators = ".,:;<=>+*-/&|()\" \t\r\n";
  80. char * keywords [73] = {
  81. };
  82. syntax_define_separators (separators);
  83. syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
  84. syntax_define_range ("--", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  85. syntax_define_range ("'", "'", '\0', COLOUR_PINK, EFFECT_BOLD);
  86. syntax_define_operators (".,:;<=>+*-/&|()'", COLOUR_BLUE, EFFECT_BOLD);
  87. syntax_define_words (keywords, 73, COLOUR_BLUE, EFFECT_NORMAL);
  88. }
  89. */
  90. int main (int argc, char * * argv) {
  91. int offset = 0;
  92. int select = 0;
  93. int length = 0;
  94. char * buffer = NULL;
  95. program_name = "Xighlight -- Syntax highlighting program\n";
  96. program_license = "GNU/GPLv3 -- GNU General Public License (version 3)\n";
  97. if (argc == 2) {
  98. int type = file_type (argv [1]);
  99. fatal_failure (type == -1, "xighlight: Unknown file type.");
  100. buffer = file_import (argv [1]);
  101. if ((type == FILE_TYPE_C_SOURCE) || (type == FILE_TYPE_C_HEADER)) {
  102. highlight_c ();
  103. } else if ((type == FILE_TYPE_ADA_BODY) || (type == FILE_TYPE_ADA_SPECIFICATION)) {
  104. highlight_ada ();
  105. } else if ((type == FILE_TYPE_CPP_SOURCE) || (type == FILE_TYPE_CPP_HEADER)) {
  106. highlight_cpp ();
  107. } else {
  108. echo ("0123456789" + type);
  109. fatal_failure (1, "Unknown highlighting mode:");
  110. }
  111. } else {
  112. buffer = record ();
  113. highlight_c ();
  114. }
  115. echo (program_name);
  116. echo (program_license);
  117. echo (program_mode);
  118. for (offset = 0; buffer [offset] != '\0'; offset += length) {
  119. select = syntax_select (& buffer [offset], & length);
  120. if (select >= syntax_count) {
  121. terminal_style (EFFECT_REVERSE, COLOUR_RED);
  122. } else {
  123. terminal_style (syntax_effect [select], syntax_colour [select]);
  124. }
  125. out (& buffer [offset], length);
  126. terminal_style (-1, -1);
  127. }
  128. buffer = deallocate (buffer);
  129. syntax_delete ();
  130. return (EXIT_SUCCESS);
  131. }