xyntax/xyntax.c
2023-09-10 14:42:51 -04:00

214 lines
9.0 KiB
C

/*
* Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
*
* Xyntax is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
* 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.
* 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.
*/
#ifndef XYNTAX_SOURCE
#define XYNTAX_SOURCE
#include <xolatile/xyntax.h>
#ifdef XYNTAX_PREDEFINED_C_HIGHLIGHT
int syntax_count = SYNTAX_C_COUNT;
int syntax_mode [SYNTAX_C_COUNT] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int syntax_enrange [SYNTAX_C_COUNT] = { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };
int syntax_derange [SYNTAX_C_COUNT] = { 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
char * syntax_begin [SYNTAX_C_COUNT] = {
"#", "//", "/*", "'", "\"", "()[]{}.,:;<=>+-*/%!&~^?|",
"register", "volatile", "auto", "const", "static", "extern", "if", "else",
"do", "while", "for", "continue", "switch", "case", "default", "break",
"enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
"char", "short", "int", "long", "signed", "unsigned", "float", "double",
"0123456789", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"
};
char * syntax_end [SYNTAX_C_COUNT] = {
"\n", "\n", "*/", "'", "\"", "",
".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n",
".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n",
".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n",
".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n",
".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n",
".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n",
".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n",
".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n",
".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n", ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n"
};
char syntax_escape [SYNTAX_C_COUNT] = {
'\\', '\0', '\0', '\\', '\\', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0'
};
int syntax_colour [SYNTAX_C_COUNT] = {
COLOUR_RED, COLOUR_GREY, COLOUR_GREY, COLOUR_RED, COLOUR_RED, COLOUR_BLUE,
COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE,
COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE,
COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE,
COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE, COLOUR_BLUE,
COLOUR_PINK, COLOUR_PINK, COLOUR_WHITE
};
int syntax_effect [SYNTAX_C_COUNT] = {
EFFECT_NORMAL, EFFECT_BOLD, EFFECT_BOLD, EFFECT_BOLD, EFFECT_NORMAL, EFFECT_BOLD,
EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL,
EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL,
EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL,
EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL, EFFECT_NORMAL,
EFFECT_BOLD, EFFECT_NORMAL, EFFECT_ITALIC
};
#else
int syntax_count = 0;
int * syntax_mode = NULL;
int * syntax_enrange = NULL;
int * syntax_derange = NULL;
char * * syntax_begin = NULL;
char * * syntax_end = NULL;
char * syntax_escape = NULL;
int * syntax_colour = NULL;
int * syntax_effect = NULL;
#endif
static int compare_character_array (char character, char * character_array, int count) {
int i = 0;
do {
if (character == character_array [i]) {
return (1);
}
} while (++i != count);
return (0);
}
#ifndef XYNTAX_PREDEFINED_C_HIGHLIGHT
void syntax_define (int * index, int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
syntax_mode = reallocate (syntax_mode, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_mode));
syntax_enrange = reallocate (syntax_enrange, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_enrange));
syntax_derange = reallocate (syntax_derange, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_derange));
syntax_begin = reallocate (syntax_begin, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_begin));
syntax_end = reallocate (syntax_end, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_end));
syntax_escape = reallocate (syntax_escape, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_escape));
syntax_colour = reallocate (syntax_colour, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_colour));
syntax_effect = reallocate (syntax_effect, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_effect));
syntax_begin [syntax_count] = allocate ((string_length (begin) + 1) * (int) sizeof (* * syntax_begin));
syntax_end [syntax_count] = allocate ((string_length (end) + 1) * (int) sizeof (* * syntax_end));
syntax_mode [syntax_count] = * index;
syntax_enrange [syntax_count] = enrange;
syntax_derange [syntax_count] = derange;
syntax_escape [syntax_count] = escape;
syntax_colour [syntax_count] = colour;
syntax_effect [syntax_count] = effect;
string_copy (syntax_begin [syntax_count], begin);
string_copy (syntax_end [syntax_count], end);
* index = syntax_count;
++syntax_count;
}
#endif
void syntax_select (char * string, int * index, int * length) {
int offset = 0;
int select = 0;
fatal_failure (string == NULL, "syntax_select: String is null.");
fatal_failure (index == NULL, "syntax_select: Index is null.");
fatal_failure (length == NULL, "syntax_select: Length is null.");
do {
if (syntax_enrange [select] == 0) {
if (string_compare_limit (string, syntax_begin [select], string_length (syntax_begin [select])) != 0) {
if (syntax_derange [select] == 0) {
break;
} else {
if (compare_character_array (string [string_length (syntax_begin [select])], syntax_end [select], string_length (syntax_end [select]))) {
break;
}
}
}
} else {
int subset = 0;
do {
if (string [offset] == syntax_begin [select] [subset]) {
goto selected;
}
} while (++subset != (int) string_length (syntax_begin [select]));
}
} while (++select != syntax_count);
selected:
if (select == syntax_count) {
* index = 0; /* Should we handle this undefined behavior? */
* length = 1;
return;
} else {
* index = select;
}
do {
++offset;
if (string [offset] == syntax_escape [select]) {
++offset;
continue;
}
if (syntax_derange [select] == 0) {
if (string_compare_limit (& string [offset], syntax_end [select], string_length (syntax_end [select]))) {
* length = offset + string_length (syntax_end [select]);
return;
}
} else {
int subset = 0;
if (string_compare (syntax_end [select], "") == 0) {
break;
} do {
if (string [offset] == syntax_end [select] [subset]) {
* length = offset;
return;
}
} while (++subset != (int) string_length (syntax_end [select]));
}
} while (string [offset - 1] != '\0');
}
#ifndef XYNTAX_PREDEFINED_C_HIGHLIGHT
void syntax_delete (void) {
--syntax_count;
do {
syntax_begin [syntax_count] = deallocate (syntax_begin [syntax_count]);
syntax_end [syntax_count] = deallocate (syntax_end [syntax_count]);
} while (--syntax_count != -1);
syntax_mode = deallocate (syntax_mode);
syntax_enrange = deallocate (syntax_enrange);
syntax_derange = deallocate (syntax_derange);
syntax_begin = deallocate (syntax_begin);
syntax_end = deallocate (syntax_end);
syntax_escape = deallocate (syntax_escape);
syntax_colour = deallocate (syntax_colour);
syntax_effect = deallocate (syntax_effect);
}
#endif
#endif