1 // Scintilla source code edit control
3 ** Lexer for C++, C, Java, and Javascript.
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.
18 #include "StyleContext.h"
20 #include "Scintilla.h"
23 static bool IsOKBeforeRE(const int ch
) {
24 return (ch
== '(') || (ch
== '=') || (ch
== ',');
27 static inline bool IsAWordChar(const int ch
) {
28 return (ch
< 0x80) && (isalnum(ch
) || ch
== '.' || ch
== '_');
31 static inline bool IsAWordStart(const int ch
) {
32 return (ch
< 0x80) && (isalnum(ch
) || ch
== '_');
35 static inline bool IsADoxygenChar(const int ch
) {
36 return (islower(ch
) || ch
== '$' || ch
== '@' ||
37 ch
== '\\' || ch
== '&' || ch
== '<' ||
38 ch
== '>' || ch
== '#' || ch
== '{' ||
39 ch
== '}' || ch
== '[' || ch
== ']');
42 static inline bool IsStateComment(const int state
) {
43 return ((state
== SCE_C_COMMENT
) ||
44 (state
== SCE_C_COMMENTLINE
) ||
45 (state
== SCE_C_COMMENTDOC
) ||
46 (state
== SCE_C_COMMENTDOCKEYWORD
) ||
47 (state
== SCE_C_COMMENTDOCKEYWORDERROR
));
50 static inline bool IsStateString(const int state
) {
51 return ((state
== SCE_C_STRING
) || (state
== SCE_C_VERBATIM
));
54 static void ColouriseCppDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
57 WordList
&keywords
= *keywordlists
[0];
58 WordList
&keywords2
= *keywordlists
[1];
59 WordList
&keywords3
= *keywordlists
[2];
61 bool stylingWithinPreprocessor
= styler
.GetPropertyInt("styling.within.preprocessor") != 0;
63 // Do not leak onto next line
64 if (initStyle
== SCE_C_STRINGEOL
)
65 initStyle
= SCE_C_DEFAULT
;
67 int chPrevNonWhite
= ' ';
69 bool lastWordWasUUID
= false;
71 StyleContext
sc(startPos
, length
, initStyle
, styler
);
73 for (; sc
.More(); sc
.Forward()) {
75 // Handle line continuation generically.
77 if (sc
.Match("\\\n")) {
82 if (sc
.Match("\\\r\n")) {
90 // Determine if the current state should terminate.
91 if (sc
.state
== SCE_C_OPERATOR
) {
92 sc
.SetState(SCE_C_DEFAULT
);
93 } else if (sc
.state
== SCE_C_NUMBER
) {
94 if (!IsAWordChar(sc
.ch
)) {
95 sc
.SetState(SCE_C_DEFAULT
);
97 } else if (sc
.state
== SCE_C_IDENTIFIER
) {
98 if (!IsAWordChar(sc
.ch
) || (sc
.ch
== '.')) {
100 sc
.GetCurrent(s
, sizeof(s
));
101 if (keywords
.InList(s
)) {
102 lastWordWasUUID
= strcmp(s
, "uuid") == 0;
103 sc
.ChangeState(SCE_C_WORD
);
104 } else if (keywords2
.InList(s
)) {
105 sc
.ChangeState(SCE_C_WORD2
);
107 sc
.SetState(SCE_C_DEFAULT
);
109 } else if (sc
.state
== SCE_C_PREPROCESSOR
) {
110 if (stylingWithinPreprocessor
) {
111 if (IsASpace(sc
.ch
)) {
112 sc
.SetState(SCE_C_DEFAULT
);
116 sc
.SetState(SCE_C_DEFAULT
);
119 } else if (sc
.state
== SCE_C_COMMENT
) {
120 if (sc
.Match('*', '/')) {
122 sc
.ForwardSetState(SCE_C_DEFAULT
);
124 } else if (sc
.state
== SCE_C_COMMENTDOC
) {
125 if (sc
.Match('*', '/')) {
127 sc
.ForwardSetState(SCE_C_DEFAULT
);
128 } else if (sc
.ch
== '@' || sc
.ch
== '\\') {
129 sc
.SetState(SCE_C_COMMENTDOCKEYWORD
);
131 } else if (sc
.state
== SCE_C_COMMENTLINE
|| sc
.state
== SCE_C_COMMENTLINEDOC
) {
133 sc
.SetState(SCE_C_DEFAULT
);
136 } else if (sc
.state
== SCE_C_COMMENTDOCKEYWORD
) {
137 if (sc
.Match('*', '/')) {
138 sc
.ChangeState(SCE_C_COMMENTDOCKEYWORDERROR
);
140 sc
.ForwardSetState(SCE_C_DEFAULT
);
141 } else if (!IsADoxygenChar(sc
.ch
)) {
143 sc
.GetCurrent(s
, sizeof(s
));
144 if (!isspace(sc
.ch
) || !keywords3
.InList(s
+1)) {
145 sc
.ChangeState(SCE_C_COMMENTDOCKEYWORDERROR
);
147 sc
.SetState(SCE_C_COMMENTDOC
);
149 } else if (sc
.state
== SCE_C_STRING
) {
151 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
154 } else if (sc
.ch
== '\"') {
155 sc
.ForwardSetState(SCE_C_DEFAULT
);
156 } else if (sc
.atLineEnd
) {
157 sc
.ChangeState(SCE_C_STRINGEOL
);
158 sc
.ForwardSetState(SCE_C_DEFAULT
);
161 } else if (sc
.state
== SCE_C_CHARACTER
) {
163 sc
.ChangeState(SCE_C_STRINGEOL
);
164 sc
.ForwardSetState(SCE_C_DEFAULT
);
166 } else if (sc
.ch
== '\\') {
167 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
170 } else if (sc
.ch
== '\'') {
171 sc
.ForwardSetState(SCE_C_DEFAULT
);
173 } else if (sc
.state
== SCE_C_REGEX
) {
174 if (sc
.ch
== '\r' || sc
.ch
== '\n' || sc
.ch
== '/') {
175 sc
.ForwardSetState(SCE_C_DEFAULT
);
176 } else if (sc
.ch
== '\\') {
177 // Gobble up the quoted character
178 if (sc
.chNext
== '\\' || sc
.chNext
== '/') {
182 } else if (sc
.state
== SCE_C_VERBATIM
) {
184 if (sc
.chNext
== '\"') {
187 sc
.ForwardSetState(SCE_C_DEFAULT
);
190 } else if (sc
.state
== SCE_C_UUID
) {
191 if (sc
.ch
== '\r' || sc
.ch
== '\n' || sc
.ch
== ')') {
192 sc
.SetState(SCE_C_DEFAULT
);
196 // Determine if a new state should be entered.
197 if (sc
.state
== SCE_C_DEFAULT
) {
198 if (sc
.Match('@', '\"')) {
199 sc
.SetState(SCE_C_VERBATIM
);
201 } else if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
202 if (lastWordWasUUID
) {
203 sc
.SetState(SCE_C_UUID
);
204 lastWordWasUUID
= false;
206 sc
.SetState(SCE_C_NUMBER
);
208 } else if (IsAWordStart(sc
.ch
) || (sc
.ch
== '@')) {
209 if (lastWordWasUUID
) {
210 sc
.SetState(SCE_C_UUID
);
211 lastWordWasUUID
= false;
213 sc
.SetState(SCE_C_IDENTIFIER
);
215 } else if (sc
.Match('/', '*')) {
216 if (sc
.Match("/**") || sc
.Match("/*!")) { // Support of Qt/Doxygen doc. style
217 sc
.SetState(SCE_C_COMMENTDOC
);
219 sc
.SetState(SCE_C_COMMENT
);
221 sc
.Forward(); // Eat the * so it isn't used for the end of the comment
222 } else if (sc
.Match('/', '/')) {
223 if (sc
.Match("///") || sc
.Match("//!")) // Support of Qt/Doxygen doc. style
224 sc
.SetState(SCE_C_COMMENTLINEDOC
);
226 sc
.SetState(SCE_C_COMMENTLINE
);
227 } else if (sc
.ch
== '/' && IsOKBeforeRE(chPrevNonWhite
)) {
228 sc
.SetState(SCE_C_REGEX
);
229 } else if (sc
.ch
== '\"') {
230 sc
.SetState(SCE_C_STRING
);
231 } else if (sc
.ch
== '\'') {
232 sc
.SetState(SCE_C_CHARACTER
);
233 } else if (sc
.ch
== '#' && visibleChars
== 0) {
234 // Preprocessor commands are alone on their line
235 sc
.SetState(SCE_C_PREPROCESSOR
);
236 // Skip whitespace between # and preprocessor word
239 } while ((sc
.ch
== ' ') && (sc
.ch
== '\t') && sc
.More());
241 sc
.SetState(SCE_C_DEFAULT
);
243 } else if (isoperator(static_cast<char>(sc
.ch
))) {
244 sc
.SetState(SCE_C_OPERATOR
);
249 // Reset states to begining of colourise so no surprises
250 // if different sets of lines lexed.
251 chPrevNonWhite
= ' ';
253 lastWordWasUUID
= false;
255 if (!IsASpace(sc
.ch
)) {
256 chPrevNonWhite
= sc
.ch
;
263 static void FoldCppDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*[],
265 bool foldComment
= styler
.GetPropertyInt("fold.comment") != 0;
266 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
267 unsigned int endPos
= startPos
+ length
;
268 int visibleChars
= 0;
269 int lineCurrent
= styler
.GetLine(startPos
);
270 int levelPrev
= styler
.LevelAt(lineCurrent
) & SC_FOLDLEVELNUMBERMASK
;
271 int levelCurrent
= levelPrev
;
272 char chNext
= styler
[startPos
];
273 int styleNext
= styler
.StyleAt(startPos
);
274 int style
= initStyle
;
275 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
277 chNext
= styler
.SafeGetCharAt(i
+ 1);
278 int stylePrev
= style
;
280 styleNext
= styler
.StyleAt(i
+ 1);
281 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
283 (style
== SCE_C_COMMENT
|| style
== SCE_C_COMMENTDOC
)) {
284 if (style
!= stylePrev
) {
286 } else if ((style
!= styleNext
) && !atEOL
) {
287 // Comments don't end at end of line and the next character may be unstyled.
291 if (style
== SCE_C_OPERATOR
) {
294 } else if (ch
== '}') {
300 if (visibleChars
== 0 && foldCompact
)
301 lev
|= SC_FOLDLEVELWHITEFLAG
;
302 if ((levelCurrent
> levelPrev
) && (visibleChars
> 0))
303 lev
|= SC_FOLDLEVELHEADERFLAG
;
304 if (lev
!= styler
.LevelAt(lineCurrent
)) {
305 styler
.SetLevel(lineCurrent
, lev
);
308 levelPrev
= levelCurrent
;
311 if (!isspacechar(ch
))
314 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
315 int flagsNext
= styler
.LevelAt(lineCurrent
) & ~SC_FOLDLEVELNUMBERMASK
;
316 styler
.SetLevel(lineCurrent
, levelPrev
| flagsNext
);
319 LexerModule
lmCPP(SCLEX_CPP
, ColouriseCppDoc
, "cpp", FoldCppDoc
);
320 LexerModule
lmTCL(SCLEX_TCL
, ColouriseCppDoc
, "tcl", FoldCppDoc
);