Skip to content

Commit a483bcc

Browse files
committed
Simplify Ruby lexer.
1 parent 05b8336 commit a483bcc

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

scintilla/lexers/LexRuby.cxx

+25-39
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ constexpr bool IsIdentifierStyle(int style) noexcept {
125125
|| style == SCE_RB_BUILTIN_FUNCTION;
126126
}
127127

128-
int ClassifyWordRb(Sci_PositionU start, Sci_PositionU end, char ch, char chNext, LexerWordList keywordLists, LexAccessor &styler, char *prevWord) {
128+
int ClassifyWordRb(Sci_PositionU end, char ch, char chNext, LexerWordList keywordLists, LexAccessor &styler, char *prevWord) {
129129
char s[MAX_KEYWORD_LENGTH + 1];
130+
const Sci_PositionU start = styler.GetStartSegment();
130131
styler.GetRange(start, end, s, sizeof(s));
131132
int chAttr = SCE_RB_IDENTIFIER;
132133
int style = SCE_RB_DEFAULT;
@@ -622,7 +623,7 @@ bool sureThisIsNotHeredoc(Sci_Position lt2StartPos, LexAccessor &styler) {
622623
// move to the start of the first line that is not in a
623624
// multi-line construct
624625

625-
void synchronizeDocStart(Sci_PositionU & startPos, Sci_Position &length, int &initStyle, LexAccessor &styler, bool skipWhiteSpace = false) {
626+
void synchronizeDocStart(Sci_PositionU &startPos, Sci_Position &length, int &initStyle, LexAccessor &styler, bool skipWhiteSpace = false) {
626627
#if 0
627628
styler.Flush();
628629
const int style = styler.StyleAt(startPos);
@@ -794,8 +795,7 @@ void ColouriseRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
794795
// Begin of here-doc (the line after the here-doc delimiter):
795796
HereDoc.State = 2;
796797
if (state == SCE_RB_WORD) {
797-
const Sci_Position wordStartPos = styler.GetStartSegment();
798-
ClassifyWordRb(wordStartPos, i, ch, chNext, keywordLists, styler, prevWord);
798+
ClassifyWordRb(i, ch, chNext, keywordLists, styler, prevWord);
799799
} else {
800800
styler.ColorTo(i, state);
801801
}
@@ -1158,8 +1158,7 @@ void ColouriseRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
11581158
// No need to handle this state -- we'll just move to the end
11591159
preferRE = false;
11601160
} else {
1161-
const Sci_Position wordStartPos = styler.GetStartSegment();
1162-
const int word_style = ClassifyWordRb(wordStartPos, i, ch, chNext, keywordLists, styler, prevWord);
1161+
const int word_style = ClassifyWordRb(i, ch, chNext, keywordLists, styler, prevWord);
11631162
preferRE = false;
11641163
switch (word_style) {
11651164
case SCE_RB_WORD:
@@ -1494,7 +1493,7 @@ void ColouriseRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
14941493
if (state == SCE_RB_WORD) {
14951494
// We've ended on a word, possibly at EOF, and need to
14961495
// classify it.
1497-
ClassifyWordRb(styler.GetStartSegment(), lengthDoc, '\0', '\0', keywordLists, styler, prevWord);
1496+
ClassifyWordRb(lengthDoc, '\0', '\0', keywordLists, styler, prevWord);
14981497
} else {
14991498
styler.ColorTo(lengthDoc, state);
15001499
}
@@ -1503,24 +1502,6 @@ void ColouriseRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
15031502
// Helper functions for folding, disambiguation keywords
15041503
// Assert that there are no high-bit chars
15051504

1506-
void getPrevWord(Sci_Position pos, char *prevWord, LexAccessor &styler, int word_state) {
1507-
Sci_Position i;
1508-
styler.Flush();
1509-
for (i = pos - 1; i > 0; i--) {
1510-
if (styler.StyleAt(i) != word_state) {
1511-
i++;
1512-
break;
1513-
}
1514-
}
1515-
if (i < pos - MAX_KEYWORD_LENGTH) // overflow
1516-
i = pos - MAX_KEYWORD_LENGTH;
1517-
char *dst = prevWord;
1518-
for (; i <= pos; i++) {
1519-
*dst++ = styler[i];
1520-
}
1521-
*dst = 0;
1522-
}
1523-
15241505
bool keywordIsAmbiguous(const char *prevWord) noexcept {
15251506
// Order from most likely used to least likely
15261507
// Lots of ways to do a loop in Ruby besides 'while/until'
@@ -1608,9 +1589,9 @@ bool keywordIsModifier(const char *word, Sci_Position pos, LexAccessor &styler)
16081589
//XXX: Make a list of other keywords where 'if' isn't a modifier
16091590
// and can appear legitimately
16101591
// Formulate this to avoid warnings from most compilers
1611-
if (StrEqual(word, "if")) {
1592+
if (StrEqual(word, "if") && pos - 3 >= lineStartPosn) {
16121593
char prevWord[MAX_KEYWORD_LENGTH + 1];
1613-
getPrevWord(pos, prevWord, styler, SCE_RB_WORD);
1594+
styler.GetRange(pos - 3, pos + 1, prevWord, sizeof(prevWord));
16141595
return !StrEqual(prevWord, "else");
16151596
}
16161597
return true;
@@ -1756,6 +1737,8 @@ void FoldRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, Lexer
17561737
};
17571738
MethodDefinition method_definition = MethodDefinition::None;
17581739
int argument_paren_count = 0;
1740+
char word[MAX_KEYWORD_LENGTH + 1];
1741+
int wordLen = 0;
17591742
bool heredocOpen = false;
17601743

17611744
while (startPos < endPos) {
@@ -1778,18 +1761,21 @@ void FoldRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, Lexer
17781761
} else if (ch == ')' || ch == '}' || ch == ']') {
17791762
levelCurrent--;
17801763
}
1781-
} else if (style == SCE_RB_WORD && styleNext != SCE_RB_WORD) {
1782-
// Look at the keyword on the left and decide what to do
1783-
char prevWord[MAX_KEYWORD_LENGTH + 1]; // 1 byte for zero
1784-
prevWord[0] = 0;
1785-
getPrevWord(startPos - 1, prevWord, styler, SCE_RB_WORD);
1786-
if (StrEqual(prevWord, "end")) {
1787-
levelCurrent--;
1788-
} else if (StrEqual(prevWord, "def")) {
1789-
levelCurrent++;
1790-
method_definition = MethodDefinition::Define;
1791-
} else if (keywordLists[KeywordIndex_CodeFolding].InList(prevWord)) {
1792-
levelCurrent++;
1764+
} else if (style == SCE_RB_WORD) {
1765+
if (wordLen < MAX_KEYWORD_LENGTH) {
1766+
word[wordLen++] = static_cast<char>(ch);
1767+
}
1768+
if (styleNext != SCE_RB_WORD) {
1769+
word[wordLen] = '\0';
1770+
wordLen = 0;
1771+
if (StrEqual(word, "end")) {
1772+
levelCurrent--;
1773+
} else if (StrEqual(word, "def")) {
1774+
levelCurrent++;
1775+
method_definition = MethodDefinition::Define;
1776+
} else if (keywordLists[KeywordIndex_CodeFolding].InList(word)) {
1777+
levelCurrent++;
1778+
}
17931779
}
17941780
} else if (style == SCE_RB_HERE_DELIM && !heredocOpen) {
17951781
if (stylePrev == SCE_RB_OPERATOR && chPrev == '<' && styler.SafeGetCharAt(startPos - 3) == '<') {

0 commit comments

Comments
 (0)