-
Notifications
You must be signed in to change notification settings - Fork 1
/
syntaxHighlighting.c
221 lines (199 loc) · 6.33 KB
/
syntaxHighlighting.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "syntaxHighlighting.h"
char *C_HL_extensions[] = {".c", ".h", ".cpp", ".cc", NULL};
char *C_HL_keywords[] = {
"switch", "if", "while", "for", "break", "continue", "return", "else",
"struct", "union", "typedef", "", "enum", "class", "case",
"int|", "long|", "double|", "float|", "char|", "unsigned|", "signed|",
"void|", NULL};
struct microSyntax HLDB[] = {
{"c",
C_HL_extensions,
C_HL_keywords,
"//", "/*", "***",
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS},
};
#define HLDB_ENTRIES (sizeof(HLDB) / sizeof(HLDB[0]))
int hasSeperator(int c)
{
return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL;
}
void microUpdateSyntax(microRow *row)
{
row->highlight = realloc(row->highlight, row->rsize);
memset(row->highlight, HL_DEFAULT, row->rsize);
if (microConfig.syntax == NULL)
return;
char **keywords = microConfig.syntax->keywords;
char *slcs = microConfig.syntax->singleLineCommentStart;
char *mlcs = microConfig.syntax->multiLineCommentStart;
char *mlce = microConfig.syntax->multiLineCommentEnd;
int slcsLen = slcs ? strlen(slcs) : 0;
int mlcsLen = mlcs ? strlen(mlcs) : 0;
int mlceLen = mlce ? strlen(mlce) : 0;
int previousSeperator = 1;
int inString = 0;
// int inComment = 0;
int inComment = (row->idx > 0 && microConfig.row[row->idx - 1].highlightOpenComment);
int i = 0;
while (i < row->rsize)
{
char c = row->render[i];
unsigned char previousHighlight = (i > 0) ? row->highlight[i - 1] : HL_DEFAULT;
if (slcsLen && !inString && !inComment)
{
if (!strncmp(&row->render[i], slcs, slcsLen))
{
memset(&row->highlight[i], HL_COMMENT, row->rsize - i);
break;
}
}
if (mlcsLen && mlceLen && !inString)
{
if (inComment)
{
row->highlight[i] = HL_ML_COMMENT;
if (!strncmp(&row->render[i], mlce, mlceLen))
{
memset(&row->highlight[i], HL_ML_COMMENT, mlceLen);
i += mlceLen;
inComment = 0;
previousSeperator = 1;
continue;
}
else
{
i++;
continue;
}
}
else if (!strncmp(&row->render[i], mlcs, mlcsLen))
{
memset(&row->highlight[i], HL_ML_COMMENT, mlcsLen);
i += mlcsLen;
inComment = 1;
continue;
}
}
if (microConfig.syntax->flags & HL_HIGHLIGHT_STRINGS)
{
if (inString)
{
row->highlight[i] = HL_STRING;
if (c == '\\' && i + 1 < row->rsize)
{
row->highlight[i + 1] = HL_STRING;
i += 2;
continue;
}
if (c == inString)
inString = 0;
i++;
previousSeperator = 1;
continue;
}
else
{
if (c == '"' || c == '\'')
{
inString = c;
row->highlight[i] = HL_STRING;
i++;
continue;
}
}
}
if (microConfig.syntax->flags & HL_HIGHLIGHT_NUMBERS)
{
if ((isdigit(c) && (previousSeperator || previousHighlight == HL_NUMBER)) || (c == '.' && previousHighlight == HL_NUMBER))
{
row->highlight[i] = HL_NUMBER;
i++;
previousSeperator = 0;
continue;
}
}
if (previousSeperator)
{
int j;
for (j = 0; keywords[j]; j++)
{
int keywordLen = strlen(keywords[j]);
int keywords2 = keywords[j][keywordLen - 1] == '|';
if (keywords2)
keywordLen--;
if (!strncmp(&row->render[i], keywords[j], keywordLen) && hasSeperator(row->render[i + keywordLen]))
{
memset(&row->highlight[i], keywords2 ? HL_KEYWORD2 : HL_KEYWORD1, keywordLen);
i += keywordLen;
break;
}
}
if (keywords[j] != NULL)
{
previousSeperator = 0;
continue;
}
}
previousSeperator = hasSeperator(c);
i++;
}
int changed = (row->highlightOpenComment != inComment);
row->highlightOpenComment = inComment;
if (changed && row->idx + 1 < microConfig.numRows)
{
microUpdateSyntax(µConfig.row[row->idx + 1]);
}
}
int microSyntaxToColour(int highlight)
{
switch (highlight)
{
case HL_NUMBER:
return 31;
case HL_STRING:
return 35;
case HL_MATCH:
return 34;
case HL_COMMENT:
case HL_ML_COMMENT:
return 36;
case HL_KEYWORD1:
return 33;
case HL_KEYWORD2:
return 32;
default:
return 37;
}
}
void microSelectSyntaxHighlight()
{
microConfig.syntax = NULL;
if (microConfig.fileName == NULL)
return;
char *ext = strrchr(microConfig.fileName, '.');
for (unsigned int j = 0; j < HLDB_ENTRIES; j++)
{
struct microSyntax *s = &HLDB[j];
unsigned int i = 0;
while (s->fileMatch[i])
{
int is_ext = (s->fileMatch[i][0] == '.');
if ((is_ext && ext && !strcmp(ext, s->fileMatch[i])) ||
(!is_ext && strstr(microConfig.fileName, s->fileMatch[i])))
{
microConfig.syntax = s;
int filerow;
for (filerow = 0; filerow < microConfig.numRows; filerow++)
{
microUpdateSyntax(µConfig.row[filerow]);
}
return;
}
i++;
}
}
}