aboutsummaryrefslogtreecommitdiffhomepage
path: root/lexers/LexPython.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'lexers/LexPython.cxx')
-rw-r--r--lexers/LexPython.cxx81
1 files changed, 39 insertions, 42 deletions
diff --git a/lexers/LexPython.cxx b/lexers/LexPython.cxx
index fc1d8584e..d5ec1d558 100644
--- a/lexers/LexPython.cxx
+++ b/lexers/LexPython.cxx
@@ -5,12 +5,9 @@
// 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 <cstdlib>
+#include <cassert>
+#include <cstring>
#include <string>
#include <vector>
@@ -65,20 +62,20 @@ enum kwType { kwOther, kwClass, kwDef, kwImport, kwCDef, kwCTypeName, kwCPDef };
enum literalsAllowed { litNone = 0, litU = 1, litB = 2, litF = 4 };
-const int indicatorWhitespace = 1;
+constexpr int indicatorWhitespace = 1;
bool IsPyComment(Accessor &styler, Sci_Position pos, Sci_Position len) {
return len > 0 && styler[pos] == '#';
}
-bool IsPyStringTypeChar(int ch, literalsAllowed allowed) {
+bool IsPyStringTypeChar(int ch, literalsAllowed allowed) noexcept {
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) {
+bool IsPyStringStart(int ch, int chNext, int chNext2, literalsAllowed allowed) noexcept {
if (ch == '\'' || ch == '"')
return true;
if (IsPyStringTypeChar(ch, allowed)) {
@@ -93,22 +90,22 @@ bool IsPyStringStart(int ch, int chNext, int chNext2, literalsAllowed allowed) {
return false;
}
-bool IsPyFStringState(int st) {
+bool IsPyFStringState(int st) noexcept {
return ((st == SCE_P_FCHARACTER) || (st == SCE_P_FSTRING) ||
(st == SCE_P_FTRIPLE) || (st == SCE_P_FTRIPLEDOUBLE));
}
-bool IsPySingleQuoteStringState(int st) {
+bool IsPySingleQuoteStringState(int st) noexcept {
return ((st == SCE_P_CHARACTER) || (st == SCE_P_STRING) ||
(st == SCE_P_FCHARACTER) || (st == SCE_P_FSTRING));
}
-bool IsPyTripleQuoteStringState(int st) {
+bool IsPyTripleQuoteStringState(int st) noexcept {
return ((st == SCE_P_TRIPLE) || (st == SCE_P_TRIPLEDOUBLE) ||
(st == SCE_P_FTRIPLE) || (st == SCE_P_FTRIPLEDOUBLE));
}
-char GetPyStringQuoteChar(int st) {
+char GetPyStringQuoteChar(int st) noexcept {
if ((st == SCE_P_CHARACTER) || (st == SCE_P_FCHARACTER) ||
(st == SCE_P_TRIPLE) || (st == SCE_P_FTRIPLE))
return '\'';
@@ -126,7 +123,7 @@ void PushStateToStack(int state, std::vector<SingleFStringExpState> &stack, Sing
currentFStringExp = &stack.back();
}
-int PopFromStateStack(std::vector<SingleFStringExpState> &stack, SingleFStringExpState *&currentFStringExp) {
+int PopFromStateStack(std::vector<SingleFStringExpState> &stack, SingleFStringExpState *&currentFStringExp) noexcept {
int state = 0;
if (!stack.empty()) {
@@ -135,7 +132,7 @@ int PopFromStateStack(std::vector<SingleFStringExpState> &stack, SingleFStringEx
}
if (stack.empty()) {
- currentFStringExp = NULL;
+ currentFStringExp = nullptr;
} else {
currentFStringExp = &stack.back();
}
@@ -186,8 +183,8 @@ int GetPyStringState(Accessor &styler, Sci_Position i, Sci_PositionU *nextIndex,
}
inline bool IsAWordChar(int ch, bool unicodeIdentifiers) {
- if (ch < 0x80)
- return (isalnum(ch) || ch == '.' || ch == '_');
+ if (IsASCII(ch))
+ return (IsAlphaNumeric(ch) || ch == '.' || ch == '_');
if (!unicodeIdentifiers)
return false;
@@ -197,8 +194,8 @@ inline bool IsAWordChar(int ch, bool unicodeIdentifiers) {
}
inline bool IsAWordStart(int ch, bool unicodeIdentifiers) {
- if (ch < 0x80)
- return (isalpha(ch) || ch == '_');
+ if (IsASCII(ch))
+ return (IsUpperOrLowerCase(ch) || ch == '_');
if (!unicodeIdentifiers)
return false;
@@ -207,9 +204,9 @@ inline bool IsAWordStart(int ch, bool unicodeIdentifiers) {
return IsXidStart(ch);
}
-static bool IsFirstNonWhitespace(Sci_Position pos, Accessor &styler) {
- Sci_Position line = styler.GetLine(pos);
- Sci_Position start_pos = styler.LineStart(line);
+bool IsFirstNonWhitespace(Sci_Position pos, Accessor &styler) {
+ const Sci_Position line = styler.GetLine(pos);
+ const Sci_Position start_pos = styler.LineStart(line);
for (Sci_Position i = start_pos; i < pos; i++) {
const char ch = styler[i];
if (!(ch == ' ' || ch == '\t'))
@@ -246,7 +243,7 @@ struct OptionsPython {
unicodeIdentifiers = true;
}
- literalsAllowed AllowedLiterals() const {
+ literalsAllowed AllowedLiterals() const noexcept {
literalsAllowed allowedLiterals = stringsU ? litU : litNone;
if (stringsB)
allowedLiterals = static_cast<literalsAllowed>(allowedLiterals | litB);
@@ -256,10 +253,10 @@ struct OptionsPython {
}
};
-static const char *const pythonWordListDesc[] = {
+const char *const pythonWordListDesc[] = {
"Keywords",
"Highlighted identifiers",
- 0
+ nullptr
};
struct OptionSetPython : public OptionSet<OptionsPython> {
@@ -376,7 +373,7 @@ public:
void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
void *SCI_METHOD PrivateCall(int, void *) override {
- return 0;
+ return nullptr;
}
int SCI_METHOD LineEndTypesSupported() override {
@@ -428,7 +425,7 @@ Sci_Position SCI_METHOD LexerPython::PropertySet(const char *key, const char *va
}
Sci_Position SCI_METHOD LexerPython::WordListSet(int n, const char *wl) {
- WordList *wordListN = 0;
+ WordList *wordListN = nullptr;
switch (n) {
case 0:
wordListN = &keywords;
@@ -492,12 +489,12 @@ void LexerPython::ProcessLineEnd(StyleContext &sc, std::vector<SingleFStringExpS
}
void SCI_METHOD LexerPython::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
- Accessor styler(pAccess, NULL);
+ Accessor styler(pAccess, nullptr);
// 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;
+ SingleFStringExpState *currentFStringExp = nullptr;
const Sci_Position endPos = startPos + length;
@@ -508,7 +505,7 @@ void SCI_METHOD LexerPython::Lex(Sci_PositionU startPos, Sci_Position length, in
lineCurrent--;
// Look for backslash-continued lines
while (lineCurrent > 0) {
- Sci_Position eolPos = styler.LineStart(lineCurrent) - 1;
+ const Sci_Position eolPos = styler.LineStart(lineCurrent) - 1;
const int eolStyle = styler.StyleAt(eolPos);
if (eolStyle == SCE_P_STRING
|| eolStyle == SCE_P_CHARACTER
@@ -628,7 +625,7 @@ void SCI_METHOD LexerPython::Lex(Sci_PositionU startPos, Sci_Position length, in
// 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;
+ const Sci_Position pos = styler.GetStartSegment() - 1;
if (pos < 0 || (styler.SafeGetCharAt(pos, '\0') != '.'))
style = SCE_P_WORD2;
} else {
@@ -770,7 +767,7 @@ void SCI_METHOD LexerPython::Lex(Sci_PositionU startPos, Sci_Position length, in
}
// 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 && !IsPySingleQuoteStringState(sc.state) && !IsPyTripleQuoteStringState(sc.state)) {
if (currentFStringExp->nestingCount == 0 && (sc.ch == '}' || sc.ch == ':' || (sc.ch == '!' && sc.chNext != '='))) {
sc.SetState(PopFromStateStack(fstringStateStack, currentFStringExp));
} else {
@@ -801,7 +798,7 @@ void SCI_METHOD LexerPython::Lex(Sci_PositionU startPos, Sci_Position length, in
base_n_number = false;
sc.SetState(SCE_P_NUMBER);
}
- } else if ((IsASCII(sc.ch) && isoperator(static_cast<char>(sc.ch))) || sc.ch == '`') {
+ } else if (isoperator(sc.ch) || sc.ch == '`') {
sc.SetState(SCE_P_OPERATOR);
} else if (sc.ch == '#') {
sc.SetState(sc.chNext == '#' ? SCE_P_COMMENTBLOCK : SCE_P_COMMENTLINE);
@@ -826,7 +823,7 @@ void SCI_METHOD LexerPython::Lex(Sci_PositionU startPos, Sci_Position length, in
}
static bool IsCommentLine(Sci_Position line, Accessor &styler) {
- Sci_Position pos = styler.LineStart(line);
+ const Sci_Position pos = styler.LineStart(line);
const Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
for (Sci_Position i = pos; i < eol_pos; i++) {
const char ch = styler[i];
@@ -848,7 +845,7 @@ void SCI_METHOD LexerPython::Fold(Sci_PositionU startPos, Sci_Position length, i
if (!options.fold)
return;
- Accessor styler(pAccess, NULL);
+ Accessor styler(pAccess, nullptr);
const Sci_Position maxPos = startPos + length;
const Sci_Position maxLines = (maxPos == styler.Length()) ? styler.GetLine(maxPos) : styler.GetLine(maxPos - 1); // Requested last line
@@ -860,10 +857,10 @@ void SCI_METHOD LexerPython::Fold(Sci_PositionU startPos, Sci_Position length, i
// at least one line in all cases)
int spaceFlags = 0;
Sci_Position lineCurrent = styler.GetLine(startPos);
- int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
+ int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, nullptr);
while (lineCurrent > 0) {
lineCurrent--;
- indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
+ indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, nullptr);
if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG) &&
(!IsCommentLine(lineCurrent, styler)) &&
(!IsQuoteLine(lineCurrent, styler)))
@@ -890,8 +887,8 @@ void SCI_METHOD LexerPython::Fold(Sci_PositionU startPos, Sci_Position length, i
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);
+ indentNext = styler.IndentAmount(lineNext, &spaceFlags, nullptr);
+ const Sci_Position lookAtPos = (styler.LineStart(lineNext) == styler.Length()) ? styler.Length() - 1 : styler.LineStart(lineNext);
const int style = styler.StyleAt(lookAtPos) & 31;
quote = options.foldQuotes && IsPyTripleQuoteStringState(style);
}
@@ -929,7 +926,7 @@ void SCI_METHOD LexerPython::Fold(Sci_PositionU startPos, Sci_Position length, i
}
lineNext++;
- indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
+ indentNext = styler.IndentAmount(lineNext, &spaceFlags, nullptr);
}
const int levelAfterComments = ((lineNext < docLines) ? indentNext & SC_FOLDLEVELNUMBERMASK : minCommentLevel);
@@ -944,13 +941,13 @@ void SCI_METHOD LexerPython::Fold(Sci_PositionU startPos, Sci_Position length, i
int skipLevel = levelAfterComments;
while (--skipLine > lineCurrent) {
- const int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, NULL);
+ const int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, nullptr);
if (options.foldCompact) {
if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
skipLevel = levelBeforeComments;
- int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
+ const int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
styler.SetLevel(skipLine, skipLevel | whiteFlag);
} else {