~kiwec/ke

ke/src/Syntax.cc -rw-r--r-- 6.4 KiB
1cc8481d — Wolf Clément Fix compilation 3 years ago
                                                                                
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <vector>
#include "Document.h"
#include "Line.h"
#include "Section.h"
#include "Syntax.h"

static const std::wstring operators[] = {
  L"auto",
  L"bool",
  L"break",
  L"case",
  L"catch",
  L"class",
  L"continue",
  L"const",
  L"constexpr",
  L"decltype",
  L"default",
  L"delete",
  L"do",
  L"double",
  L"else",
  L"false",
  L"float",
  L"for",
  L"if",
  L"inline",
  L"int",
  L"long",
  L"namespace",
  L"new",
  L"noexcept",
  L"nullptr",
  L"override",
  L"private",
  L"protected",
  L"public",
  L"return",
  L"short",
  L"sizeof",
  L"static",
  L"struct",
  L"switch",
  L"this",
  L"throw",
  L"true",
  L"try",
  L"unsigned",
  L"virtual",
  L"void",
  L"wchar_t",
  L"while",
};

static constexpr uint operators_size = std::extent<decltype(operators)>::value;

bool isAlpha(wchar_t c) {
	const std::wstring letters = L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
	return letters.find(c) != std::string::npos;
}

bool isNumber(wchar_t c) {
	const std::wstring numbers = L"0123456789";
	return numbers.find(c) != std::string::npos;
}

bool isAlphaNum(wchar_t c) {
	return isAlpha(c) || isNumber(c);
}

void Syntax::prepare(Line line) {
	auto syntax = line.syntax();
	for(uint i = 0; i < line.length(); i++) {
		syntax[i] = DEFAULT_COLOR;
	}
}

std::vector<Section> Syntax::get_inline_matches(Line line, std::wstring match) {
  std::vector<Section> sections;
  auto match_length = match.length();
  
  auto start = std::string::npos;
  for(uint i = 0; i < line.length(); i++) {
    auto len = start == std::string::npos ? 0 : i - start;
    
    if(line[i] == match[len]) {
      if(start == std::string::npos) {
        start = i;
      }
      len++;
      if(len == match_length) {
        sections.push_back({ (uint)start, i + 1 });
        start = std::string::npos;
      }
    } else {
      start = std::string::npos;
    }
  }
  
  return sections;
}

std::vector<Section> Syntax::get_inline_strings(Line line, wchar_t delimiter) {
  std::vector<Section> sections;
  
  auto start = std::string::npos;
  for(uint i = 0; i < line.length(); i++) {
    // Ignore escaped characters
    if(line[i] == L'\\') {
      i++;
      if(line[i] == L'\\') {
        i++;
      }
      continue;
    }
    
    if(start == std::string::npos) {
      if(line[i] == delimiter) {
        start = i;
      }
    } else {
      if(line[i] == delimiter) {
        sections.push_back({ (uint)start, i });
        start = std::string::npos;
      }
    }
  }
  
  return sections;
}

void Syntax::markPreprocessor(Line line) {
	// if(line->start_commented) return;

	const auto str = line.begin();
	if(str[0] != L'#')
		return;

	auto syntax = line.syntax();
	for(uint i = 0; i < line.length(); i++) {
		if(syntax[i] != STRING_COLOR)
			syntax[i] = 249;
	}
}

void Syntax::markComments(Line line) {
	// Mark line comments
  auto line_comment_matches = Syntax::get_inline_matches(line, L"//");
  for(auto& match : line_comment_matches) {
    // Ignore comment in a string
    if(line.syntax()[match.start] == STRING_COLOR) continue;
    
    for(uint i = match.start; i < line.length(); i++) {
      line.syntax()[i] = COMMENT_COLOR;
    }
    
    break;
  }
  
  // Storing start_commented in previous line's newline syntax location
  bool commenting = line.is_first() ? false : line.syntax()[-1] == COMMENT_COLOR;
	auto pos = 0;
  auto max = line.length();
  auto starts = Syntax::get_inline_matches(line, L"/*");
  auto ends = Syntax::get_inline_matches(line, L"*/");
	while(pos < max) {
    if(commenting) {
      line.syntax()[pos] = COMMENT_COLOR;
    
      for(auto& end : ends) {
        if(end.end == pos) {
          if(line.syntax()[end.end] == STRING_COLOR) continue;
          commenting = false;
          break;
        }
      }
    } else {
      for(auto& start : starts) {
        if(start.start == pos) {
          if(line.syntax()[start.start] == STRING_COLOR) continue;
          commenting = true;
          line.syntax()[pos] = COMMENT_COLOR;
          line.syntax()[pos + 1] = COMMENT_COLOR;
          pos++;
          break;
        }
      }
    }
    
    pos++;
	}

	if(!line.is_last()) {
		if(commenting) {
			if(line.syntax()[max] != COMMENT_COLOR) {
        line.syntax()[max] = COMMENT_COLOR;
				Syntax::update(line.next());
			}
		} else {
			if(line.syntax()[max] == COMMENT_COLOR) {
        line.syntax()[max] = DEFAULT_COLOR;
				Syntax::update(line.next());
			}
		}
	}
}

void Syntax::markNumbers(Line line) {
	const auto str = line.begin();
	auto syntax = line.syntax();
	for(uint i = 0; i < line.length(); i++) {
		if(syntax[i] == DEFAULT_COLOR && isNumber(str[i])) {
			syntax[i] = 208;
		}
	}
}

void Syntax::markChars(Line line) {
	auto chars = Syntax::get_inline_strings(line, L'\'');
	auto syntax = line.syntax();
	for(auto& sec : chars) {
		for(uint i = sec.start; i <= sec.end; i++) {
			syntax[i] = CHAR_COLOR;
		}
	}
}

void Syntax::markStrings(Line line) {
	auto strings = Syntax::get_inline_strings(line, L'"');
	auto syntax = line.syntax();
	for(auto& sec : strings) {
		for(uint i = sec.start; i <= sec.end; i++) {
			syntax[i] = STRING_COLOR;
		}
	}
}

void Syntax::markOperators(Line line) {
	const auto str = line.begin();

	for(uint i = 0; i < operators_size; i++) {
    auto line_operator_matches = Syntax::get_inline_matches(line, operators[i]);
    for(auto& match : line_operator_matches) {
      if(line.syntax()[match.start] != DEFAULT_COLOR) continue;
      if(match.start != 0 && isAlphaNum(line[match.start - 1])) continue;
      for(uint j = match.start; j < match.end; j++) {
        line.syntax()[j] = OPERATOR_COLOR;
      }
    }
	}
}

void Syntax::markSpecial(Line line) {
	std::wstring special = L"-+*/&|^\\=:;><!?][)(}{,.";
#if 0
	const auto str = line.begin();
	for(uint i = 0; i < line.length(); i++) {
		if(line->syntax[i] != DEFAULT_COLOR) continue;
		if(special.find(str[i]) == std::string::npos)
			continue;
		line->syntax[i] = 220;
	}
#endif
}

void Syntax::mark_start_commented(Line line) {
	if(line.is_first()) {
		return;
	}

	// Updates prev line's comment syntax and this line's start_commented
	markComments(line.prev());
}

void Syntax::update(Text& text) {
	Line line = text.get_line(1);
	for(;;) {
		update(line);
		if(line.is_last()) break;
		line = line.next();
	}
}

void Syntax::update(Line line) {
	if(line.length() == 0) return;

	prepare(line);
	markStrings(line);
	markChars(line);
	markComments(line);
	markPreprocessor(line);
	markSpecial(line);
	markOperators(line);
	markNumbers(line);
}