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 <stdio.h>
11 #include <stdarg.h>
12 #include <assert.h>
13 #include <ctype.h>
14 
15 #include <string>
16 #include <vector>
17 #include <map>
18 #include <algorithm>
19 
20 #include "ILexer.h"
21 #include "Scintilla.h"
22 #include "SciLexer.h"
23 
24 #include "StringCopy.h"
25 #include "WordList.h"
26 #include "LexAccessor.h"
27 #include "Accessor.h"
28 #include "StyleContext.h"
29 #include "CharacterSet.h"
30 #include "CharacterCategory.h"
31 #include "LexerModule.h"
32 #include "OptionSet.h"
33 #include "SubStyles.h"
34 #include "DefaultLexer.h"
35 
36 using namespace Scintilla;
37 
38 namespace {
39 // Use an unnamed namespace to protect the functions and classes from name conflicts
40 
41 /* Notes on f-strings: f-strings are strings prefixed with f (e.g. f'') that may
42    have arbitrary expressions in {}.  The tokens in the expressions are lexed as if
43    they were outside of any string.  Expressions may contain { and } characters as
44    long as there is a closing } for every {, may be 2+ lines in a triple quoted
45    string, and may have a formatting specifier following a ! or :, but both !
46    and : are valid inside of a bracketed expression and != is a valid
47    expression token even outside of a bracketed expression.
48 
49    When in an f-string expression, the lexer keeps track of the state value of
50    the f-string and the nesting count for the expression (# of [, (, { seen - # of
51    }, ), ] seen).  f-strings may be nested (e.g. f'{ a + f"{1+2}"') so a stack of
52    states and nesting counts is kept.  If a f-string expression continues beyond
53    the end of a line, this stack is saved in a std::map that maps a line number to
54    the stack at the end of that line.  std::vector is used for the stack.
55 
56    The PEP for f-strings is at https://www.python.org/dev/peps/pep-0498/
57 */
58 struct SingleFStringExpState {
59 	int state;
60 	int nestingCount;
61 };
62 
63 /* kwCDef, kwCTypeName only used for Cython */
64 enum kwType { kwOther, kwClass, kwDef, kwImport, kwCDef, kwCTypeName, kwCPDef };
65 
66 enum literalsAllowed { litNone = 0, litU = 1, litB = 2, litF = 4 };
67 
68 const int indicatorWhitespace = 1;
69 
IsPyComment(Accessor & styler,Sci_Position pos,Sci_Position len)70 bool IsPyComment(Accessor &styler, Sci_Position pos, Sci_Position len) {
71 	return len > 0 && styler[pos] == '#';
72 }
73 
IsPyStringTypeChar(int ch,literalsAllowed allowed)74 bool IsPyStringTypeChar(int ch, literalsAllowed allowed) {
75 	return
76 		((allowed & litB) && (ch == 'b' || ch == 'B')) ||
77 		((allowed & litU) && (ch == 'u' || ch == 'U')) ||
78 		((allowed & litF) && (ch == 'f' || ch == 'F'));
79 }
80 
IsPyStringStart(int ch,int chNext,int chNext2,literalsAllowed allowed)81 bool IsPyStringStart(int ch, int chNext, int chNext2, literalsAllowed allowed) {
82 	if (ch == '\'' || ch == '"')
83 		return true;
84 	if (IsPyStringTypeChar(ch, allowed)) {
85 		if (chNext == '"' || chNext == '\'')
86 			return true;
87 		if ((chNext == 'r' || chNext == 'R') && (chNext2 == '"' || chNext2 == '\''))
88 			return true;
89 	}
90 	if ((ch == 'r' || ch == 'R') && (chNext == '"' || chNext == '\''))
91 		return true;
92 
93 	return false;
94 }
95 
IsPyFStringState(int st)96 bool IsPyFStringState(int st) {
97 	return ((st == SCE_P_FCHARACTER) || (st == SCE_P_FSTRING) ||
98 		(st == SCE_P_FTRIPLE) || (st == SCE_P_FTRIPLEDOUBLE));
99 }
100 
IsPySingleQuoteStringState(int st)101 bool IsPySingleQuoteStringState(int st) {
102 	return ((st == SCE_P_CHARACTER) || (st == SCE_P_STRING) ||
103 		(st == SCE_P_FCHARACTER) || (st == SCE_P_FSTRING));
104 }
105 
IsPyTripleQuoteStringState(int st)106 bool IsPyTripleQuoteStringState(int st) {
107 	return ((st == SCE_P_TRIPLE) || (st == SCE_P_TRIPLEDOUBLE) ||
108 		(st == SCE_P_FTRIPLE) || (st == SCE_P_FTRIPLEDOUBLE));
109 }
110 
GetPyStringQuoteChar(int st)111 char GetPyStringQuoteChar(int st) {
112 	if ((st == SCE_P_CHARACTER) || (st == SCE_P_FCHARACTER) ||
113 			(st == SCE_P_TRIPLE) || (st == SCE_P_FTRIPLE))
114 		return '\'';
115 	if ((st == SCE_P_STRING) || (st == SCE_P_FSTRING) ||
116 			(st == SCE_P_TRIPLEDOUBLE) || (st == SCE_P_FTRIPLEDOUBLE))
117 		return '"';
118 
119 	return '\0';
120 }
121 
PushStateToStack(int state,std::vector<SingleFStringExpState> & stack,SingleFStringExpState * & currentFStringExp)122 void PushStateToStack(int state, std::vector<SingleFStringExpState> &stack, SingleFStringExpState *&currentFStringExp) {
123 	SingleFStringExpState single = {state, 0};
124 	stack.push_back(single);
125 
126 	currentFStringExp = &stack.back();
127 }
128 
PopFromStateStack(std::vector<SingleFStringExpState> & stack,SingleFStringExpState * & currentFStringExp)129 int PopFromStateStack(std::vector<SingleFStringExpState> &stack, SingleFStringExpState *&currentFStringExp) {
130 	int state = 0;
131 
132 	if (!stack.empty()) {
133 		state = stack.back().state;
134 		stack.pop_back();
135 	}
136 
137 	if (stack.empty()) {
138 		currentFStringExp = NULL;
139 	} else {
140 		currentFStringExp = &stack.back();
141 	}
142 
143 	return state;
144 }
145 
146 /* Return the state to use for the string starting at i; *nextIndex will be set to the first index following the quote(s) */
GetPyStringState(Accessor & styler,Sci_Position i,Sci_PositionU * nextIndex,literalsAllowed allowed)147 int GetPyStringState(Accessor &styler, Sci_Position i, Sci_PositionU *nextIndex, literalsAllowed allowed) {
148 	char ch = styler.SafeGetCharAt(i);
149 	char chNext = styler.SafeGetCharAt(i + 1);
150 	const int firstIsF = (ch == 'f' || ch == 'F');
151 
152 	// Advance beyond r, u, or ur prefix (or r, b, or br in Python 2.7+ and r, f, or fr in Python 3.6+), but bail if there are any unexpected chars
153 	if (ch == 'r' || ch == 'R') {
154 		i++;
155 		ch = styler.SafeGetCharAt(i);
156 		chNext = styler.SafeGetCharAt(i + 1);
157 	} else if (IsPyStringTypeChar(ch, allowed)) {
158 		if (chNext == 'r' || chNext == 'R')
159 			i += 2;
160 		else
161 			i += 1;
162 		ch = styler.SafeGetCharAt(i);
163 		chNext = styler.SafeGetCharAt(i + 1);
164 	}
165 
166 	if (ch != '"' && ch != '\'') {
167 		*nextIndex = i + 1;
168 		return SCE_P_DEFAULT;
169 	}
170 
171 	if (ch == chNext && ch == styler.SafeGetCharAt(i + 2)) {
172 		*nextIndex = i + 3;
173 
174 		if (ch == '"')
175 			return (firstIsF ? SCE_P_FTRIPLEDOUBLE : SCE_P_TRIPLEDOUBLE);
176 		else
177 			return (firstIsF ? SCE_P_FTRIPLE : SCE_P_TRIPLE);
178 	} else {
179 		*nextIndex = i + 1;
180 
181 		if (ch == '"')
182 			return (firstIsF ? SCE_P_FSTRING : SCE_P_STRING);
183 		else
184 			return (firstIsF ? SCE_P_FCHARACTER : SCE_P_CHARACTER);
185 	}
186 }
187 
IsAWordChar(int ch,bool unicodeIdentifiers)188 inline bool IsAWordChar(int ch, bool unicodeIdentifiers) {
189 	if (ch < 0x80)
190 		return (isalnum(ch) || ch == '.' || ch == '_');
191 
192 	if (!unicodeIdentifiers)
193 		return false;
194 
195 	// Python uses the XID_Continue set from unicode data
196 	return IsXidContinue(ch);
197 }
198 
IsAWordStart(int ch,bool unicodeIdentifiers)199 inline bool IsAWordStart(int ch, bool unicodeIdentifiers) {
200 	if (ch < 0x80)
201 		return (isalpha(ch) || ch == '_');
202 
203 	if (!unicodeIdentifiers)
204 		return false;
205 
206 	// Python uses the XID_Start set from unicode data
207 	return IsXidStart(ch);
208 }
209 
IsFirstNonWhitespace(Sci_Position pos,Accessor & styler)210 static bool IsFirstNonWhitespace(Sci_Position pos, Accessor &styler) {
211 	Sci_Position line = styler.GetLine(pos);
212 	Sci_Position start_pos = styler.LineStart(line);
213 	for (Sci_Position i = start_pos; i < pos; i++) {
214 		const char ch = styler[i];
215 		if (!(ch == ' ' || ch == '\t'))
216 			return false;
217 	}
218 	return true;
219 }
220 
221 // Options used for LexerPython
222 struct OptionsPython {
223 	int whingeLevel;
224 	bool base2or8Literals;
225 	bool stringsU;
226 	bool stringsB;
227 	bool stringsF;
228 	bool stringsOverNewline;
229 	bool keywords2NoSubIdentifiers;
230 	bool fold;
231 	bool foldQuotes;
232 	bool foldCompact;
233 	bool unicodeIdentifiers;
234 
OptionsPython__anonc23d012e0111::OptionsPython235 	OptionsPython() {
236 		whingeLevel = 0;
237 		base2or8Literals = true;
238 		stringsU = true;
239 		stringsB = true;
240 		stringsF = true;
241 		stringsOverNewline = false;
242 		keywords2NoSubIdentifiers = false;
243 		fold = false;
244 		foldQuotes = false;
245 		foldCompact = false;
246 		unicodeIdentifiers = true;
247 	}
248 
AllowedLiterals__anonc23d012e0111::OptionsPython249 	literalsAllowed AllowedLiterals() const {
250 		literalsAllowed allowedLiterals = stringsU ? litU : litNone;
251 		if (stringsB)
252 			allowedLiterals = static_cast<literalsAllowed>(allowedLiterals | litB);
253 		if (stringsF)
254 			allowedLiterals = static_cast<literalsAllowed>(allowedLiterals | litF);
255 		return allowedLiterals;
256 	}
257 };
258 
259 static const char *const pythonWordListDesc[] = {
260 	"Keywords",
261 	"Highlighted identifiers",
262 	0
263 };
264 
265 struct OptionSetPython : public OptionSet<OptionsPython> {
OptionSetPython__anonc23d012e0111::OptionSetPython266 	OptionSetPython() {
267 		DefineProperty("tab.timmy.whinge.level", &OptionsPython::whingeLevel,
268 			       "For Python code, checks whether indenting is consistent. "
269 			       "The default, 0 turns off indentation checking, "
270 			       "1 checks whether each line is potentially inconsistent with the previous line, "
271 			       "2 checks whether any space characters occur before a tab character in the indentation, "
272 			       "3 checks whether any spaces are in the indentation, and "
273 			       "4 checks for any tab characters in the indentation. "
274 			       "1 is a good level to use.");
275 
276 		DefineProperty("lexer.python.literals.binary", &OptionsPython::base2or8Literals,
277 			       "Set to 0 to not recognise Python 3 binary and octal literals: 0b1011 0o712.");
278 
279 		DefineProperty("lexer.python.strings.u", &OptionsPython::stringsU,
280 			       "Set to 0 to not recognise Python Unicode literals u\"x\" as used before Python 3.");
281 
282 		DefineProperty("lexer.python.strings.b", &OptionsPython::stringsB,
283 			       "Set to 0 to not recognise Python 3 bytes literals b\"x\".");
284 
285 		DefineProperty("lexer.python.strings.f", &OptionsPython::stringsF,
286 			       "Set to 0 to not recognise Python 3.6 f-string literals f\"var={var}\".");
287 
288 		DefineProperty("lexer.python.strings.over.newline", &OptionsPython::stringsOverNewline,
289 			       "Set to 1 to allow strings to span newline characters.");
290 
291 		DefineProperty("lexer.python.keywords2.no.sub.identifiers", &OptionsPython::keywords2NoSubIdentifiers,
292 			       "When enabled, it will not style keywords2 items that are used as a sub-identifier. "
293 			       "Example: when set, will not highlight \"foo.open\" when \"open\" is a keywords2 item.");
294 
295 		DefineProperty("fold", &OptionsPython::fold);
296 
297 		DefineProperty("fold.quotes.python", &OptionsPython::foldQuotes,
298 			       "This option enables folding multi-line quoted strings when using the Python lexer.");
299 
300 		DefineProperty("fold.compact", &OptionsPython::foldCompact);
301 
302 		DefineProperty("lexer.python.unicode.identifiers", &OptionsPython::unicodeIdentifiers,
303 			       "Set to 0 to not recognise Python 3 unicode identifiers.");
304 
305 		DefineWordListSets(pythonWordListDesc);
306 	}
307 };
308 
309 const char styleSubable[] = { SCE_P_IDENTIFIER, 0 };
310 
311 LexicalClass lexicalClasses[] = {
312 	// Lexer Python SCLEX_PYTHON SCE_P_:
313 	0, "SCE_P_DEFAULT", "default", "White space",
314 	1, "SCE_P_COMMENTLINE", "comment line", "Comment",
315 	2, "SCE_P_NUMBER", "literal numeric", "Number",
316 	3, "SCE_P_STRING", "literal string", "String",
317 	4, "SCE_P_CHARACTER", "literal string", "Single quoted string",
318 	5, "SCE_P_WORD", "keyword", "Keyword",
319 	6, "SCE_P_TRIPLE", "literal string", "Triple quotes",
320 	7, "SCE_P_TRIPLEDOUBLE", "literal string", "Triple double quotes",
321 	8, "SCE_P_CLASSNAME", "identifier", "Class name definition",
322 	9, "SCE_P_DEFNAME", "identifier", "Function or method name definition",
323 	10, "SCE_P_OPERATOR", "operator", "Operators",
324 	11, "SCE_P_IDENTIFIER", "identifier", "Identifiers",
325 	12, "SCE_P_COMMENTBLOCK", "comment", "Comment-blocks",
326 	13, "SCE_P_STRINGEOL", "error literal string", "End of line where string is not closed",
327 	14, "SCE_P_WORD2", "identifier", "Highlighted identifiers",
328 	15, "SCE_P_DECORATOR", "preprocessor", "Decorators",
329 	16, "SCE_P_FSTRING", "literal string interpolated", "F-String",
330 	17, "SCE_P_FCHARACTER", "literal string interpolated", "Single quoted f-string",
331 	18, "SCE_P_FTRIPLE", "literal string interpolated", "Triple quoted f-string",
332 	19, "SCE_P_FTRIPLEDOUBLE", "literal string interpolated", "Triple double quoted f-string",
333 };
334 
335 }
336 
337 class LexerPython : public DefaultLexer {
338 	WordList keywords;
339 	WordList keywords2;
340 	OptionsPython options;
341 	OptionSetPython osPython;
342 	enum { ssIdentifier };
343 	SubStyles subStyles;
344 	std::map<Sci_Position, std::vector<SingleFStringExpState> > ftripleStateAtEol;
345 public:
LexerPython()346 	explicit LexerPython() :
347 		DefaultLexer(lexicalClasses, ELEMENTS(lexicalClasses)),
348 		subStyles(styleSubable, 0x80, 0x40, 0) {
349 	}
~LexerPython()350 	~LexerPython() override {
351 	}
Release()352 	void SCI_METHOD Release() override {
353 		delete this;
354 	}
Version() const355 	int SCI_METHOD Version() const override {
356 		return lvSubStyles;
357 	}
PropertyNames()358 	const char *SCI_METHOD PropertyNames() override {
359 		return osPython.PropertyNames();
360 	}
PropertyType(const char * name)361 	int SCI_METHOD PropertyType(const char *name) override {
362 		return osPython.PropertyType(name);
363 	}
DescribeProperty(const char * name)364 	const char *SCI_METHOD DescribeProperty(const char *name) override {
365 		return osPython.DescribeProperty(name);
366 	}
367 	Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
DescribeWordListSets()368 	const char *SCI_METHOD DescribeWordListSets() override {
369 		return osPython.DescribeWordListSets();
370 	}
371 	Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
372 	void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
373 	void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
374 
PrivateCall(int,void *)375 	void *SCI_METHOD PrivateCall(int, void *) override {
376 		return 0;
377 	}
378 
LineEndTypesSupported()379 	int SCI_METHOD LineEndTypesSupported() override {
380 		return SC_LINE_END_TYPE_UNICODE;
381 	}
382 
AllocateSubStyles(int styleBase,int numberStyles)383 	int SCI_METHOD AllocateSubStyles(int styleBase, int numberStyles) override {
384 		return subStyles.Allocate(styleBase, numberStyles);
385 	}
SubStylesStart(int styleBase)386 	int SCI_METHOD SubStylesStart(int styleBase) override {
387 		return subStyles.Start(styleBase);
388 	}
SubStylesLength(int styleBase)389 	int SCI_METHOD SubStylesLength(int styleBase) override {
390 		return subStyles.Length(styleBase);
391 	}
StyleFromSubStyle(int subStyle)392 	int SCI_METHOD StyleFromSubStyle(int subStyle) override {
393 		const int styleBase = subStyles.BaseStyle(subStyle);
394 		return styleBase;
395 	}
PrimaryStyleFromStyle(int style)396 	int SCI_METHOD PrimaryStyleFromStyle(int style) override {
397 		return style;
398 	}
FreeSubStyles()399 	void SCI_METHOD FreeSubStyles() override {
400 		subStyles.Free();
401 	}
SetIdentifiers(int style,const char * identifiers)402 	void SCI_METHOD SetIdentifiers(int style, const char *identifiers) override {
403 		subStyles.SetIdentifiers(style, identifiers);
404 	}
DistanceToSecondaryStyles()405 	int SCI_METHOD DistanceToSecondaryStyles() override {
406 		return 0;
407 	}
GetSubStyleBases()408 	const char *SCI_METHOD GetSubStyleBases() override {
409 		return styleSubable;
410 	}
411 
LexerFactoryPython()412 	static ILexer *LexerFactoryPython() {
413 		return new LexerPython();
414 	}
415 
416 private:
417 	void ProcessLineEnd(StyleContext &sc, std::vector<SingleFStringExpState> &fstringStateStack, SingleFStringExpState *&currentFStringExp, bool &inContinuedString);
418 };
419 
PropertySet(const char * key,const char * val)420 Sci_Position SCI_METHOD LexerPython::PropertySet(const char *key, const char *val) {
421 	if (osPython.PropertySet(&options, key, val)) {
422 		return 0;
423 	}
424 	return -1;
425 }
426 
WordListSet(int n,const char * wl)427 Sci_Position SCI_METHOD LexerPython::WordListSet(int n, const char *wl) {
428 	WordList *wordListN = 0;
429 	switch (n) {
430 	case 0:
431 		wordListN = &keywords;
432 		break;
433 	case 1:
434 		wordListN = &keywords2;
435 		break;
436 	}
437 	Sci_Position firstModification = -1;
438 	if (wordListN) {
439 		WordList wlNew;
440 		wlNew.Set(wl);
441 		if (*wordListN != wlNew) {
442 			wordListN->Set(wl);
443 			firstModification = 0;
444 		}
445 	}
446 	return firstModification;
447 }
448 
ProcessLineEnd(StyleContext & sc,std::vector<SingleFStringExpState> & fstringStateStack,SingleFStringExpState * & currentFStringExp,bool & inContinuedString)449 void LexerPython::ProcessLineEnd(StyleContext &sc, std::vector<SingleFStringExpState> &fstringStateStack, SingleFStringExpState *&currentFStringExp, bool &inContinuedString) {
450 	long deepestSingleStateIndex = -1;
451 	unsigned long i;
452 
453 	// Find the deepest single quote state because that string will end; no \ continuation in f-string
454 	for (i = 0; i < fstringStateStack.size(); i++) {
455 		if (IsPySingleQuoteStringState(fstringStateStack[i].state)) {
456 			deepestSingleStateIndex = i;
457 			break;
458 		}
459 	}
460 
461 	if (deepestSingleStateIndex != -1) {
462 		sc.SetState(fstringStateStack[deepestSingleStateIndex].state);
463 		while (fstringStateStack.size() > static_cast<unsigned long>(deepestSingleStateIndex)) {
464 			PopFromStateStack(fstringStateStack, currentFStringExp);
465 		}
466 	}
467 	if (!fstringStateStack.empty()) {
468 		std::pair<Sci_Position, std::vector<SingleFStringExpState> > val;
469 		val.first = sc.currentLine;
470 		val.second = fstringStateStack;
471 
472 		ftripleStateAtEol.insert(val);
473 	}
474 
475 	if ((sc.state == SCE_P_DEFAULT)
476 			|| IsPyTripleQuoteStringState(sc.state)) {
477 		// Perform colourisation of white space and triple quoted strings at end of each line to allow
478 		// tab marking to work inside white space and triple quoted strings
479 		sc.SetState(sc.state);
480 	}
481 	if (IsPySingleQuoteStringState(sc.state)) {
482 		if (inContinuedString || options.stringsOverNewline) {
483 			inContinuedString = false;
484 		} else {
485 			sc.ChangeState(SCE_P_STRINGEOL);
486 			sc.ForwardSetState(SCE_P_DEFAULT);
487 		}
488 	}
489 }
490 
Lex(Sci_PositionU startPos,Sci_Position length,int initStyle,IDocument * pAccess)491 void SCI_METHOD LexerPython::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
492 	Accessor styler(pAccess, NULL);
493 
494 	// Track whether in f-string expression; vector is used for a stack to
495 	// handle nested f-strings such as f"""{f'''{f"{f'{1}'}"}'''}"""
496 	std::vector<SingleFStringExpState> fstringStateStack;
497 	SingleFStringExpState *currentFStringExp = NULL;
498 
499 	const Sci_Position endPos = startPos + length;
500 
501 	// Backtrack to previous line in case need to fix its tab whinging
502 	Sci_Position lineCurrent = styler.GetLine(startPos);
503 	if (startPos > 0) {
504 		if (lineCurrent > 0) {
505 			lineCurrent--;
506 			// Look for backslash-continued lines
507 			while (lineCurrent > 0) {
508 				Sci_Position eolPos = styler.LineStart(lineCurrent) - 1;
509 				const int eolStyle = styler.StyleAt(eolPos);
510 				if (eolStyle == SCE_P_STRING
511 						|| eolStyle == SCE_P_CHARACTER
512 						|| eolStyle == SCE_P_STRINGEOL) {
513 					lineCurrent -= 1;
514 				} else {
515 					break;
516 				}
517 			}
518 			startPos = styler.LineStart(lineCurrent);
519 		}
520 		initStyle = startPos == 0 ? SCE_P_DEFAULT : styler.StyleAt(startPos - 1);
521 	}
522 
523 	const literalsAllowed allowedLiterals = options.AllowedLiterals();
524 
525 	initStyle = initStyle & 31;
526 	if (initStyle == SCE_P_STRINGEOL) {
527 		initStyle = SCE_P_DEFAULT;
528 	}
529 
530 	// Set up fstate stack from last line and remove any subsequent ftriple at eol states
531 	std::map<Sci_Position, std::vector<SingleFStringExpState> >::iterator it;
532 	it = ftripleStateAtEol.find(lineCurrent - 1);
533 	if (it != ftripleStateAtEol.end() && !it->second.empty()) {
534 		fstringStateStack = it->second;
535 		currentFStringExp = &fstringStateStack.back();
536 	}
537 	it = ftripleStateAtEol.lower_bound(lineCurrent);
538 	if (it != ftripleStateAtEol.end()) {
539 		ftripleStateAtEol.erase(it, ftripleStateAtEol.end());
540 	}
541 
542 	kwType kwLast = kwOther;
543 	int spaceFlags = 0;
544 	styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
545 	bool base_n_number = false;
546 
547 	const WordClassifier &classifierIdentifiers = subStyles.Classifier(SCE_P_IDENTIFIER);
548 
549 	StyleContext sc(startPos, endPos - startPos, initStyle, styler);
550 
551 	bool indentGood = true;
552 	Sci_Position startIndicator = sc.currentPos;
553 	bool inContinuedString = false;
554 
555 	for (; sc.More(); sc.Forward()) {
556 
557 		if (sc.atLineStart) {
558 			styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
559 			indentGood = true;
560 			if (options.whingeLevel == 1) {
561 				indentGood = (spaceFlags & wsInconsistent) == 0;
562 			} else if (options.whingeLevel == 2) {
563 				indentGood = (spaceFlags & wsSpaceTab) == 0;
564 			} else if (options.whingeLevel == 3) {
565 				indentGood = (spaceFlags & wsSpace) == 0;
566 			} else if (options.whingeLevel == 4) {
567 				indentGood = (spaceFlags & wsTab) == 0;
568 			}
569 			if (!indentGood) {
570 				styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 0);
571 				startIndicator = sc.currentPos;
572 			}
573 		}
574 
575 		if (sc.atLineEnd) {
576 			ProcessLineEnd(sc, fstringStateStack, currentFStringExp, inContinuedString);
577 			lineCurrent++;
578 			if (!sc.More())
579 				break;
580 		}
581 
582 		bool needEOLCheck = false;
583 
584 
585 		if (sc.state == SCE_P_OPERATOR) {
586 			kwLast = kwOther;
587 			sc.SetState(SCE_P_DEFAULT);
588 		} else if (sc.state == SCE_P_NUMBER) {
589 			if (!IsAWordChar(sc.ch, false) &&
590 					!(!base_n_number && ((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E')))) {
591 				sc.SetState(SCE_P_DEFAULT);
592 			}
593 		} else if (sc.state == SCE_P_IDENTIFIER) {
594 			if ((sc.ch == '.') || (!IsAWordChar(sc.ch, options.unicodeIdentifiers))) {
595 				char s[100];
596 				sc.GetCurrent(s, sizeof(s));
597 				int style = SCE_P_IDENTIFIER;
598 				if ((kwLast == kwImport) && (strcmp(s, "as") == 0)) {
599 					style = SCE_P_WORD;
600 				} else if (keywords.InList(s)) {
601 					style = SCE_P_WORD;
602 				} else if (kwLast == kwClass) {
603 					style = SCE_P_CLASSNAME;
604 				} else if (kwLast == kwDef) {
605 					style = SCE_P_DEFNAME;
606 				} else if (kwLast == kwCDef || kwLast == kwCPDef) {
607 					Sci_Position pos = sc.currentPos;
608 					unsigned char ch = styler.SafeGetCharAt(pos, '\0');
609 					while (ch != '\0') {
610 						if (ch == '(') {
611 							style = SCE_P_DEFNAME;
612 							break;
613 						} else if (ch == ':') {
614 							style = SCE_P_CLASSNAME;
615 							break;
616 						} else if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') {
617 							pos++;
618 							ch = styler.SafeGetCharAt(pos, '\0');
619 						} else {
620 							break;
621 						}
622 					}
623 				} else if (keywords2.InList(s)) {
624 					if (options.keywords2NoSubIdentifiers) {
625 						// We don't want to highlight keywords2
626 						// that are used as a sub-identifier,
627 						// i.e. not open in "foo.open".
628 						Sci_Position pos = styler.GetStartSegment() - 1;
629 						if (pos < 0 || (styler.SafeGetCharAt(pos, '\0') != '.'))
630 							style = SCE_P_WORD2;
631 					} else {
632 						style = SCE_P_WORD2;
633 					}
634 				} else {
635 					int subStyle = classifierIdentifiers.ValueFor(s);
636 					if (subStyle >= 0) {
637 						style = subStyle;
638 					}
639 				}
640 				sc.ChangeState(style);
641 				sc.SetState(SCE_P_DEFAULT);
642 				if (style == SCE_P_WORD) {
643 					if (0 == strcmp(s, "class"))
644 						kwLast = kwClass;
645 					else if (0 == strcmp(s, "def"))
646 						kwLast = kwDef;
647 					else if (0 == strcmp(s, "import"))
648 						kwLast = kwImport;
649 					else if (0 == strcmp(s, "cdef"))
650 						kwLast = kwCDef;
651 					else if (0 == strcmp(s, "cpdef"))
652 						kwLast = kwCPDef;
653 					else if (0 == strcmp(s, "cimport"))
654 						kwLast = kwImport;
655 					else if (kwLast != kwCDef && kwLast != kwCPDef)
656 						kwLast = kwOther;
657 				} else if (kwLast != kwCDef && kwLast != kwCPDef) {
658 					kwLast = kwOther;
659 				}
660 			}
661 		} else if ((sc.state == SCE_P_COMMENTLINE) || (sc.state == SCE_P_COMMENTBLOCK)) {
662 			if (sc.ch == '\r' || sc.ch == '\n') {
663 				sc.SetState(SCE_P_DEFAULT);
664 			}
665 		} else if (sc.state == SCE_P_DECORATOR) {
666 			if (!IsAWordStart(sc.ch, options.unicodeIdentifiers)) {
667 				sc.SetState(SCE_P_DEFAULT);
668 			}
669 		} else if (IsPySingleQuoteStringState(sc.state)) {
670 			if (sc.ch == '\\') {
671 				if ((sc.chNext == '\r') && (sc.GetRelative(2) == '\n')) {
672 					sc.Forward();
673 				}
674 				if (sc.chNext == '\n' || sc.chNext == '\r') {
675 					inContinuedString = true;
676 				} else {
677 					// Don't roll over the newline.
678 					sc.Forward();
679 				}
680 			} else if (sc.ch == GetPyStringQuoteChar(sc.state)) {
681 				sc.ForwardSetState(SCE_P_DEFAULT);
682 				needEOLCheck = true;
683 			}
684 		} else if ((sc.state == SCE_P_TRIPLE) || (sc.state == SCE_P_FTRIPLE)) {
685 			if (sc.ch == '\\') {
686 				sc.Forward();
687 			} else if (sc.Match(R"(''')")) {
688 				sc.Forward();
689 				sc.Forward();
690 				sc.ForwardSetState(SCE_P_DEFAULT);
691 				needEOLCheck = true;
692 			}
693 		} else if ((sc.state == SCE_P_TRIPLEDOUBLE) || (sc.state == SCE_P_FTRIPLEDOUBLE)) {
694 			if (sc.ch == '\\') {
695 				sc.Forward();
696 			} else if (sc.Match(R"(""")")) {
697 				sc.Forward();
698 				sc.Forward();
699 				sc.ForwardSetState(SCE_P_DEFAULT);
700 				needEOLCheck = true;
701 			}
702 		}
703 
704 		// Note if used and not if else because string states also match
705 		// some of the above clauses
706 		if (IsPyFStringState(sc.state) && sc.ch == '{') {
707 			if (sc.chNext == '{') {
708 				sc.Forward();
709 			} else {
710 				PushStateToStack(sc.state, fstringStateStack, currentFStringExp);
711 				sc.ForwardSetState(SCE_P_DEFAULT);
712 			}
713 			needEOLCheck = true;
714 		}
715 
716 		// If in an f-string expression, check for the ending quote(s)
717 		// and end f-string to handle syntactically incorrect cases like
718 		// f'{' and f"""{"""
719 		if (!fstringStateStack.empty() && (sc.ch == '\'' || sc.ch == '"')) {
720 			long matching_stack_i = -1;
721 			for (unsigned long stack_i = 0; stack_i < fstringStateStack.size() && matching_stack_i == -1; stack_i++) {
722 				const int stack_state = fstringStateStack[stack_i].state;
723 				const char quote = GetPyStringQuoteChar(stack_state);
724 				if (sc.ch == quote) {
725 					if (IsPySingleQuoteStringState(stack_state)) {
726 						matching_stack_i = stack_i;
727 					} else if (quote == '"' ? sc.Match(R"(""")") : sc.Match("'''")) {
728 						matching_stack_i = stack_i;
729 					}
730 				}
731 			}
732 
733 			if (matching_stack_i != -1) {
734 				sc.SetState(fstringStateStack[matching_stack_i].state);
735 				if (IsPyTripleQuoteStringState(fstringStateStack[matching_stack_i].state)) {
736 					sc.Forward();
737 					sc.Forward();
738 				}
739 				sc.ForwardSetState(SCE_P_DEFAULT);
740 				needEOLCheck = true;
741 
742 				while (fstringStateStack.size() > static_cast<unsigned long>(matching_stack_i)) {
743 					PopFromStateStack(fstringStateStack, currentFStringExp);
744 				}
745 			}
746 		}
747 		// End of code to find the end of a state
748 
749 		if (!indentGood && !IsASpaceOrTab(sc.ch)) {
750 			styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 1);
751 			startIndicator = sc.currentPos;
752 			indentGood = true;
753 		}
754 
755 		// One cdef or cpdef line, clear kwLast only at end of line
756 		if ((kwLast == kwCDef || kwLast == kwCPDef) && sc.atLineEnd) {
757 			kwLast = kwOther;
758 		}
759 
760 		// State exit code may have moved on to end of line
761 		if (needEOLCheck && sc.atLineEnd) {
762 			ProcessLineEnd(sc, fstringStateStack, currentFStringExp, inContinuedString);
763 			lineCurrent++;
764 			styler.IndentAmount(lineCurrent, &spaceFlags, IsPyComment);
765 			if (!sc.More())
766 				break;
767 		}
768 
769 		// If in f-string expression, check for }, :, ! to resume f-string state or update nesting count
770 		if (currentFStringExp != NULL && !IsPySingleQuoteStringState(sc.state) && !IsPyTripleQuoteStringState(sc.state)) {
771 			if (currentFStringExp->nestingCount == 0 && (sc.ch == '}' || sc.ch == ':' || (sc.ch == '!' && sc.chNext != '='))) {
772 				sc.SetState(PopFromStateStack(fstringStateStack, currentFStringExp));
773 			} else {
774 				if (sc.ch == '{' || sc.ch == '[' || sc.ch == '(') {
775 					currentFStringExp->nestingCount++;
776 				} else if (sc.ch == '}' || sc.ch == ']' || sc.ch == ')') {
777 					currentFStringExp->nestingCount--;
778 				}
779 			}
780 		}
781 
782 		// Check for a new state starting character
783 		if (sc.state == SCE_P_DEFAULT) {
784 			if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
785 				if (sc.ch == '0' && (sc.chNext == 'x' || sc.chNext == 'X')) {
786 					base_n_number = true;
787 					sc.SetState(SCE_P_NUMBER);
788 				} else if (sc.ch == '0' &&
789 						(sc.chNext == 'o' || sc.chNext == 'O' || sc.chNext == 'b' || sc.chNext == 'B')) {
790 					if (options.base2or8Literals) {
791 						base_n_number = true;
792 						sc.SetState(SCE_P_NUMBER);
793 					} else {
794 						sc.SetState(SCE_P_NUMBER);
795 						sc.ForwardSetState(SCE_P_IDENTIFIER);
796 					}
797 				} else {
798 					base_n_number = false;
799 					sc.SetState(SCE_P_NUMBER);
800 				}
801 			} else if ((IsASCII(sc.ch) && isoperator(static_cast<char>(sc.ch))) || sc.ch == '`') {
802 				sc.SetState(SCE_P_OPERATOR);
803 			} else if (sc.ch == '#') {
804 				sc.SetState(sc.chNext == '#' ? SCE_P_COMMENTBLOCK : SCE_P_COMMENTLINE);
805 			} else if (sc.ch == '@') {
806 				if (IsFirstNonWhitespace(sc.currentPos, styler))
807 					sc.SetState(SCE_P_DECORATOR);
808 				else
809 					sc.SetState(SCE_P_OPERATOR);
810 			} else if (IsPyStringStart(sc.ch, sc.chNext, sc.GetRelative(2), allowedLiterals)) {
811 				Sci_PositionU nextIndex = 0;
812 				sc.SetState(GetPyStringState(styler, sc.currentPos, &nextIndex, allowedLiterals));
813 				while (nextIndex > (sc.currentPos + 1) && sc.More()) {
814 					sc.Forward();
815 				}
816 			} else if (IsAWordStart(sc.ch, options.unicodeIdentifiers)) {
817 				sc.SetState(SCE_P_IDENTIFIER);
818 			}
819 		}
820 	}
821 	styler.IndicatorFill(startIndicator, sc.currentPos, indicatorWhitespace, 0);
822 	sc.Complete();
823 }
824 
IsCommentLine(Sci_Position line,Accessor & styler)825 static bool IsCommentLine(Sci_Position line, Accessor &styler) {
826 	Sci_Position pos = styler.LineStart(line);
827 	const Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
828 	for (Sci_Position i = pos; i < eol_pos; i++) {
829 		const char ch = styler[i];
830 		if (ch == '#')
831 			return true;
832 		else if (ch != ' ' && ch != '\t')
833 			return false;
834 	}
835 	return false;
836 }
837 
IsQuoteLine(Sci_Position line,const Accessor & styler)838 static bool IsQuoteLine(Sci_Position line, const Accessor &styler) {
839 	const int style = styler.StyleAt(styler.LineStart(line)) & 31;
840 	return IsPyTripleQuoteStringState(style);
841 }
842 
843 
Fold(Sci_PositionU startPos,Sci_Position length,int,IDocument * pAccess)844 void SCI_METHOD LexerPython::Fold(Sci_PositionU startPos, Sci_Position length, int /*initStyle - unused*/, IDocument *pAccess) {
845 	if (!options.fold)
846 		return;
847 
848 	Accessor styler(pAccess, NULL);
849 
850 	const Sci_Position maxPos = startPos + length;
851 	const Sci_Position maxLines = (maxPos == styler.Length()) ? styler.GetLine(maxPos) : styler.GetLine(maxPos - 1);	// Requested last line
852 	const Sci_Position docLines = styler.GetLine(styler.Length());	// Available last line
853 
854 	// Backtrack to previous non-blank line so we can determine indent level
855 	// for any white space lines (needed esp. within triple quoted strings)
856 	// and so we can fix any preceding fold level (which is why we go back
857 	// at least one line in all cases)
858 	int spaceFlags = 0;
859 	Sci_Position lineCurrent = styler.GetLine(startPos);
860 	int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
861 	while (lineCurrent > 0) {
862 		lineCurrent--;
863 		indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
864 		if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG) &&
865 				(!IsCommentLine(lineCurrent, styler)) &&
866 				(!IsQuoteLine(lineCurrent, styler)))
867 			break;
868 	}
869 	int indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
870 
871 	// Set up initial loop state
872 	startPos = styler.LineStart(lineCurrent);
873 	int prev_state = SCE_P_DEFAULT & 31;
874 	if (lineCurrent >= 1)
875 		prev_state = styler.StyleAt(startPos - 1) & 31;
876 	int prevQuote = options.foldQuotes && IsPyTripleQuoteStringState(prev_state);
877 
878 	// Process all characters to end of requested range or end of any triple quote
879 	//that hangs over the end of the range.  Cap processing in all cases
880 	// to end of document (in case of unclosed quote at end).
881 	while ((lineCurrent <= docLines) && ((lineCurrent <= maxLines) || prevQuote)) {
882 
883 		// Gather info
884 		int lev = indentCurrent;
885 		Sci_Position lineNext = lineCurrent + 1;
886 		int indentNext = indentCurrent;
887 		int quote = false;
888 		if (lineNext <= docLines) {
889 			// Information about next line is only available if not at end of document
890 			indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
891 			Sci_Position lookAtPos = (styler.LineStart(lineNext) == styler.Length()) ? styler.Length() - 1 : styler.LineStart(lineNext);
892 			const int style = styler.StyleAt(lookAtPos) & 31;
893 			quote = options.foldQuotes && IsPyTripleQuoteStringState(style);
894 		}
895 		const int quote_start = (quote && !prevQuote);
896 		const int quote_continue = (quote && prevQuote);
897 		if (!quote || !prevQuote)
898 			indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
899 		if (quote)
900 			indentNext = indentCurrentLevel;
901 		if (indentNext & SC_FOLDLEVELWHITEFLAG)
902 			indentNext = SC_FOLDLEVELWHITEFLAG | indentCurrentLevel;
903 
904 		if (quote_start) {
905 			// Place fold point at start of triple quoted string
906 			lev |= SC_FOLDLEVELHEADERFLAG;
907 		} else if (quote_continue || prevQuote) {
908 			// Add level to rest of lines in the string
909 			lev = lev + 1;
910 		}
911 
912 		// Skip past any blank lines for next indent level info; we skip also
913 		// comments (all comments, not just those starting in column 0)
914 		// which effectively folds them into surrounding code rather
915 		// than screwing up folding.  If comments end file, use the min
916 		// comment indent as the level after
917 
918 		int minCommentLevel = indentCurrentLevel;
919 		while (!quote &&
920 				(lineNext < docLines) &&
921 				((indentNext & SC_FOLDLEVELWHITEFLAG) ||
922 				 (lineNext <= docLines && IsCommentLine(lineNext, styler)))) {
923 
924 			if (IsCommentLine(lineNext, styler) && indentNext < minCommentLevel) {
925 				minCommentLevel = indentNext;
926 			}
927 
928 			lineNext++;
929 			indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
930 		}
931 
932 		const int levelAfterComments = ((lineNext < docLines) ? indentNext & SC_FOLDLEVELNUMBERMASK : minCommentLevel);
933 		const int levelBeforeComments = std::max(indentCurrentLevel, levelAfterComments);
934 
935 		// Now set all the indent levels on the lines we skipped
936 		// Do this from end to start.  Once we encounter one line
937 		// which is indented more than the line after the end of
938 		// the comment-block, use the level of the block before
939 
940 		Sci_Position skipLine = lineNext;
941 		int skipLevel = levelAfterComments;
942 
943 		while (--skipLine > lineCurrent) {
944 			const int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, NULL);
945 
946 			if (options.foldCompact) {
947 				if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
948 					skipLevel = levelBeforeComments;
949 
950 				int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
951 
952 				styler.SetLevel(skipLine, skipLevel | whiteFlag);
953 			} else {
954 				if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments &&
955 						!(skipLineIndent & SC_FOLDLEVELWHITEFLAG) &&
956 						!IsCommentLine(skipLine, styler))
957 					skipLevel = levelBeforeComments;
958 
959 				styler.SetLevel(skipLine, skipLevel);
960 			}
961 		}
962 
963 		// Set fold header on non-quote line
964 		if (!quote && !(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
965 			if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK))
966 				lev |= SC_FOLDLEVELHEADERFLAG;
967 		}
968 
969 		// Keep track of triple quote state of previous line
970 		prevQuote = quote;
971 
972 		// Set fold level for this line and move to next line
973 		styler.SetLevel(lineCurrent, options.foldCompact ? lev : lev & ~SC_FOLDLEVELWHITEFLAG);
974 		indentCurrent = indentNext;
975 		lineCurrent = lineNext;
976 	}
977 
978 	// NOTE: Cannot set level of last line here because indentCurrent doesn't have
979 	// header flag set; the loop above is crafted to take care of this case!
980 	//styler.SetLevel(lineCurrent, indentCurrent);
981 }
982 
983 LexerModule lmPython(SCLEX_PYTHON, LexerPython::LexerFactoryPython, "python",
984 		     pythonWordListDesc);
985