Skip to content

Commit

Permalink
[Python] Fix infinite loop for new line in PEP 701 f-string examples …
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Mar 14, 2023
1 parent e5dd9a4 commit 91f9e9c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions scintilla/lexers/LexPython.cxx
Expand Up @@ -84,6 +84,12 @@ struct EscapeSequence {
}
};

enum class FormattedStringPart {
None,
FormatSpec,
End,
};

struct FormattedStringState {
int state;
int nestedLevel;
Expand Down Expand Up @@ -386,7 +392,7 @@ void ColourisePyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyl
bool prevLineContinuation = false;
bool lineContinuation = false;
bool insideUrl = false;
bool insideFStringFormatSpec = false;
FormattedStringPart fstringPart = FormattedStringPart::None;
EscapeSequence escSeq;

std::vector<FormattedStringState> nestedState;
Expand Down Expand Up @@ -494,7 +500,7 @@ void ColourisePyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyl
case SCE_PY_TRIPLE_RAWBYTES_SQ:
case SCE_PY_TRIPLE_RAWBYTES_DQ:
if (sc.atLineStart && !lineContinuation) {
if (!IsPyTripleQuotedString(sc.state)) {
if (fstringPart == FormattedStringPart::None && !IsPyTripleQuotedString(sc.state)) {
sc.SetState(SCE_PY_DEFAULT);
break;
}
Expand Down Expand Up @@ -558,7 +564,7 @@ void ColourisePyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyl
} else {
nestedState.back().nestedLevel += 1;
}
insideFStringFormatSpec = false;
fstringPart = FormattedStringPart::None;
sc.SetState(SCE_PY_OPERATOR2);
sc.ForwardSetState(SCE_PY_DEFAULT);
} else if (sc.chNext == '}' || sc.chNext == '!' || sc.chNext == ':' || IsIdentifierCharEx(sc.chNext)) {
Expand All @@ -584,7 +590,7 @@ void ColourisePyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyl
const int state = sc.state;
sc.SetState(SCE_PY_OPERATOR2);
sc.ForwardSetState(state);
insideFStringFormatSpec = false;
fstringPart = FormattedStringPart::None;
continue;
}
}
Expand Down Expand Up @@ -638,7 +644,7 @@ void ColourisePyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyl

case '!':
case ':':
if (insideFStringFormatSpec) {
if (fstringPart == FormattedStringPart::FormatSpec) {
const Sci_Position length = CheckBraceFormatSpecifier(sc, styler);
if (length != 0) {
const int state = sc.state;
Expand Down Expand Up @@ -807,7 +813,7 @@ void ColourisePyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyl
if (interpolating) {
const FormattedStringState &state = nestedState.back();
if (state.parenCount == 0 && IsPyFormattedStringEnd(sc, braceCount)) {
insideFStringFormatSpec = sc.ch != '}';
fstringPart = (sc.ch == '}') ? FormattedStringPart::End : FormattedStringPart::FormatSpec;
sc.SetState(state.state);
continue;
}
Expand Down Expand Up @@ -860,7 +866,7 @@ void ColourisePyDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyl
styler.SetLineState(sc.currentLine, lineState);
lineState = 0;
kwType = KeywordType::None;
insideFStringFormatSpec = false;
fstringPart = FormattedStringPart::None;
visibleChars = 0;
visibleCharsBefore = 0;
indentCount = 0;
Expand Down

0 comments on commit 91f9e9c

Please sign in to comment.