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