-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.c
267 lines (236 loc) · 6.57 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "cedit.h"
static Line *anc = 0;
static size_t delay = 0;
static size_t BC_lock = 0;
static size_t BC_status = 0;
/*
* decides if there is an block comment or not
*/
int syntax_BC(Line *l, size_t bcnt, size_t len)
{
size_t x;
char *ftype;
/* syntax highlighting depending on filetype */
char **bc = 0;
if(strcmp(CF->type, "c" ) == 0) bc = bc_c;
if(strcmp(CF->type, "og") == 0) bc = bc_go;
if(strcmp(CF->type, "ppc") == 0) bc = bc_cpp;
/* check every block comment delimiter */
for(x = 0; bc[x] != 0; x++){
/* closing braces */
if(x % 2 == 1 && BC_lock == 1){
delay = match_line_buf(l, bcnt, bc[x]);
if(delay > 0) return 1;
}
/* opening braces */
if(x % 2 == 0 && BC_lock == 0) {
if(match_line_buf(l, bcnt, bc[x]) > 0){
FG = SYNTAX_BC;
BC_lock = 1;
return 1;
}
}
}
return 0;
}
/*
* pre calculates every blockcomment before anchor and saves status
*/
void syntax_BC_open()
{
Line *l;
size_t x;
size_t bcnt;
int open;
int close;
/* initialize counters */
open = 0;
close = 0;
/* syntax highlighting depending on filetype */
char **bc = 0;
if(strcmp(CF->type, "c" ) == 0) bc = bc_c;
if(strcmp(CF->type, "og") == 0) bc = bc_go;
if(strcmp(CF->type, "ppc") == 0) bc = bc_cpp;
/* scroll through every utf8 char from first line to anchor */
l = CF->first;
while(l != CF->anc->l){
bcnt = 0;
while(bcnt <= l->blen) {
for(x = 0; bc[x] != 0; x++){
if(match_line_buf(l, bcnt, bc[x]) > 0){
if(x % 2 == 1) close++;
if(x % 2 == 0) open++;
}
}
bcnt += tb_utf8_char_length(l->c[bcnt]);
}
l = l->next;
}
/* change BC_status accordingly */
if(open-close > 0){
BC_status = 1;
} else {
BC_status = 0;
}
}
/*
* decides if there is an inline comment or not
*/
int syntax_ILC(Line *l, size_t bcnt, size_t len)
{
size_t x;
size_t d;
/* syntax highlighting depending on filetype */
char **ilc = 0;
if(strcmp(CF->type, "c" ) == 0) ilc = ilc_c;
if(strcmp(CF->type, "og") == 0) ilc = ilc_go;
if(strcmp(CF->type, "ppc") == 0) ilc = ilc_cpp;
/* check every inline comment delimiter */
for(x = 0; ilc[x] != 0; x++){
if(match_line_buf(l, bcnt, ilc[x]) > 0){
for(d = 0; bcnt < l->blen; d++){
bcnt += tb_utf8_char_length(l->c[bcnt]);
}
FG = SYNTAX_ILC;
delay = d;
return 1;
}
}
return 0;
}
/*
* if quotation is detected count the utf8 chars until the next quotation mark
* of the same kind and set the calculated value as delay
*/
int syntax_QM(Line *l, size_t bcnt, size_t len)
{
char c;
size_t i;
size_t d;
/* if there is nothing detected just return */
if(l->c[bcnt] != 39 && l->c[bcnt] != 34) return 0;
/* save char and initialize delay var */
c = l->c[bcnt];
d = 1;
/* find next quotation mark of same type, set delay and return */
for(i = 1; bcnt+i <= l->blen; i += tb_utf8_char_length(l->c[bcnt+i])){
d++;
if(l->c[bcnt+i] == c){
FG = SYNTAX_QM;
delay = d;
return 1;
}
}
return 0;
}
/*
* hightlights every number that is not enclosed by invalid chars
*/
int syntax_NUM(Line *l, size_t bcnt, size_t len)
{
size_t i;
if(isNumber(l->c[bcnt])){
/* check front */
if(bcnt != 0){
if(!((32 <= l->c[bcnt-1] && l->c[bcnt-1] <= 47 )||
(58 <= l->c[bcnt-1] && l->c[bcnt-1] <= 64 )||
(91 <= l->c[bcnt-1] && l->c[bcnt-1] <= 94 )||
(123 <= l->c[bcnt-1] && l->c[bcnt-1] <= 126)||
(l->c[bcnt-1] == 96))) return 0;
}
/* break if a non-number was found */
for(i = 1; bcnt+i <= l->blen; i++){
if(!(isNumber(l->c[bcnt+i]))) break;
}
/* check back */
if(bcnt+i < l->blen){
if(!((32 <= l->c[bcnt+i] && l->c[bcnt+i] <= 64 )||
(91 <= l->c[bcnt+i] && l->c[bcnt+i] <= 94 )||
(123 <= l->c[bcnt+i] && l->c[bcnt+i] <= 126)||
(l->c[bcnt+i] == 96))) return 0;
}
/* set FG and a delay */
FG = SYNTAX_NUM;
delay = i;
return 1;
}
return 0;
}
/*
* parses every word, decides if it is a type or a reserved word and
* hightlights it - does not care about utf8 at all which will be no problem
* as types and reserved words are ASCII
*/
int syntax_WORD(Line *l, size_t bcnt, size_t len)
{
size_t x;
size_t r;
size_t i;
/* type will hold datatypes, res(erved) will hold reserved words and
word will point to one of them */
char **word;
char **type = 0;
char **res = 0;
/* decide which words to highlight depending on filetype */
if(strcmp(CF->type, "c" ) == 0) { type = type_c; res = res_c; }
if(strcmp(CF->type, "og") == 0) { type = type_go; res = res_go; }
if(strcmp(CF->type, "ppc") == 0) { type = type_cpp; res = res_cpp; }
/* run two times 0 == type and 1 == reserved */
for(r = 0; r < 2; r++) {
if(r == 0) word = type;
if(r == 1) word = res;
/* detect words and highlight them if they match */
for(x = 0; word[x] != 0; x++){
delay = match_line_buf(l, bcnt, word[x]);
if(delay > 0){
i = strlen(word[x])-1;
if((bcnt+i+1 < l->blen && !isSpecial(l->c[bcnt+i+1])) ||
(bcnt!=0 && !isSpecial(l->c[bcnt-1]))) return 0;
if(r == 0) FG = SYNTAX_TYPE;
if(r == 1) FG = SYNTAX_RES;
return 1;
}
}}
return 0;
}
/*
* resets the whole syntax system to the defaults
*/
void syntax_reset()
{
BC_lock = 0;
FG = TB_WHITE | TB_BOLD;
BG = TB_BLACK;
}
/*
* called from the draw function once every char
*/
void syntax_all(Line *line, size_t bcnt, size_t len)
{
/* disable syntax system if filetype not known */
if(CF->type == 0) return;
if(!(strcmp(CF->type, "c") == 0 || strcmp(CF->type, "og") == 0 ||
strcmp(CF->type, "ppc") == 0 )) return;
/* always reset the syntax system on first call */
if(line == CF->anc->l && bcnt == 0) syntax_reset();
/* if the anchor changed run syntax_BC_open once - set BC accordingly */
if(anc != CF->anc->l){
anc = CF->anc->l;
syntax_BC_open();
}
if(BC_status == 1 && line == CF->anc->l && bcnt == 0) {
FG = SYNTAX_BC;
BC_lock = 1;
delay = 0;
}
/* if there is delay on the counter wait */
if(delay == 1) syntax_reset();
if(delay > 0) delay--;
if(delay > 0) return;
/* highlight everything */
if(syntax_BC(line, bcnt, len) || BC_lock) return;
if(syntax_ILC(line, bcnt, len)) return;
if(syntax_QM(line, bcnt, len)) return;
if(syntax_NUM(line, bcnt, len)) return;
if(syntax_WORD(line, bcnt, len)) return;
}