107 lines
2.1 KiB
C
107 lines
2.1 KiB
C
|
#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 {
|
||
|
KEYWORD,
|
||
|
MATCH,
|
||
|
REGION
|
||
|
} token_t;
|
||
|
|
||
|
typedef struct {
|
||
|
hl_group_t * hl;
|
||
|
token_t t;
|
||
|
char* syntax;
|
||
|
} token; // XXX: this will have to be renamed
|
||
|
|
||
|
/* Temp solution
|
||
|
*/
|
||
|
token * token_table[1000];
|
||
|
int token_table_top = 0;
|
||
|
|
||
|
token * new_token(const char * const syntax,
|
||
|
const token_t t,
|
||
|
const hl_group_t * const g) {
|
||
|
token * mt = (token*)malloc(sizeof(token));
|
||
|
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) {
|
||
|
while (*words) {
|
||
|
new_token(*words, KEYWORD, g);
|
||
|
words = words + 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int token_fits(const char* const pattern,
|
||
|
const char* const to) {
|
||
|
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);
|
||
|
if(f){ break; };
|
||
|
}
|
||
|
//
|
||
|
display_t * display;
|
||
|
HASH_FIND_STR(display_table, mode, display);
|
||
|
//
|
||
|
if(f){
|
||
|
display->callback(s, f, token_table[i]->hl->attributes);
|
||
|
s += f;
|
||
|
} else {
|
||
|
display->callback(s, 0, NULL);
|
||
|
++s;
|
||
|
}
|
||
|
}
|
||
|
}
|