2023-08-22 04:59:25 -04:00
# xyntax
2023-09-12 06:38:58 -04:00
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.
2023-11-18 17:42:58 -05:00
Compile:
```bash
sh compile.sh
```
Install:
2023-09-12 06:38:58 -04:00
```bash
$ sudo sh install.sh
```
2023-11-18 17:42:58 -05:00
Usage:
2023-09-12 06:38:58 -04:00
```c
#include <xolatile/xyntax.h>
2023-11-18 17:42:58 -05:00
#include <xolatile/xyntax.c> /* Instead of '#define BLA_BLA_IMPLEMENTATION' if you want to compile it all together. */
2023-09-12 06:38:58 -04:00
...
2023-11-18 17:42:58 -05:00
int symbols = syntax_define (1, 0, ".,:;< =>+-*/%!& ~^?|()[]{}", "", '\0', 0, 0);
/* Variable 'symbols' will become the index of current 'syntax_count', and you can use it to count elements or select them without null-termination. */
2023-09-12 06:38:58 -04:00
...
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. */
```
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
2023-09-17 14:28:54 -04:00
char * separators = ".,:;<=>+-* /%!& ~^?|()[]{}'\" \t\r\n";
2023-09-12 06:38:58 -04:00
2023-11-18 17:42:58 -05:00
char * keywords [] = {
2023-09-12 06:38:58 -04:00
"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"
};
2023-09-17 14:28:54 -04:00
2023-11-18 17:42:58 -05:00
int word;
2023-09-17 14:28:54 -04:00
2023-11-18 17:42:58 -05:00
(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, "'", "'", '\0', COLOUR_RED, EFFECT_NORMAL);
(void) syntax_define (0, 0, "\"", "\"", '\0', COLOUR_RED, EFFECT_BOLD);
(void) syntax_define (1, 0, ".,:;< =>+-*/%!& ~^?|()[]{}", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
2023-09-12 06:38:58 -04:00
2023-11-18 17:42:58 -05:00
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
(void) syntax_define (0, 1, keywords [word], separators, '\0', COLOUR_BLUE, EFFECT_BOLD);
2023-09-12 06:38:58 -04:00
}
2023-11-18 17:42:58 -05:00
(void) syntax_define (1, 1, "0123456789", separators, '\0', COLOUR_CYAN, EFFECT_BOLD);
(void) syntax_define (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_PINK, EFFECT_ITALIC);
(void) syntax_define (1, 1, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
(void) syntax_define (0, 1, "_", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
2023-09-12 06:38:58 -04:00
```
2023-09-17 14:28:54 -04:00
2023-11-18 17:42:58 -05:00
If you want to do parsing, counting, tokenization, you can use return value of 'syntax_define'...