xyntax/xyntax.c

143 lines
4.5 KiB
C

#ifndef XYNTAX_SOURCE
#define XYNTAX_SOURCE
#include <xolatile/xtandard.c>
#include <xolatile/xyntax.h>
static int syntax_count = 0;
static int syntax_active = false;
static int * syntax_enrange = null;
static int * syntax_derange = null;
static char * * syntax_begin = null;
static char * * syntax_end = null;
static char * syntax_escape = null;
static int * syntax_colour = null;
static int * syntax_effect = null;
static void syntax_delete (void) {
int offset;
if (syntax_active == false) {
return;
}
for (offset = 0; offset < syntax_count; ++offset) {
syntax_begin [offset] = deallocate (syntax_begin [offset]);
syntax_end [offset] = deallocate (syntax_end [offset]);
}
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_active = false;
syntax_count = 0;
}
int syntax_define (int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
if (syntax_active == false) {
syntax_active = true;
atexit (syntax_delete);
}
fatal_failure (begin == null, "syntax_define: Begin string is null pointer.");
fatal_failure (end == null, "syntax_define: End string is null pointer.");
++syntax_count;
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));
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));
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;
string_copy (syntax_begin [syntax_count - 1], begin);
string_copy (syntax_end [syntax_count - 1], end);
return (syntax_count - 1);
}
int syntax_select (char * string, int * length) {
int offset, subset, select;
fatal_failure (syntax_active == false, "syntax_select: Syntax is not active.");
fatal_failure (string == null, "syntax_select: String is null.");
fatal_failure (length == null, "syntax_select: Length is null.");
for (select = offset = 0; select != syntax_count; ++select) {
if (syntax_enrange [select] == false) {
if (syntax_derange [select] == false) {
if (string_compare_limit (string, syntax_begin [select], string_length (syntax_begin [select])) == true) {
break;
}
} else {
if ((string_compare_limit (string, syntax_begin [select], string_length (syntax_begin [select])) == true)
&& (character_compare_array (string [offset + string_length (syntax_begin [select])], syntax_end [select]) == true)) {
break;
}
}
} else {
for (subset = 0; subset != string_length (syntax_begin [select]); ++subset) {
if (string [offset] == syntax_begin [select] [subset]) {
goto selected;
}
}
}
}
selected:
if (select >= syntax_count) {
* length = 1;
return (syntax_count);
}
for (offset = 1; string [offset - 1] != '\0'; ++offset) {
if (string [offset] == syntax_escape [select]) {
++offset;
continue;
}
if (syntax_derange [select] == false) {
if (string_compare_limit (& string [offset], syntax_end [select], string_length (syntax_end [select])) == true) {
* length = offset + string_length (syntax_end [select]);
return (select);
}
} else {
subset = 0;
if (string_compare (syntax_end [select], "") == false) {
break;
} do {
if (string [offset] == syntax_end [select] [subset]) {
* length = offset;
goto finished;
}
} while (++subset != string_length (syntax_end [select]));
}
}
finished:
return (select);
}
#endif