2023-08-19 07:18:34 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <uthash.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "chad.h"
|
|
|
|
|
|
|
|
typedef void (*attribute_callback_t)(const char * const string,
|
|
|
|
const int length,
|
|
|
|
void * const attributes);
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char * key;
|
|
|
|
attribute_callback_t callback;
|
|
|
|
UT_hash_handle hh;
|
|
|
|
} display_t;
|
|
|
|
display_t * display_table = NULL;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void * attributes;
|
|
|
|
struct hl_group_t * link;
|
|
|
|
} hl_group_t;
|
|
|
|
|
|
|
|
typedef enum {
|
2023-08-19 14:47:42 -04:00
|
|
|
KEYSYMBOL,
|
2023-08-19 07:18:34 -04:00
|
|
|
KEYWORD,
|
|
|
|
MATCH,
|
|
|
|
REGION
|
2023-08-19 14:47:42 -04:00
|
|
|
} token_type_t;
|
2023-08-19 07:18:34 -04:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
hl_group_t * hl;
|
2023-08-19 14:47:42 -04:00
|
|
|
token_type_t t;
|
2023-08-19 07:18:34 -04:00
|
|
|
char* syntax;
|
2023-08-19 14:47:42 -04:00
|
|
|
} token_t;
|
2023-08-19 07:18:34 -04:00
|
|
|
|
|
|
|
/* Temp solution
|
2023-08-19 14:47:42 -04:00
|
|
|
* this should be dynamic
|
2023-08-19 07:18:34 -04:00
|
|
|
*/
|
2023-08-19 14:47:42 -04:00
|
|
|
token_t * token_table[1000];
|
2023-08-19 07:18:34 -04:00
|
|
|
int token_table_top = 0;
|
|
|
|
|
2023-08-19 14:47:42 -04:00
|
|
|
token_t * new_token(const char * const syntax,
|
|
|
|
const token_type_t t,
|
2023-08-19 07:21:43 -04:00
|
|
|
const hl_group_t * const g) {
|
2023-08-19 14:47:42 -04:00
|
|
|
token_t * mt = (token_t*)malloc(sizeof(token_t));
|
2023-08-19 07:18:34 -04:00
|
|
|
mt->hl = g;
|
|
|
|
mt->t = t;
|
|
|
|
mt->syntax = syntax;
|
|
|
|
token_table[token_table_top++] = mt;
|
|
|
|
return mt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void new_keyword_tokens(const char * const * words,
|
|
|
|
hl_group_t * const g) {
|
2023-08-19 14:47:42 -04:00
|
|
|
int i = 0;
|
2023-08-19 07:18:34 -04:00
|
|
|
while (*words) {
|
2023-08-19 14:47:42 -04:00
|
|
|
if(new_token(*words, KEYWORD, g)){
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
++words;
|
2023-08-19 07:18:34 -04:00
|
|
|
}
|
2023-08-19 14:47:42 -04:00
|
|
|
|
|
|
|
return i;
|
2023-08-19 07:18:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int token_fits(const char* const pattern,
|
2023-08-19 07:21:43 -04:00
|
|
|
const char* const to) {
|
2023-08-19 07:18:34 -04:00
|
|
|
if (pattern == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (int i = 0;; i++) {
|
|
|
|
if (pattern[i] == '\00') {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (to[i] == '\00'
|
|
|
|
|| pattern[i] != to[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_word_separator(const char character) {
|
|
|
|
if (( isascii(character))
|
|
|
|
&& (!isalnum(character))
|
|
|
|
&& ( character != '_')) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void render_string(const char * const string,
|
|
|
|
const char * const mode) {
|
|
|
|
for (const char * s = string; *s != '\00';) {
|
|
|
|
int f;
|
|
|
|
int i = 0;
|
|
|
|
for (; i < token_table_top; i++) {
|
|
|
|
f = token_fits(token_table[i]->syntax, s);
|
2023-08-19 14:47:42 -04:00
|
|
|
if(f){ break; }
|
2023-08-19 07:18:34 -04:00
|
|
|
}
|
|
|
|
//
|
|
|
|
display_t * display;
|
2023-08-19 14:47:42 -04:00
|
|
|
HASH_FIND_STR(display_table,
|
|
|
|
mode,
|
|
|
|
display);
|
2023-08-19 07:18:34 -04:00
|
|
|
//
|
2023-08-19 07:21:43 -04:00
|
|
|
if (f) {
|
2023-08-19 14:47:42 -04:00
|
|
|
display->callback(s,
|
|
|
|
f,
|
|
|
|
token_table[i]->hl->attributes);
|
2023-08-19 07:18:34 -04:00
|
|
|
s += f;
|
|
|
|
} else {
|
2023-08-19 14:47:42 -04:00
|
|
|
display->callback(s,
|
|
|
|
0,
|
|
|
|
NULL);
|
2023-08-19 07:18:34 -04:00
|
|
|
++s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-19 14:47:42 -04:00
|
|
|
|
|
|
|
void new_display_mode(display_t * mode) {
|
|
|
|
HASH_ADD_STR(display_table,
|
|
|
|
key,
|
|
|
|
mode);
|
|
|
|
}
|