]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexCPP.cxx
9c8ea416ae6b3b36063090f26addb4696f7fe626
[wxWidgets.git] / contrib / src / stc / scintilla / src / LexCPP.cxx
1 // Scintilla source code edit control
2 /** @file LexCPP.cxx
3 ** Lexer for C++, C, Java, and Javascript.
4 **/
5 // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13
14 #include "Platform.h"
15
16 #include "PropSet.h"
17 #include "Accessor.h"
18 #include "StyleContext.h"
19 #include "KeyWords.h"
20 #include "Scintilla.h"
21 #include "SciLexer.h"
22
23 #define KEYWORD_BOXHEADER 1
24 #define KEYWORD_FOLDCONTRACTED 2
25
26 static bool IsOKBeforeRE(const int ch) {
27 return (ch == '(') || (ch == '=') || (ch == ',');
28 }
29
30 static inline bool IsAWordChar(const int ch) {
31 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
32 }
33
34 static inline bool IsAWordStart(const int ch) {
35 return (ch < 0x80) && (isalnum(ch) || ch == '_');
36 }
37
38 static inline bool IsADoxygenChar(const int ch) {
39 return (islower(ch) || ch == '$' || ch == '@' ||
40 ch == '\\' || ch == '&' || ch == '<' ||
41 ch == '>' || ch == '#' || ch == '{' ||
42 ch == '}' || ch == '[' || ch == ']');
43 }
44
45 static inline bool IsStateComment(const int state) {
46 return ((state == SCE_C_COMMENT) ||
47 (state == SCE_C_COMMENTLINE) ||
48 (state == SCE_C_COMMENTDOC) ||
49 (state == SCE_C_COMMENTDOCKEYWORD) ||
50 (state == SCE_C_COMMENTDOCKEYWORDERROR));
51 }
52
53 static inline bool IsStateString(const int state) {
54 return ((state == SCE_C_STRING) || (state == SCE_C_VERBATIM));
55 }
56
57 static void ColouriseCppDoc(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
58 Accessor &styler, bool caseSensitive) {
59
60 WordList &keywords = *keywordlists[0];
61 WordList &keywords2 = *keywordlists[1];
62 WordList &keywords3 = *keywordlists[2];
63 WordList &keywords4 = *keywordlists[3];
64
65 bool stylingWithinPreprocessor = styler.GetPropertyInt("styling.within.preprocessor") != 0;
66
67 // Do not leak onto next line
68 if (initStyle == SCE_C_STRINGEOL)
69 initStyle = SCE_C_DEFAULT;
70
71 int chPrevNonWhite = ' ';
72 int visibleChars = 0;
73 bool lastWordWasUUID = false;
74
75 StyleContext sc(startPos, length, initStyle, styler);
76
77 for (; sc.More(); sc.Forward()) {
78
79 if (sc.atLineStart && (sc.state == SCE_C_STRING)) {
80 // Prevent SCE_C_STRINGEOL from leaking back to previous line
81 sc.SetState(SCE_C_STRING);
82 }
83
84 // Handle line continuation generically.
85 if (sc.ch == '\\') {
86 if (sc.chNext == '\n' || sc.chNext == '\r') {
87 sc.Forward();
88 if (sc.ch == '\r' && sc.chNext == '\n') {
89 sc.Forward();
90 }
91 continue;
92 }
93 }
94
95 // Determine if the current state should terminate.
96 if (sc.state == SCE_C_OPERATOR) {
97 sc.SetState(SCE_C_DEFAULT);
98 } else if (sc.state == SCE_C_NUMBER) {
99 if (!IsAWordChar(sc.ch)) {
100 sc.SetState(SCE_C_DEFAULT);
101 }
102 } else if (sc.state == SCE_C_IDENTIFIER) {
103 if (!IsAWordChar(sc.ch) || (sc.ch == '.')) {
104 char s[100];
105 if (caseSensitive) {
106 sc.GetCurrent(s, sizeof(s));
107 } else {
108 sc.GetCurrentLowered(s, sizeof(s));
109 }
110 if (keywords.InList(s)) {
111 lastWordWasUUID = strcmp(s, "uuid") == 0;
112 sc.ChangeState(SCE_C_WORD);
113 } else if (keywords2.InList(s)) {
114 sc.ChangeState(SCE_C_WORD2);
115 } else if (keywords4.InList(s)) {
116 sc.ChangeState(SCE_C_GLOBALCLASS);
117 }
118 sc.SetState(SCE_C_DEFAULT);
119 }
120 } else if (sc.state == SCE_C_PREPROCESSOR) {
121 if (stylingWithinPreprocessor) {
122 if (IsASpace(sc.ch)) {
123 sc.SetState(SCE_C_DEFAULT);
124 }
125 } else {
126 if ((sc.atLineEnd) || (sc.Match('/', '*')) || (sc.Match('/', '/'))) {
127 sc.SetState(SCE_C_DEFAULT);
128 }
129 }
130 } else if (sc.state == SCE_C_COMMENT) {
131 if (sc.Match('*', '/')) {
132 sc.Forward();
133 sc.ForwardSetState(SCE_C_DEFAULT);
134 }
135 } else if (sc.state == SCE_C_COMMENTDOC) {
136 if (sc.Match('*', '/')) {
137 sc.Forward();
138 sc.ForwardSetState(SCE_C_DEFAULT);
139 } else if (sc.ch == '@' || sc.ch == '\\') {
140 sc.SetState(SCE_C_COMMENTDOCKEYWORD);
141 }
142 } else if (sc.state == SCE_C_COMMENTLINE || sc.state == SCE_C_COMMENTLINEDOC) {
143 if (sc.atLineEnd) {
144 sc.SetState(SCE_C_DEFAULT);
145 visibleChars = 0;
146 }
147 } else if (sc.state == SCE_C_COMMENTDOCKEYWORD) {
148 if (sc.Match('*', '/')) {
149 sc.ChangeState(SCE_C_COMMENTDOCKEYWORDERROR);
150 sc.Forward();
151 sc.ForwardSetState(SCE_C_DEFAULT);
152 } else if (!IsADoxygenChar(sc.ch)) {
153 char s[100];
154 if (caseSensitive) {
155 sc.GetCurrent(s, sizeof(s));
156 } else {
157 sc.GetCurrentLowered(s, sizeof(s));
158 }
159 if (!isspace(sc.ch) || !keywords3.InList(s + 1)) {
160 sc.ChangeState(SCE_C_COMMENTDOCKEYWORDERROR);
161 }
162 sc.SetState(SCE_C_COMMENTDOC);
163 }
164 } else if (sc.state == SCE_C_STRING) {
165 if (sc.ch == '\\') {
166 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
167 sc.Forward();
168 }
169 } else if (sc.ch == '\"') {
170 sc.ForwardSetState(SCE_C_DEFAULT);
171 } else if (sc.atLineEnd) {
172 sc.ChangeState(SCE_C_STRINGEOL);
173 sc.ForwardSetState(SCE_C_DEFAULT);
174 visibleChars = 0;
175 }
176 } else if (sc.state == SCE_C_CHARACTER) {
177 if (sc.atLineEnd) {
178 sc.ChangeState(SCE_C_STRINGEOL);
179 sc.ForwardSetState(SCE_C_DEFAULT);
180 visibleChars = 0;
181 } else if (sc.ch == '\\') {
182 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
183 sc.Forward();
184 }
185 } else if (sc.ch == '\'') {
186 sc.ForwardSetState(SCE_C_DEFAULT);
187 }
188 } else if (sc.state == SCE_C_REGEX) {
189 if (sc.ch == '\r' || sc.ch == '\n' || sc.ch == '/') {
190 sc.ForwardSetState(SCE_C_DEFAULT);
191 } else if (sc.ch == '\\') {
192 // Gobble up the quoted character
193 if (sc.chNext == '\\' || sc.chNext == '/') {
194 sc.Forward();
195 }
196 }
197 } else if (sc.state == SCE_C_VERBATIM) {
198 if (sc.ch == '\"') {
199 if (sc.chNext == '\"') {
200 sc.Forward();
201 } else {
202 sc.ForwardSetState(SCE_C_DEFAULT);
203 }
204 }
205 } else if (sc.state == SCE_C_UUID) {
206 if (sc.ch == '\r' || sc.ch == '\n' || sc.ch == ')') {
207 sc.SetState(SCE_C_DEFAULT);
208 }
209 }
210
211 // Determine if a new state should be entered.
212 if (sc.state == SCE_C_DEFAULT) {
213 if (sc.Match('@', '\"')) {
214 sc.SetState(SCE_C_VERBATIM);
215 sc.Forward();
216 } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
217 if (lastWordWasUUID) {
218 sc.SetState(SCE_C_UUID);
219 lastWordWasUUID = false;
220 } else {
221 sc.SetState(SCE_C_NUMBER);
222 }
223 } else if (IsAWordStart(sc.ch) || (sc.ch == '@')) {
224 if (lastWordWasUUID) {
225 sc.SetState(SCE_C_UUID);
226 lastWordWasUUID = false;
227 } else {
228 sc.SetState(SCE_C_IDENTIFIER);
229 }
230 } else if (sc.Match('/', '*')) {
231 if (sc.Match("/**") || sc.Match("/*!")) { // Support of Qt/Doxygen doc. style
232 sc.SetState(SCE_C_COMMENTDOC);
233 } else {
234 sc.SetState(SCE_C_COMMENT);
235 }
236 sc.Forward(); // Eat the * so it isn't used for the end of the comment
237 } else if (sc.Match('/', '/')) {
238 if (sc.Match("///") || sc.Match("//!")) // Support of Qt/Doxygen doc. style
239 sc.SetState(SCE_C_COMMENTLINEDOC);
240 else
241 sc.SetState(SCE_C_COMMENTLINE);
242 } else if (sc.ch == '/' && IsOKBeforeRE(chPrevNonWhite)) {
243 sc.SetState(SCE_C_REGEX);
244 } else if (sc.ch == '\"') {
245 sc.SetState(SCE_C_STRING);
246 } else if (sc.ch == '\'') {
247 sc.SetState(SCE_C_CHARACTER);
248 } else if (sc.ch == '#' && visibleChars == 0) {
249 // Preprocessor commands are alone on their line
250 sc.SetState(SCE_C_PREPROCESSOR);
251 // Skip whitespace between # and preprocessor word
252 do {
253 sc.Forward();
254 } while ((sc.ch == ' ' || sc.ch == '\t') && sc.More());
255 if (sc.atLineEnd) {
256 sc.SetState(SCE_C_DEFAULT);
257 }
258 } else if (isoperator(static_cast<char>(sc.ch))) {
259 sc.SetState(SCE_C_OPERATOR);
260 }
261 }
262
263 if (sc.atLineEnd) {
264 // Reset states to begining of colourise so no surprises
265 // if different sets of lines lexed.
266 chPrevNonWhite = ' ';
267 visibleChars = 0;
268 lastWordWasUUID = false;
269 }
270 if (!IsASpace(sc.ch)) {
271 chPrevNonWhite = sc.ch;
272 visibleChars++;
273 }
274 }
275 sc.Complete();
276 }
277
278 static bool IsStreamCommentStyle(int style) {
279 return style == SCE_C_COMMENT ||
280 style == SCE_C_COMMENTDOC ||
281 style == SCE_C_COMMENTDOCKEYWORD ||
282 style == SCE_C_COMMENTDOCKEYWORDERROR;
283 }
284
285 // Store both the current line's fold level and the next lines in the
286 // level store to make it easy to pick up with each increment
287 // and to make it possible to fiddle the current level for "} else {".
288 static void FoldNoBoxCppDoc(unsigned int startPos, int length, int initStyle,
289 Accessor &styler) {
290 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
291 bool foldPreprocessor = styler.GetPropertyInt("fold.preprocessor") != 0;
292 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
293 bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) != 0;
294 unsigned int endPos = startPos + length;
295 int visibleChars = 0;
296 int lineCurrent = styler.GetLine(startPos);
297 int levelCurrent = SC_FOLDLEVELBASE;
298 if (lineCurrent > 0)
299 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
300 int levelMinCurrent = levelCurrent;
301 int levelNext = levelCurrent;
302 char chNext = styler[startPos];
303 int styleNext = styler.StyleAt(startPos);
304 int style = initStyle;
305 for (unsigned int i = startPos; i < endPos; i++) {
306 char ch = chNext;
307 chNext = styler.SafeGetCharAt(i + 1);
308 int stylePrev = style;
309 style = styleNext;
310 styleNext = styler.StyleAt(i + 1);
311 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
312 if (foldComment && IsStreamCommentStyle(style)) {
313 if (!IsStreamCommentStyle(stylePrev)) {
314 levelNext++;
315 } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
316 // Comments don't end at end of line and the next character may be unstyled.
317 levelNext--;
318 }
319 }
320 if (foldComment && (style == SCE_C_COMMENTLINE)) {
321 if ((ch == '/') && (chNext == '/')) {
322 char chNext2 = styler.SafeGetCharAt(i + 2);
323 if (chNext2 == '{') {
324 levelNext++;
325 } else if (chNext2 == '}') {
326 levelNext--;
327 }
328 }
329 }
330 if (foldPreprocessor && (style == SCE_C_PREPROCESSOR)) {
331 if (ch == '#') {
332 unsigned int j = i + 1;
333 while ((j < endPos) && IsASpaceOrTab(styler.SafeGetCharAt(j))) {
334 j++;
335 }
336 if (styler.Match(j, "region") || styler.Match(j, "if")) {
337 levelNext++;
338 } else if (styler.Match(j, "end")) {
339 levelNext--;
340 }
341 }
342 }
343 if (style == SCE_C_OPERATOR) {
344 if (ch == '{') {
345 // Measure the minimum before a '{' to allow
346 // folding on "} else {"
347 if (levelMinCurrent > levelNext) {
348 levelMinCurrent = levelNext;
349 }
350 levelNext++;
351 } else if (ch == '}') {
352 levelNext--;
353 }
354 }
355 if (atEOL) {
356 int levelUse = levelCurrent;
357 if (foldAtElse) {
358 levelUse = levelMinCurrent;
359 }
360 int lev = levelUse | levelNext << 16;
361 if (visibleChars == 0 && foldCompact)
362 lev |= SC_FOLDLEVELWHITEFLAG;
363 if (levelUse < levelNext)
364 lev |= SC_FOLDLEVELHEADERFLAG;
365 if (lev != styler.LevelAt(lineCurrent)) {
366 styler.SetLevel(lineCurrent, lev);
367 }
368 lineCurrent++;
369 levelCurrent = levelNext;
370 levelMinCurrent = levelCurrent;
371 visibleChars = 0;
372 }
373 if (!isspacechar(ch))
374 visibleChars++;
375 }
376 }
377
378 static void FoldCppDoc(unsigned int startPos, int length, int initStyle, WordList *[],
379 Accessor &styler) {
380 FoldNoBoxCppDoc(startPos, length, initStyle, styler);
381 }
382
383 static const char * const cppWordLists[] = {
384 "Primary keywords and identifiers",
385 "Secondary keywords and identifiers",
386 "Documentation comment keywords",
387 "Unused",
388 "Global classes and typedefs",
389 0,
390 };
391
392 static void ColouriseCppDocSensitive(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
393 Accessor &styler) {
394 ColouriseCppDoc(startPos, length, initStyle, keywordlists, styler, true);
395 }
396
397 static void ColouriseCppDocInsensitive(unsigned int startPos, int length, int initStyle, WordList *keywordlists[],
398 Accessor &styler) {
399 ColouriseCppDoc(startPos, length, initStyle, keywordlists, styler, false);
400 }
401
402 LexerModule lmCPP(SCLEX_CPP, ColouriseCppDocSensitive, "cpp", FoldCppDoc, cppWordLists);
403 LexerModule lmCPPNoCase(SCLEX_CPPNOCASE, ColouriseCppDocInsensitive, "cppnocase", FoldCppDoc, cppWordLists);
404 LexerModule lmTCL(SCLEX_TCL, ColouriseCppDocSensitive, "tcl", FoldCppDoc, cppWordLists);