Structural Repair

This commit is contained in:
Emil
2023-08-28 12:56:34 -06:00
parent ab00a0d894
commit 2eb38da4e6
16 changed files with 18 additions and 558 deletions

33
include/chad.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef CHAD_H
#include <stdbool.h>
#define UNUSED(x) ((void)x)
// Terminal manipulation
#define TERMINAL_RESET "\033[0m"
#define TERMINAL_COLOR_FG_BLACK "\033[30m"
#define TERMINAL_COLOR_FG_RED "\033[31m"
#define TERMINAL_COLOR_FG_GREEN "\033[32m"
#define TERMINAL_COLOR_FG_YELLOW "\033[33m"
#define TERMINAL_COLOR_FG_BLUE "\033[34m"
#define TERMINAL_COLOR_FG_MAGENTA "\033[35m"
#define TERMINAL_COLOR_FG_CYAN "\033[36m"
#define TERMINAL_COLOR_FG_WHITE "\033[37m"
#define TERMINAL_COLOR_BG_BLACK "\033[40m"
#define TERMINAL_COLOR_BG_RED "\033[41m"
#define TERMINAL_COLOR_BG_GREEN "\033[42m"
#define TERMINAL_COLOR_BG_YELLOW "\033[43m"
#define TERMINAL_COLOR_BG_BLUE "\033[44m"
#define TERMINAL_COLOR_BG_MAGENTA "\033[45m"
#define TERMINAL_COLOR_BG_CYAN "\033[46m"
#define TERMINAL_COLOR_BG_WHITE "\033[47m"
#define TERMINAL_STYLE_BOLD "\033[1m"
#define TERMINAL_STYLE_ITALICS "\033[3m"
#define TERMINAL_STYLE_REVERSE "\033[7m"
#define CHAD_H
#endif

323
include/hl.h Normal file
View File

@ -0,0 +1,323 @@
/* hl.h
* Copyright 2023 Anon Anonson, Ognjen 'xolatile' Milan Robovic, Emil Williams
* SPDX Identifier: GPL-3.0-only / NO WARRANTY / NO GUARANTEE */
#include <stdio.h>
#include <uthash.h>
#include <ctype.h>
#include <string.h>
#include "chad.h"
#include "regex.h"
// -------------------
// ### Definitions ###
// -------------------
typedef enum {
KEYSYMBOL,
KEYWORD,
MATCH,
REGION
} token_type_t;
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;
typedef struct {
void * attributes;
struct hl_group_t * link;
} hl_group_t;
typedef struct {
hl_group_t * hl;
regex_t * syntax;
token_type_t t;
char _pad[4];
} token_t;
extern vector_t token_table;
extern display_t * display_table;
extern hl_group_t * keyword_hl;
extern hl_group_t * preprocessor_hl;
extern hl_group_t * symbol_hl;
extern void new_display_mode(display_t * mode);
extern int free_token(token_t * token);
extern int append_token(token_t * token);
// TODO: ALIGN PROPERLY...
extern token_t * new_symbol_token(const char * const c,
hl_group_t * const g);
extern int new_symbol_tokens(const char * const * symbols,
hl_group_t * const g);
extern int new_char_tokens(const char * characters,
hl_group_t * const g);
extern token_t * new_keyword_token(const char * const word,
hl_group_t * const g);
extern int new_keyword_tokens(const char * const * words,
hl_group_t * const g);
extern token_t * new_token(const char * const word,
const token_type_t t,
hl_group_t * const g);
// TODO: ALIGN PROPERLY...
extern int token_fits(const token_t * const token,
const char * const to,
const int string_offset,
int * match_offset);
extern void render_string(const char * const string,
const char * const mode);
extern int hl_init(void);
extern int hl_deinit(void);
// GLOBALS
vector_t token_table = {
.data = NULL,
.element_size = sizeof(token_t *),
.element_count = 0UL
};
display_t * display_table = NULL;
// --------------------------------
// ### Constructors/Destructors ###
// --------------------------------
void new_display_mode(display_t * mode) {
HASH_ADD_STR(display_table,
key,
mode);
}
int free_token(token_t * token) {
free(token->hl);
regex_free(token->syntax);
return 0;
}
int append_token(token_t * token) {
vector_push(&token_table, &token);
return 0;
}
token_t * new_symbol_token(const char * const c,
hl_group_t * const g) {
token_t * mt = (token_t*)malloc(sizeof(token_t));
mt->hl = g;
mt->t = KEYSYMBOL;
mt->syntax = regex_compile(c);
append_token(mt);
return mt;
}
int new_symbol_tokens(const char * const * symbols,
hl_group_t * const g) {
int i = 0;
while (*symbols) {
if(new_symbol_token(*symbols, g)) {
++i;
} else {
assert(!(bool)"Kinda failed to new symbol token thing.");
}
++symbols;
}
return i;
}
int new_char_tokens(const char * characters,
hl_group_t * const g) {
int i = 0;
char buffer[3];
buffer[0] = '\\';
buffer[2] = '\0';
for(const char * s = characters; *s != '\0'; s++) {
buffer[1] = *s;
if(new_symbol_token(is_magic(*s) ? buffer : buffer + 1, g)) {
++i;
} else {
assert(!(bool)"Kinda failed to new char token thing.");
}
}
return i;
}
token_t * new_keyword_token(const char * const word,
hl_group_t * const g) {
//char * new_word = strdup(word);
//size_t word_length = strlen(word);
//char * new_word = (char*)malloc(word_length + 4 + 1);
//memcpy(new_word, "\\<", 2);
//memcpy(new_word + 2, word, word_length);
//strcpy(new_word + 2 + word_length, "\\>");
token_t * mt = (token_t*)malloc(sizeof(token_t));
mt->hl = g;
mt->t = KEYWORD;
//mt->syntax = regex_compile(new_word);
mt->syntax = regex_compile(word);
append_token(mt);
return mt;
}
int new_keyword_tokens(const char * const * words,
hl_group_t * const g) {
int i = 0;
while (*words) {
if(new_keyword_token(*words, g)) {
++i;
}
++words;
}
return i;
}
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;
}
token_t * new_token(const char * const word,
const token_type_t t,
hl_group_t * const g) {
switch (t) {
case KEYSYMBOL: {
return new_symbol_token(word, g);
}
case KEYWORD: {
return new_keyword_token(word, g);
}
case MATCH: {
} break;
case REGION: {
} break;
}
return NULL;
}
// --------------------
// ### Highlighting ###
// --------------------
int token_fits(const token_t * const token,
const char * const to,
const int string_offset,
int * match_offset) {
UNUSED(match_offset);
//return regex_match(pattern, to, string_offset, match_offset);
return regex_match(token->syntax, to + string_offset);
}
void render_string(const char * const string,
const char * const mode) {
for (const char * s = string; *s != '\00';) {
int f = 0;
size_t token_index = 0;
int offset = 0;
for (; token_index < token_table.element_count; token_index++) {
token_t * t = *(token_t**)vector_get(&token_table,
token_index);
f = token_fits(t, string, (int) (s - string), &offset);
if (f) {
break;
}
}
//
display_t * display;
HASH_FIND_STR(display_table,
mode,
display);
//
if (f) {
for (int i = 0; i < offset; i++) {
token_t * t = *(token_t**)vector_get(&token_table,
token_index);
display->callback(s + i,
0,
t->hl->attributes);
}
token_t * t = *(token_t**)vector_get(&token_table,
token_index);
display->callback(s + offset,
f,
t->hl->attributes);
s += f + offset;
} else {
display->callback(s,
0,
NULL);
++s;
}
}
}
// -------------------------
// ### Library Mangement ###
// -------------------------
hl_group_t * special_hl = NULL;
hl_group_t * control_hl = NULL;
hl_group_t * keyword_hl = NULL;
hl_group_t * block_hl = NULL;
hl_group_t * separator_hl = NULL;
hl_group_t * operator_hl = NULL;
hl_group_t * comment_hl = NULL;
hl_group_t * string_literal_hl = NULL;
int hl_init(void) {
return 0;
}
int hl_deinit(void) {
for (size_t i = 0; i < token_table.element_count; i++) {
free_token(*(token_t**)vector_get(&token_table, i));
}
return 0;
}

23
include/regex.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef REGEX_H
#define REGEX_H
#include "chad.h"
#include "vector.h"
extern bool is_case_on;
typedef struct {
int accepting_state;
char * str;
vector_t delta_table; // <delta_t>
vector_t catch_table; // <offshoot_t>
} regex_t;
extern regex_t * regex_compile(const char * const pattern);
extern int regex_free(regex_t * const regex);
extern bool regex_search(regex_t * regex, const char * const string);
extern int regex_match(regex_t * regex, const char * const string);
extern bool is_magic(const char c);
#endif

70
include/terminal_hl.h Normal file
View File

@ -0,0 +1,70 @@
#include "hl.h"
typedef struct {
char * attribute;
char * foreground_color;
char * background_color;
} terminal_hl_t;
extern display_t * cterm;
extern void cterm_render_callback(const char * const string,
const int length,
void * const attributes);
extern int terminal_hl_init(void);
display_t * cterm = &(display_t) {
.key = "cterm",
.callback = cterm_render_callback
};
void cterm_render_callback(const char * const string,
const int length,
void * const attributes) {
if(!length){
fputs(TERMINAL_STYLE_BOLD, stdout);
putchar(*string);
fputs(TERMINAL_RESET, stdout);
return;
}
terminal_hl_t * term_hl = (terminal_hl_t*)attributes;
if (term_hl->attribute) {
fputs(term_hl->attribute, stdout);
}
if (term_hl->foreground_color) {
fputs(term_hl->foreground_color, stdout);
}
for (int i = 0; i < length; i++) {
putchar(*(string+i));
}
fputs(TERMINAL_RESET, stdout);
}
void fun(const char * const attribute,
const char * const color,
hl_group_t * * group){
terminal_hl_t * t = (terminal_hl_t *)malloc(sizeof(terminal_hl_t));
t->attribute = attribute;
t->foreground_color = color;;
t->background_color = NULL;
(*group) = (hl_group_t *)malloc(sizeof(hl_group_t));
(*group)->link = NULL;
(*group)->attributes = (void*)t;
}
int terminal_hl_init(void) {
hl_init();
new_display_mode(cterm);
//
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_CYAN, &special_hl);
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_YELLOW, &control_hl);
fun(NULL, TERMINAL_COLOR_FG_YELLOW, &operator_hl);
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_GREEN, &keyword_hl);
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_BLUE, &comment_hl);
fun(TERMINAL_STYLE_BOLD, TERMINAL_COLOR_FG_RED, &string_literal_hl);
return 0;
}

36
include/vector.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef VECTOR_H
#define VECTOR_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
// TODO: Handle error warnings?
// TODO: Implement more useful functions?
typedef struct {
char * data;
size_t element_size;
size_t element_count;
} vector_t;
extern void vector_init(vector_t * vector,
size_t element_size,
size_t element_count);
extern void vector_push(vector_t * vector,
void * data);
extern void vector_pop(vector_t * vector);
extern void * vector_get(const vector_t * const vector,
const size_t element);
extern void vector_set(vector_t * vector,
void * data,
size_t element);
extern void vector_free(vector_t * vector);
#endif