1 // Scintilla source code edit control
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
16 #pragma warning(disable: 4786)
19 // Borland C++ displays warnings in vector header without this
20 #pragma option -w-ccc -w-rch
29 #include "Scintilla.h"
32 #include "PropSetSimple.h"
34 #include "LexAccessor.h"
36 #include "StyleContext.h"
37 #include "CharacterSet.h"
38 #include "LexerModule.h"
39 #include "OptionSet.h"
41 #define SET_LOWER "abcdefghijklmnopqrstuvwxyz"
42 #define SET_UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
43 #define SET_DIGITS "0123456789"
46 using namespace Scintilla
;
49 static bool IsSpaceEquiv(int state
) {
53 case SCE_ECL_COMMENTLINE
:
54 case SCE_ECL_COMMENTLINEDOC
:
55 case SCE_ECL_COMMENTDOCKEYWORD
:
56 case SCE_ECL_COMMENTDOCKEYWORDERROR
:
57 case SCE_ECL_COMMENTDOC
:
65 static void ColouriseEclDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
67 WordList
&keywords0
= *keywordlists
[0];
68 WordList
&keywords1
= *keywordlists
[1];
69 WordList
&keywords2
= *keywordlists
[2];
70 WordList
&keywords3
= *keywordlists
[3]; //Value Types
71 WordList
&keywords4
= *keywordlists
[4];
72 WordList
&keywords5
= *keywordlists
[5];
73 WordList
&keywords6
= *keywordlists
[6]; //Javadoc Tags
75 cplusplus
.Set("beginc endc");
77 bool stylingWithinPreprocessor
= false;
79 CharacterSet
setOKBeforeRE(CharacterSet::setNone
, "(=,");
80 CharacterSet
setDoxygen(CharacterSet::setLower
, "$@\\&<>#{}[]");
81 CharacterSet
setWordStart(CharacterSet::setAlpha
, "_", 0x80, true);
82 CharacterSet
setWord(CharacterSet::setAlphaNum
, "._", 0x80, true);
83 CharacterSet
setQualified(CharacterSet::setNone
, "uUxX");
85 int chPrevNonWhite
= ' ';
87 bool lastWordWasUUID
= false;
88 int styleBeforeDCKeyword
= SCE_ECL_DEFAULT
;
89 bool continuationLine
= false;
91 if (initStyle
== SCE_ECL_PREPROCESSOR
) {
92 // Set continuationLine if last character of previous line is '\'
93 int lineCurrent
= styler
.GetLine(startPos
);
94 if (lineCurrent
> 0) {
95 int chBack
= styler
.SafeGetCharAt(startPos
-1, 0);
96 int chBack2
= styler
.SafeGetCharAt(startPos
-2, 0);
97 int lineEndChar
= '!';
98 if (chBack2
== '\r' && chBack
== '\n') {
99 lineEndChar
= styler
.SafeGetCharAt(startPos
-3, 0);
100 } else if (chBack
== '\n' || chBack
== '\r') {
101 lineEndChar
= chBack2
;
103 continuationLine
= lineEndChar
== '\\';
107 // look back to set chPrevNonWhite properly for better regex colouring
110 while (--back
&& IsSpaceEquiv(styler
.StyleAt(back
)))
112 if (styler
.StyleAt(back
) == SCE_ECL_OPERATOR
) {
113 chPrevNonWhite
= styler
.SafeGetCharAt(back
);
117 StyleContext
sc(startPos
, length
, initStyle
, styler
);
119 for (; sc
.More(); sc
.Forward()) {
120 if (sc
.atLineStart
) {
121 if (sc
.state
== SCE_ECL_STRING
) {
122 // Prevent SCE_ECL_STRINGEOL from leaking back to previous line which
123 // ends with a line continuation by locking in the state upto this position.
124 sc
.SetState(SCE_ECL_STRING
);
126 // Reset states to begining of colourise so no surprises
127 // if different sets of lines lexed.
129 lastWordWasUUID
= false;
132 // Handle line continuation generically.
134 if (sc
.chNext
== '\n' || sc
.chNext
== '\r') {
136 if (sc
.ch
== '\r' && sc
.chNext
== '\n') {
139 continuationLine
= true;
144 // Determine if the current state should terminate.
147 case SCE_ECL_DELETED
:
148 case SCE_ECL_CHANGED
:
151 sc
.SetState(SCE_ECL_DEFAULT
);
153 case SCE_ECL_OPERATOR
:
154 sc
.SetState(SCE_ECL_DEFAULT
);
157 // We accept almost anything because of hex. and number suffixes
158 if (!setWord
.Contains(sc
.ch
)) {
159 sc
.SetState(SCE_ECL_DEFAULT
);
162 case SCE_ECL_IDENTIFIER
:
163 if (!setWord
.Contains(sc
.ch
) || (sc
.ch
== '.')) {
165 sc
.GetCurrentLowered(s
, sizeof(s
));
166 if (keywords0
.InList(s
)) {
167 lastWordWasUUID
= strcmp(s
, "uuid") == 0;
168 sc
.ChangeState(SCE_ECL_WORD0
);
169 } else if (keywords1
.InList(s
)) {
170 sc
.ChangeState(SCE_ECL_WORD1
);
171 } else if (keywords2
.InList(s
)) {
172 sc
.ChangeState(SCE_ECL_WORD2
);
173 } else if (keywords4
.InList(s
)) {
174 sc
.ChangeState(SCE_ECL_WORD4
);
175 } else if (keywords5
.InList(s
)) {
176 sc
.ChangeState(SCE_ECL_WORD5
);
178 else //Data types are of from KEYWORD##
180 int i
= static_cast<int>(strlen(s
)) - 1;
181 while(i
>= 0 && (isdigit(s
[i
]) || s
[i
] == '_'))
185 strncpy(s2
, s
, i
+ 1);
187 if (keywords3
.InList(s2
)) {
188 sc
.ChangeState(SCE_ECL_WORD3
);
191 sc
.SetState(SCE_ECL_DEFAULT
);
194 case SCE_ECL_PREPROCESSOR
:
195 if (sc
.atLineStart
&& !continuationLine
) {
196 sc
.SetState(SCE_ECL_DEFAULT
);
197 } else if (stylingWithinPreprocessor
) {
198 if (IsASpace(sc
.ch
)) {
199 sc
.SetState(SCE_ECL_DEFAULT
);
202 if (sc
.Match('/', '*') || sc
.Match('/', '/')) {
203 sc
.SetState(SCE_ECL_DEFAULT
);
207 case SCE_ECL_COMMENT
:
208 if (sc
.Match('*', '/')) {
210 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
213 case SCE_ECL_COMMENTDOC
:
214 if (sc
.Match('*', '/')) {
216 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
217 } else if (sc
.ch
== '@' || sc
.ch
== '\\') { // JavaDoc and Doxygen support
218 // Verify that we have the conditions to mark a comment-doc-keyword
219 if ((IsASpace(sc
.chPrev
) || sc
.chPrev
== '*') && (!IsASpace(sc
.chNext
))) {
220 styleBeforeDCKeyword
= SCE_ECL_COMMENTDOC
;
221 sc
.SetState(SCE_ECL_COMMENTDOCKEYWORD
);
225 case SCE_ECL_COMMENTLINE
:
226 if (sc
.atLineStart
) {
227 sc
.SetState(SCE_ECL_DEFAULT
);
230 case SCE_ECL_COMMENTLINEDOC
:
231 if (sc
.atLineStart
) {
232 sc
.SetState(SCE_ECL_DEFAULT
);
233 } else if (sc
.ch
== '@' || sc
.ch
== '\\') { // JavaDoc and Doxygen support
234 // Verify that we have the conditions to mark a comment-doc-keyword
235 if ((IsASpace(sc
.chPrev
) || sc
.chPrev
== '/' || sc
.chPrev
== '!') && (!IsASpace(sc
.chNext
))) {
236 styleBeforeDCKeyword
= SCE_ECL_COMMENTLINEDOC
;
237 sc
.SetState(SCE_ECL_COMMENTDOCKEYWORD
);
241 case SCE_ECL_COMMENTDOCKEYWORD
:
242 if ((styleBeforeDCKeyword
== SCE_ECL_COMMENTDOC
) && sc
.Match('*', '/')) {
243 sc
.ChangeState(SCE_ECL_COMMENTDOCKEYWORDERROR
);
245 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
246 } else if (!setDoxygen
.Contains(sc
.ch
)) {
248 sc
.GetCurrentLowered(s
, sizeof(s
));
249 if (!IsASpace(sc
.ch
) || !keywords6
.InList(s
+1)) {
250 sc
.ChangeState(SCE_ECL_COMMENTDOCKEYWORDERROR
);
252 sc
.SetState(styleBeforeDCKeyword
);
257 sc
.ChangeState(SCE_ECL_STRINGEOL
);
258 } else if (sc
.ch
== '\\') {
259 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
262 } else if (sc
.ch
== '\"') {
263 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
266 case SCE_ECL_CHARACTER
:
268 sc
.ChangeState(SCE_ECL_STRINGEOL
);
269 } else if (sc
.ch
== '\\') {
270 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
273 } else if (sc
.ch
== '\'') {
274 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
278 if (sc
.atLineStart
) {
279 sc
.SetState(SCE_ECL_DEFAULT
);
280 } else if (sc
.ch
== '/') {
282 while ((sc
.ch
< 0x80) && islower(sc
.ch
))
283 sc
.Forward(); // gobble regex flags
284 sc
.SetState(SCE_ECL_DEFAULT
);
285 } else if (sc
.ch
== '\\') {
286 // Gobble up the quoted character
287 if (sc
.chNext
== '\\' || sc
.chNext
== '/') {
292 case SCE_ECL_STRINGEOL
:
293 if (sc
.atLineStart
) {
294 sc
.SetState(SCE_ECL_DEFAULT
);
297 case SCE_ECL_VERBATIM
:
299 if (sc
.chNext
== '\"') {
302 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
307 if (sc
.ch
== '\r' || sc
.ch
== '\n' || sc
.ch
== ')') {
308 sc
.SetState(SCE_ECL_DEFAULT
);
313 // Determine if a new state should be entered.
314 int lineCurrent
= styler
.GetLine(sc
.currentPos
);
315 int lineState
= styler
.GetLineState(lineCurrent
);
316 if (sc
.state
== SCE_ECL_DEFAULT
) {
318 sc
.SetState(lineState
);
320 else if (sc
.Match('@', '\"')) {
321 sc
.SetState(SCE_ECL_VERBATIM
);
323 } else if (setQualified
.Contains(sc
.ch
) && sc
.chNext
== '\'') {
324 sc
.SetState(SCE_ECL_CHARACTER
);
326 } else if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
327 if (lastWordWasUUID
) {
328 sc
.SetState(SCE_ECL_UUID
);
329 lastWordWasUUID
= false;
331 sc
.SetState(SCE_ECL_NUMBER
);
333 } else if (setWordStart
.Contains(sc
.ch
) || (sc
.ch
== '@')) {
334 if (lastWordWasUUID
) {
335 sc
.SetState(SCE_ECL_UUID
);
336 lastWordWasUUID
= false;
338 sc
.SetState(SCE_ECL_IDENTIFIER
);
340 } else if (sc
.Match('/', '*')) {
341 if (sc
.Match("/**") || sc
.Match("/*!")) { // Support of Qt/Doxygen doc. style
342 sc
.SetState(SCE_ECL_COMMENTDOC
);
344 sc
.SetState(SCE_ECL_COMMENT
);
346 sc
.Forward(); // Eat the * so it isn't used for the end of the comment
347 } else if (sc
.Match('/', '/')) {
348 if ((sc
.Match("///") && !sc
.Match("////")) || sc
.Match("//!"))
349 // Support of Qt/Doxygen doc. style
350 sc
.SetState(SCE_ECL_COMMENTLINEDOC
);
352 sc
.SetState(SCE_ECL_COMMENTLINE
);
353 } else if (sc
.ch
== '/' && setOKBeforeRE
.Contains(chPrevNonWhite
)) {
354 sc
.SetState(SCE_ECL_REGEX
); // JavaScript's RegEx
355 // } else if (sc.ch == '\"') {
356 // sc.SetState(SCE_ECL_STRING);
357 } else if (sc
.ch
== '\'') {
358 sc
.SetState(SCE_ECL_CHARACTER
);
359 } else if (sc
.ch
== '#' && visibleChars
== 0) {
360 // Preprocessor commands are alone on their line
361 sc
.SetState(SCE_ECL_PREPROCESSOR
);
362 // Skip whitespace between # and preprocessor word
365 } while ((sc
.ch
== ' ' || sc
.ch
== '\t') && sc
.More());
367 sc
.SetState(SCE_ECL_DEFAULT
);
369 } else if (isoperator(static_cast<char>(sc
.ch
))) {
370 sc
.SetState(SCE_ECL_OPERATOR
);
374 if (!IsASpace(sc
.ch
) && !IsSpaceEquiv(sc
.state
)) {
375 chPrevNonWhite
= sc
.ch
;
378 continuationLine
= false;
384 static bool IsStreamCommentStyle(int style
) {
385 return style
== SCE_ECL_COMMENT
||
386 style
== SCE_ECL_COMMENTDOC
||
387 style
== SCE_ECL_COMMENTDOCKEYWORD
||
388 style
== SCE_ECL_COMMENTDOCKEYWORDERROR
;
391 bool MatchNoCase(Accessor
& styler
, unsigned int & pos
, const char *s
) {
394 char compare_char
= tolower(*s
);
395 char styler_char
= tolower(styler
.SafeGetCharAt(pos
+i
));
396 if (compare_char
!= styler_char
)
405 // Store both the current line's fold level and the next lines in the
406 // level store to make it easy to pick up with each increment
407 // and to make it possible to fiddle the current level for "} else {".
408 static void FoldEclDoc(unsigned int startPos
, int length
, int initStyle
,
409 WordList
*[], Accessor
&styler
) {
410 bool foldComment
= true;
411 bool foldPreprocessor
= true;
412 bool foldCompact
= true;
413 bool foldAtElse
= true;
414 unsigned int endPos
= startPos
+ length
;
415 int visibleChars
= 0;
416 int lineCurrent
= styler
.GetLine(startPos
);
417 int levelCurrent
= SC_FOLDLEVELBASE
;
419 levelCurrent
= styler
.LevelAt(lineCurrent
-1) >> 16;
420 int levelMinCurrent
= levelCurrent
;
421 int levelNext
= levelCurrent
;
422 char chNext
= styler
[startPos
];
423 int styleNext
= styler
.StyleAt(startPos
);
424 int style
= initStyle
;
425 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
427 chNext
= styler
.SafeGetCharAt(i
+ 1);
428 int stylePrev
= style
;
430 styleNext
= styler
.StyleAt(i
+ 1);
431 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
432 if (foldComment
&& IsStreamCommentStyle(style
)) {
433 if (!IsStreamCommentStyle(stylePrev
) && (stylePrev
!= SCE_ECL_COMMENTLINEDOC
)) {
435 } else if (!IsStreamCommentStyle(styleNext
) && (styleNext
!= SCE_ECL_COMMENTLINEDOC
) && !atEOL
) {
436 // Comments don't end at end of line and the next character may be unstyled.
440 if (foldComment
&& (style
== SCE_ECL_COMMENTLINE
)) {
441 if ((ch
== '/') && (chNext
== '/')) {
442 char chNext2
= styler
.SafeGetCharAt(i
+ 2);
443 if (chNext2
== '{') {
445 } else if (chNext2
== '}') {
450 if (foldPreprocessor
&& (style
== SCE_ECL_PREPROCESSOR
)) {
452 unsigned int j
= i
+ 1;
453 while ((j
< endPos
) && IsASpaceOrTab(styler
.SafeGetCharAt(j
))) {
456 if (MatchNoCase(styler
, j
, "region") || MatchNoCase(styler
, j
, "if")) {
458 } else if (MatchNoCase(styler
, j
, "endregion") || MatchNoCase(styler
, j
, "end")) {
463 if (style
== SCE_ECL_OPERATOR
) {
465 // Measure the minimum before a '{' to allow
466 // folding on "} else {"
467 if (levelMinCurrent
> levelNext
) {
468 levelMinCurrent
= levelNext
;
471 } else if (ch
== '}') {
475 if (style
== SCE_ECL_WORD2
) {
476 if (MatchNoCase(styler
, i
, "record") || MatchNoCase(styler
, i
, "transform") || MatchNoCase(styler
, i
, "type") || MatchNoCase(styler
, i
, "function") ||
477 MatchNoCase(styler
, i
, "module") || MatchNoCase(styler
, i
, "service") || MatchNoCase(styler
, i
, "interface") || MatchNoCase(styler
, i
, "ifblock") ||
478 MatchNoCase(styler
, i
, "macro") || MatchNoCase(styler
, i
, "beginc++")) {
480 } else if (MatchNoCase(styler
, i
, "endmacro") || MatchNoCase(styler
, i
, "endc++") || MatchNoCase(styler
, i
, "end")) {
484 if (atEOL
|| (i
== endPos
-1)) {
485 int levelUse
= levelCurrent
;
487 levelUse
= levelMinCurrent
;
489 int lev
= levelUse
| levelNext
<< 16;
490 if (visibleChars
== 0 && foldCompact
)
491 lev
|= SC_FOLDLEVELWHITEFLAG
;
492 if (levelUse
< levelNext
)
493 lev
|= SC_FOLDLEVELHEADERFLAG
;
494 if (lev
!= styler
.LevelAt(lineCurrent
)) {
495 styler
.SetLevel(lineCurrent
, lev
);
498 levelCurrent
= levelNext
;
499 levelMinCurrent
= levelCurrent
;
500 if (atEOL
&& (i
== static_cast<unsigned int>(styler
.Length()-1))) {
501 // There is an empty line at end of file so give it same level and empty
502 styler
.SetLevel(lineCurrent
, (levelCurrent
| levelCurrent
<< 16) | SC_FOLDLEVELWHITEFLAG
);
511 static const char * const EclWordListDesc
[] = {