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
) {
50 return (state
<= SCE_ECL_COMMENTDOC
) ||
51 // including SCE_ECL_DEFAULT, SCE_ECL_COMMENT, SCE_ECL_COMMENTLINE
52 (state
== SCE_ECL_COMMENTLINEDOC
) || (state
== SCE_ECL_COMMENTDOCKEYWORD
) ||
53 (state
== SCE_ECL_COMMENTDOCKEYWORDERROR
);
56 static void ColouriseEclDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
58 WordList
&keywords0
= *keywordlists
[0];
59 WordList
&keywords1
= *keywordlists
[1];
60 WordList
&keywords2
= *keywordlists
[2];
61 WordList
&keywords3
= *keywordlists
[3]; //Value Types
62 WordList
&keywords4
= *keywordlists
[4];
63 WordList
&keywords5
= *keywordlists
[5];
64 WordList
&keywords6
= *keywordlists
[6]; //Javadoc Tags
66 cplusplus
.Set("beginc endc");
68 bool stylingWithinPreprocessor
= false;
70 CharacterSet
setOKBeforeRE(CharacterSet::setNone
, "(=,");
71 CharacterSet
setDoxygen(CharacterSet::setLower
, "$@\\&<>#{}[]");
72 CharacterSet
setWordStart(CharacterSet::setAlpha
, "_", 0x80, true);
73 CharacterSet
setWord(CharacterSet::setAlphaNum
, "._", 0x80, true);
74 CharacterSet
setQualified(CharacterSet::setNone
, "uUxX");
76 int chPrevNonWhite
= ' ';
78 bool lastWordWasUUID
= false;
79 int styleBeforeDCKeyword
= SCE_ECL_DEFAULT
;
80 bool continuationLine
= false;
82 if (initStyle
== SCE_ECL_PREPROCESSOR
) {
83 // Set continuationLine if last character of previous line is '\'
84 int lineCurrent
= styler
.GetLine(startPos
);
85 if (lineCurrent
> 0) {
86 int chBack
= styler
.SafeGetCharAt(startPos
-1, 0);
87 int chBack2
= styler
.SafeGetCharAt(startPos
-2, 0);
88 int lineEndChar
= '!';
89 if (chBack2
== '\r' && chBack
== '\n') {
90 lineEndChar
= styler
.SafeGetCharAt(startPos
-3, 0);
91 } else if (chBack
== '\n' || chBack
== '\r') {
92 lineEndChar
= chBack2
;
94 continuationLine
= lineEndChar
== '\\';
98 // look back to set chPrevNonWhite properly for better regex colouring
101 while (--back
&& IsSpaceEquiv(styler
.StyleAt(back
)))
103 if (styler
.StyleAt(back
) == SCE_ECL_OPERATOR
) {
104 chPrevNonWhite
= styler
.SafeGetCharAt(back
);
108 StyleContext
sc(startPos
, length
, initStyle
, styler
);
110 for (; sc
.More(); sc
.Forward()) {
111 if (sc
.atLineStart
) {
112 if (sc
.state
== SCE_ECL_STRING
) {
113 // Prevent SCE_ECL_STRINGEOL from leaking back to previous line which
114 // ends with a line continuation by locking in the state upto this position.
115 sc
.SetState(SCE_ECL_STRING
);
117 // Reset states to begining of colourise so no surprises
118 // if different sets of lines lexed.
120 lastWordWasUUID
= false;
123 // Handle line continuation generically.
125 if (sc
.chNext
== '\n' || sc
.chNext
== '\r') {
127 if (sc
.ch
== '\r' && sc
.chNext
== '\n') {
130 continuationLine
= true;
135 // Determine if the current state should terminate.
138 case SCE_ECL_DELETED
:
139 case SCE_ECL_CHANGED
:
142 sc
.SetState(SCE_ECL_DEFAULT
);
144 case SCE_ECL_OPERATOR
:
145 sc
.SetState(SCE_ECL_DEFAULT
);
148 // We accept almost anything because of hex. and number suffixes
149 if (!setWord
.Contains(sc
.ch
)) {
150 sc
.SetState(SCE_ECL_DEFAULT
);
153 case SCE_ECL_IDENTIFIER
:
154 if (!setWord
.Contains(sc
.ch
) || (sc
.ch
== '.')) {
156 sc
.GetCurrentLowered(s
, sizeof(s
));
157 if (keywords0
.InList(s
)) {
158 lastWordWasUUID
= strcmp(s
, "uuid") == 0;
159 sc
.ChangeState(SCE_ECL_WORD0
);
160 } else if (keywords1
.InList(s
)) {
161 sc
.ChangeState(SCE_ECL_WORD1
);
162 } else if (keywords2
.InList(s
)) {
163 sc
.ChangeState(SCE_ECL_WORD2
);
164 } else if (keywords4
.InList(s
)) {
165 sc
.ChangeState(SCE_ECL_WORD4
);
166 } else if (keywords5
.InList(s
)) {
167 sc
.ChangeState(SCE_ECL_WORD5
);
169 else //Data types are of from KEYWORD##
171 int i
= static_cast<int>(strlen(s
)) - 1;
172 while(i
>= 0 && (isdigit(s
[i
]) || s
[i
] == '_'))
176 strncpy(s2
, s
, i
+ 1);
178 if (keywords3
.InList(s2
)) {
179 sc
.ChangeState(SCE_ECL_WORD3
);
182 sc
.SetState(SCE_ECL_DEFAULT
);
185 case SCE_ECL_PREPROCESSOR
:
186 if (sc
.atLineStart
&& !continuationLine
) {
187 sc
.SetState(SCE_ECL_DEFAULT
);
188 } else if (stylingWithinPreprocessor
) {
189 if (IsASpace(sc
.ch
)) {
190 sc
.SetState(SCE_ECL_DEFAULT
);
193 if (sc
.Match('/', '*') || sc
.Match('/', '/')) {
194 sc
.SetState(SCE_ECL_DEFAULT
);
198 case SCE_ECL_COMMENT
:
199 if (sc
.Match('*', '/')) {
201 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
204 case SCE_ECL_COMMENTDOC
:
205 if (sc
.Match('*', '/')) {
207 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
208 } else if (sc
.ch
== '@' || sc
.ch
== '\\') { // JavaDoc and Doxygen support
209 // Verify that we have the conditions to mark a comment-doc-keyword
210 if ((IsASpace(sc
.chPrev
) || sc
.chPrev
== '*') && (!IsASpace(sc
.chNext
))) {
211 styleBeforeDCKeyword
= SCE_ECL_COMMENTDOC
;
212 sc
.SetState(SCE_ECL_COMMENTDOCKEYWORD
);
216 case SCE_ECL_COMMENTLINE
:
217 if (sc
.atLineStart
) {
218 sc
.SetState(SCE_ECL_DEFAULT
);
221 case SCE_ECL_COMMENTLINEDOC
:
222 if (sc
.atLineStart
) {
223 sc
.SetState(SCE_ECL_DEFAULT
);
224 } else if (sc
.ch
== '@' || sc
.ch
== '\\') { // JavaDoc and Doxygen support
225 // Verify that we have the conditions to mark a comment-doc-keyword
226 if ((IsASpace(sc
.chPrev
) || sc
.chPrev
== '/' || sc
.chPrev
== '!') && (!IsASpace(sc
.chNext
))) {
227 styleBeforeDCKeyword
= SCE_ECL_COMMENTLINEDOC
;
228 sc
.SetState(SCE_ECL_COMMENTDOCKEYWORD
);
232 case SCE_ECL_COMMENTDOCKEYWORD
:
233 if ((styleBeforeDCKeyword
== SCE_ECL_COMMENTDOC
) && sc
.Match('*', '/')) {
234 sc
.ChangeState(SCE_ECL_COMMENTDOCKEYWORDERROR
);
236 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
237 } else if (!setDoxygen
.Contains(sc
.ch
)) {
239 sc
.GetCurrentLowered(s
, sizeof(s
));
240 if (!IsASpace(sc
.ch
) || !keywords6
.InList(s
+1)) {
241 sc
.ChangeState(SCE_ECL_COMMENTDOCKEYWORDERROR
);
243 sc
.SetState(styleBeforeDCKeyword
);
248 sc
.ChangeState(SCE_ECL_STRINGEOL
);
249 } else if (sc
.ch
== '\\') {
250 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
253 } else if (sc
.ch
== '\"') {
254 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
257 case SCE_ECL_CHARACTER
:
259 sc
.ChangeState(SCE_ECL_STRINGEOL
);
260 } else if (sc
.ch
== '\\') {
261 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
264 } else if (sc
.ch
== '\'') {
265 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
269 if (sc
.atLineStart
) {
270 sc
.SetState(SCE_ECL_DEFAULT
);
271 } else if (sc
.ch
== '/') {
273 while ((sc
.ch
< 0x80) && islower(sc
.ch
))
274 sc
.Forward(); // gobble regex flags
275 sc
.SetState(SCE_ECL_DEFAULT
);
276 } else if (sc
.ch
== '\\') {
277 // Gobble up the quoted character
278 if (sc
.chNext
== '\\' || sc
.chNext
== '/') {
283 case SCE_ECL_STRINGEOL
:
284 if (sc
.atLineStart
) {
285 sc
.SetState(SCE_ECL_DEFAULT
);
288 case SCE_ECL_VERBATIM
:
290 if (sc
.chNext
== '\"') {
293 sc
.ForwardSetState(SCE_ECL_DEFAULT
);
298 if (sc
.ch
== '\r' || sc
.ch
== '\n' || sc
.ch
== ')') {
299 sc
.SetState(SCE_ECL_DEFAULT
);
304 // Determine if a new state should be entered.
305 int lineCurrent
= styler
.GetLine(sc
.currentPos
);
306 int lineState
= styler
.GetLineState(lineCurrent
);
307 if (sc
.state
== SCE_ECL_DEFAULT
) {
309 sc
.SetState(lineState
);
311 else if (sc
.Match('@', '\"')) {
312 sc
.SetState(SCE_ECL_VERBATIM
);
314 } else if (setQualified
.Contains(sc
.ch
) && sc
.chNext
== '\'') {
315 sc
.SetState(SCE_ECL_CHARACTER
);
317 } else if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
318 if (lastWordWasUUID
) {
319 sc
.SetState(SCE_ECL_UUID
);
320 lastWordWasUUID
= false;
322 sc
.SetState(SCE_ECL_NUMBER
);
324 } else if (setWordStart
.Contains(sc
.ch
) || (sc
.ch
== '@')) {
325 if (lastWordWasUUID
) {
326 sc
.SetState(SCE_ECL_UUID
);
327 lastWordWasUUID
= false;
329 sc
.SetState(SCE_ECL_IDENTIFIER
);
331 } else if (sc
.Match('/', '*')) {
332 if (sc
.Match("/**") || sc
.Match("/*!")) { // Support of Qt/Doxygen doc. style
333 sc
.SetState(SCE_ECL_COMMENTDOC
);
335 sc
.SetState(SCE_ECL_COMMENT
);
337 sc
.Forward(); // Eat the * so it isn't used for the end of the comment
338 } else if (sc
.Match('/', '/')) {
339 if ((sc
.Match("///") && !sc
.Match("////")) || sc
.Match("//!"))
340 // Support of Qt/Doxygen doc. style
341 sc
.SetState(SCE_ECL_COMMENTLINEDOC
);
343 sc
.SetState(SCE_ECL_COMMENTLINE
);
344 } else if (sc
.ch
== '/' && setOKBeforeRE
.Contains(chPrevNonWhite
)) {
345 sc
.SetState(SCE_ECL_REGEX
); // JavaScript's RegEx
346 // } else if (sc.ch == '\"') {
347 // sc.SetState(SCE_ECL_STRING);
348 } else if (sc
.ch
== '\'') {
349 sc
.SetState(SCE_ECL_CHARACTER
);
350 } else if (sc
.ch
== '#' && visibleChars
== 0) {
351 // Preprocessor commands are alone on their line
352 sc
.SetState(SCE_ECL_PREPROCESSOR
);
353 // Skip whitespace between # and preprocessor word
356 } while ((sc
.ch
== ' ' || sc
.ch
== '\t') && sc
.More());
358 sc
.SetState(SCE_ECL_DEFAULT
);
360 } else if (isoperator(static_cast<char>(sc
.ch
))) {
361 sc
.SetState(SCE_ECL_OPERATOR
);
365 if (!IsASpace(sc
.ch
) && !IsSpaceEquiv(sc
.state
)) {
366 chPrevNonWhite
= sc
.ch
;
369 continuationLine
= false;
375 static bool IsStreamCommentStyle(int style
) {
376 return style
== SCE_ECL_COMMENT
||
377 style
== SCE_ECL_COMMENTDOC
||
378 style
== SCE_ECL_COMMENTDOCKEYWORD
||
379 style
== SCE_ECL_COMMENTDOCKEYWORDERROR
;
382 bool MatchNoCase(Accessor
& styler
, unsigned int & pos
, const char *s
) {
385 char compare_char
= tolower(*s
);
386 char styler_char
= tolower(styler
.SafeGetCharAt(pos
+i
));
387 if (compare_char
!= styler_char
)
396 // Store both the current line's fold level and the next lines in the
397 // level store to make it easy to pick up with each increment
398 // and to make it possible to fiddle the current level for "} else {".
399 static void FoldEclDoc(unsigned int startPos
, int length
, int initStyle
,
400 WordList
*[], Accessor
&styler
) {
401 bool foldComment
= true;
402 bool foldPreprocessor
= true;
403 bool foldCompact
= true;
404 bool foldAtElse
= true;
405 unsigned int endPos
= startPos
+ length
;
406 int visibleChars
= 0;
407 int lineCurrent
= styler
.GetLine(startPos
);
408 int levelCurrent
= SC_FOLDLEVELBASE
;
410 levelCurrent
= styler
.LevelAt(lineCurrent
-1) >> 16;
411 int levelMinCurrent
= levelCurrent
;
412 int levelNext
= levelCurrent
;
413 char chNext
= styler
[startPos
];
414 int styleNext
= styler
.StyleAt(startPos
);
415 int style
= initStyle
;
416 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
418 chNext
= styler
.SafeGetCharAt(i
+ 1);
419 int stylePrev
= style
;
421 styleNext
= styler
.StyleAt(i
+ 1);
422 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
423 if (foldComment
&& IsStreamCommentStyle(style
)) {
424 if (!IsStreamCommentStyle(stylePrev
) && (stylePrev
!= SCE_ECL_COMMENTLINEDOC
)) {
426 } else if (!IsStreamCommentStyle(styleNext
) && (styleNext
!= SCE_ECL_COMMENTLINEDOC
) && !atEOL
) {
427 // Comments don't end at end of line and the next character may be unstyled.
431 if (foldComment
&& (style
== SCE_ECL_COMMENTLINE
)) {
432 if ((ch
== '/') && (chNext
== '/')) {
433 char chNext2
= styler
.SafeGetCharAt(i
+ 2);
434 if (chNext2
== '{') {
436 } else if (chNext2
== '}') {
441 if (foldPreprocessor
&& (style
== SCE_ECL_PREPROCESSOR
)) {
443 unsigned int j
= i
+ 1;
444 while ((j
< endPos
) && IsASpaceOrTab(styler
.SafeGetCharAt(j
))) {
447 if (MatchNoCase(styler
, j
, "region") || MatchNoCase(styler
, j
, "if")) {
449 } else if (MatchNoCase(styler
, j
, "endregion") || MatchNoCase(styler
, j
, "end")) {
454 if (style
== SCE_ECL_OPERATOR
) {
456 // Measure the minimum before a '{' to allow
457 // folding on "} else {"
458 if (levelMinCurrent
> levelNext
) {
459 levelMinCurrent
= levelNext
;
462 } else if (ch
== '}') {
466 if (style
== SCE_ECL_WORD2
) {
467 if (MatchNoCase(styler
, i
, "record") || MatchNoCase(styler
, i
, "transform") || MatchNoCase(styler
, i
, "type") || MatchNoCase(styler
, i
, "function") ||
468 MatchNoCase(styler
, i
, "module") || MatchNoCase(styler
, i
, "service") || MatchNoCase(styler
, i
, "interface") || MatchNoCase(styler
, i
, "ifblock") ||
469 MatchNoCase(styler
, i
, "macro") || MatchNoCase(styler
, i
, "beginc++")) {
471 } else if (MatchNoCase(styler
, i
, "endmacro") || MatchNoCase(styler
, i
, "endc++") || MatchNoCase(styler
, i
, "end")) {
475 if (atEOL
|| (i
== endPos
-1)) {
476 int levelUse
= levelCurrent
;
478 levelUse
= levelMinCurrent
;
480 int lev
= levelUse
| levelNext
<< 16;
481 if (visibleChars
== 0 && foldCompact
)
482 lev
|= SC_FOLDLEVELWHITEFLAG
;
483 if (levelUse
< levelNext
)
484 lev
|= SC_FOLDLEVELHEADERFLAG
;
485 if (lev
!= styler
.LevelAt(lineCurrent
)) {
486 styler
.SetLevel(lineCurrent
, lev
);
489 levelCurrent
= levelNext
;
490 levelMinCurrent
= levelCurrent
;
491 if (atEOL
&& (i
== static_cast<unsigned int>(styler
.Length()-1))) {
492 // There is an empty line at end of file so give it same level and empty
493 styler
.SetLevel(lineCurrent
, (levelCurrent
| levelCurrent
<< 16) | SC_FOLDLEVELWHITEFLAG
);
502 static const char * const EclWordListDesc
[] = {