Skip to content

Commit

Permalink
Optimize YAML and TOML lexers.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Jun 29, 2020
1 parent 12bf67d commit 62c0182
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
5 changes: 1 addition & 4 deletions scintilla/lexers/LexTOML.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ void ColouriseTOMLDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
braceCount = (lineState >> 8) & 0xff;
}

Sci_Position lineStartNext = styler.LineStart(sc.currentLine + 1);

while (sc.More()) {
switch (sc.state) {
case SCE_TOML_OPERATOR:
Expand Down Expand Up @@ -182,7 +180,7 @@ void ColouriseTOMLDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
if (sc.ch == ']') {
sc.Forward();
}
const int chNext = LexGetNextChar(sc.currentPos, lineStartNext, styler);
const int chNext = LexGetNextChar(sc.currentPos, sc.lineStartNext, styler);
if (chNext == '#') {
sc.SetState(SCE_TOML_DEFAULT);
}
Expand Down Expand Up @@ -320,7 +318,6 @@ void ColouriseTOMLDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
if (sc.atLineEnd) {
const int lineState = tableLevel | (braceCount << 8) | (lineType << 16);
styler.SetLineState(sc.currentLine, lineState);
lineStartNext = styler.LineStart(sc.currentLine + 2);
lineType = TOMLLineType_None;
visibleChars = 0;
tableLevel = 0;
Expand Down
17 changes: 7 additions & 10 deletions scintilla/lexers/LexYAML.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ constexpr bool IsYAMLDateTime(int ch, int chNext) noexcept {
|| (ch == ' ' && (chNext == '-' || IsADigit(chNext)));
}

bool IsYAMLText(StyleContext& sc, Sci_Position lineStartNext, int braceCount, const WordList *kwList) {
bool IsYAMLText(StyleContext& sc, int braceCount, const WordList *kwList) {
const int state = sc.state;
const Sci_Position endPos = braceCount? sc.styler.Length() : lineStartNext;
const Sci_Position endPos = braceCount? sc.styler.Length() : sc.lineStartNext;
const int chNext = LexGetNextChar(sc.currentPos, endPos, sc.styler);
if (chNext == ':') {
// possible key
Expand Down Expand Up @@ -166,11 +166,8 @@ void ColouriseYAMLDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
textIndentCount = (lineState >> 7) & 0x1ff;
}

Sci_Position lineStartNext = styler.LineStart(sc.currentLine + 1);

while (sc.More()) {
if (sc.atLineStart) {
lineStartNext = styler.LineStart(sc.currentLine + 1);
visibleChars = 0;
indentBefore = 0;
indentCount = 0;
Expand All @@ -180,7 +177,7 @@ void ColouriseYAMLDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
if (sc.state == SCE_YAML_BLOCK_SCALAR || (sc.state == SCE_YAML_TEXT && !braceCount) || sc.state == SCE_YAML_INDENTED_TEXT) {
indentEnded = true;
const bool hasComment = sc.state != SCE_YAML_BLOCK_SCALAR;
if (IsYAMLTextBlockEnd(hasComment, indentCount, textIndentCount, sc.currentPos, lineStartNext, styler)) {
if (IsYAMLTextBlockEnd(hasComment, indentCount, textIndentCount, sc.currentPos, sc.lineStartNext, styler)) {
textIndentCount = 0;
sc.SetState(SCE_YAML_DEFAULT);
sc.Forward(indentCount);
Expand All @@ -204,23 +201,23 @@ void ColouriseYAMLDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
if (!IsDecimalNumber(sc.chPrev, sc.ch, sc.chNext)) {
if (IsYAMLDateTime(sc.ch, sc.chNext)) {
sc.ChangeState(SCE_YAML_DATETIME);
} else if (IsYAMLText(sc, lineStartNext, braceCount, nullptr)) {
} else if (IsYAMLText(sc, braceCount, nullptr)) {
continue;
}
}
break;

case SCE_YAML_DATETIME:
if (!(IsIdentifierChar(sc.ch) || IsYAMLDateTime(sc.ch, sc.chNext))) {
if (IsYAMLText(sc, lineStartNext, braceCount, nullptr)) {
if (IsYAMLText(sc, braceCount, nullptr)) {
continue;
}
}
break;

case SCE_YAML_IDENTIFIER:
if (!IsAlpha(sc.ch)) {
if (IsYAMLText(sc, lineStartNext, braceCount, keywordLists[0])) {
if (IsYAMLText(sc, braceCount, keywordLists[0])) {
continue;
}
}
Expand Down Expand Up @@ -330,7 +327,7 @@ void ColouriseYAMLDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initSt
visibleChars = 1;
sc.SetState(SCE_YAML_DOCUMENT);
sc.Forward(3);
const int chNext = LexGetNextChar(sc.currentPos + 1, lineStartNext, styler);
const int chNext = LexGetNextChar(sc.currentPos + 1, sc.lineStartNext, styler);
if (chNext != '\0') {
sc.SetState(SCE_YAML_DEFAULT);
}
Expand Down

0 comments on commit 62c0182

Please sign in to comment.