200 рядки
5.6 KiB
C
200 рядки
5.6 KiB
C
#ifndef UMORNA_SOURCE
|
|
#define UMORNA_SOURCE
|
|
|
|
#include <string.h>
|
|
|
|
#define SYNTAX_LIMIT (64)
|
|
|
|
enum {
|
|
COLOUR_GREY,
|
|
COLOUR_WHITE,
|
|
COLOUR_BLUE,
|
|
COLOUR_YELLOW,
|
|
COLOUR_CYAN,
|
|
COLOUR_PINK,
|
|
COLOUR_LMAO
|
|
};
|
|
|
|
static int syntax_count = 0;
|
|
|
|
static int syntax_enrange [SYNTAX_LIMIT];
|
|
static int syntax_derange [SYNTAX_LIMIT];
|
|
static char syntax_begin [SYNTAX_LIMIT] [96];
|
|
static char syntax_end [SYNTAX_LIMIT] [96];
|
|
static char syntax_escape [SYNTAX_LIMIT];
|
|
static int syntax_colour [SYNTAX_LIMIT];
|
|
|
|
static int character_compare_array (char character, char * character_array) {
|
|
int i = 0;
|
|
|
|
do {
|
|
if (character == character_array [i]) {
|
|
return (1);
|
|
}
|
|
} while (++i != (int) strlen (character_array));
|
|
|
|
return (0);
|
|
}
|
|
|
|
static void syntax_rule (int enrange,
|
|
int derange,
|
|
char * begin,
|
|
char * end,
|
|
char escape,
|
|
int colour) {
|
|
if (syntax_count >= SYNTAX_LIMIT) {
|
|
return;
|
|
}
|
|
|
|
strncpy (syntax_begin [syntax_count], begin, 96);
|
|
strncpy (syntax_end [syntax_count], end, 96);
|
|
|
|
syntax_enrange [syntax_count] = enrange;
|
|
syntax_derange [syntax_count] = derange;
|
|
syntax_escape [syntax_count] = escape;
|
|
syntax_colour [syntax_count] = colour;
|
|
|
|
++syntax_count;
|
|
}
|
|
|
|
static int syntax_loop (char * string,
|
|
int * length) {
|
|
int offset, subset, select;
|
|
|
|
for (select = offset = 0; select != syntax_count; ++select) {
|
|
if (syntax_enrange [select] == 0) {
|
|
if (syntax_derange [select] == 0) {
|
|
if (strncmp (string, syntax_begin [select], strlen (syntax_begin [select])) == 0) {
|
|
break;
|
|
}
|
|
} else {
|
|
if ((strncmp (string, syntax_begin [select], strlen (syntax_begin [select])) == 0)
|
|
&& (character_compare_array (string [offset + (int) strlen (syntax_begin [select])], syntax_end [select]) == 1)) {
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
for (subset = 0; subset != (int) strlen (syntax_begin [select]); ++subset) {
|
|
if (string [offset] == syntax_begin [select] [subset]) {
|
|
goto selected;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
selected:
|
|
|
|
if (select >= syntax_count) {
|
|
* length = 1;
|
|
return (select);
|
|
}
|
|
|
|
for (offset = 1; string [offset - 1] != '\0'; ++offset) {
|
|
if (string [offset] == syntax_escape [select]) {
|
|
++offset;
|
|
continue;
|
|
}
|
|
|
|
if (syntax_derange [select] == 0) {
|
|
if (strncmp (& string [offset], syntax_end [select], strlen (syntax_end [select])) == 0) {
|
|
* length = offset + (int) strlen (syntax_end [select]);
|
|
goto finished;
|
|
}
|
|
} else {
|
|
subset = 0;
|
|
if (strcmp (syntax_end [select], "") == 0) {
|
|
break;
|
|
} do {
|
|
if (string [offset] == syntax_end [select] [subset]) {
|
|
* length = offset;
|
|
goto finished;
|
|
}
|
|
} while (++subset != (int) strlen (syntax_end [select]));
|
|
}
|
|
}
|
|
|
|
finished:
|
|
|
|
return (select);
|
|
}
|
|
|
|
static void syntax_c (void) {
|
|
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
|
|
|
|
char * keywords [] = {
|
|
"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"
|
|
};
|
|
|
|
int word;
|
|
|
|
syntax_rule (0, 0, "/*", "*/", '\0', COLOUR_GREY);
|
|
syntax_rule (0, 0, "//", "\n", '\0', COLOUR_GREY);
|
|
syntax_rule (0, 0, "#", "\n", '\\', COLOUR_LMAO);
|
|
syntax_rule (0, 0, "'", "'", '\\', COLOUR_PINK);
|
|
syntax_rule (0, 0, "\"", "\"", '\\', COLOUR_PINK);
|
|
|
|
for (word = 0; word != sizeof (keywords) / sizeof (keywords [0]); ++word) {
|
|
syntax_rule (0, 1, keywords [word], separators, '\0', COLOUR_YELLOW);
|
|
}
|
|
|
|
syntax_rule (1, 0, "()[]{}", "", '\0', COLOUR_BLUE);
|
|
syntax_rule (1, 0, ".,:;<=>+*-/%!&~^?|", "", '\0', COLOUR_CYAN);
|
|
|
|
syntax_rule (1, 1, "0123456789", separators, '\0', COLOUR_PINK);
|
|
syntax_rule (1, 1, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE);
|
|
syntax_rule (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE);
|
|
syntax_rule (1, 1, "_", separators, '\0', COLOUR_WHITE);
|
|
}
|
|
|
|
static void render_c_code (char * string,
|
|
int x,
|
|
int y) {
|
|
int select, length = 0, offset, reset_x;
|
|
|
|
reset_x = x;
|
|
|
|
for (offset = 0; offset < (int) strlen (string); offset += length) {
|
|
char substring [1024] = "";
|
|
|
|
int i = 0, square = 16;
|
|
|
|
Color colour = RED;
|
|
|
|
select = syntax_loop (& string [offset], & length);
|
|
|
|
strncpy (substring, & string [offset], (unsigned long int) length);
|
|
|
|
switch (syntax_colour [select]) {
|
|
case COLOUR_GREY: colour = GRAY; break;
|
|
case COLOUR_WHITE: colour = WHITE; break;
|
|
case COLOUR_BLUE: colour = BLUE; break;
|
|
case COLOUR_YELLOW: colour = YELLOW; break;
|
|
case COLOUR_CYAN: colour = PURPLE; break;
|
|
case COLOUR_PINK: colour = RED; break;
|
|
case COLOUR_LMAO: colour = GREEN; break;
|
|
default: colour = GREEN; break;
|
|
}
|
|
|
|
for (i = 0; i < (int) strlen (substring); ++i) {
|
|
if (substring [i] == '\n') {
|
|
y += square;
|
|
x = reset_x;
|
|
} else if (substring [i] == '\t') {
|
|
x += square * 4;
|
|
} else {
|
|
DrawTextCodepoint (GetFontDefault (), substring [i], (Vector2) { x, y }, square, colour);
|
|
x += square;
|
|
}
|
|
}
|
|
|
|
if (y > 900) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif
|