xyntax/xyntax.c

178 lines
6.2 KiB
C
Raw Normal View History

2023-08-25 17:00:12 -04:00
/*
* Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
*
2023-08-28 09:03:06 -04:00
* Xyntax is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
2023-08-25 17:00:12 -04:00
* 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>
int syntax_count = 0;
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;
char * syntax_separator = NULL;
2023-08-25 17:00:12 -04:00
int syntax_insert (int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
2023-09-12 05:03:51 -04:00
fatal_failure (begin == NULL, "syntax_define: Begin string is null pointer.");
fatal_failure (end == NULL, "syntax_define: End string is null pointer.");
2023-08-25 17:00:12 -04:00
2023-09-12 05:03:51 -04:00
++syntax_count;
2023-08-25 17:00:12 -04:00
2023-09-12 05:03:51 -04:00
syntax_enrange = reallocate (syntax_enrange, syntax_count * (int) sizeof (* syntax_enrange));
syntax_derange = reallocate (syntax_derange, syntax_count * (int) sizeof (* syntax_derange));
syntax_begin = reallocate (syntax_begin, syntax_count * (int) sizeof (* syntax_begin));
syntax_end = reallocate (syntax_end, syntax_count * (int) sizeof (* syntax_end));
syntax_escape = reallocate (syntax_escape, syntax_count * (int) sizeof (* syntax_escape));
syntax_colour = reallocate (syntax_colour, syntax_count * (int) sizeof (* syntax_colour));
syntax_effect = reallocate (syntax_effect, syntax_count * (int) sizeof (* syntax_effect));
2023-08-25 17:00:12 -04:00
2023-09-12 05:03:51 -04:00
syntax_begin [syntax_count - 1] = allocate ((string_length (begin) + 1) * (int) sizeof (* * syntax_begin));
syntax_end [syntax_count - 1] = allocate ((string_length (end) + 1) * (int) sizeof (* * syntax_end));
2023-08-25 17:00:12 -04:00
2023-09-12 05:03:51 -04:00
syntax_enrange [syntax_count - 1] = enrange;
syntax_derange [syntax_count - 1] = derange;
syntax_escape [syntax_count - 1] = escape;
syntax_colour [syntax_count - 1] = colour;
syntax_effect [syntax_count - 1] = effect;
2023-08-28 09:03:06 -04:00
2023-09-12 05:03:51 -04:00
string_copy (syntax_begin [syntax_count - 1], begin);
string_copy (syntax_end [syntax_count - 1], end);
return (syntax_count - 1);
2023-08-25 17:00:12 -04:00
}
2023-09-12 05:03:51 -04:00
int syntax_select (char * string, int * length) {
2023-08-28 09:03:06 -04:00
int offset = 0;
int select = 0;
2023-08-25 17:00:12 -04:00
2023-08-28 09:03:06 -04:00
fatal_failure (string == NULL, "syntax_select: String is null.");
fatal_failure (length == NULL, "syntax_select: Length is null.");
2023-08-25 17:00:12 -04:00
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 {
2023-09-12 05:23:11 -04:00
if (character_compare_array (string [string_length (syntax_begin [select])], syntax_end [select], string_length (syntax_end [select])) != 0) {
2023-08-25 17:00:12 -04:00
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:
2023-09-12 09:58:36 -04:00
if (select == syntax_count) {
* length = 1;
return (select);
}
2023-08-25 17:00:12 -04:00
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]))) {
2023-08-28 09:03:06 -04:00
* length = offset + string_length (syntax_end [select]);
2023-09-12 05:03:51 -04:00
return (select);
2023-08-25 17:00:12 -04:00
}
} else {
int subset = 0;
2023-08-28 09:03:06 -04:00
if (string_compare (syntax_end [select], "") == 0) {
2023-08-25 17:00:12 -04:00
break;
} do {
if (string [offset] == syntax_end [select] [subset]) {
2023-08-28 09:03:06 -04:00
* length = offset;
2023-09-12 05:03:51 -04:00
return (select);
2023-08-25 17:00:12 -04:00
}
} while (++subset != (int) string_length (syntax_end [select]));
}
2023-09-10 14:42:51 -04:00
} while (string [offset - 1] != '\0');
2023-09-12 05:03:51 -04:00
return (select);
2023-08-25 17:00:12 -04:00
}
void syntax_delete (void) {
if (syntax_count == 0) {
return;
}
2023-08-25 17:00:12 -04:00
--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_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);
syntax_count = 0;
}
/* Simplification... */
void syntax_define_separators (char * separator) {
syntax_separator = separator;
}
void syntax_define_default (int string_colour, int string_effect, int number_colour, int number_effect) {
(void) syntax_insert (1, 0, " \t\r\n", "", '\0', COLOUR_WHITE, EFFECT_BOLD);
(void) syntax_insert (0, 0, "\"", "\"", '\\', string_colour, string_effect);
(void) syntax_insert (1, 1, "0123456789", syntax_separator, '\0', number_colour, number_effect);
}
2023-10-01 06:07:18 -04:00
void syntax_define_words (int case_sensitive, char * * word_array, int word_count, int word_colour, int word_effect) {
int word = 0;
for (word = 0; word != word_count; ++word) {
(void) syntax_insert (0, 1, word_array [word], syntax_separator, '\0', word_colour, word_effect);
}
2023-10-01 06:07:18 -04:00
if (case_sensitive != 0) {
(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);
} else {
(void) syntax_insert (1, 1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", syntax_separator, '\0', COLOUR_WHITE, EFFECT_NORMAL);
}
}
void syntax_define_range (char * range_begin, char * range_end, char range_escape, int range_colour, int range_effect) {
(void) syntax_insert (0, 0, range_begin, range_end, range_escape, range_colour, range_effect);
}
void syntax_define_operators (char * operator_array, int operator_colour, int operator_effect) {
(void) syntax_insert (1, 0, operator_array, "", '\0', operator_colour, operator_effect);
2023-08-25 17:00:12 -04:00
}
#endif