-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathst_c.c
267 lines (215 loc) · 5.5 KB
/
st_c.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 <string.h>
#include <syntax.h>
#include <ctype.h>
int st_c_depth(const char *line, int length) {
char in_string = '\0';
int depth = 0;
for (int i = 0; i < length; i++) {
if (in_string) {
if (line[i] == '\\') {
i++;
} else if (line[i] == in_string) {
in_string = '\0';
}
} else if (strchr("\"'", line[i])) {
in_string = line[i];
} else if (strchr("([{", line[i])) {
depth++;
} else if (strchr(")]}", line[i])) {
depth--;
}
if (depth < 0) {
depth = 0;
}
}
return depth;
}
char st_c_pair(const char *line, int length, char chr) {
char in_string = '\0';
for (int i = 0; i < length; i++) {
if (in_string) {
if (line[i] == '\\') {
i++;
} else if (line[i] == in_string) {
in_string = '\0';
}
} else if (strchr("\"'", line[i])) {
in_string = line[i];
}
}
if (in_string) {
return '\0';
}
if (chr == '(') {
return ')';
}
if (chr == '[') {
return ']';
}
if (chr == '{') {
return '}';
}
if (chr == '"') {
return '"';
}
if (chr == '\'') {
return '\'';
}
return '\0';
}
static int is_ident(char chr) {
return isalnum(chr) || strchr("_$", chr);
}
enum {
st_c_default,
st_c_ident,
st_c_number,
st_c_string,
st_c_char,
st_c_line_comment,
st_c_block_comment,
st_c_string_escape,
st_c_char_escape,
st_c_block_comment_exit,
};
int st_c_color(int prev_state, int *state, const char *text, int length) {
if (prev_state == st_c_line_comment) {
if (text[0] == '\n') {
*state = st_c_default;
return st_color_default;
}
return st_color_comment;
}
if (prev_state == st_c_block_comment_exit) {
*state = st_c_default;
return st_color_comment;
}
if (prev_state == st_c_block_comment) {
if (length >= 2 && text[0] == '*' && text[1] == '/') {
*state = st_c_block_comment_exit;
}
return st_color_comment;
}
if (prev_state == st_c_string_escape) {
*state = st_c_string;
return st_color_string;
}
if (prev_state == st_c_char_escape) {
*state = st_c_char;
return st_color_string;
}
if (prev_state == st_c_string) {
if (text[0] == '\\') {
*state = st_c_string_escape;
} else if (text[0] == '"') {
*state = st_c_default;
}
return st_color_string;
}
if (prev_state == st_c_char) {
if (text[0] == '\\') {
*state = st_c_char_escape;
} else if (text[0] == '\'') {
*state = st_c_default;
}
return st_color_string;
}
if (prev_state == st_c_line_comment) {
if (text[0] == '\n') {
*state = st_c_default;
}
return st_color_comment;
}
if (length >= 2 && text[0] == '/') {
if (text[1] == '/') {
*state = st_c_line_comment;
return st_color_comment;
} else if (text[1] == '*') {
*state = st_c_block_comment;
return st_color_comment;
}
}
if (strchr("+-*/%=&|^!?:.,;><\\~", text[0])) {
*state = st_c_default;
return st_color_symbol;
}
if (isspace(text[0]) || strchr("()[]{}\n", text[0])) {
*state = st_c_default;
return st_color_default;
}
if (text[0] == '"') {
*state = st_c_string;
return st_color_string;
}
if (text[0] == '\'') {
*state = st_c_char;
return st_color_string;
}
if (text[0] == '#') {
*state = st_c_ident;
return st_color_keyword;
}
if (prev_state == st_c_default) {
if (isdigit(text[0])) {
*state = st_c_number;
return st_color_number;
} else if (is_ident(text[0])) {
int is_func = 0, is_type = 1, is_keyword = 0;
int ident_length = 1;
for (int i = 1; i < length; i++) {
if (text[i] == '(') {
is_func = 1;
}
if (!is_ident(text[i])) {
break;
}
ident_length++;
}
if (ident_length < 2) {
is_type = 0;
} else if (text[ident_length - 1] != 't') {
is_type = 0;
} else if (text[ident_length - 2] != '_') {
is_type = 0;
}
char buffer[ident_length + 1];
memcpy(buffer, text, ident_length);
buffer[ident_length] = '\0';
if (ident_length == 2 && strstr("do,if", buffer)) {
is_keyword = 1;
}
if (ident_length == 3 && strstr("for,int,new", buffer)) {
is_keyword = 1;
}
if (ident_length == 4 && strstr("auto,bool,case,char,else,enum,goto,long,true,void", buffer)) {
is_keyword = 1;
}
if (ident_length == 5 && strstr("break,class,const,false,float,short,union,using,while", buffer)) {
is_keyword = 1;
}
if (ident_length == 6 && strstr("delete,double,extern,inline,public,return,signed,sizeof,static,struct,switch", buffer)) {
is_keyword = 1;
}
if (ident_length == 7 && strstr("default,private,typedef", buffer)) {
is_keyword = 1;
}
if (ident_length == 8 && strstr("continue,register,unsigned,volatile", buffer)) {
is_keyword = 1;
}
if (ident_length == 9 && strstr("constexpr,namespace,protected", buffer)) {
is_keyword = 1;
}
*state = st_c_ident;
if (is_type) {
return st_color_type;
} else if (is_keyword) {
return st_color_keyword;
} else if (is_func) {
return st_color_function;
} else {
return st_color_default;
}
}
}
return st_color_none;
}