90 lines
3.8 KiB
Markdown
90 lines
3.8 KiB
Markdown
# xyntax
|
|
|
|
xyntax -- Xolatile-style "header-only" library for syntax definition control.
|
|
|
|
- Primary focus of this library is for syntax highlighting, hence the name...
|
|
- Important note: Regular expressions are more robust, this is simple solution for simple problems.
|
|
- Everything related to my libraries is clean of all warning options on Clang, GCC and Valgrind.
|
|
|
|
Installing:
|
|
|
|
```bash
|
|
$ sudo sh install.sh
|
|
```
|
|
|
|
Using:
|
|
|
|
```c
|
|
#include <xolatile/xyntax.h>
|
|
#include <xolatile/xyntax.c> /* Instead of '#define BLA_BLA_IMPLEMENTATION'. */
|
|
...
|
|
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. */
|
|
...
|
|
syntax_delete ();
|
|
/* Free used memory, after this you can define some other syntax rules... */
|
|
```
|
|
|
|
Xolatile-style "header-only" library is my take on 'stb' header-only libraries. There are a lot of ideas that came from Ada, which is my second language.
|
|
Main idea behind them is to avoid standard library and macros in programs. Also, I like to avoid C-style "namespaces" and bad function names...
|
|
|
|
It can be used for:
|
|
- syntax highlighting in terminal or graphical text editors...
|
|
- source code processing, parsing, tokenization...
|
|
- counting source code elements such as keywords, literals, brackets...
|
|
|
|
For example, your can make simple ANSI C syntax highlight like this:
|
|
|
|
```c
|
|
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
|
|
|
|
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"
|
|
};
|
|
```
|
|
|
|
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_insert (0, 1, c_keywords [word], separator, '\0', COLOUR_BLUE, 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'...
|