the implemented parts of regex werk

This commit is contained in:
anon 2023-08-21 20:07:39 +02:00
parent 8708016752
commit 385a8f9818
5 changed files with 50 additions and 25 deletions

View File

@ -5,6 +5,7 @@ ARGS:=${TARGET} < source/main.c
GCC:=gcc GCC:=gcc
D.versions:=-D_XOPEN_SOURCE=700 D.versions:=-D_XOPEN_SOURCE=700
GCC.warnings:=-Wall -Wextra -Wpedantic -Wvla -Wshadow -Wundef GCC.warnings:=-Wall -Wextra -Wpedantic -Wvla -Wshadow -Wundef
GCC.debug:=-Og -ggdb -pg -fno-inline
CLANG:=clang CLANG:=clang
CLANG.warnings:=-Weverything CLANG.warnings:=-Weverything
@ -13,6 +14,6 @@ VALGRIND:=valgrind
VALGRIND.flags:=--track-origins=yes --leak-check=full --show-leak-kinds=all VALGRIND.flags:=--track-origins=yes --leak-check=full --show-leak-kinds=all
chad_test: chad_test:
${GCC} ${D.versions} ${GCC.warnings} ${SRC} -o ${TARGET}
${CLANG} ${D.versions} ${GCC.warnings} ${SRC} -o ${TARGET} ${CLANG} ${D.versions} ${GCC.warnings} ${SRC} -o ${TARGET}
${GCC} ${D.versions} ${GCC.debug} ${GCC.warnings} ${SRC} -o ${TARGET}
${VALGRIND} ${VALGRIND.flags} $(shell pwd)/${TARGET} ${ARGS} ${VALGRIND} ${VALGRIND.flags} $(shell pwd)/${TARGET} ${ARGS}

View File

@ -56,6 +56,10 @@ void new_display_mode(display_t * mode) {
} }
int free_token(token_t * token){ int free_token(token_t * token){
/* XXX: since hl could be shared,
* this might free an already freed object
* when invoked from a loop
*/
free(token->hl); free(token->hl);
free(token->syntax); free(token->syntax);
return 0; return 0;
@ -164,7 +168,9 @@ token_t * new_token(const char * const word,
// -------------------- // --------------------
int token_fits(const token_t * const token, int token_fits(const token_t * const token,
const char* const to) { const char * const to,
const int string_offset,
int * match_offset) {
const char * const pattern = token->syntax; const char * const pattern = token->syntax;
@ -172,16 +178,17 @@ int token_fits(const token_t* const token,
return true; return true;
} }
return regex_match(pattern, to); return regex_match(pattern, to, string_offset, match_offset);
} }
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';) { for (const char * s = string; *s != '\00';) {
int f; int f;
int i = 0; int token_index = 0;
for (; i < token_table_top; i++) { int offset;
f = token_fits(token_table[i], s); for (; token_index < token_table_top; token_index++) {
f = token_fits(token_table[token_index], string, s - string, &offset);
if(f){ break; } if(f){ break; }
} }
// //
@ -191,10 +198,15 @@ void render_string(const char * const string,
display); display);
// //
if (f) { if (f) {
display->callback(s, for(int i = 0; i < offset; i++){
display->callback(s + i,
0,
token_table[token_index]->hl->attributes);
}
display->callback(s + offset,
f, f,
token_table[i]->hl->attributes); token_table[token_index]->hl->attributes);
s += f; s += f + offset;
} else { } else {
display->callback(s, display->callback(s,
0, 0,

View File

@ -1,5 +1,4 @@
//register const int ew;
//putchar()
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -114,13 +113,14 @@ int main(int argc,
}; };
// //
new_display_mode(cterm); new_display_mode(cterm);
new_char_tokens("&|()[]{}*,", &symbol_hl);
new_keyword_tokens(c_keywords, &keyword_hl); new_keyword_tokens(c_keywords, &keyword_hl);
new_keyword_tokens(preprocessor_keywords, &preprocessor_hl); new_keyword_tokens(preprocessor_keywords, &preprocessor_hl);
new_char_tokens("&|()[]{}*,", &symbol_hl);
// //
render_string(buffer, "cterm"); render_string(buffer, "cterm");
putchar('\n'); putchar('\n');
hl_deinit(); fflush(stdout);
//hl_deinit();
free(buffer); free(buffer);
return 0; return 0;

View File

@ -98,9 +98,13 @@ static bool magic(const char magic_char, const char to_enchant) {
} }
int regex_match(const char * const pattern, int regex_match(const char * const pattern,
const char * const string) { const char * const string_start,
const int string_offset,
int * match_offset_) {
const char * pattern_pointer = pattern; const char * pattern_pointer = pattern;
const char * string_pointer = string; const char * string_pointer = string_start + string_offset;
const char * const match_base = string_pointer;
int match_offset = 0;
while (1488) { while (1488) {
// End of one of the arguments // End of one of the arguments
@ -160,15 +164,20 @@ int regex_match(const char * const pattern,
} }
} }
if (*(pattern_pointer + 1) == '<' if (*(pattern_pointer + 1) == '<') {
&& (is_word_separator(*string_pointer))) { if (is_word_separator(*string_pointer)) {
pattern_pointer += 2; pattern_pointer += 2;
string_pointer += 1; string_pointer += 1;
match_offset += 1;
continue; continue;
} else if (string_pointer == string_start) {
pattern_pointer += 2;
continue;
}
} }
if (*(pattern_pointer + 1) == '>') { if (*(pattern_pointer + 1) == '>') {
if (is_word_separator(*(string_pointer + 1))) { if (is_word_separator(*string_pointer)) {
pattern_pointer += 2; pattern_pointer += 2;
continue; continue;
} }
@ -195,5 +204,8 @@ int regex_match(const char * const pattern,
} }
} }
return (string_pointer - string); if (match_offset_) {
*match_offset_ = match_offset;
}
return (string_pointer - match_base) - match_offset;
} }

View File

@ -3,4 +3,4 @@
extern bool is_case_on; extern bool is_case_on;
int regex_match(const char * const pattern, const char * const string); extern int regex_match(const char * const pattern, const char * const string, const int string_offset, int * match_offset_);