This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
/
syntax.c
158 lines (129 loc) · 3.77 KB
/
syntax.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "syntax.h"
int syntax_lookup_color(char* key) {
if (!strncmp(key, "number", 6)) return HL_NUM;
else if (!strncmp(key, "string", 6)) return HL_STRING;
else if (!strncmp(key, "string", 6)) return HL_STRING;
else if (!strncmp(key, "comment", 7)) return HL_COMMENT;
else if (!strncmp(key, "keyword", 7)) return HL_KEYWORD;
else if (!strncmp(key, "type", 4)) return HL_TYPE;
else if (!strncmp(key, "pragma", 6)) return HL_PRAGMA;
else if (!strncmp(key, "predefined", 10)) return HL_PREDEF;
else if (!strncmp(key, "match", 5)) return HL_MATCH;
return HL_NORMAL;
}
void syntax_read_extensions(syntax* c, FILE* f, char* line) {
int regl = 1;
size_t ln;
regex_t* reg = malloc(sizeof(regex_t));
int err;
regcomp(reg, line, REG_EXTENDED);
while (fpeek(f) == ' ' || fpeek(f) == '\t') {
if (fgets(line, MAX_LINE_WIDTH, f)) {
ln = strlen(line)-1;
line[ln] = '\0'; // replace newline
reg = realloc(reg, sizeof(regex_t) * ++regl);
err = regcomp(®[regl-1], strtriml(line), REG_EXTENDED);
if (err) exit(err);
}
}
c->matchlen = regl;
c->filematch = reg;
}
void syntax_read_pattern(syntax* c, FILE* f, char* key, char* value) {
pattern* pat = malloc(sizeof(pattern));
char line[MAX_LINE_WIDTH];
size_t ln;
int err;
key = strtok(key, "|");
char* no_sep = strtok(NULL, "|");
pat->color = syntax_lookup_color(key);
pat->needs_sep = !(no_sep && !strncmp(no_sep, "no_sep", 6));
pat->multiline = 0;
ln = strlen(value);
memmove(value+1, value, ln);
value[0] = '^';
err = regcomp(&pat->pattern, value, REG_EXTENDED);
if (err) exit(err);
if ((fpeek(f) == ' ' || fpeek(f) == '\t') && fgets(line, MAX_LINE_WIDTH, f)) {
char* l = strtriml(line);
ln = strlen(l)-1;
memmove(l+1, l, ln);
l[0] = '^';
err = regcomp(&pat->closing, l, REG_EXTENDED);
if (err) exit(err);
pat->multiline = 1;
}
c->npatterns++;
c->patterns = realloc(c->patterns, sizeof(pattern)*c->npatterns);
memmove(&c->patterns[c->npatterns-1], pat, sizeof(pattern));
free(pat);
}
syntax* syntax_read_file(char* fname) {
FILE* f;
syntax* c;
char* key;
char* value;
char* line = malloc(MAX_LINE_WIDTH);
size_t ln;
f = fopen(fname, "r");
if (!f) return NULL;
c = malloc(sizeof(syntax));
c->patterns = NULL;
c->npatterns = 0;
while (fgets(line, MAX_LINE_WIDTH, f)) {
ln = strlen(line)-1;
line[ln] = '\0'; // replace newline
key = strtok(line, ":");
value = strtok(NULL, ":");
if (!strncmp(key, "displayname", 11)) {
c->ftype = strdup(strtriml(value));
} else if (!strncmp(key, "extensions", 10)) {
syntax_read_extensions(c, f, strtriml(value));
} else {
if (value) syntax_read_pattern(c, f, key, strtriml(value));
}
}
fclose(f);
free(line);
return c;
}
syntax** syntax_init(char* dir) {
int retl = 1;
DIR* dp = opendir(dir);
char fname[MAX_LINE_WIDTH];
struct dirent* ep;
syntax** ret = malloc(sizeof(syntax*));
syntax* c;
if (!dp) return NULL;
while ((ep = readdir(dp))) {
if (!strcmpr(ep->d_name, (char*) ".stx")) continue;
snprintf(fname, sizeof(fname), "%s/%s", dir, ep->d_name);
c = syntax_read_file(fname);
if (!c) continue;
ret = realloc(ret, sizeof(syntax*)*retl);
ret[retl-1] = c;
retl++;
}
closedir(dp);
ret = realloc(ret, sizeof(syntax*)*retl);
ret[retl-1] = NULL;
return ret;
}
void syntax_free(syntax** stx) {
int i = 0;
int j;
while (stx[i]) {
syntax* s = stx[i];
free(s->ftype);
for (j = 0; j < s->matchlen; j++) regfree(&s->filematch[j]);
free(s->filematch);
for (j = 0; j < s->npatterns; j++) {
regfree(&s->patterns[j].pattern);
if (s->patterns[j].multiline) regfree(&s->patterns[j].closing);
}
free(s->patterns);
free(s);
i++;
}
free(stx);
}