2023-08-24 06:24:46 -04:00
|
|
|
/* hl.h
|
|
|
|
* Copyright 2023 Anon Anonson, Ognjen 'xolatile' Milan Robovic, Emil Williams
|
|
|
|
* SPDX Identifier: GPL-3.0-only / NO WARRANTY / NO GUARANTEE */
|
|
|
|
|
2023-08-19 07:18:34 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <uthash.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "chad.h"
|
2023-08-19 18:49:10 -04:00
|
|
|
#include "regex.h"
|
2023-08-19 07:18:34 -04:00
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
// -------------------
|
|
|
|
// ### Definitions ###
|
|
|
|
// -------------------
|
2023-08-19 07:18:34 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-08-23 21:37:40 -04:00
|
|
|
typedef void (*attribute_callback_t) (const char * const string,
|
|
|
|
const int length,
|
|
|
|
void * const attributes);
|
|
|
|
|
2023-08-19 07:18:34 -04:00
|
|
|
typedef struct {
|
2023-08-23 19:58:38 -04:00
|
|
|
char * key;
|
|
|
|
attribute_callback_t callback;
|
|
|
|
UT_hash_handle hh;
|
|
|
|
} display_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void * attributes;
|
|
|
|
struct hl_group_t * link;
|
|
|
|
} hl_group_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
hl_group_t * hl;
|
|
|
|
token_type_t t;
|
2023-08-23 22:11:46 -04:00
|
|
|
regex_t * syntax;
|
2023-08-19 14:47:42 -04:00
|
|
|
} token_t;
|
2023-08-19 07:18:34 -04:00
|
|
|
|
2023-08-23 19:58:38 -04:00
|
|
|
// GLOBALS
|
|
|
|
|
2023-08-24 08:58:51 -04:00
|
|
|
vector_t token_table = {
|
|
|
|
.data = NULL,
|
|
|
|
.element_size = sizeof(token_t *),
|
|
|
|
.element_count = 0UL
|
|
|
|
};
|
2023-08-19 07:18:34 -04:00
|
|
|
|
2023-08-23 19:58:38 -04:00
|
|
|
display_t * display_table = NULL;
|
2023-08-21 10:13:24 -04:00
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
// ### Constructors/Destructors ###
|
|
|
|
// --------------------------------
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
void new_display_mode(display_t * mode) {
|
|
|
|
HASH_ADD_STR(display_table,
|
|
|
|
key,
|
|
|
|
mode);
|
|
|
|
}
|
|
|
|
|
2023-08-23 19:58:38 -04:00
|
|
|
int free_token(token_t * token) {
|
2023-08-21 10:13:24 -04:00
|
|
|
free(token->hl);
|
2023-08-23 22:11:46 -04:00
|
|
|
regex_free(token->syntax);
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-23 19:58:38 -04:00
|
|
|
int append_token(token_t * token) {
|
2023-08-25 15:06:26 -04:00
|
|
|
vector_push(&token_table, &token);
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-23 22:11:46 -04:00
|
|
|
token_t * new_symbol_token(const char * const c,
|
|
|
|
hl_group_t * const g) {
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-19 14:47:42 -04:00
|
|
|
token_t * mt = (token_t*)malloc(sizeof(token_t));
|
2023-08-23 19:58:38 -04:00
|
|
|
|
|
|
|
mt->hl = g;
|
|
|
|
mt->t = KEYSYMBOL;
|
2023-08-24 15:07:46 -04:00
|
|
|
mt->syntax = regex_compile(c);
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
append_token(mt);
|
|
|
|
|
2023-08-23 19:58:38 -04:00
|
|
|
return mt;
|
2023-08-19 07:18:34 -04:00
|
|
|
}
|
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
int new_symbol_tokens(const char * const * symbols,
|
|
|
|
hl_group_t * const g) {
|
2023-08-19 14:47:42 -04:00
|
|
|
int i = 0;
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
while (*symbols) {
|
2023-08-23 19:58:38 -04:00
|
|
|
if(new_symbol_token(*symbols, g)) {
|
2023-08-19 14:47:42 -04:00
|
|
|
++i;
|
2023-08-25 13:44:07 -04:00
|
|
|
} else {
|
|
|
|
assert(!"Kinda failed to new symbol token thing.");
|
2023-08-19 14:47:42 -04:00
|
|
|
}
|
2023-08-19 18:49:10 -04:00
|
|
|
++symbols;
|
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
|
|
|
}
|
|
|
|
|
2023-08-21 14:07:39 -04:00
|
|
|
int new_char_tokens(const char * characters,
|
|
|
|
hl_group_t * const g) {
|
2023-08-24 15:07:46 -04:00
|
|
|
int i = 0;
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-24 15:07:46 -04:00
|
|
|
char buffer[3];
|
|
|
|
buffer[0] = '\\';
|
|
|
|
buffer[2] = '\0';
|
2023-08-23 19:58:38 -04:00
|
|
|
|
|
|
|
for(const char * s = characters; *s != '\0'; s++) {
|
2023-08-24 15:07:46 -04:00
|
|
|
buffer[1] = *s;
|
|
|
|
if(new_symbol_token(is_magic(*s) ? buffer : buffer + 1, g)) {
|
2023-08-19 18:49:10 -04:00
|
|
|
++i;
|
2023-08-25 13:44:07 -04:00
|
|
|
} else {
|
|
|
|
assert(!"Kinda failed to new char token thing.");
|
2023-08-19 07:18:34 -04:00
|
|
|
}
|
2023-08-19 18:49:10 -04:00
|
|
|
}
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
token_t * new_keyword_token(const char * const word,
|
|
|
|
hl_group_t * const g) {
|
2023-08-24 15:07:46 -04:00
|
|
|
//char * new_word = strdup(word);
|
2023-08-24 13:17:14 -04:00
|
|
|
//size_t word_length = strlen(word);
|
|
|
|
//char * new_word = (char*)malloc(word_length + 4 + 1);
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-24 13:17:14 -04:00
|
|
|
//memcpy(new_word, "\\<", 2);
|
|
|
|
//memcpy(new_word + 2, word, word_length);
|
|
|
|
//strcpy(new_word + 2 + word_length, "\\>");
|
2023-08-19 18:49:10 -04:00
|
|
|
|
|
|
|
token_t * mt = (token_t*)malloc(sizeof(token_t));
|
2023-08-23 19:58:38 -04:00
|
|
|
|
|
|
|
mt->hl = g;
|
|
|
|
mt->t = KEYWORD;
|
2023-08-24 15:07:46 -04:00
|
|
|
//mt->syntax = regex_compile(new_word);
|
|
|
|
mt->syntax = regex_compile(word);
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
append_token(mt);
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
return mt;
|
|
|
|
}
|
|
|
|
|
2023-08-23 19:58:38 -04:00
|
|
|
int new_keyword_tokens(const char * const * words,
|
|
|
|
hl_group_t * const g) {
|
2023-08-21 10:13:24 -04:00
|
|
|
int i = 0;
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
while (*words) {
|
2023-08-23 19:58:38 -04:00
|
|
|
if(new_keyword_token(*words, g)) {
|
2023-08-21 10:13:24 -04:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
++words;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2023-08-28 09:39:16 -04:00
|
|
|
token_t * new_region_token(const char * const * start,
|
|
|
|
const char * const * end,
|
|
|
|
hl_group_t * const g) {
|
|
|
|
char buffer[100];
|
|
|
|
buffer[0] = '\0';
|
|
|
|
strcat(buffer, start);
|
|
|
|
strcat(buffer, "[\\d\\D]*");
|
|
|
|
strcat(buffer, end);
|
|
|
|
|
|
|
|
token_t * mt = (token_t*)malloc(sizeof(token_t));
|
|
|
|
|
|
|
|
mt->hl = g;
|
|
|
|
mt->t = KEYSYMBOL;
|
|
|
|
mt->syntax = regex_compile(buffer);
|
|
|
|
|
|
|
|
append_token(mt);
|
|
|
|
|
|
|
|
return mt;
|
|
|
|
}
|
|
|
|
|
2023-08-19 18:49:10 -04:00
|
|
|
token_t * new_token(const char * const word,
|
|
|
|
const token_type_t t,
|
|
|
|
hl_group_t * const g) {
|
2023-08-23 19:58:38 -04:00
|
|
|
switch (t) {
|
2023-08-19 18:49:10 -04:00
|
|
|
case KEYSYMBOL: {
|
|
|
|
return new_symbol_token(word, g);
|
2023-08-23 19:58:38 -04:00
|
|
|
}
|
2023-08-19 18:49:10 -04:00
|
|
|
case KEYWORD: {
|
|
|
|
return new_keyword_token(word, g);
|
2023-08-23 19:58:38 -04:00
|
|
|
}
|
2023-08-19 18:49:10 -04:00
|
|
|
case MATCH: {
|
|
|
|
} break;
|
|
|
|
case REGION: {
|
|
|
|
} break;
|
|
|
|
}
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
return NULL;
|
2023-08-19 18:49:10 -04:00
|
|
|
}
|
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
// --------------------
|
|
|
|
// ### Highlighting ###
|
|
|
|
// --------------------
|
2023-08-19 07:18:34 -04:00
|
|
|
|
2023-08-23 19:58:38 -04:00
|
|
|
int token_fits(const token_t * const token,
|
|
|
|
const char * const to,
|
|
|
|
const int string_offset,
|
2023-08-23 22:11:46 -04:00
|
|
|
int * match_offset) {
|
2023-08-26 16:39:07 -04:00
|
|
|
UNUSED(match_offset);
|
2023-08-19 18:49:10 -04:00
|
|
|
|
2023-08-23 21:15:13 -04:00
|
|
|
//return regex_match(pattern, to, string_offset, match_offset);
|
2023-08-26 16:39:07 -04:00
|
|
|
return regex_match(token->syntax, to + string_offset);
|
2023-08-19 07:18:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void render_string(const char * const string,
|
2023-08-23 19:58:38 -04:00
|
|
|
const char * const mode) {
|
2023-08-19 07:18:34 -04:00
|
|
|
for (const char * s = string; *s != '\00';) {
|
2023-08-25 13:44:07 -04:00
|
|
|
int f = 0;
|
2023-08-24 15:07:46 -04:00
|
|
|
size_t token_index = 0;
|
2023-08-25 13:44:07 -04:00
|
|
|
int offset = 0;
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-24 08:58:51 -04:00
|
|
|
for (; token_index < token_table.element_count; token_index++) {
|
2023-08-25 15:06:26 -04:00
|
|
|
token_t * t = *(token_t**)vector_get(&token_table,
|
|
|
|
token_index);
|
2023-08-24 08:58:51 -04:00
|
|
|
f = token_fits(t, string, s - string, &offset);
|
2023-08-23 19:58:38 -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-23 19:58:38 -04:00
|
|
|
for (int i = 0; i < offset; i++) {
|
2023-08-25 15:06:26 -04:00
|
|
|
token_t * t = *(token_t**)vector_get(&token_table,
|
|
|
|
token_index);
|
2023-08-21 14:07:39 -04:00
|
|
|
display->callback(s + i,
|
2023-08-23 19:58:38 -04:00
|
|
|
0,
|
2023-08-24 08:58:51 -04:00
|
|
|
t->hl->attributes);
|
2023-08-21 14:07:39 -04:00
|
|
|
}
|
2023-08-25 15:06:26 -04:00
|
|
|
token_t * t = *(token_t**)vector_get(&token_table,
|
|
|
|
token_index);
|
2023-08-21 14:07:39 -04:00
|
|
|
display->callback(s + offset,
|
2023-08-19 14:47:42 -04:00
|
|
|
f,
|
2023-08-24 08:58:51 -04:00
|
|
|
t->hl->attributes);
|
2023-08-21 14:07:39 -04:00
|
|
|
s += f + offset;
|
2023-08-19 07:18:34 -04:00
|
|
|
} 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
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
// -------------------------
|
|
|
|
// ### Library Mangement ###
|
|
|
|
// -------------------------
|
2023-08-23 21:37:40 -04:00
|
|
|
hl_group_t * keyword_hl = NULL;
|
|
|
|
hl_group_t * preprocessor_hl = NULL;
|
|
|
|
hl_group_t * symbol_hl = NULL;
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
int hl_init(void) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hl_deinit(void) {
|
2023-08-24 15:07:46 -04:00
|
|
|
for (size_t i = 0; i < token_table.element_count; i++) {
|
2023-08-25 15:06:26 -04:00
|
|
|
free_token(*(token_t**)vector_get(&token_table, i));
|
2023-08-21 10:13:24 -04:00
|
|
|
}
|
2023-08-23 19:58:38 -04:00
|
|
|
|
2023-08-21 10:13:24 -04:00
|
|
|
return 0;
|
2023-08-19 14:47:42 -04:00
|
|
|
}
|