]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/stc/scintilla/src/LexCPP.cxx
4f042bd61dd82f077177bf0a008c82371f063c82
1 // SciTE - Scintilla based Text Editor
2 // LexCPP.cxx - lexer for C++, C, Java, and Javascript
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
17 #include "Scintilla.h"
20 static bool classifyWordCpp(unsigned int start
, unsigned int end
, WordList
&keywords
, Accessor
&styler
) {
22 bool wordIsNumber
= isdigit(styler
[start
]) || (styler
[start
] == '.');
23 bool wordIsUUID
= false;
24 for (unsigned int i
= 0; i
< end
- start
+ 1 && i
< 30; i
++) {
25 s
[i
] = styler
[start
+ i
];
28 char chAttr
= SCE_C_IDENTIFIER
;
30 chAttr
= SCE_C_NUMBER
;
32 if (keywords
.InList(s
)) {
34 wordIsUUID
= strcmp(s
, "uuid") == 0;
37 styler
.ColourTo(end
, chAttr
);
41 static void ColouriseCppDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*keywordlists
[],
44 WordList
&keywords
= *keywordlists
[0];
46 styler
.StartAt(startPos
);
48 bool fold
= styler
.GetPropertyInt("fold");
49 int lineCurrent
= styler
.GetLine(startPos
);
50 int levelPrev
= styler
.LevelAt(lineCurrent
) & SC_FOLDLEVELNUMBERMASK
;
51 int levelCurrent
= levelPrev
;
53 int state
= initStyle
;
54 if (state
== SCE_C_STRINGEOL
) // Does not leak onto next line
55 state
= SCE_C_DEFAULT
;
57 char chNext
= styler
[startPos
];
58 unsigned int lengthDoc
= startPos
+ length
;
60 styler
.StartSegment(startPos
);
61 bool lastWordWasUUID
= false;
62 for (unsigned int i
= startPos
; i
< lengthDoc
; i
++) {
64 chNext
= styler
.SafeGetCharAt(i
+ 1);
66 if ((ch
== '\r' && chNext
!= '\n') || (ch
== '\n')) {
68 if (state
== SCE_C_STRINGEOL
) {
69 styler
.ColourTo(i
, state
);
70 state
= SCE_C_DEFAULT
;
75 lev
|= SC_FOLDLEVELWHITEFLAG
;
76 if ((levelCurrent
> levelPrev
) && (visChars
> 0))
77 lev
|= SC_FOLDLEVELHEADERFLAG
;
78 styler
.SetLevel(lineCurrent
, lev
);
81 levelPrev
= levelCurrent
;
87 if (styler
.IsLeadByte(ch
)) {
88 chNext
= styler
.SafeGetCharAt(i
+ 2);
94 if (state
== SCE_C_DEFAULT
) {
95 if (iswordstart(ch
)) {
96 styler
.ColourTo(i
-1, state
);
97 if (lastWordWasUUID
) {
99 lastWordWasUUID
= false;
103 } else if (ch
== '/' && chNext
== '*') {
104 styler
.ColourTo(i
-1, state
);
105 if (styler
.SafeGetCharAt(i
+ 2) == '*')
106 state
= SCE_C_COMMENTDOC
;
108 state
= SCE_C_COMMENT
;
109 } else if (ch
== '/' && chNext
== '/') {
110 styler
.ColourTo(i
-1, state
);
111 state
= SCE_C_COMMENTLINE
;
112 } else if (ch
== '\"') {
113 styler
.ColourTo(i
-1, state
);
114 state
= SCE_C_STRING
;
115 } else if (ch
== '\'') {
116 styler
.ColourTo(i
-1, state
);
117 state
= SCE_C_CHARACTER
;
118 } else if (ch
== '#') {
119 styler
.ColourTo(i
-1, state
);
120 state
= SCE_C_PREPROCESSOR
;
121 } else if (isoperator(ch
)) {
122 styler
.ColourTo(i
-1, state
);
123 styler
.ColourTo(i
, SCE_C_OPERATOR
);
124 if ((ch
== '{') || (ch
== '}')) {
125 levelCurrent
+= (ch
== '{') ? 1 : -1;
128 } else if (state
== SCE_C_WORD
) {
129 if (!iswordchar(ch
)) {
130 lastWordWasUUID
= classifyWordCpp(styler
.GetStartSegment(), i
- 1, keywords
, styler
);
131 state
= SCE_C_DEFAULT
;
132 if (ch
== '/' && chNext
== '*') {
133 if (styler
.SafeGetCharAt(i
+ 2) == '*')
134 state
= SCE_C_COMMENTDOC
;
136 state
= SCE_C_COMMENT
;
137 } else if (ch
== '/' && chNext
== '/') {
138 state
= SCE_C_COMMENTLINE
;
139 } else if (ch
== '\"') {
140 state
= SCE_C_STRING
;
141 } else if (ch
== '\'') {
142 state
= SCE_C_CHARACTER
;
143 } else if (ch
== '#') {
144 state
= SCE_C_PREPROCESSOR
;
145 } else if (isoperator(ch
)) {
146 styler
.ColourTo(i
, SCE_C_OPERATOR
);
147 if ((ch
== '{') || (ch
== '}')) {
148 levelCurrent
+= (ch
== '{') ? 1 : -1;
153 if (state
== SCE_C_PREPROCESSOR
) {
154 if ((ch
== '\r' || ch
== '\n') && !(chPrev
== '\\' || chPrev
== '\r')) {
155 styler
.ColourTo(i
-1, state
);
156 state
= SCE_C_DEFAULT
;
158 } else if (state
== SCE_C_COMMENT
) {
159 if (ch
== '/' && chPrev
== '*') {
160 if (((i
> styler
.GetStartSegment() + 2) || (
161 (initStyle
== SCE_C_COMMENT
) &&
162 (styler
.GetStartSegment() == static_cast<unsigned int>(startPos
))))) {
163 styler
.ColourTo(i
, state
);
164 state
= SCE_C_DEFAULT
;
167 } else if (state
== SCE_C_COMMENTDOC
) {
168 if (ch
== '/' && chPrev
== '*') {
169 if (((i
> styler
.GetStartSegment() + 2) || (
170 (initStyle
== SCE_C_COMMENTDOC
) &&
171 (styler
.GetStartSegment() == static_cast<unsigned int>(startPos
))))) {
172 styler
.ColourTo(i
, state
);
173 state
= SCE_C_DEFAULT
;
176 } else if (state
== SCE_C_COMMENTLINE
) {
177 if (ch
== '\r' || ch
== '\n') {
178 styler
.ColourTo(i
-1, state
);
179 state
= SCE_C_DEFAULT
;
181 } else if (state
== SCE_C_STRING
) {
183 if (chNext
== '\"' || chNext
== '\'' || chNext
== '\\') {
186 chNext
= styler
.SafeGetCharAt(i
+ 1);
188 } else if (ch
== '\"') {
189 styler
.ColourTo(i
, state
);
190 state
= SCE_C_DEFAULT
;
193 chNext
= styler
.SafeGetCharAt(i
+ 1);
194 } else if (chNext
== '\r' || chNext
== '\n') {
195 styler
.ColourTo(i
-1, SCE_C_STRINGEOL
);
196 state
= SCE_C_STRINGEOL
;
198 } else if (state
== SCE_C_CHARACTER
) {
199 if ((ch
== '\r' || ch
== '\n') && (chPrev
!= '\\')) {
200 styler
.ColourTo(i
-1, SCE_C_STRINGEOL
);
201 state
= SCE_C_STRINGEOL
;
202 } else if (ch
== '\\') {
203 if (chNext
== '\"' || chNext
== '\'' || chNext
== '\\') {
206 chNext
= styler
.SafeGetCharAt(i
+ 1);
208 } else if (ch
== '\'') {
209 styler
.ColourTo(i
, state
);
210 state
= SCE_C_DEFAULT
;
213 chNext
= styler
.SafeGetCharAt(i
+ 1);
215 } else if (state
== SCE_C_UUID
) {
216 if (ch
== '\r' || ch
== '\n' || ch
== ')') {
217 styler
.ColourTo(i
-1, state
);
218 state
= SCE_C_DEFAULT
;
221 if (state
== SCE_C_DEFAULT
) { // One of the above succeeded
222 if (ch
== '/' && chNext
== '*') {
223 if (styler
.SafeGetCharAt(i
+ 2) == '*')
224 state
= SCE_C_COMMENTDOC
;
226 state
= SCE_C_COMMENT
;
227 } else if (ch
== '/' && chNext
== '/') {
228 state
= SCE_C_COMMENTLINE
;
229 } else if (ch
== '\"') {
230 state
= SCE_C_STRING
;
231 } else if (ch
== '\'') {
232 state
= SCE_C_CHARACTER
;
233 } else if (ch
== '#') {
234 state
= SCE_C_PREPROCESSOR
;
235 } else if (iswordstart(ch
)) {
237 } else if (isoperator(ch
)) {
238 styler
.ColourTo(i
, SCE_C_OPERATOR
);
239 if ((ch
== '{') || (ch
== '}')) {
240 levelCurrent
+= (ch
== '{') ? 1 : -1;
247 styler
.ColourTo(lengthDoc
- 1, state
);
249 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
251 int flagsNext
= styler
.LevelAt(lineCurrent
) & ~SC_FOLDLEVELNUMBERMASK
;
252 //styler.SetLevel(lineCurrent, levelCurrent | flagsNext);
253 styler
.SetLevel(lineCurrent
, levelPrev
| flagsNext
);
258 LexerModule
lmCPP(SCLEX_CPP
, ColouriseCppDoc
);