I do not remember what I fixed yesterday...

This commit is contained in:
Ognjen Milan Robovic 2023-09-17 14:28:54 -04:00
parent 2d9b199962
commit ec24deb2c2
3 changed files with 52 additions and 29 deletions

View File

@ -16,10 +16,9 @@ Using:
```c
#include <xolatile/xyntax.h>
#include <xolatile/xyntax.c>
/* Instead of '#define BLA_BLA_IMPLEMENTATION'. */
#include <xolatile/xyntax.c> /* Instead of '#define BLA_BLA_IMPLEMENTATION'. */
...
int symbols = syntax_define (1, 0, ".,:;<=>+-*/%!&~^?|()[]{}", "", '\0', 0, 0);
int symbols = syntax_insert (1, 0, ".,:;<=>+-*/%!&~^?|()[]{}", "", '\0', 0, 0);
...
int select = syntax_select (& buffer [offset], & length);
/* Variable 'select' will become the index of syntax rule you defined previously, or 'syntax_count' if there is no match. */
@ -39,29 +38,52 @@ It can be used for:
For example, your can make simple ANSI C syntax highlight like this:
```c
char * separator = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
char * c_keywords [32] = {
char * keywords [32] = {
"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"
};
```
(void) syntax_define (1, 0, " \t\r\n", "", '\0', COLOUR_WHITE, EFFECT_NORMAL);
(void) syntax_define (0, 0, "#", "\n", '\\', COLOUR_PINK, EFFECT_BOLD);
(void) syntax_define (0, 0, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
(void) syntax_define (0, 0, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
(void) syntax_define (0, 0, "'", "'", '\\', COLOUR_RED, EFFECT_NORMAL);
(void) syntax_define (0, 0, "\"", "\"", '\\', COLOUR_RED, EFFECT_BOLD);
(void) syntax_define (1, 0, ".,:;<=>+-*/%!&~^?|()[]{}", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
With using helper 'syntax_define_*' functions:
```c
syntax_define_separators (separators);
syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
syntax_define_range ("/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
syntax_define_range ("//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
syntax_define_range ("#", "\n", '\\', COLOUR_PINK, EFFECT_NORMAL);
syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
syntax_define_words (keywords, 32, COLOUR_BLUE, EFFECT_NORMAL);
```
Or using core 'syntax_push' functions:
```c
(void) syntax_insert (1, 0, " \t\r\n", "", '\0', COLOUR_WHITE, EFFECT_NORMAL);
(void) syntax_insert (0, 0, "#", "\n", '\\', COLOUR_PINK, EFFECT_BOLD);
(void) syntax_insert (0, 0, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
(void) syntax_insert (0, 0, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
(void) syntax_insert (0, 0, "'", "'", '\\', COLOUR_RED, EFFECT_NORMAL);
(void) syntax_insert (0, 0, "\"", "\"", '\\', COLOUR_RED, EFFECT_BOLD);
(void) syntax_insert (1, 0, ".,:;<=>+-*/%!&~^?|()[]{}", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
for (word = 0; word != 32; ++word) {
(void) syntax_define (0, 1, c_keywords [word], separator, '\0', COLOUR_BLUE, EFFECT_BOLD);
(void) syntax_insert (0, 1, c_keywords [word], separator, '\0', COLOUR_BLUE, EFFECT_BOLD);
}
(void) syntax_define (1, 1, "0123456789", separator, '\0', COLOUR_CYAN, EFFECT_BOLD);
(void) syntax_define (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separator, '\0', COLOUR_PINK, EFFECT_ITALIC);
(void) syntax_define (1, 1, "abcdefghijklmnopqrstuvwxyz", separator, '\0', COLOUR_WHITE, EFFECT_ITALIC);
(void) syntax_define (0, 1, "_", separator, '\0', COLOUR_PINK, EFFECT_BOLD);
(void) syntax_insert (1, 1, "0123456789", separator, '\0', COLOUR_CYAN, EFFECT_BOLD);
(void) syntax_insert (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separator, '\0', COLOUR_PINK, EFFECT_ITALIC);
(void) syntax_insert (1, 1, "abcdefghijklmnopqrstuvwxyz", separator, '\0', COLOUR_WHITE, EFFECT_ITALIC);
(void) syntax_insert (0, 1, "_", separator, '\0', COLOUR_PINK, EFFECT_BOLD);
```
If you want to do parsing, counting, tokenization, you can use return value of 'syntax_push'...

View File

@ -21,7 +21,7 @@ int * syntax_colour = NULL;
int * syntax_effect = NULL;
char * syntax_separator = NULL;
int syntax_define (int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
int syntax_insert (int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
fatal_failure (begin == NULL, "syntax_define: Begin string is null pointer.");
fatal_failure (end == NULL, "syntax_define: End string is null pointer.");
@ -138,34 +138,36 @@ void syntax_delete (void) {
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_define (1, 0, " \t\r\n", "", '\0', COLOUR_WHITE, EFFECT_BOLD);
(void) syntax_define (0, 0, "\"", "\"", '\\', string_colour, string_effect);
(void) syntax_define (1, 1, "0123456789", syntax_separator, '\0', number_colour, 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);
}
void syntax_define_words (char * * word_array, int word_count, int word_colour, int word_effect) {
int word = 0;
for (word = 0; word != word_count; ++word) {
(void) syntax_define (0, 1, word_array [word], syntax_separator, '\0', word_colour, word_effect);
(void) syntax_insert (0, 1, word_array [word], syntax_separator, '\0', word_colour, word_effect);
}
(void) syntax_define (1, 1, "abcdefghijklmnopqrstuvwxyz", syntax_separator, '\0', COLOUR_WHITE, EFFECT_NORMAL);
(void) syntax_define (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", syntax_separator, '\0', COLOUR_WHITE, EFFECT_BOLD);
(void) syntax_define (1, 1, "_", syntax_separator, '\0', COLOUR_PINK, 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);
}
void syntax_define_range (char * range_begin, char * range_end, char range_escape, int range_colour, int range_effect) {
(void) syntax_define (0, 0, range_begin, range_end, range_escape, range_colour, 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_define (1, 0, operator_array, "", '\0', operator_colour, operator_effect);
(void) syntax_insert (1, 0, operator_array, "", '\0', operator_colour, operator_effect);
}
#endif

View File

@ -22,9 +22,8 @@ extern int * syntax_colour;
extern int * syntax_effect;
extern char * syntax_separator;
extern int syntax_styles;
extern int syntax_insert (int, int, char *, char *, char, int, int);
extern int syntax_define (int, int, char *, char *, char, int, int);
extern int syntax_select (char *, int *);
extern void syntax_delete (void);