Просмотр исходного кода

Added Fortran support and general refactoring...

master
Ognjen Milan Robovic 2 месяцев назад
Родитель
Сommit
6567c1bc4b
2 измененных файлов: 99 добавлений и 51 удалений
  1. +2
    -2
      README.md
  2. +97
    -49
      xighlight.c

+ 2
- 2
README.md Просмотреть файл

@@ -3,7 +3,8 @@
xighlight -- Program for highlighting program output, source code and various text files in terminal.

- Everything related to my libraries is clean of all warning options on Clang, GCC and Valgrind.
- I don't know what else to write here, program is being done in my free time, so expect more...
- You can easily add new syntax support with it, for your own language or some that I didn't include.
- There are 2 ways to use this program, you can see them below, or use -h / --help to see it.

Compile:
```bash
@@ -24,5 +25,4 @@ Use:
```bash
$ cat my_c_program.ext | xighlight
$ xighlight < my_c_program.ext
$ xighlight -i my_c_program.ext
```

+ 97
- 49
xighlight.c Просмотреть файл

@@ -1,17 +1,10 @@
#include <xolatile/xtandard.c>
#include <xolatile/xyntax.c>

static void echo_version (void) {
echo ("xighlight: Terminal syntax highlighter (version 144000)\n");
}

static void echo_license (void) {
echo ("xighlight: Terminal syntax highlighter (GNU general public license version 3)\n");
}

static void highlight_common (void) {
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\"@#$` \t\r\n";

syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
syntax_define (true, false, "()[]{}", "", '\0', colour_blue, effect_normal);
syntax_define (true, false, ".,:;<=>+*-/%!&~^?|@#$`", "", '\0', colour_cyan, effect_normal);
@@ -30,11 +23,11 @@ static void highlight_c (void) {

int word;

syntax_define (false, false, "/*", "*/", '\0', colour_grey, effect_bold);
syntax_define (false, false, "//", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "#", "\n", '\\', colour_yellow, effect_italic);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
syntax_define (false, false, "/*", "*/", '\0', colour_grey, effect_bold);
syntax_define (false, false, "//", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "#", "\n", '\\', colour_yellow, effect_italic);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);

for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
@@ -67,9 +60,9 @@ static void highlight_ada (void) {

int word;

syntax_define (false, false, "--", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
syntax_define (false, false, "--", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);

for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
@@ -104,11 +97,11 @@ static void highlight_cpp (void) {

int word;

syntax_define (false, false, "/*", "*/", '\0', colour_grey, effect_bold);
syntax_define (false, false, "//", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "#", "\n", '\\', colour_yellow, effect_italic);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
syntax_define (false, false, "/*", "*/", '\0', colour_grey, effect_bold);
syntax_define (false, false, "//", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "#", "\n", '\\', colour_yellow, effect_italic);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);

for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
@@ -123,15 +116,6 @@ static void highlight_cpp (void) {
syntax_define (true, true, "_", separators, '\0', colour_white, effect_italic);
}

static void highlight_valgrind (void) {
char * separators = "./-=?() \t\r\n";

syntax_define (false, false, "==", "==", '\0', colour_grey, effect_bold);
syntax_define (false, false, "???", "\n", '\0', colour_yellow, effect_bold);

syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
}

static void highlight_common_assembly (void) {
char * separators = ".,+-[]<> \t\r\n";

@@ -257,32 +241,98 @@ static void highlight_flat_assembly (void) {
syntax_define (true, true, "_", separators, '\0', colour_white, effect_italic);
}

static void highlight_fortran (void) {
char * separators = ",:<=>+-*/&()[]\"\' \t\r\n";

char * keywords [] = {
"allocatable", "allocate", "associate", "backspace", "block", "call", "case", "common",
"contains", "cycle", "data", "deallocate", "d0", "do", "else", "elseif",
"end", "enddo", "endfile", "endif", "entry", "equivalence", "exit", "external",
"forall", "format", "function", "goto", "if", "implicit", "inquire", "intent",
"intrinsic", "module", "namelist", "none", "nullify", "only", "open", "optional",
"parameter", "pointer", "print", "private", "program", "public", "read", "recursive",
"return", "rewind", "save", "select", "sequence", "stop", "subroutine", "target",
"then", "to", "type", "use", "where", "write"
};

char * constants [] = {
".and.", ".or.", ".not.", ".true.", ".false.", "in", "out", "character",
"integer", "logical", "real", "complex", "dimension", "modulo", "len", "advance"
};

int word;

syntax_define (false, false, "!", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);

for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
}

for (word = 0; word != (int) (sizeof (constants) / sizeof (constants [0])); ++word) {
syntax_define (false, true, constants [word], separators, '\0', colour_yellow, effect_italic);
}

syntax_define (true, false, "()[]", "", '\0', colour_blue, effect_normal);
syntax_define (true, false, ",:<=>+-*/&", "", '\0', colour_cyan, effect_normal);

syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
syntax_define (true, true, "abcdefghijklmnopqrstuvwxyz", separators, '\0', colour_white, effect_normal);
syntax_define (true, true, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', colour_white, effect_bold);
syntax_define (true, true, "_", separators, '\0', colour_white, effect_italic);
}

int main (int argc, char * * argv) {
int offset = 0;
int select = 0;
int length = 0;
char * buffer = null;

argument_define ("-v", "--version", echo_version);
argument_define ("-l", "--license", echo_license);
argument_define ("-c", "--c", highlight_c);
argument_define ("-a", "--ada", highlight_ada);
argument_define ("-C", "--c++", highlight_cpp);
argument_define ("-V", "--valgrind", highlight_valgrind);
argument_define ("-A", "--assembly", highlight_common_assembly);
argument_define ("-F", "--flat-assembly", highlight_flat_assembly);

if (argc != 1) {
argument_select (argc, argv);
if (string_compare (argv [1], "-v") || string_compare (argv [1], "--version")) {
echo ("xighlight: Terminal syntax highlighter (version 144)\n");
exit (log_success);
} else if (string_compare (argv [1], "-l") || string_compare (argv [1], "--license")) {
echo ("xighlight: Terminal syntax highlighter (GNU/GPLv3)\n");
exit (log_success);
} else if (string_compare (argv [1], "-a") || string_compare (argv [1], "--author")) {
echo ("xighlight: Terminal syntax highlighter (Ognjen 'xolatile' Milan Robovic)\n");
exit (log_success);
} else if (string_compare (argv [1], "-h") || string_compare (argv [1], "--help")) {
echo ("xighlight: Terminal syntax highlighter:\n");
echo ("Example usage:\n");
echo ("\t$ cat file.ext | xighlight [flag] -- You need to pass language flag in this case.\n");
echo ("\t$ xighlight [flag] < file.ext -- You need to pass language flag in this case.\n");
echo ("\t$ xighlight file.ext -- Language is automatically detected in this case.\n");
echo ("Supported languages:\n");
echo ("\t -C --c -- C syntax\n");
echo ("\t -A --ada -- Ada syntax\n");
echo ("\t -P --cpp -- C++ syntax \n");
echo ("\t -S --assembly -- General assembly syntax\n");
echo ("\t -T --flat -- Flat assembly syntax\n");
echo ("\t -F --fortran -- Fortran syntax\n");
echo ("\t -H --shell -- Bourne shell syntax\n");
} else if (string_compare (argv [1], "-C") || string_compare (argv [1], "--c")) {
highlight_c ();
} else if (string_compare (argv [1], "-A") || string_compare (argv [1], "--ada")) {
highlight_ada ();
} else if (string_compare (argv [1], "-P") || string_compare (argv [1], "--cpp")) {
highlight_cpp ();
} else if (string_compare (argv [1], "-S") || string_compare (argv [1], "--assembly")) {
highlight_common_assembly ();
} else if (string_compare (argv [1], "-T") || string_compare (argv [1], "--flat")) {
highlight_flat_assembly ();
} else if (string_compare (argv [1], "-F") || string_compare (argv [1], "--fortran")) {
highlight_fortran ();
} else {
select = file_type (argv [1]);
buffer = file_import (argv [1]);
}
}

if (buffer == null) {
if (argument_input == null) {
buffer = record ();
} else {
select = file_type (argument_input);
buffer = file_import (argument_input);
}
buffer = record ();
}

if (syntax_active == false) {
@@ -317,7 +367,5 @@ int main (int argc, char * * argv) {

buffer = deallocate (buffer);

argument_delete ();

return (0);
return (log_success);
}

Загрузка…
Отмена
Сохранить