]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexPython.cxx
Update to Scintilla 1.48
[wxWidgets.git] / src / stc / scintilla / src / LexPython.cxx
1 // Scintilla source code edit control
2 /** @file LexPython.cxx
3 ** Lexer for Python.
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 enum kwType { kwOther, kwClass, kwDef, kwImport };
24
25 static bool IsPyComment(Accessor &styler, int pos, int len) {
26 return len > 0 && styler[pos] == '#';
27 }
28
29 static bool IsPyStringStart(int ch, int chNext, int chNext2) {
30 if (ch == '\'' || ch == '"')
31 return true;
32 if (ch == 'u' || ch == 'U') {
33 if (chNext == '"' || chNext == '\'')
34 return true;
35 if ((chNext == 'r' || chNext == 'R') && (chNext2 == '"' || chNext2 == '\''))
36 return true;
37 }
38 if ((ch == 'r' || ch == 'R') && (chNext == '"' || chNext == '\''))
39 return true;
40
41 return false;
42 }
43
44 /* Return the state to use for the string starting at i; *nextIndex will be set to the first index following the quote(s) */
45 static int GetPyStringState(Accessor &styler, int i, int *nextIndex) {
46 char ch = styler.SafeGetCharAt(i);
47 char chNext = styler.SafeGetCharAt(i + 1);
48
49 // Advance beyond r, u, or ur prefix, but bail if there are any unexpected chars
50 if (ch == 'r' || ch == 'R') {
51 i++;
52 ch = styler.SafeGetCharAt(i);
53 chNext = styler.SafeGetCharAt(i + 1);
54 } else if (ch == 'u' || ch == 'U') {
55 if (chNext == 'r' || chNext == 'R')
56 i += 2;
57 else
58 i += 1;
59 ch = styler.SafeGetCharAt(i);
60 chNext = styler.SafeGetCharAt(i + 1);
61 }
62
63 if (ch != '"' && ch != '\'') {
64 *nextIndex = i + 1;
65 return SCE_P_DEFAULT;
66 }
67
68 if (ch == chNext && ch == styler.SafeGetCharAt(i + 2)) {
69 *nextIndex = i + 3;
70
71 if (ch == '"')
72 return SCE_P_TRIPLEDOUBLE;
73 else
74 return SCE_P_TRIPLE;
75 } else {
76 *nextIndex = i + 1;
77
78 if (ch == '"')
79 return SCE_P_STRING;
80 else
81 return SCE_P_CHARACTER;
82 }
83 }
84
85 static inline bool IsAWordChar(int ch) {
86 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_');
87 }
88
89 static inline bool IsAWordStart(int ch) {
90 return (ch < 0x80) && (isalnum(ch) || ch == '_');
91 }
92
93 static void ColourisePyDoc(unsigned int startPos, int length, int initStyle,
94 WordList *keywordlists[], Accessor &styler) {
95
96 int endPos = startPos + length;
97
98 // Backtrack to previous line in case need to fix its tab whinging
99 int lineCurrent = styler.GetLine(startPos);
100 if (startPos > 0) {
101 if (lineCurrent > 0) {
102 startPos = styler.LineStart(lineCurrent - 1);
103 if (startPos == 0)
104 initStyle = SCE_P_DEFAULT;
105 else
106 initStyle = styler.StyleAt(startPos - 1);
107 }
108 }
109
110 WordList &keywords = *keywordlists[0];
111
112 const int whingeLevel = styler.GetPropertyInt("tab.timmy.whinge.level");
113
114 initStyle = initStyle & 31;
115 if (initStyle == SCE_P_STRINGEOL) {
116 initStyle = SCE_P_DEFAULT;
117 }
118
119 kwType kwLast = kwOther;
120 int spaceFlags = 0;
121 styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
122 bool hexadecimal = false;
123
124 // Python uses a different mask because bad indentation is marked by oring with 32
125 StyleContext sc(startPos, endPos - startPos, initStyle, styler, 0x7f);
126
127 for (; sc.More(); sc.Forward()) {
128
129 if (sc.atLineStart) {
130 const char chBad = static_cast<char>(64);
131 const char chGood = static_cast<char>(0);
132 char chFlags = chGood;
133 if (whingeLevel == 1) {
134 chFlags = (spaceFlags & wsInconsistent) ? chBad : chGood;
135 } else if (whingeLevel == 2) {
136 chFlags = (spaceFlags & wsSpaceTab) ? chBad : chGood;
137 } else if (whingeLevel == 3) {
138 chFlags = (spaceFlags & wsSpace) ? chBad : chGood;
139 } else if (whingeLevel == 4) {
140 chFlags = (spaceFlags & wsTab) ? chBad : chGood;
141 }
142 styler.SetFlags(chFlags, static_cast<char>(sc.state));
143 }
144
145 if (sc.atLineEnd) {
146 if ((sc.state == SCE_P_DEFAULT) ||
147 (sc.state == SCE_P_TRIPLE) ||
148 (sc.state == SCE_P_TRIPLEDOUBLE)) {
149 // Perform colourisation of white space and triple quoted strings at end of each line to allow
150 // tab marking to work inside white space and triple quoted strings
151 sc.ForwardSetState(sc.state);
152 }
153 lineCurrent++;
154 styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
155 if ((sc.state == SCE_P_STRING) || (sc.state == SCE_P_CHARACTER)) {
156 sc.ChangeState(SCE_P_STRINGEOL);
157 sc.ForwardSetState(SCE_P_DEFAULT);
158 }
159 if (!sc.More())
160 break;
161 }
162
163 // Check for a state end
164 if (sc.state == SCE_P_OPERATOR) {
165 kwLast = kwOther;
166 sc.SetState(SCE_P_DEFAULT);
167 } else if (sc.state == SCE_P_NUMBER) {
168 if (!IsAWordChar(sc.ch) &&
169 !(!hexadecimal && ((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E')))) {
170 sc.SetState(SCE_P_DEFAULT);
171 }
172 } else if (sc.state == SCE_P_IDENTIFIER) {
173 if ((sc.ch == '.') || (!IsAWordChar(sc.ch))) {
174 char s[100];
175 sc.GetCurrent(s, sizeof(s));
176 int style = SCE_P_IDENTIFIER;
177 if ((kwLast == kwImport) && (strcmp(s, "as") == 0)) {
178 style = SCE_P_WORD;
179 } else if (keywords.InList(s)) {
180 style = SCE_P_WORD;
181 } else if (kwLast == kwClass) {
182 style = SCE_P_CLASSNAME;
183 } else if (kwLast == kwDef) {
184 style = SCE_P_DEFNAME;
185 }
186 sc.ChangeState(style);
187 sc.SetState(SCE_P_DEFAULT);
188 if (style == SCE_P_WORD) {
189 if (0 == strcmp(s, "class"))
190 kwLast = kwClass;
191 else if (0 == strcmp(s, "def"))
192 kwLast = kwDef;
193 else if (0 == strcmp(s, "import"))
194 kwLast = kwImport;
195 else
196 kwLast = kwOther;
197 } else if (style == SCE_P_CLASSNAME) {
198 kwLast = kwOther;
199 } else if (style == SCE_P_DEFNAME) {
200 kwLast = kwOther;
201 }
202 }
203 } else if ((sc.state == SCE_P_COMMENTLINE) || (sc.state == SCE_P_COMMENTBLOCK)) {
204 if (sc.ch == '\r' || sc.ch == '\n') {
205 sc.SetState(SCE_P_DEFAULT);
206 }
207 } else if ((sc.state == SCE_P_STRING) || (sc.state == SCE_P_CHARACTER)) {
208 if (sc.ch == '\\') {
209 if ((sc.chNext == '\r') && (sc.GetRelative(2) == '\n')) {
210 sc.Forward();
211 }
212 sc.Forward();
213 } else if ((sc.state == SCE_P_STRING) && (sc.ch == '\"')) {
214 sc.ForwardSetState(SCE_P_DEFAULT);
215 } else if ((sc.state == SCE_P_CHARACTER) && (sc.ch == '\'')) {
216 sc.ForwardSetState(SCE_P_DEFAULT);
217 }
218 } else if (sc.state == SCE_P_TRIPLE) {
219 if (sc.ch == '\\') {
220 sc.Forward();
221 } else if (sc.Match("\'\'\'")) {
222 sc.Forward();
223 sc.Forward();
224 sc.ForwardSetState(SCE_P_DEFAULT);
225 }
226 } else if (sc.state == SCE_P_TRIPLEDOUBLE) {
227 if (sc.ch == '\\') {
228 sc.Forward();
229 } else if (sc.Match("\"\"\"")) {
230 sc.Forward();
231 sc.Forward();
232 sc.ForwardSetState(SCE_P_DEFAULT);
233 }
234 }
235
236 // Check for a new state starting character
237 if (sc.state == SCE_P_DEFAULT) {
238 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
239 if (sc.ch == '0' && (sc.chNext == 'x' || sc.chNext == 'X')) {
240 hexadecimal = true;
241 } else {
242 hexadecimal = false;
243 }
244 sc.SetState(SCE_P_NUMBER);
245 } else if (isascii(sc.ch) && isoperator(static_cast<char>(sc.ch)) || sc.ch == '`') {
246 sc.SetState(SCE_P_OPERATOR);
247 } else if (sc.ch == '#') {
248 sc.SetState(sc.chNext == '#' ? SCE_P_COMMENTBLOCK : SCE_P_COMMENTLINE);
249 } else if (IsPyStringStart(sc.ch, sc.chNext, sc.GetRelative(2))) {
250 int nextIndex = 0;
251 sc.SetState(GetPyStringState(styler, sc.currentPos, &nextIndex));
252 while (nextIndex > (sc.currentPos + 1)) {
253 sc.Forward();
254 }
255 } else if (IsAWordStart(sc.ch)) {
256 sc.SetState(SCE_P_IDENTIFIER);
257 }
258 }
259 }
260 sc.Complete();
261 }
262
263 static bool IsCommentLine(int line, Accessor &styler) {
264 int pos = styler.LineStart(line);
265 int eol_pos = styler.LineStart(line + 1) - 1;
266 for (int i = pos; i < eol_pos; i++) {
267 char ch = styler[i];
268 if (ch == '#')
269 return true;
270 else if (ch != ' ' && ch != '\t')
271 return false;
272 }
273 return false;
274 }
275
276 static bool IsQuoteLine(int line, Accessor &styler) {
277 int style = styler.StyleAt(styler.LineStart(line)) & 31;
278 return ((style == SCE_P_TRIPLE) || (style == SCE_P_TRIPLEDOUBLE));
279 }
280
281
282 static void FoldPyDoc(unsigned int startPos, int length, int /*initStyle - unused*/,
283 WordList *[], Accessor &styler) {
284 const int maxPos = startPos + length;
285 const int maxLines = styler.GetLine(maxPos - 1); // Requested last line
286 const int docLines = styler.GetLine(styler.Length() - 1); // Available last line
287 const bool foldComment = styler.GetPropertyInt("fold.comment.python") != 0;
288 const bool foldQuotes = styler.GetPropertyInt("fold.quotes.python") != 0;
289
290 // Backtrack to previous non-blank line so we can determine indent level
291 // for any white space lines (needed esp. within triple quoted strings)
292 // and so we can fix any preceding fold level (which is why we go back
293 // at least one line in all cases)
294 int spaceFlags = 0;
295 int lineCurrent = styler.GetLine(startPos);
296 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
297 while (lineCurrent > 0) {
298 lineCurrent--;
299 indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
300 if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG) &&
301 (!IsCommentLine(lineCurrent, styler)) &&
302 (!IsQuoteLine(lineCurrent, styler)))
303 break;
304 }
305 int indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
306
307 // Set up initial loop state
308 startPos = styler.LineStart(lineCurrent);
309 int prev_state = SCE_P_DEFAULT & 31;
310 if (lineCurrent >= 1)
311 prev_state = styler.StyleAt(startPos - 1) & 31;
312 int prevQuote = foldQuotes && ((prev_state == SCE_P_TRIPLE) || (prev_state == SCE_P_TRIPLEDOUBLE));
313 int prevComment = 0;
314 if (lineCurrent >= 1)
315 prevComment = foldComment && IsCommentLine(lineCurrent - 1, styler);
316
317 // Process all characters to end of requested range or end of any triple quote
318 // or comment that hangs over the end of the range. Cap processing in all cases
319 // to end of document (in case of unclosed quote or comment at end).
320 while ((lineCurrent <= docLines) && ((lineCurrent <= maxLines) || prevQuote || prevComment)) {
321
322 // Gather info
323 int lev = indentCurrent;
324 int lineNext = lineCurrent + 1;
325 int indentNext = indentCurrent;
326 int quote = false;
327 if (lineNext <= docLines) {
328 // Information about next line is only available if not at end of document
329 indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
330 int style = styler.StyleAt(styler.LineStart(lineNext)) & 31;
331 quote = foldQuotes && ((style == SCE_P_TRIPLE) || (style == SCE_P_TRIPLEDOUBLE));
332 }
333 const int quote_start = (quote && !prevQuote);
334 const int quote_continue = (quote && prevQuote);
335 const int comment = foldComment && IsCommentLine(lineCurrent, styler);
336 const int comment_start = (comment && !prevComment && (lineNext <= docLines) &&
337 IsCommentLine(lineNext, styler) && (lev > SC_FOLDLEVELBASE));
338 const int comment_continue = (comment && prevComment);
339 if ((!quote || !prevQuote) && !comment)
340 indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
341 if (quote)
342 indentNext = indentCurrentLevel;
343 if (indentNext & SC_FOLDLEVELWHITEFLAG)
344 indentNext = SC_FOLDLEVELWHITEFLAG | indentCurrentLevel;
345
346 if (quote_start) {
347 // Place fold point at start of triple quoted string
348 lev |= SC_FOLDLEVELHEADERFLAG;
349 } else if (quote_continue || prevQuote) {
350 // Add level to rest of lines in the string
351 lev = lev + 1;
352 } else if (comment_start) {
353 // Place fold point at start of a block of comments
354 lev |= SC_FOLDLEVELHEADERFLAG;
355 } else if (comment_continue) {
356 // Add level to rest of lines in the block
357 lev = lev + 1;
358 }
359
360 // Skip past any blank lines for next indent level info; we skip also comments
361 // starting in column 0 which effectively folds them into surrounding code rather
362 // than screwing up folding.
363 const int saveIndentNext = indentNext;
364 while (!quote &&
365 (lineNext < docLines) &&
366 ((indentNext & SC_FOLDLEVELWHITEFLAG) ||
367 (lineNext <= docLines && styler[styler.LineStart(lineNext)] == '#'))) {
368
369 lineNext++;
370 indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
371 }
372
373 // Next compute max indent level of current line and next non-blank line.
374 // This is the level to which we set all the intervening blank or comment lines.
375 const int skip_level = Platform::Maximum(indentCurrentLevel,
376 indentNext & SC_FOLDLEVELNUMBERMASK);
377
378 // Now set all the indent levels on the lines we skipped
379 int skipLine = lineCurrent + 1;
380 int skipIndentNext = saveIndentNext;
381 while (skipLine < lineNext) {
382 int skipLineLevel = skip_level;
383 if (skipIndentNext & SC_FOLDLEVELWHITEFLAG)
384 skipLineLevel = SC_FOLDLEVELWHITEFLAG | skipLineLevel;
385 styler.SetLevel(skipLine, skipLineLevel);
386 skipLine++;
387 skipIndentNext = styler.IndentAmount(skipLine, &spaceFlags, NULL);
388 }
389
390 // Set fold header on non-quote/non-comment line
391 if (!quote && !comment && !(indentCurrent & SC_FOLDLEVELWHITEFLAG) ) {
392 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK))
393 lev |= SC_FOLDLEVELHEADERFLAG;
394 }
395
396 // Keep track of triple quote and block comment state of previous line
397 prevQuote = quote;
398 prevComment = comment_start || comment_continue;
399
400 // Set fold level for this line and move to next line
401 styler.SetLevel(lineCurrent, lev);
402 indentCurrent = indentNext;
403 lineCurrent = lineNext;
404 }
405
406 // NOTE: Cannot set level of last line here because indentCurrent doesn't have
407 // header flag set; the loop above is crafted to take care of this case!
408 //styler.SetLevel(lineCurrent, indentCurrent);
409 }
410
411 static const char * const pythonWordListDesc[] = {
412 "Python keywords",
413 0
414 };
415
416 LexerModule lmPython(SCLEX_PYTHON, ColourisePyDoc, "python", FoldPyDoc,
417 pythonWordListDesc);