Read me not and ignore me please...
This commit is contained in:
parent
5e706af311
commit
ef2cf04a3e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
xyntax.o
|
48
README.md
48
README.md
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
xyntax -- Xolatile-style "header-only" library for syntax definition control.
|
xyntax -- Xolatile-style "header-only" library for syntax definition control.
|
||||||
|
|
||||||
- Primary focus of this library is for syntax highlighting, hence the name...
|
- Primary focus of this library is for syntax highlighting, hence the name, but it can do more...
|
||||||
- Important note: Regular expressions are more robust, this is simple solution for simple problems.
|
- 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.
|
- Everything related to my libraries is clean of all warning options on Clang, GCC and Valgrind.
|
||||||
|
|
||||||
@ -21,53 +21,57 @@ $ sudo sh install.sh
|
|||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#include <xolatile/xyntax.h>
|
#include <xolatile/xyntax.h> /* Or: */
|
||||||
#include <xolatile/xyntax.c> /* Instead of '#define BLA_BLA_IMPLEMENTATION' if you want to compile it all together. */
|
#include <xolatile/xyntax.c> /* Instead of '#define BLA_BLA_IMPLEMENTATION' if you want to compile it all together. */
|
||||||
...
|
...
|
||||||
int symbols = syntax_define (1, 0, ".,:;<=>+-*/%!&~^?|()[]{}", "", '\0', 0, 0);
|
int symbols = syntax_define (true, false, ".,:;<=>+-*/%!&~^?|()[]{}", "", '\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. */
|
/* Variable 'symbols' will become the index of current 'syntax_count', and you can use it to count elements or select them without null-termination. */
|
||||||
...
|
...
|
||||||
int select = syntax_select (& buffer [offset], & length);
|
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. */
|
/* Variable 'select' will become the index of syntax rule you defined previously, or 'syntax_count' if there is no match. */
|
||||||
|
/* It's important to note that for performance reasons I'm not returning a string or structure, but index and length of the token. */
|
||||||
```
|
```
|
||||||
|
|
||||||
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:
|
It can be used for:
|
||||||
- syntax highlighting in terminal or graphical text editors...
|
- syntax highlighting in terminal or graphical text editors...
|
||||||
- source code processing, parsing, tokenization...
|
- source code processing, parsing, tokenization...
|
||||||
- counting source code elements such as keywords, literals, brackets...
|
- counting source code elements such as keywords, literals, brackets...
|
||||||
|
|
||||||
For example, your can make simple ANSI C syntax highlight like this:
|
For example, your can define simple ANSI C syntax highlight like this:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
|
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
|
||||||
|
|
||||||
char * keywords [] = {
|
char * keywords [] = {
|
||||||
"register", "volatile", "auto", "const", "static", "extern", "if", "else",
|
"register", "volatile", "auto", "const",
|
||||||
"do", "while", "for", "continue", "switch", "case", "default", "break",
|
"static", "extern", "if", "else",
|
||||||
"enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
|
"do", "while", "for", "continue",
|
||||||
"char", "short", "int", "long", "signed", "unsigned", "float", "double"
|
"switch", "case", "default", "break",
|
||||||
|
"enum", "union", "struct", "typedef",
|
||||||
|
"goto", "void", "return", "sizeof",
|
||||||
|
"char", "short", "int", "long",
|
||||||
|
"signed", "unsigned", "float", "double"
|
||||||
};
|
};
|
||||||
|
|
||||||
int word;
|
int word;
|
||||||
|
|
||||||
(void) syntax_define (0, 0, "#", "\n", '\\', COLOUR_PINK, EFFECT_BOLD);
|
syntax_define (false, false, "/*", "*/", '\0', colour_grey, effect_bold);
|
||||||
(void) syntax_define (0, 0, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
|
syntax_define (false, false, "//", "\n", '\0', colour_grey, effect_bold);
|
||||||
(void) syntax_define (0, 0, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
|
syntax_define (false, false, "#", "\n", '\\', colour_yellow, effect_italic);
|
||||||
(void) syntax_define (0, 0, "'", "'", '\0', COLOUR_RED, EFFECT_NORMAL);
|
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
|
||||||
(void) syntax_define (0, 0, "\"", "\"", '\0', COLOUR_RED, EFFECT_BOLD);
|
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
|
||||||
(void) syntax_define (1, 0, ".,:;<=>+-*/%!&~^?|()[]{}", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
|
|
||||||
|
|
||||||
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
|
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
|
||||||
(void) syntax_define (0, 1, keywords [word], separators, '\0', COLOUR_BLUE, EFFECT_BOLD);
|
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
|
||||||
}
|
}
|
||||||
|
|
||||||
(void) syntax_define (1, 1, "0123456789", separators, '\0', COLOUR_CYAN, EFFECT_BOLD);
|
syntax_define (true, false, "()[]{}", "", '\0', colour_blue, effect_normal);
|
||||||
(void) syntax_define (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_PINK, EFFECT_ITALIC);
|
syntax_define (true, false, ".,:;<=>+*-/%!&~^?|", "", '\0', colour_cyan, effect_normal);
|
||||||
(void) syntax_define (1, 1, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
|
|
||||||
(void) syntax_define (0, 1, "_", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
|
syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
|
||||||
|
syntax_define (true, true, "abcdefghijklmnopqrstuvwxyz", separators, '\0', colour_white, effect_normal);
|
||||||
|
syntax_define (true, true, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', colour_white, effect_bold);
|
||||||
|
syntax_define (true, true, "_", separators, '\0', colour_white, effect_italic);
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to do parsing, counting, tokenization, you can use return value of 'syntax_define'...
|
If you want to do parsing, counting, tokenization, you can use return value of 'syntax_define'...
|
||||||
|
Loading…
Reference in New Issue
Block a user