Compare commits
5 Commits
1bcd8e0fb3
...
1c0387ad22
Author | SHA1 | Date | |
---|---|---|---|
1c0387ad22 | |||
d48b078d70 | |||
c37c2546e5 | |||
55ffd41892 | |||
dcdd3abb8c |
@ -32,9 +32,9 @@ Fit a specific token against a string. `render_string()` uses this function inte
|
|||||||
typedef void (*attribute_callback_t)(const char * const string, const int length, void * const attributes);
|
typedef void (*attribute_callback_t)(const char * const string, const int length, void * const attributes);
|
||||||
```
|
```
|
||||||
The type used for defining appropriate callbacks for render\_string().
|
The type used for defining appropriate callbacks for render\_string().
|
||||||
+ string - string to be outputed
|
+ string - string to be processed (probably printed)
|
||||||
+ length - number of characters that matched a highlighting rule
|
+ length - number of characters to be processed from _string_
|
||||||
+ attributes - arbitrary data associated with the matched rule; intended to hold color/font information for example
|
+ attributes - arbitrary data associated with the matched token; intended to hold color/font information for example; if no token was matched NULL will be passed
|
||||||
|
|
||||||
|
|
||||||
```C
|
```C
|
||||||
|
@ -12,12 +12,12 @@ const char * preprocessor_keywords[] = {
|
|||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
//new_char_tokens("+-&|.()[]{}", operator_hl);
|
new_char_tokens("+-&|.()[]{}", operator_hl);
|
||||||
//new_keyword_tokens(c_keywords, control_hl);
|
new_keyword_tokens(c_keywords, control_hl);
|
||||||
//new_keyword_tokens(preprocessor_keywords, special_hl);
|
new_keyword_tokens(preprocessor_keywords, special_hl);
|
||||||
//new_region_token("/\\*", "\\*/", comment_hl);
|
new_region_token("/\\*", "\\*/", comment_hl);
|
||||||
//new_region_token("//", "\\n", comment_hl);
|
new_region_token("//", "\\n", comment_hl);
|
||||||
//new_region_token("\"", "\"", string_literal_hl);
|
new_region_token("\"", "\"", string_literal_hl);
|
||||||
//new_region_token("<", ">", string_literal_hl);
|
new_region_token("<", ">", string_literal_hl);
|
||||||
//new_keyword_token("keyword", special_hl);
|
new_keyword_token("keyword", special_hl);
|
||||||
new_keyword_token("while", operator_hl);
|
new_keyword_token("while", operator_hl);
|
||||||
|
107
source/hl.c
107
source/hl.c
@ -184,51 +184,82 @@ int token_fits(const token_t * const token,
|
|||||||
const int string_offset,
|
const int string_offset,
|
||||||
const bool is_start_of_line,
|
const bool is_start_of_line,
|
||||||
int * match_offset) {
|
int * match_offset) {
|
||||||
|
return 0;
|
||||||
UNUSED(match_offset);
|
UNUSED(match_offset);
|
||||||
//return regex_match(pattern, to, string_offset, match_offset);
|
|
||||||
return (int)regex_match(token->syntax, to, is_start_of_line);
|
match_t * matches = regex_match(token->syntax, to, is_start_of_line);
|
||||||
|
|
||||||
|
if (matches->position == -1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int r = matches->width;
|
||||||
|
match_offset = matches->position;
|
||||||
|
|
||||||
|
free(matches);
|
||||||
|
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
void render_string(const char * const string,
|
void render_string(const char * const string,
|
||||||
const char * const mode) {
|
const char * const mode) {
|
||||||
for (const char * s = string; *s != '\00';) {
|
display_t * display;
|
||||||
int f = 0;
|
HASH_FIND_STR(display_table,
|
||||||
size_t token_index = 0;
|
mode,
|
||||||
int offset = 0;
|
display);
|
||||||
|
|
||||||
for (; token_index < token_table.element_count; token_index++) {
|
typedef struct {
|
||||||
token_t * t = *(token_t**)vector_get(&token_table,
|
const token_t * t;
|
||||||
token_index);
|
const match_t * m;
|
||||||
const bool is_start_of_line = (s == string) || (*s == '\n');
|
int i;
|
||||||
f = token_fits(t, string, (int)(s - string), is_start_of_line, &offset);
|
} result_t;
|
||||||
if (f) {
|
|
||||||
break;
|
result_t * const r = (result_t *)malloc(sizeof(result_t) * 1024); // XXX: dont
|
||||||
|
int rrs = 0;
|
||||||
|
for (int i = 0; i < token_table.element_count; i++) {
|
||||||
|
token_t * t = *(token_t**)vector_get(&token_table,
|
||||||
|
i);
|
||||||
|
match_t * match = regex_match(t->syntax, string, true);
|
||||||
|
if (match->position == -1) {
|
||||||
|
free(match);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
r[rrs++] = (result_t){
|
||||||
|
.t = t,
|
||||||
|
.m = match,
|
||||||
|
.i = 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const char * s = string; *s != '\00';) {
|
||||||
|
const result_t sentinel = (result_t){NULL, &(match_t){ -1, -1}, -1};
|
||||||
|
const result_t * max;
|
||||||
|
max = &sentinel;
|
||||||
|
for (int h = 0; h < rrs; h++) {
|
||||||
|
result_t * const current_result = r + h;
|
||||||
|
for (int j = 0; current_result->m[j].position != -1; j++) {
|
||||||
|
if (current_result->m[j].position == (s - string)) {
|
||||||
|
if (current_result->m[j].width > max->m->width) {
|
||||||
|
current_result->i = j;
|
||||||
|
max = current_result;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//
|
if (max != &sentinel) {
|
||||||
display_t * display;
|
const int padding = max->m[max->i].position - (s - string);
|
||||||
HASH_FIND_STR(display_table,
|
if (padding) {
|
||||||
mode,
|
display->callback(s,
|
||||||
display);
|
padding,
|
||||||
//
|
NULL);
|
||||||
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,
|
display->callback(s + padding,
|
||||||
token_index);
|
max->m->width,
|
||||||
display->callback(s + offset,
|
max->t->hl->attributes);
|
||||||
f,
|
s += padding + max->m->width;
|
||||||
t->hl->attributes);
|
|
||||||
s += f + offset;
|
|
||||||
} else {
|
} else {
|
||||||
display->callback(s,
|
display->callback(s, 1, NULL);
|
||||||
0,
|
|
||||||
NULL);
|
|
||||||
++s;
|
++s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -239,9 +270,9 @@ int hl_init(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int hl_deinit(void) {
|
int hl_deinit(void) {
|
||||||
for (size_t i = 0; i < token_table.element_count; i++) {
|
//for (size_t i = 0; i < token_table.element_count; i++) {
|
||||||
free_token(*(token_t**)vector_get(&token_table, i));
|
// free_token(*(token_t**)vector_get(&token_table, i));
|
||||||
}
|
//}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -119,7 +119,7 @@ main(int argc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
//hl_deinit();
|
hl_deinit();
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|
||||||
//terminal_hl_deinit();
|
//terminal_hl_deinit();
|
||||||
|
@ -16,11 +16,13 @@ void cterm_render_callback(const char * const string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
terminal_hl_t * term_hl = (terminal_hl_t*)attributes;
|
terminal_hl_t * term_hl = (terminal_hl_t*)attributes;
|
||||||
if (term_hl->attribute) {
|
if (term_hl) {
|
||||||
fputs(term_hl->attribute, stdout);
|
if (term_hl->attribute) {
|
||||||
}
|
fputs(term_hl->attribute, stdout);
|
||||||
if (term_hl->foreground_color) {
|
}
|
||||||
fputs(term_hl->foreground_color, stdout);
|
if (term_hl->foreground_color) {
|
||||||
|
fputs(term_hl->foreground_color, stdout);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
putchar(*(string+i));
|
putchar(*(string+i));
|
||||||
|
Loading…
Reference in New Issue
Block a user