typedef struct {
hl_group_t * hl;
token_type_t t;
- char * syntax;
+ regex_t * syntax;
} token_t;
// GLOBALS
int free_token(token_t * token) {
free(token->hl);
- free(token->syntax);
+ regex_free(token->syntax);
return 0;
}
return 0;
}
-token_t * new_symbol_token(const char * const word,
- hl_group_t * const g) {
- char * new_word = strdup(word);
+token_t * new_symbol_token(const char * const c,
+ hl_group_t * const g) {
+ char * new_word;
+ if (is_magic(*c)) {
+ new_word = (char *)malloc(sizeof(char)*3);
+ new_word[0] = '\\';
+ new_word[1] = *c;
+ new_word[2] = '\00';
+ } else {
+ new_word = strdup(c);
+ }
token_t * mt = (token_t*)malloc(sizeof(token_t));
mt->hl = g;
mt->t = KEYSYMBOL;
- mt->syntax = new_word;
+ mt->syntax = regex_compile(new_word);
append_token(mt);
mt->hl = g;
mt->t = KEYWORD;
- mt->syntax = new_word;
+ mt->syntax = regex_compile(new_word);
append_token(mt);
int token_fits(const token_t * const token,
const char * const to,
const int string_offset,
- int * match_offset) {
- const char * const pattern = token->syntax;
-
- if (! pattern) {
- return true;
- }
+ int * match_offset) {
//return regex_match(pattern, to, string_offset, match_offset);
- return regex_search(NULL, pattern + string_offset);
+ return regex_search(token->syntax, to + string_offset);
}
void render_string(const char * const string,
s += n; \
} while (0)
-bool is_quantifier(const char c){
+static bool is_quantifier(const char c) {
for (const char * s = "+*?"; *s != '\00'; s++) {
if (*s == c) {
return true;
return false;
}
+bool is_magic(const char c) {
+ if (is_quantifier(c)) {
+ return true;
+ }
+ for (const char * s = "\\[]"; *s != '\00'; s++) {
+ if (*s == c) {
+ return true;
+ }
+ }
+ return false;
+}
+
-int escape_1_to_1(const char c, char * whitelist) {
+static int escape_1_to_1(const char c, char * whitelist) {
switch(c) {
case 't': {
strcat(whitelist, "\t");
return 0;
}
-int escape_1_to_N(const char c, char * whitelist) {
+static int escape_1_to_N(const char c, char * whitelist) {
switch(c) {
case 'i': {
const char identifier_chars[] = "@0123456789_\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337";
return 0;
}
-int compile_range(const char * const range,
+static int compile_range(const char * const range,
char * whitelist) {
assert(range[0] == '[' && "Not a range.");
extern bool regex_search(regex_t * regex, const char * const string);
extern int regex_free(regex_t * const regex);
+extern bool is_magic(const char c);
+
#endif