Extended language support...

This commit is contained in:
Ognjen Milan Robovic 2023-09-17 16:17:16 -04:00
parent e8e1121f5c
commit 6c15c79cc0
2 changed files with 82 additions and 49 deletions

View File

@ -1,5 +1,7 @@
# xighlight # xighlight
I'm gonna update this, I swear...
xighlight -- Program for highlighting C source code in terminal. xighlight -- Program for highlighting C source code in terminal.
- Quickly written program to test out xtandard and xyntax, doesn't handle files, instead it records from standard input... - Quickly written program to test out xtandard and xyntax, doesn't handle files, instead it records from standard input...
@ -21,8 +23,9 @@ $ sudo sh install.sh
Using: Using:
```bash ```bash
$ cat my_c_program.c | xighlight $ cat my_c_program.ext | xighlight
$ xighlight < my_c_program.c $ xighlight < my_c_program.ext
$ xighlight -i my_c_program.ext
``` ```
This is what it prints to standard output, when standard input is C source code: This is what it prints to standard output, when standard input is C source code:

View File

@ -9,6 +9,39 @@
#include <xolatile/xyntax.h> #include <xolatile/xyntax.h>
#include <xolatile/xyntax.c> #include <xolatile/xyntax.c>
static int highlighted = 0;
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) { /* Should be changed to support basic GCC, Clang, Valgrind and Splint output... */
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
if (highlighted != 0) {
syntax_delete ();
}
syntax_define_separators (separators);
syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
syntax_define_range ("`", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
(void) syntax_insert (1, 1, "abcdefghijklmnopqrstuvwxyz", syntax_separator, '\0', COLOUR_WHITE, EFFECT_NORMAL);
(void) syntax_insert (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", syntax_separator, '\0', COLOUR_WHITE, EFFECT_BOLD);
(void) syntax_insert (1, 1, "_", syntax_separator, '\0', COLOUR_PINK, EFFECT_BOLD);
highlighted = 1;
}
static void highlight_c (void) { static void highlight_c (void) {
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n"; char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
@ -19,7 +52,9 @@ static void highlight_c (void) {
"char", "short", "int", "long", "signed", "unsigned", "float", "double" "char", "short", "int", "long", "signed", "unsigned", "float", "double"
}; };
program_mode = "C highlighting mode\n"; if (highlighted != 0) {
syntax_delete ();
}
syntax_define_separators (separators); syntax_define_separators (separators);
@ -33,6 +68,8 @@ static void highlight_c (void) {
syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD); syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
syntax_define_words (keywords, 32, COLOUR_BLUE, EFFECT_NORMAL); syntax_define_words (keywords, 32, COLOUR_BLUE, EFFECT_NORMAL);
highlighted = 1;
} }
static void highlight_ada (void) { static void highlight_ada (void) {
@ -51,7 +88,9 @@ static void highlight_ada (void) {
"terminate" "terminate"
}; };
program_mode = "Ada highlighting mode\n"; if (highlighted != 0) {
syntax_delete ();
}
syntax_define_separators (separators); syntax_define_separators (separators);
@ -63,6 +102,8 @@ static void highlight_ada (void) {
syntax_define_operators (".,:;<=>+-*/&|()'", COLOUR_BLUE, EFFECT_BOLD); syntax_define_operators (".,:;<=>+-*/&|()'", COLOUR_BLUE, EFFECT_BOLD);
syntax_define_words (keywords, 73, COLOUR_BLUE, EFFECT_NORMAL); syntax_define_words (keywords, 73, COLOUR_BLUE, EFFECT_NORMAL);
highlighted = 1;
} }
static void highlight_cpp (void) { static void highlight_cpp (void) {
@ -84,7 +125,9 @@ static void highlight_cpp (void) {
"xor_eq", "final", "override", "import", "module", "transaction_safe" "xor_eq", "final", "override", "import", "module", "transaction_safe"
}; };
program_mode = "C++ highlighting mode\n"; if (highlighted != 0) {
syntax_delete ();
}
syntax_define_separators (separators); syntax_define_separators (separators);
@ -98,61 +141,46 @@ static void highlight_cpp (void) {
syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD); syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
syntax_define_words (keywords, 102, COLOUR_BLUE, EFFECT_NORMAL); syntax_define_words (keywords, 102, COLOUR_BLUE, EFFECT_NORMAL);
highlighted = 1;
} }
/*
static void highlight_ (void) {
char * separators = ".,:;<=>+*-/&|()\" \t\r\n";
char * keywords [73] = {
};
syntax_define_separators (separators);
syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
syntax_define_range ("--", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
syntax_define_range ("'", "'", '\0', COLOUR_PINK, EFFECT_BOLD);
syntax_define_operators (".,:;<=>+*-/&|()'", COLOUR_BLUE, EFFECT_BOLD);
syntax_define_words (keywords, 73, COLOUR_BLUE, EFFECT_NORMAL);
}
*/
int main (int argc, char * * argv) { int main (int argc, char * * argv) {
int offset = 0; int offset = 0;
int select = 0; int select = 0;
int length = 0; int length = 0;
char * buffer = NULL; char * buffer = NULL;
program_name = "Xighlight -- Syntax highlighting program\n"; argument_define ("-v", "--version", echo_version);
program_license = "GNU/GPLv3 -- GNU General Public License (version 3)\n"; argument_define ("-l", "--license", echo_license);
argument_define ("-c", "--c", highlight_c);
argument_define ("-a", "--ada", highlight_ada);
argument_define ("-C", "--c++", highlight_cpp);
if (argc == 2) { if (argc != 1) {
int type = file_type (argv [1]); argument_select (argc, argv);
fatal_failure (type == -1, "xighlight: Unknown file type.");
buffer = file_import (argv [1]);
if ((type == FILE_TYPE_C_SOURCE) || (type == FILE_TYPE_C_HEADER)) {
highlight_c ();
} else if ((type == FILE_TYPE_ADA_BODY) || (type == FILE_TYPE_ADA_SPECIFICATION)) {
highlight_ada ();
} else if ((type == FILE_TYPE_CPP_SOURCE) || (type == FILE_TYPE_CPP_HEADER)) {
highlight_cpp ();
} else {
echo ("0123456789" + type);
fatal_failure (1, "Unknown highlighting mode:");
}
} else {
buffer = record ();
highlight_c ();
} }
echo (program_name); if (buffer == NULL) {
echo (program_license); if (argument_input == NULL) {
echo (program_mode); buffer = record ();
} else {
select = file_type (argument_input);
buffer = file_import (argument_input);
}
}
if (highlighted == 0) {
if ((select == FILE_TYPE_C_SOURCE) || (select == FILE_TYPE_C_HEADER)) {
highlight_c ();
} else if ((select == FILE_TYPE_ADA_BODY) || (select == FILE_TYPE_ADA_SPECIFICATION)) {
highlight_ada ();
} else if ((select == FILE_TYPE_CPP_SOURCE) || (select == FILE_TYPE_CPP_HEADER)) {
highlight_cpp ();
} else {
highlight_common ();
}
}
for (offset = 0; buffer [offset] != '\0'; offset += length) { for (offset = 0; buffer [offset] != '\0'; offset += length) {
select = syntax_select (& buffer [offset], & length); select = syntax_select (& buffer [offset], & length);
@ -172,5 +200,7 @@ int main (int argc, char * * argv) {
syntax_delete (); syntax_delete ();
argument_delete ();
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }