Debian Codegraph @

scintilla/lexilla/lexers/LexRuby.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexRuby.cxx
3 ** Lexer for Ruby.
4 **/
5 // Copyright 2001- by Clemens Wyss <[email protected]>
6 // The License.txt file describes the conditions under which this software may be distributed.
7
8 #include <cstdlib>
9 #include <cassert>
10 #include <cstring>
11 #include <cctype>
12 #include <cstdio>
13 #include <cstdarg>
14
15 #include <string>
16 #include <string_view>
17
18 #include "ILexer.h"
19 #include "Scintilla.h"
20 #include "SciLexer.h"
21
22 #include "WordList.h"
23 #include "LexAccessor.h"
24 #include "Accessor.h"
25 #include "StyleContext.h"
26 #include "CharacterSet.h"
27 #include "LexerModule.h"
28
29 using namespace Lexilla;
30
31 namespace {
32
33 //XXX Identical to Perl, put in common area
34 constexpr bool isEOLChar(char ch) noexcept {
35 return (ch == '\r') || (ch == '\n');
36 }
37
38 constexpr bool isSafeASCII(char ch) noexcept {
39 return static_cast<unsigned char>(ch) <= 127;
40 }
41
42 // This one's redundant, but makes for more readable code
43 constexpr bool isHighBitChar(char ch) noexcept {
44 return static_cast<unsigned char>(ch) > 127;
45 }
46
47 inline bool isSafeAlpha(char ch) noexcept {
48 return (isSafeASCII(ch) && isalpha(ch)) || ch == '_';
49 }
50
51 inline bool isSafeAlphaOrHigh(char ch) noexcept {
52 return isHighBitChar(ch) || isalpha(ch) || ch == '_';
53 }
54
55 inline bool isSafeAlnum(char ch) noexcept {
56 return (isSafeASCII(ch) && isalnum(ch)) || ch == '_';
57 }
58
59 inline bool isSafeAlnumOrHigh(char ch) noexcept {
60 return isHighBitChar(ch) || isalnum(ch) || ch == '_';
61 }
62
63 inline bool isSafeDigit(char ch) noexcept {
64 return isSafeASCII(ch) && isdigit(ch);
65 }
66
67 inline bool isSafeWordcharOrHigh(char ch) noexcept {
68 // Error: scintilla's KeyWords.h includes '.' as a word-char
69 // we want to separate things that can take methods from the
70 // methods.
71 return isHighBitChar(ch) || isalnum(ch) || ch == '_';
72 }
73
74 constexpr bool isWhiteSpace(char ch) noexcept {
75 return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
76 }
77
78 inline bool isQestionMarkChar(char chNext, char chNext2) noexcept {
79 // followed by a single character or escape sequence that corresponds to a single codepoint
80 if (isSafeAlnum(chNext)) {
81 return !isSafeWordcharOrHigh(chNext2);
82 }
83 // multibyte character, escape sequence, punctuation
84 return !IsASpace(chNext);
85 }
86
87 #define MAX_KEYWORD_LENGTH 200
88
89 #define STYLE_MASK 63
90 #define actual_style(style) ((style) & STYLE_MASK)
91
92 bool followsDot(Sci_PositionU pos, Accessor &styler) {
93 styler.Flush();
94 for (; pos >= 1; --pos) {
95 const int style = actual_style(styler.StyleAt(pos));
96 char ch;
97 switch (style) {
98 case SCE_RB_DEFAULT:
99 ch = styler[pos];
100 if (ch == ' ' || ch == '\t') {
101 //continue
102 } else {
103 return false;
104 }
105 break;
106
107 case SCE_RB_OPERATOR:
108 return styler[pos] == '.';
109
110 default:
111 return false;
112 }
113 }
114 return false;
115 }
116
117 // Forward declarations
118 bool keywordIsAmbiguous(const char *prevWord) noexcept;
119 bool keywordDoStartsLoop(Sci_Position pos, Accessor &styler);
120 bool keywordIsModifier(const char *word, Sci_Position pos, Accessor &styler);
121
122 // pseudo style: prefer regex after identifier
123 #define SCE_RB_IDENTIFIER_PREFERRE SCE_RB_UPPER_BOUND
124
125 int ClassifyWordRb(Sci_PositionU start, Sci_PositionU end, char ch, WordList &keywords, Accessor &styler, char *prevWord) {
126 char s[MAX_KEYWORD_LENGTH];
127 Sci_PositionU j = 0;
128 Sci_PositionU lim = end - start + 1; // num chars to copy
129 if (lim >= MAX_KEYWORD_LENGTH) {
130 lim = MAX_KEYWORD_LENGTH - 1;
131 }
132 for (Sci_PositionU i = start; j < lim; i++, j++) {
133 s[j] = styler[i];
134 }
135 s[j] = '\0';
136 int chAttr = SCE_RB_IDENTIFIER;
137 int style = SCE_RB_DEFAULT;
138 if (0 == strcmp(prevWord, "class"))
139 chAttr = SCE_RB_CLASSNAME;
140 else if (0 == strcmp(prevWord, "module"))
141 chAttr = SCE_RB_MODULE_NAME;
142 else if (0 == strcmp(prevWord, "def")) {
143 chAttr = SCE_RB_DEFNAME;
144 if (ch == '.') {
145 if (strcmp(s, "self") == 0) {
146 style = SCE_RB_WORD_DEMOTED;
147 } else {
148 style = SCE_RB_IDENTIFIER;
149 }
150 }
151 } else if (keywords.InList(s) && ((start == 0) || !followsDot(start - 1, styler))) {
152 if (keywordIsAmbiguous(s)
153 && keywordIsModifier(s, start, styler)) {
154
155 // Demoted keywords are colored as keywords,
156 // but do not affect changes in indentation.
157 //
158 // Consider the word 'if':
159 // 1. <<if test ...>> : normal
160 // 2. <<stmt if test>> : demoted
161 // 3. <<lhs = if ...>> : normal: start a new indent level
162 // 4. <<obj.if = 10>> : color as identifier, since it follows '.'
163
164 chAttr = SCE_RB_WORD_DEMOTED;
165 } else {
166 chAttr = SCE_RB_WORD;
167 style = SCE_RB_WORD;
168 strcpy(prevWord, s);
169 }
170 }
171 if (style == SCE_RB_DEFAULT) {
172 style = chAttr;
173 prevWord[0] = 0;
174 }
175 styler.ColourTo(end, style);
176
177 if (chAttr == SCE_RB_IDENTIFIER) {
178 // find heredoc in lib/ruby folder: rg "\w+\s+<<[\w\-~'\"`]"
179 // Kernel methods
180 if (!strcmp(s, "puts") || !strcmp(s, "print") || !strcmp(s, "warn") || !strcmp(s, "eval")) {
181 chAttr = SCE_RB_IDENTIFIER_PREFERRE;
182 }
183 }
184 return chAttr;
185 }
186
187
188 //XXX Identical to Perl, put in common area
189 bool isMatch(Accessor &styler, Sci_Position lengthDoc, Sci_Position pos, const char *val) {
190 if ((pos + static_cast<int>(strlen(val))) >= lengthDoc) {
191 return false;
192 }
193 while (*val) {
194 if (*val != styler[pos++]) {
195 return false;
196 }
197 val++;
198 }
199 return true;
200 }
201
202 // Do Ruby better -- find the end of the line, work back,
203 // and then check for leading white space
204
205 // Precondition: the here-doc target can be indented
206 bool lookingAtHereDocDelim(Accessor &styler, Sci_Position pos, Sci_Position lengthDoc, const char *HereDocDelim) {
207 if (!isMatch(styler, lengthDoc, pos, HereDocDelim)) {
208 return false;
209 }
210 while (--pos > 0) {
211 const char ch = styler[pos];
212 if (isEOLChar(ch)) {
213 return true;
214 } else if (ch != ' ' && ch != '\t') {
215 return false;
216 }
217 }
218 return false;
219 }
220
221 //XXX Identical to Perl, put in common area
222 constexpr char opposite(char ch) noexcept {
223 if (ch == '(')
224 return ')';
225 if (ch == '[')
226 return ']';
227 if (ch == '{')
228 return '}';
229 if (ch == '<')
230 return '>';
231 return ch;
232 }
233
234 // Null transitions when we see we've reached the end
235 // and need to relex the curr char.
236
237 void redo_char(Sci_Position &i, char &ch, char &chNext, char &chNext2, int &state) noexcept {
238 i--;
239 chNext2 = chNext;
240 chNext = ch;
241 state = SCE_RB_DEFAULT;
242 }
243
244 void advance_char(Sci_Position &i, char &ch, char &chNext, char &chNext2) noexcept {
245 i++;
246 ch = chNext;
247 chNext = chNext2;
248 }
249
250 // precondition: startPos points to one after the EOL char
251 bool currLineContainsHereDelims(Sci_Position &startPos, Accessor &styler) {
252 if (startPos <= 1)
253 return false;
254
255 Sci_Position pos;
256 for (pos = startPos - 1; pos > 0; pos--) {
257 const char ch = styler.SafeGetCharAt(pos);
258 if (isEOLChar(ch)) {
259 // Leave the pointers where they are -- there are no
260 // here doc delims on the current line, even if
261 // the EOL isn't default style
262
263 return false;
264 } else {
265 styler.Flush();
266 if (actual_style(styler.StyleAt(pos)) == SCE_RB_HERE_DELIM) {
267 break;
268 }
269 }
270 }
271 if (pos == 0) {
272 return false;
273 }
274 // Update the pointers so we don't have to re-analyze the string
275 startPos = pos;
276 return true;
277 }
278
279 // This class is used by the enter and exit methods, so it needs
280 // to be hoisted out of the function.
281
282 class QuoteCls {
283 public:
284 int Count;
285 char Up;
286 char Down;
287 QuoteCls() noexcept {
288 New();
289 }
290 void New() noexcept {
291 Count = 0;
292 Up = '\0';
293 Down = '\0';
294 }
295 void Open(char u) noexcept {
296 Count++;
297 Up = u;
298 Down = opposite(Up);
299 }
300 };
301
302 constexpr bool isPercentLiteral(int state) noexcept {
303 return state == SCE_RB_STRING_Q
304 || state == SCE_RB_STRING_QQ
305 // excluded SCE_RB_STRING_QR
306 || state == SCE_RB_STRING_W
307 || state == SCE_RB_STRING_QW
308 || state == SCE_RB_STRING_I
309 || state == SCE_RB_STRING_QI
310 || state == SCE_RB_STRING_QS
311 || state == SCE_RB_STRING_QX;
312 }
313
314 constexpr bool isInterpolableLiteral(int state) noexcept {
315 return state != SCE_RB_STRING_Q
316 && state != SCE_RB_STRING_W
317 && state != SCE_RB_STRING_I
318 && state != SCE_RB_STRING_QS
319 && state != SCE_RB_CHARACTER;
320 }
321
322 inline bool isSingleSpecialVariable(char ch) noexcept {
323 // https://docs.ruby-lang.org/en/master/globals_rdoc.html
324 return strchr("~*$?!@/\\;,.=:<>\"&`'+", ch) != nullptr;
325 }
326
327 void InterpolateVariable(LexAccessor &styler, int state, Sci_Position &i, char &ch, char &chNext, char chNext2) {
328 Sci_Position pos = i;
329 styler.ColourTo(pos - 1, state);
330 styler.ColourTo(pos, SCE_RB_OPERATOR);
331 state = SCE_RB_GLOBAL;
332 pos += 2;
333 unsigned len = 0;
334 if (chNext == '$') {
335 if (chNext2 == '-') {
336 ++pos;
337 len = 2;
338 } else if (isSingleSpecialVariable(chNext2)) {
339 ++pos;
340 len = 1;
341 }
342 } else {
343 state = SCE_RB_INSTANCE_VAR;
344 if (chNext2 == '@') {
345 state = SCE_RB_CLASS_VAR;
346 ++pos;
347 }
348 }
349 while (true) {
350 chNext2 = styler.SafeGetCharAt(pos);
351 --len;
352 if (len == 0 || !isSafeWordcharOrHigh(chNext2)) {
353 break;
354 }
355 ++pos;
356 }
357 --pos;
358 styler.ColourTo(pos, state);
359 i = pos;
360 ch = chNext;
361 chNext = chNext2;
362 }
363
364 bool isEmptyLine(Sci_Position pos, Accessor &styler) {
365 int spaceFlags = 0;
366 const Sci_Position lineCurrent = styler.GetLine(pos);
367 const int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, nullptr);
368 return (indentCurrent & SC_FOLDLEVELWHITEFLAG) != 0;
369 }
370
371 bool RE_CanFollowKeyword(const char *keyword) noexcept {
372 if (!strcmp(keyword, "and")
373 || !strcmp(keyword, "begin")
374 || !strcmp(keyword, "break")
375 || !strcmp(keyword, "case")
376 || !strcmp(keyword, "do")
377 || !strcmp(keyword, "else")
378 || !strcmp(keyword, "elsif")
379 || !strcmp(keyword, "if")
380 || !strcmp(keyword, "next")
381 || !strcmp(keyword, "return")
382 || !strcmp(keyword, "when")
383 || !strcmp(keyword, "unless")
384 || !strcmp(keyword, "until")
385 || !strcmp(keyword, "not")
386 || !strcmp(keyword, "or")) {
387 return true;
388 }
389 return false;
390 }
391
392 // Look at chars up to but not including endPos
393 // Don't look at styles in case we're looking forward
394
395 Sci_Position skipWhitespace(Sci_Position startPos, Sci_Position endPos, Accessor &styler) {
396 for (Sci_Position i = startPos; i < endPos; i++) {
397 if (!IsASpaceOrTab(styler[i])) {
398 return i;
399 }
400 }
401 return endPos;
402 }
403
404 // This routine looks for false positives like
405 // undef foo, <<
406 // There aren't too many.
407 //
408 // iPrev points to the start of <<
409
410 bool sureThisIsHeredoc(Sci_Position iPrev, Accessor &styler, char *prevWord) {
411
412 // Not so fast, since Ruby's so dynamic. Check the context
413 // to make sure we're OK.
414 int prevStyle;
415 const Sci_Position lineStart = styler.GetLine(iPrev);
416 const Sci_Position lineStartPosn = styler.LineStart(lineStart);
417 styler.Flush();
418
419 // Find the first word after some whitespace
420 const Sci_Position firstWordPosn = skipWhitespace(lineStartPosn, iPrev, styler);
421 if (firstWordPosn >= iPrev) {
422 // Have something like {^ <<}
423 //XXX Look at the first previous non-comment non-white line
424 // to establish the context. Not too likely though.
425 return true;
426 } else {
427 prevStyle = styler.StyleAt(firstWordPosn);
428 switch (prevStyle) {
429 case SCE_RB_WORD:
430 case SCE_RB_WORD_DEMOTED:
431 case SCE_RB_IDENTIFIER:
432 break;
433 default:
434 return true;
435 }
436 }
437 Sci_Position firstWordEndPosn = firstWordPosn;
438 char *dst = prevWord;
439 for (;;) {
440 if (firstWordEndPosn >= iPrev ||
441 styler.StyleAt(firstWordEndPosn) != prevStyle) {
442 *dst = 0;
443 break;
444 }
445 *dst++ = styler[firstWordEndPosn];
446 firstWordEndPosn += 1;
447 }
448 //XXX Write a style-aware thing to regex scintilla buffer objects
449 if (!strcmp(prevWord, "undef")
450 || !strcmp(prevWord, "def")
451 || !strcmp(prevWord, "alias")) {
452 // These keywords are what we were looking for
453 return false;
454 }
455 return true;
456 }
457
458 // Routine that saves us from allocating a buffer for the here-doc target
459 // targetEndPos points one past the end of the current target
460 bool haveTargetMatch(Sci_Position currPos, Sci_Position lengthDoc, Sci_Position targetStartPos, Sci_Position targetEndPos, Accessor &styler) {
461 if (lengthDoc - currPos < targetEndPos - targetStartPos) {
462 return false;
463 }
464 for (Sci_Position i = targetStartPos, j = currPos;
465 i < targetEndPos && j < lengthDoc;
466 i++, j++) {
467 if (styler[i] != styler[j]) {
468 return false;
469 }
470 }
471 return true;
472 }
473
474 // Finds the start position of the expression containing @p pos
475 // @p min_pos should be a known expression start, e.g. the start of the line
476 Sci_Position findExpressionStart(Sci_Position pos, Sci_Position min_pos, Accessor &styler) {
477 int depth = 0;
478 for (; pos > min_pos; pos -= 1) {
479 const int style = styler.StyleAt(pos - 1);
480 if (style == SCE_RB_OPERATOR) {
481 const int ch = styler[pos - 1];
482 if (ch == '}' || ch == ')' || ch == ']') {
483 depth += 1;
484 } else if (ch == '{' || ch == '(' || ch == '[') {
485 if (depth == 0) {
486 break;
487 } else {
488 depth -= 1;
489 }
490 } else if (ch == ';' && depth == 0) {
491 break;
492 }
493 }
494 }
495 return pos;
496 }
497
498 // We need a check because the form
499 // [identifier] <<[target]
500 // is ambiguous. The Ruby lexer/parser resolves it by
501 // looking to see if [identifier] names a variable or a
502 // function. If it's the first, it's the start of a here-doc.
503 // If it's a var, it's an operator. This lexer doesn't
504 // maintain a symbol table, so it looks ahead to see what's
505 // going on, in cases where we have
506 // ^[white-space]*[identifier([.|::]identifier)*][white-space]*<<[target]
507 //
508 // If there's no occurrence of [target] on a line, assume we don't.
509
510 // return true == yes, we have no heredocs
511
512 bool sureThisIsNotHeredoc(Sci_Position lt2StartPos, Accessor &styler) {
513 // Use full document, not just part we're styling
514 const Sci_Position lengthDoc = styler.Length();
515 const Sci_Position lineStart = styler.GetLine(lt2StartPos);
516 const Sci_Position lineStartPosn = styler.LineStart(lineStart);
517 styler.Flush();
518 constexpr bool definitely_not_a_here_doc = true;
519 constexpr bool looks_like_a_here_doc = false;
520
521 // find the expression start rather than the line start
522 const Sci_Position exprStartPosn = findExpressionStart(lt2StartPos, lineStartPosn, styler);
523
524 // Find the first word after some whitespace
525 Sci_Position firstWordPosn = skipWhitespace(exprStartPosn, lt2StartPos, styler);
526 if (firstWordPosn >= lt2StartPos) {
527 return definitely_not_a_here_doc;
528 }
529 int prevStyle = styler.StyleAt(firstWordPosn);
530 // If we have '<<' following a keyword, it's not a heredoc
531 if (prevStyle != SCE_RB_IDENTIFIER
532 && prevStyle != SCE_RB_GLOBAL // $stdout and $stderr
533 && prevStyle != SCE_RB_SYMBOL
534 && prevStyle != SCE_RB_INSTANCE_VAR
535 && prevStyle != SCE_RB_CLASS_VAR) {
536 return definitely_not_a_here_doc;
537 }
538 int newStyle = prevStyle;
539 // Some compilers incorrectly warn about uninit newStyle
540 for (firstWordPosn += 1; firstWordPosn <= lt2StartPos; firstWordPosn += 1) {
541 // Inner loop looks at the name
542 for (; firstWordPosn <= lt2StartPos; firstWordPosn += 1) {
543 newStyle = styler.StyleAt(firstWordPosn);
544 if (newStyle != prevStyle) {
545 break;
546 }
547 }
548 // Do we have '::' or '.'?
549 if (firstWordPosn < lt2StartPos && newStyle == SCE_RB_OPERATOR) {
550 const char ch = styler[firstWordPosn];
551 if (ch == '.') {
552 // yes
553 } else if (ch == ':') {
554 if (styler.StyleAt(++firstWordPosn) != SCE_RB_OPERATOR) {
555 return definitely_not_a_here_doc;
556 } else if (styler[firstWordPosn] != ':') {
557 return definitely_not_a_here_doc;
558 }
559 } else {
560 break;
561 }
562 } else {
563 break;
564 }
565 // on second and next passes, only identifiers may appear since
566 // class and instance variable are private
567 prevStyle = SCE_RB_IDENTIFIER;
568 }
569 // Skip next batch of white-space
570 firstWordPosn = skipWhitespace(firstWordPosn, lt2StartPos, styler);
571 // possible symbol for an implicit hash argument
572 if (firstWordPosn < lt2StartPos && styler.StyleAt(firstWordPosn) == SCE_RB_SYMBOL) {
573 for (; firstWordPosn <= lt2StartPos; firstWordPosn += 1) {
574 if (styler.StyleAt(firstWordPosn) != SCE_RB_SYMBOL) {
575 break;
576 }
577 }
578 // Skip next batch of white-space
579 firstWordPosn = skipWhitespace(firstWordPosn, lt2StartPos, styler);
580 }
581 if (firstWordPosn != lt2StartPos) {
582 // Have [[^ws[identifier]ws[*something_else*]ws<<
583 return definitely_not_a_here_doc;
584 }
585 // OK, now 'j' will point to the current spot moving ahead
586 Sci_Position j = firstWordPosn + 1;
587 if (styler.StyleAt(j) != SCE_RB_OPERATOR || styler[j] != '<') {
588 // This shouldn't happen
589 return definitely_not_a_here_doc;
590 }
591 const Sci_Position nextLineStartPosn = styler.LineStart(lineStart + 1);
592 if (nextLineStartPosn >= lengthDoc) {
593 return definitely_not_a_here_doc;
594 }
595 j = skipWhitespace(j + 1, nextLineStartPosn, styler);
596 if (j >= lengthDoc) {
597 return definitely_not_a_here_doc;
598 }
599 bool allow_indent;
600 Sci_Position target_start;
601 Sci_Position target_end;
602 // From this point on no more styling, since we're looking ahead
603 if (styler[j] == '-' || styler[j] == '~') {
604 allow_indent = true;
605 j++;
606 } else {
607 allow_indent = false;
608 }
609
610 // Allow for quoted targets.
611 char target_quote = 0;
612 switch (styler[j]) {
613 case '\'':
614 case '"':
615 case '`':
616 target_quote = styler[j];
617 j += 1;
618 }
619
620 if (isSafeAlnumOrHigh(styler[j])) {
621 // Init target_end because some compilers think it won't
622 // be initialized by the time it's used
623 target_start = target_end = j;
624 j++;
625 } else {
626 return definitely_not_a_here_doc;
627 }
628 for (; j < lengthDoc; j++) {
629 if (!isSafeAlnumOrHigh(styler[j])) {
630 if (target_quote && styler[j] != target_quote) {
631 // unquoted end
632 return definitely_not_a_here_doc;
633 }
634
635 // And for now make sure that it's a newline
636 // don't handle arbitrary expressions yet
637
638 target_end = j;
639 if (target_quote) {
640 // Now we can move to the character after the string delimiter.
641 j += 1;
642 }
643 j = skipWhitespace(j, lengthDoc, styler);
644 if (j >= lengthDoc) {
645 return definitely_not_a_here_doc;
646 } else {
647 const char ch = styler[j];
648 if (ch == '#' || isEOLChar(ch) || ch == '.' || ch == ',' || IsLowerCase(ch)) {
649 // This is OK, so break and continue;
650 break;
651 } else {
652 return definitely_not_a_here_doc;
653 }
654 }
655 }
656 }
657
658 // Just look at the start of each line
659 Sci_Position last_line = styler.GetLine(lengthDoc - 1);
660 // But don't go too far
661 if (last_line > lineStart + 50) {
662 last_line = lineStart + 50;
663 }
664 for (Sci_Position line_num = lineStart + 1; line_num <= last_line; line_num++) {
665 j = styler.LineStart(line_num);
666 if (allow_indent) {
667 j = skipWhitespace(j, lengthDoc, styler);
668 }
669 // target_end is one past the end
670 if (haveTargetMatch(j, lengthDoc, target_start, target_end, styler)) {
671 // We got it
672 return looks_like_a_here_doc;
673 }
674 }
675 return definitely_not_a_here_doc;
676 }
677
678 //todo: if we aren't looking at a stdio character,
679 // move to the start of the first line that is not in a
680 // multi-line construct
681
682 void synchronizeDocStart(Sci_PositionU &startPos, Sci_Position &length, int &initStyle, Accessor &styler, bool skipWhiteSpace=false) {
683
684 styler.Flush();
685 const int style = actual_style(styler.StyleAt(startPos));
686 switch (style) {
687 case SCE_RB_STDIN:
688 case SCE_RB_STDOUT:
689 case SCE_RB_STDERR:
690 // Don't do anything else with these.
691 return;
692 }
693
694 Sci_Position pos = startPos;
695 // Quick way to characterize each line
696 Sci_Position lineStart;
697 for (lineStart = styler.GetLine(pos); lineStart > 0; lineStart--) {
698 // Now look at the style before the previous line's EOL
699 pos = styler.LineStart(lineStart) - 1;
700 if (pos <= 10) {
701 lineStart = 0;
702 break;
703 }
704 const char ch = styler.SafeGetCharAt(pos);
705 const char chPrev = styler.SafeGetCharAt(pos - 1);
706 if (ch == '\n' && chPrev == '\r') {
707 pos--;
708 }
709 if (styler.SafeGetCharAt(pos - 1) == '\\') {
710 // Continuation line -- keep going
711 } else if (actual_style(styler.StyleAt(pos)) != SCE_RB_DEFAULT) {
712 // Part of multi-line construct -- keep going
713 } else if (currLineContainsHereDelims(pos, styler)) {
714 // Keep going, with pos and length now pointing
715 // at the end of the here-doc delimiter
716 } else if (skipWhiteSpace && isEmptyLine(pos, styler)) {
717 // Keep going
718 } else {
719 break;
720 }
721 }
722 pos = styler.LineStart(lineStart);
723 length += (startPos - pos);
724 startPos = pos;
725 initStyle = SCE_RB_DEFAULT;
726 }
727
728 void ColouriseRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[], Accessor &styler) {
729
730 // Lexer for Ruby often has to backtrack to start of current style to determine
731 // which characters are being used as quotes, how deeply nested is the
732 // start position and what the termination string is for here documents
733
734 WordList &keywords = *keywordlists[0];
735
736 class HereDocCls {
737 public:
738 int State = 0;
739 // States
740 // 0: '<<' encountered
741 // 1: collect the delimiter
742 // 1b: text between the end of the delimiter and the EOL
743 // 2: here doc text (lines after the delimiter)
744 char Quote = 0; // the char after '<<'
745 bool Quoted = false; // true if Quote in ('\'','"','`')
746 int DelimiterLength = 0; // strlen(Delimiter)
747 char Delimiter[256] {}; // the Delimiter, limit of 256: from Perl
748 bool CanBeIndented = false;
749 };
750 HereDocCls HereDoc;
751
752 QuoteCls Quote;
753
754 synchronizeDocStart(startPos, length, initStyle, styler, false);
755
756 bool preferRE = true;
757 bool afterDef = false;
758 int state = initStyle;
759 const Sci_Position lengthDoc = startPos + length;
760
761 char prevWord[MAX_KEYWORD_LENGTH + 1] = ""; // 1 byte for zero
762 if (length == 0)
763 return;
764
765 char chPrev = styler.SafeGetCharAt(startPos - 1);
766 char chNext = styler.SafeGetCharAt(startPos);
767 bool is_real_number = true; // Differentiate between constants and ?-sequences.
768 styler.StartAt(startPos);
769 styler.StartSegment(startPos);
770
771 static constexpr int q_states[] = {
772 SCE_RB_STRING_Q,
773 SCE_RB_STRING_QQ,
774 SCE_RB_STRING_QR,
775 SCE_RB_STRING_W,
776 SCE_RB_STRING_QW,
777 SCE_RB_STRING_QX,
778 SCE_RB_STRING_I,
779 SCE_RB_STRING_QI,
780 SCE_RB_STRING_QS,
781 };
782 constexpr const char *q_chars = "qQrwWxiIs";
783
784 // In most cases a value of 2 should be ample for the code in the
785 // Ruby library, and the code the user is likely to enter.
786 // For example,
787 // fu_output_message "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}"
788 // if options[:verbose]
789 // from fileutils.rb nests to a level of 2
790 // If the user actually hits a 6th occurrence of '#{' in a double-quoted
791 // string (including regex'es, %Q, %<sym>, %w, and other strings
792 // that interpolate), it will stay as a string. The problem with this
793 // is that quotes might flip, a 7th '#{' will look like a comment,
794 // and code-folding might be wrong.
795
796 // If anyone runs into this problem, I recommend raising this
797 // value slightly higher to replacing the fixed array with a linked
798 // list. Keep in mind this code will be called every time the lexer
799 // is invoked.
800
801 #define INNER_STRINGS_MAX_COUNT 5
802 class InnerExpression {
803 // These vars track our instances of "...#{,,,%Q<..#{,,,}...>,,,}..."
804 int inner_string_types[INNER_STRINGS_MAX_COUNT] {};
805 // Track # braces when we push a new #{ thing
806 int inner_expn_brace_counts[INNER_STRINGS_MAX_COUNT] {};
807 QuoteCls inner_quotes[INNER_STRINGS_MAX_COUNT];
808 int inner_string_count = 0;
809
810 public:
811 int brace_counts = 0; // Number of #{ ... } things within an expression
812
813 bool canEnter() const noexcept {
814 return inner_string_count < INNER_STRINGS_MAX_COUNT;
815 }
816 bool canExit() const noexcept {
817 return inner_string_count > 0;
818 }
819 void enter(int &state, const QuoteCls &curr_quote) noexcept {
820 inner_string_types[inner_string_count] = state;
821 state = SCE_RB_DEFAULT;
822 inner_expn_brace_counts[inner_string_count] = brace_counts;
823 brace_counts = 0;
824 inner_quotes[inner_string_count] = curr_quote;
825 ++inner_string_count;
826 }
827 void exit(int &state, QuoteCls &curr_quote) noexcept {
828 --inner_string_count;
829 state = inner_string_types[inner_string_count];
830 brace_counts = inner_expn_brace_counts[inner_string_count];
831 curr_quote = inner_quotes[inner_string_count];
832 }
833 };
834 InnerExpression innerExpr;
835
836 for (Sci_Position i = startPos; i < lengthDoc; i++) {
837 char ch = chNext;
838 chNext = styler.SafeGetCharAt(i + 1);
839 char chNext2 = styler.SafeGetCharAt(i + 2);
840
841 if (styler.IsLeadByte(ch)) {
842 chNext = chNext2;
843 chPrev = ' ';
844 i += 1;
845 continue;
846 }
847
848 // skip on DOS/Windows
849 //No, don't, because some things will get tagged on,
850 // so we won't recognize keywords, for example
851 #if 0
852 if (ch == '\r' && chNext == '\n') {
853 continue;
854 }
855 #endif
856
857 if (HereDoc.State == 1 && isEOLChar(ch)) {
858 // Begin of here-doc (the line after the here-doc delimiter):
859 HereDoc.State = 2;
860 if (state == SCE_RB_WORD) {
861 const Sci_Position wordStartPos = styler.GetStartSegment();
862 ClassifyWordRb(wordStartPos, i - 1, ch, keywords, styler, prevWord);
863 } else {
864 styler.ColourTo(i - 1, state);
865 }
866 // Don't check for a missing quote, just jump into
867 // the here-doc state
868 state = SCE_RB_HERE_QQ;
869 if (HereDoc.Quoted) {
870 if (HereDoc.Quote == '\'') {
871 state = SCE_RB_HERE_Q;
872 } else if (HereDoc.Quote == '`') {
873 state = SCE_RB_HERE_QX;
874 }
875 }
876 }
877
878 // Regular transitions
879 if (state == SCE_RB_DEFAULT) {
880 if (isSafeDigit(ch)) {
881 styler.ColourTo(i - 1, state);
882 state = SCE_RB_NUMBER;
883 is_real_number = true;
884 } else if (isSafeAlphaOrHigh(ch)) {
885 styler.ColourTo(i - 1, state);
886 state = SCE_RB_WORD;
887 } else if (ch == '#') {
888 styler.ColourTo(i - 1, state);
889 state = SCE_RB_COMMENTLINE;
890 } else if (ch == '=') {
891 // =begin indicates the start of a comment (doc) block
892 if ((i == 0 || isEOLChar(chPrev))
893 && chNext == 'b'
894 && styler.SafeGetCharAt(i + 2) == 'e'
895 && styler.SafeGetCharAt(i + 3) == 'g'
896 && styler.SafeGetCharAt(i + 4) == 'i'
897 && styler.SafeGetCharAt(i + 5) == 'n'
898 && !isSafeWordcharOrHigh(styler.SafeGetCharAt(i + 6))) {
899 styler.ColourTo(i - 1, state);
900 state = SCE_RB_POD;
901 } else {
902 styler.ColourTo(i - 1, state);
903 styler.ColourTo(i, SCE_RB_OPERATOR);
904 preferRE = true;
905 }
906 } else if (ch == '"') {
907 styler.ColourTo(i - 1, state);
908 state = SCE_RB_STRING;
909 Quote.New();
910 Quote.Open(ch);
911 } else if (ch == '\'') {
912 styler.ColourTo(i - 1, state);
913 state = SCE_RB_CHARACTER;
914 Quote.New();
915 Quote.Open(ch);
916 } else if (ch == '`') {
917 styler.ColourTo(i - 1, state);
918 state = SCE_RB_BACKTICKS;
919 Quote.New();
920 Quote.Open(ch);
921 } else if (ch == '@') {
922 // Instance or class var
923 styler.ColourTo(i - 1, state);
924 if (chNext == '@') {
925 state = SCE_RB_CLASS_VAR;
926 advance_char(i, ch, chNext, chNext2); // pass by ref
927 } else {
928 state = SCE_RB_INSTANCE_VAR;
929 }
930 } else if (ch == '$') {
931 // Check for a builtin global
932 styler.ColourTo(i - 1, state);
933 // Recognize it bit by bit
934 state = SCE_RB_GLOBAL;
935 } else if (ch == '/' && preferRE) {
936 // Ambiguous operator
937 styler.ColourTo(i - 1, state);
938 state = SCE_RB_REGEX;
939 Quote.New();
940 Quote.Open(ch);
941 } else if (ch == '<' && chNext == '<' && chNext2 != '=') {
942 if (afterDef) {
943 afterDef = false;
944 prevWord[0] = 0;
945 }
946 // Recognise the '<<' symbol - either a here document or a binary op
947 styler.ColourTo(i - 1, state);
948 i++;
949 chNext = chNext2;
950 styler.ColourTo(i, SCE_RB_OPERATOR);
951
952 if (!(strchr("\"\'`_-~", chNext2) || isSafeAlphaOrHigh(chNext2))) {
953 // It's definitely not a here-doc,
954 // based on Ruby's lexer/parser in the
955 // heredoc_identifier routine.
956 // Nothing else to do.
957 } else if (preferRE) {
958 if (sureThisIsHeredoc(i - 1, styler, prevWord)) {
959 state = SCE_RB_HERE_DELIM;
960 HereDoc.State = 0;
961 }
962 // else leave it in default state
963 } else {
964 if (sureThisIsNotHeredoc(i - 1, styler)) {
965 // leave state as default
966 // We don't have all the heuristics Perl has for indications
967 // of a here-doc, because '<<' is overloadable and used
968 // for so many other classes.
969 } else {
970 state = SCE_RB_HERE_DELIM;
971 HereDoc.State = 0;
972 }
973 }
974 preferRE = (state != SCE_RB_HERE_DELIM);
975 } else if (ch == ':') {
976 afterDef = false;
977 styler.ColourTo(i - 1, state);
978 if (chNext == ':') {
979 // Mark "::" as an operator, not symbol start
980 styler.ColourTo(i + 1, SCE_RB_OPERATOR);
981 advance_char(i, ch, chNext, chNext2); // pass by ref
982 state = SCE_RB_DEFAULT;
983 preferRE = false;
984 } else if (isSafeWordcharOrHigh(chNext)) {
985 state = SCE_RB_SYMBOL;
986 } else if ((chNext == '@' || chNext == '$') &&
987 isSafeWordcharOrHigh(chNext2)) {
988 // instance and global variable followed by an identifier
989 advance_char(i, ch, chNext, chNext2);
990 state = SCE_RB_SYMBOL;
991 } else if (((chNext == '@' && chNext2 == '@') ||
992 (chNext == '$' && chNext2 == '-')) &&
993 isSafeWordcharOrHigh(styler.SafeGetCharAt(i+3))) {
994 // class variables and special global variable "$-IDENTCHAR"
995 state = SCE_RB_SYMBOL;
996 // $-IDENTCHAR doesn't continue past the IDENTCHAR
997 if (chNext == '$') {
998 styler.ColourTo(i+3, SCE_RB_SYMBOL);
999 state = SCE_RB_DEFAULT;
1000 }
1001 i += 3;
1002 ch = styler.SafeGetCharAt(i);
1003 chNext = styler.SafeGetCharAt(i+1);
1004 } else if (chNext == '$' && isSingleSpecialVariable(chNext2)) {
1005 // single-character special global variables
1006 i += 2;
1007 ch = chNext2;
1008 chNext = styler.SafeGetCharAt(i+1);
1009 styler.ColourTo(i, SCE_RB_SYMBOL);
1010 state = SCE_RB_DEFAULT;
1011 } else if (strchr("[*!~+-*/%=<>&^|", chNext)) {
1012 // Do the operator analysis in-line, looking ahead
1013 // Based on the table in pickaxe 2nd ed., page 339
1014 bool doColoring = true;
1015 switch (chNext) {
1016 case '[':
1017 if (chNext2 == ']') {
1018 const char ch_tmp = styler.SafeGetCharAt(i + 3);
1019 if (ch_tmp == '=') {
1020 i += 3;
1021 ch = ch_tmp;
1022 chNext = styler.SafeGetCharAt(i + 1);
1023 } else {
1024 i += 2;
1025 ch = chNext2;
1026 chNext = ch_tmp;
1027 }
1028 } else {
1029 doColoring = false;
1030 }
1031 break;
1032
1033 case '*':
1034 if (chNext2 == '*') {
1035 i += 2;
1036 ch = chNext2;
1037 chNext = styler.SafeGetCharAt(i + 1);
1038 } else {
1039 advance_char(i, ch, chNext, chNext2);
1040 }
1041 break;
1042
1043 case '!':
1044 if (chNext2 == '=' || chNext2 == '~') {
1045 i += 2;
1046 ch = chNext2;
1047 chNext = styler.SafeGetCharAt(i + 1);
1048 } else {
1049 advance_char(i, ch, chNext, chNext2);
1050 }
1051 break;
1052
1053 case '<':
1054 if (chNext2 == '<') {
1055 i += 2;
1056 ch = chNext2;
1057 chNext = styler.SafeGetCharAt(i + 1);
1058 } else if (chNext2 == '=') {
1059 const char ch_tmp = styler.SafeGetCharAt(i + 3);
1060 if (ch_tmp == '>') { // <=> operator
1061 i += 3;
1062 ch = ch_tmp;
1063 chNext = styler.SafeGetCharAt(i + 1);
1064 } else {
1065 i += 2;
1066 ch = chNext2;
1067 chNext = ch_tmp;
1068 }
1069 } else {
1070 advance_char(i, ch, chNext, chNext2);
1071 }
1072 break;
1073
1074 default:
1075 // Simple one-character operators
1076 advance_char(i, ch, chNext, chNext2);
1077 break;
1078 }
1079 if (doColoring) {
1080 styler.ColourTo(i, SCE_RB_SYMBOL);
1081 state = SCE_RB_DEFAULT;
1082 }
1083 } else if (!preferRE && !IsASpace(chNext)) {
1084 // Don't color symbol strings (yet)
1085 // Just color the ":" and color rest as string
1086 styler.ColourTo(i, SCE_RB_SYMBOL);
1087 state = SCE_RB_DEFAULT;
1088 } else {
1089 styler.ColourTo(i, SCE_RB_OPERATOR);
1090 state = SCE_RB_DEFAULT;
1091 preferRE = true;
1092 }
1093 } else if (ch == '%' && !afterDef) {
1094 styler.ColourTo(i - 1, state);
1095 bool have_string = false;
1096 const char *hit = strchr(q_chars, chNext);
1097 if (hit != nullptr && !isSafeWordcharOrHigh(chNext2)) {
1098 Quote.New();
1099 state = q_states[hit - q_chars];
1100 Quote.Open(chNext2);
1101 i += 2;
1102 ch = chNext2;
1103 chNext = styler.SafeGetCharAt(i + 1);
1104 have_string = true;
1105 } else if ((preferRE || (!isWhiteSpace(chNext) && chNext != '=')) && !isSafeWordcharOrHigh(chNext)) {
1106 // Ruby doesn't allow high bit chars here,
1107 // but the editor host might
1108 Quote.New();
1109 state = SCE_RB_STRING_QQ;
1110 Quote.Open(chNext);
1111 advance_char(i, ch, chNext, chNext2); // pass by ref
1112 have_string = true;
1113 }
1114 if (!have_string) {
1115 styler.ColourTo(i, SCE_RB_OPERATOR);
1116 // stay in default
1117 preferRE = true;
1118 }
1119 } else if (ch == '?') {
1120 afterDef = false;
1121 styler.ColourTo(i - 1, state);
1122 if (isHighBitChar(chNext)) {
1123 preferRE = false;
1124 Sci_Position width = 1;
1125 styler.MultiByteAccess()->GetCharacterAndWidth(i + 1, &width);
1126 chNext = styler.SafeGetCharAt(i + 1 + width);
1127 if (isSafeWordcharOrHigh(chNext)) {
1128 styler.ColourTo(i, SCE_RB_OPERATOR);
1129 i += width;
1130 state = SCE_RB_WORD;
1131 } else {
1132 i += width;
1133 styler.ColourTo(i, SCE_RB_NUMBER);
1134 }
1135 } else if (!isQestionMarkChar(chNext, chNext2)) {
1136 styler.ColourTo(i, SCE_RB_OPERATOR);
1137 preferRE = chNext <= ' ';
1138 } else {
1139 // It's the start of a character code escape sequence
1140 // Color it as a number.
1141 state = SCE_RB_NUMBER;
1142 is_real_number = false;
1143 }
1144 } else if (isoperator(ch) || ch == '.') {
1145 styler.ColourTo(i - 1, state);
1146 if (afterDef && ch != '.') {
1147 afterDef = false;
1148 prevWord[0] = 0;
1149 if (chNext == '@' && (ch == '+' || ch == '-' || ch == '!')) {
1150 // unary operator method
1151 ch = chNext;
1152 chNext = chNext2;
1153 i += 1;
1154 }
1155 }
1156 styler.ColourTo(i, SCE_RB_OPERATOR);
1157 // If we're ending an expression or block,
1158 // assume it ends an object, and the ambivalent
1159 // constructs are binary operators
1160 //
1161 // So if we don't have one of these chars,
1162 // we aren't ending an object exp'n, and ops
1163 // like : << / are unary operators.
1164
1165 if (ch == '{') {
1166 ++innerExpr.brace_counts;
1167 preferRE = true;
1168 } else if (ch == '}' && --innerExpr.brace_counts < 0
1169 && innerExpr.canExit()) {
1170 styler.ColourTo(i, SCE_RB_OPERATOR);
1171 innerExpr.exit(state, Quote);
1172 } else {
1173 preferRE = !AnyOf(ch, ')', '}', ']', '.');
1174 }
1175 // Stay in default state
1176 } else if (isEOLChar(ch)) {
1177 afterDef = false;
1178 // Make sure it's a true line-end, with no backslash
1179 if ((ch == '\r' || (ch == '\n' && chPrev != '\r'))
1180 && chPrev != '\\') {
1181 // Assume we've hit the end of the statement.
1182 preferRE = true;
1183 }
1184 }
1185 if (afterDef && state != SCE_RB_DEFAULT) {
1186 afterDef = false;
1187 }
1188 } else if (state == SCE_RB_WORD) {
1189 if (ch == '.' || !isSafeWordcharOrHigh(ch)) {
1190 // Words include x? in all contexts,
1191 // and <letters>= after either 'def' or a dot
1192 // Move along until a complete word is on our left
1193
1194 // Default accessor treats '.' as word-chars,
1195 // but we don't for now.
1196
1197 if (ch == '='
1198 && isSafeWordcharOrHigh(chPrev)
1199 && (chNext == '('
1200 || isWhiteSpace(chNext))
1201 && (!strcmp(prevWord, "def")
1202 || followsDot(styler.GetStartSegment(), styler))) {
1203 // <name>= is a name only when being def'd -- Get it the next time
1204 // This means that <name>=<name> is always lexed as
1205 // <name>, (op, =), <name>
1206 } else if (ch == ':'
1207 && isSafeWordcharOrHigh(chPrev)
1208 && isWhiteSpace(chNext)) {
1209 // keyword argument, symbol Hash key
1210 styler.ColourTo(i, SCE_RB_SYMBOL);
1211 state = SCE_RB_DEFAULT;
1212 preferRE = true;
1213 } else if ((ch == '?' || ch == '!')
1214 && isSafeWordcharOrHigh(chPrev)
1215 && !isSafeWordcharOrHigh(chNext)) {
1216 // <name>? is a name -- Get it the next time
1217 // But <name>?<name> is always lexed as
1218 // <name>, (op, ?), <name>
1219 // Same with <name>! to indicate a method that
1220 // modifies its target
1221 } else if (isEOLChar(ch)
1222 && isMatch(styler, lengthDoc, i - 7, "__END__")) {
1223 styler.ColourTo(i, SCE_RB_DATASECTION);
1224 state = SCE_RB_DATASECTION;
1225 // No need to handle this state -- we'll just move to the end
1226 preferRE = false;
1227 } else {
1228 const Sci_Position wordStartPos = styler.GetStartSegment();
1229 const int word_style = ClassifyWordRb(wordStartPos, i - 1, ch, keywords, styler, prevWord);
1230 switch (word_style) {
1231 case SCE_RB_WORD:
1232 afterDef = strcmp(prevWord, "def") == 0;
1233 preferRE = RE_CanFollowKeyword(prevWord);
1234 break;
1235
1236 case SCE_RB_WORD_DEMOTED:
1237 case SCE_RB_DEFNAME:
1238 case SCE_RB_IDENTIFIER_PREFERRE:
1239 preferRE = true;
1240 break;
1241
1242 case SCE_RB_IDENTIFIER:
1243 preferRE = isEOLChar(ch);
1244 break;
1245
1246 default:
1247 preferRE = false;
1248 }
1249 if (ch == '.') {
1250 // We might be redefining an operator-method
1251 afterDef = word_style == SCE_RB_DEFNAME;
1252 }
1253 // And if it's the first
1254 redo_char(i, ch, chNext, chNext2, state); // pass by ref
1255 }
1256 }
1257 } else if (state == SCE_RB_NUMBER) {
1258 if (!is_real_number) {
1259 if (ch != '\\' || chPrev == '\\') {
1260 styler.ColourTo(i, state);
1261 state = SCE_RB_DEFAULT;
1262 preferRE = false;
1263 } else if (strchr("\\ntrfvaebs", chNext)) {
1264 // Terminal escape sequence -- handle it next time
1265 // Nothing more to do this time through the loop
1266 } else if (chNext == 'C' || chNext == 'M') {
1267 if (chNext2 != '-') {
1268 // \C or \M ends the sequence -- handle it next time
1269 } else {
1270 // Move from abc?\C-x
1271 // ^
1272 // to
1273 // ^
1274 i += 2;
1275 ch = chNext2;
1276 chNext = styler.SafeGetCharAt(i + 1);
1277 }
1278 } else if (chNext == 'c') {
1279 // Stay here, \c is a combining sequence
1280 advance_char(i, ch, chNext, chNext2); // pass by ref
1281 } else {
1282 // ?\x, including ?\\ is final.
1283 styler.ColourTo(i + 1, state);
1284 state = SCE_RB_DEFAULT;
1285 preferRE = false;
1286 advance_char(i, ch, chNext, chNext2);
1287 }
1288 } else if (isSafeAlnumOrHigh(ch) || ch == '_' || (ch == '.' && isSafeDigit(chNext))) {
1289 // Keep going
1290 } else if (ch == '.' && chNext == '.') {
1291 styler.ColourTo(i - 1, state);
1292 redo_char(i, ch, chNext, chNext2, state); // pass by ref
1293 } else {
1294 styler.ColourTo(i - 1, state);
1295 redo_char(i, ch, chNext, chNext2, state); // pass by ref
1296 preferRE = false;
1297 }
1298 } else if (state == SCE_RB_COMMENTLINE) {
1299 if (isEOLChar(ch)) {
1300 styler.ColourTo(i - 1, state);
1301 state = SCE_RB_DEFAULT;
1302 // Use whatever setting we had going into the comment
1303 }
1304 } else if (state == SCE_RB_HERE_DELIM) {
1305 // See the comment for SCE_RB_HERE_DELIM in LexPerl.cxx
1306 // Slightly different: if we find an immediate '-',
1307 // the target can appear indented.
1308
1309 if (HereDoc.State == 0) { // '<<' encountered
1310 HereDoc.State = 1;
1311 HereDoc.DelimiterLength = 0;
1312 if (ch == '-' || ch == '~') {
1313 HereDoc.CanBeIndented = true;
1314 advance_char(i, ch, chNext, chNext2); // pass by ref
1315 } else {
1316 HereDoc.CanBeIndented = false;
1317 }
1318 if (isEOLChar(ch)) {
1319 // Bail out of doing a here doc if there's no target
1320 state = SCE_RB_DEFAULT;
1321 preferRE = false;
1322 } else {
1323 HereDoc.Quote = ch;
1324
1325 if (ch == '\'' || ch == '"' || ch == '`') {
1326 HereDoc.Quoted = true;
1327 HereDoc.Delimiter[0] = '\0';
1328 } else {
1329 HereDoc.Quoted = false;
1330 HereDoc.Delimiter[0] = ch;
1331 HereDoc.Delimiter[1] = '\0';
1332 HereDoc.DelimiterLength = 1;
1333 }
1334 }
1335 } else if (HereDoc.State == 1) { // collect the delimiter
1336 if (isEOLChar(ch)) {
1337 // End the quote now, and go back for more
1338 styler.ColourTo(i - 1, state);
1339 state = SCE_RB_DEFAULT;
1340 i--;
1341 chNext = ch;
1342 preferRE = false;
1343 } else if (HereDoc.Quoted) {
1344 if (ch == HereDoc.Quote) { // closing quote => end of delimiter
1345 styler.ColourTo(i, state);
1346 state = SCE_RB_DEFAULT;
1347 preferRE = false;
1348 } else {
1349 if (ch == '\\' && !isEOLChar(chNext)) {
1350 advance_char(i, ch, chNext, chNext2);
1351 }
1352 HereDoc.Delimiter[HereDoc.DelimiterLength++] = ch;
1353 HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
1354 }
1355 } else { // an unquoted here-doc delimiter
1356 if (isSafeAlnumOrHigh(ch) || ch == '_') {
1357 HereDoc.Delimiter[HereDoc.DelimiterLength++] = ch;
1358 HereDoc.Delimiter[HereDoc.DelimiterLength] = '\0';
1359 } else {
1360 styler.ColourTo(i - 1, state);
1361 redo_char(i, ch, chNext, chNext2, state);
1362 preferRE = false;
1363 }
1364 }
1365 if (HereDoc.DelimiterLength >= static_cast<int>(sizeof(HereDoc.Delimiter)) - 1) {
1366 styler.ColourTo(i - 1, state);
1367 state = SCE_RB_ERROR;
1368 preferRE = false;
1369 }
1370 }
1371 } else if (state == SCE_RB_HERE_Q || state == SCE_RB_HERE_QQ || state == SCE_RB_HERE_QX) {
1372 if (ch == '\\' && !isEOLChar(chNext)) {
1373 advance_char(i, ch, chNext, chNext2);
1374 } else if (ch == '#' && state != SCE_RB_HERE_Q
1375 && (chNext == '{' || chNext == '@' || chNext == '$')) {
1376 if (chNext == '{') {
1377 if (innerExpr.canEnter()) {
1378 // process #{ ... }
1379 styler.ColourTo(i - 1, state);
1380 styler.ColourTo(i + 1, SCE_RB_OPERATOR);
1381 innerExpr.enter(state, Quote);
1382 preferRE = true;
1383 // Skip one
1384 advance_char(i, ch, chNext, chNext2);
1385 }
1386 } else {
1387 InterpolateVariable(styler, state, i, ch, chNext, chNext2);
1388 }
1389 }
1390
1391 // Not needed: HereDoc.State == 2
1392 // Indentable here docs: look backwards
1393 // Non-indentable: look forwards, like in Perl
1394 //
1395 // Why: so we can quickly resolve things like <<-" abc"
1396
1397 else if (!HereDoc.CanBeIndented) {
1398 if (isEOLChar(chPrev)
1399 && isMatch(styler, lengthDoc, i, HereDoc.Delimiter)) {
1400 styler.ColourTo(i - 1, state);
1401 i += static_cast<Sci_Position>(HereDoc.DelimiterLength) - 1;
1402 chNext = styler.SafeGetCharAt(i + 1);
1403 if (isEOLChar(chNext)) {
1404 styler.ColourTo(i, SCE_RB_HERE_DELIM);
1405 state = SCE_RB_DEFAULT;
1406 HereDoc.State = 0;
1407 preferRE = false;
1408 }
1409 // Otherwise we skipped through the here doc faster.
1410 }
1411 } else if (isEOLChar(chNext)
1412 && lookingAtHereDocDelim(styler,
1413 i - HereDoc.DelimiterLength + 1,
1414 lengthDoc,
1415 HereDoc.Delimiter)) {
1416 styler.ColourTo(i - HereDoc.DelimiterLength, state);
1417 styler.ColourTo(i, SCE_RB_HERE_DELIM);
1418 state = SCE_RB_DEFAULT;
1419 preferRE = false;
1420 HereDoc.State = 0;
1421 }
1422 } else if (state == SCE_RB_CLASS_VAR
1423 || state == SCE_RB_INSTANCE_VAR
1424 || state == SCE_RB_SYMBOL) {
1425 if (state == SCE_RB_SYMBOL &&
1426 // FIDs suffices '?' and '!'
1427 (((ch == '!' || ch == '?') && chNext != '=') ||
1428 // identifier suffix '='
1429 (ch == '=' && (chNext != '~' && chNext != '>' &&
1430 (chNext != '=' || chNext2 == '>'))))) {
1431 styler.ColourTo(i, state);
1432 state = SCE_RB_DEFAULT;
1433 preferRE = false;
1434 } else if (!isSafeWordcharOrHigh(ch)) {
1435 styler.ColourTo(i - 1, state);
1436 redo_char(i, ch, chNext, chNext2, state); // pass by ref
1437 preferRE = false;
1438 }
1439 } else if (state == SCE_RB_GLOBAL) {
1440 if (!isSafeWordcharOrHigh(ch)) {
1441 // handle special globals here as well
1442 if (chPrev == '$') {
1443 if (ch == '-') {
1444 // Include the next char, like $-a
1445 advance_char(i, ch, chNext, chNext2);
1446 }
1447 styler.ColourTo(i, state);
1448 state = SCE_RB_DEFAULT;
1449 } else {
1450 styler.ColourTo(i - 1, state);
1451 redo_char(i, ch, chNext, chNext2, state); // pass by ref
1452 }
1453 preferRE = false;
1454 }
1455 } else if (state == SCE_RB_POD) {
1456 // PODs end with ^=end\s, -- any whitespace can follow =end
1457 if (isWhiteSpace(ch)
1458 && i > 5
1459 && isEOLChar(styler[i - 5])
1460 && isMatch(styler, lengthDoc, i - 4, "=end")) {
1461 styler.ColourTo(i - 1, state);
1462 state = SCE_RB_DEFAULT;
1463 preferRE = false;
1464 }
1465 } else if (state == SCE_RB_REGEX || state == SCE_RB_STRING_QR) {
1466 if (ch == '\\' && Quote.Up != '\\') {
1467 // Skip one
1468 advance_char(i, ch, chNext, chNext2);
1469 } else if (ch == Quote.Down) {
1470 Quote.Count--;
1471 if (Quote.Count == 0) {
1472 // Include the options
1473 while (isSafeAlpha(chNext)) {
1474 i++;
1475 ch = chNext;
1476 chNext = styler.SafeGetCharAt(i + 1);
1477 }
1478 styler.ColourTo(i, state);
1479 state = SCE_RB_DEFAULT;
1480 preferRE = false;
1481 }
1482 } else if (ch == Quote.Up) {
1483 // Only if close quoter != open quoter
1484 Quote.Count++;
1485
1486 } else if (ch == '#') {
1487 if (chNext == '{') {
1488 if (innerExpr.canEnter()) {
1489 // process #{ ... }
1490 styler.ColourTo(i - 1, state);
1491 styler.ColourTo(i + 1, SCE_RB_OPERATOR);
1492 innerExpr.enter(state, Quote);
1493 preferRE = true;
1494 // Skip one
1495 advance_char(i, ch, chNext, chNext2);
1496 }
1497 } else if (chNext == '@' || chNext == '$') {
1498 InterpolateVariable(styler, state, i, ch, chNext, chNext2);
1499 } else {
1500 //todo: distinguish comments from pound chars
1501 // for now, handle as comment
1502 styler.ColourTo(i - 1, state);
1503 bool inEscape = false;
1504 while (++i < lengthDoc) {
1505 ch = styler.SafeGetCharAt(i);
1506 if (ch == '\\') {
1507 inEscape = true;
1508 } else if (isEOLChar(ch)) {
1509 // Comment inside a regex
1510 styler.ColourTo(i - 1, SCE_RB_COMMENTLINE);
1511 break;
1512 } else if (inEscape) {
1513 inEscape = false; // don't look at char
1514 } else if (ch == Quote.Down) {
1515 // Have the regular handler deal with this
1516 // to get trailing modifiers.
1517 i--;
1518 ch = styler[i];
1519 break;
1520 }
1521 }
1522 chNext = styler.SafeGetCharAt(i + 1);
1523 }
1524 }
1525 // Quotes of all kinds...
1526 } else if (isPercentLiteral(state) ||
1527 state == SCE_RB_STRING || state == SCE_RB_CHARACTER ||
1528 state == SCE_RB_BACKTICKS) {
1529 if (!Quote.Down && !isspacechar(ch)) {
1530 Quote.Open(ch);
1531 } else if (ch == '\\' && Quote.Up != '\\') {
1532 //Riddle me this: Is it safe to skip *every* escaped char?
1533 advance_char(i, ch, chNext, chNext2);
1534 } else if (ch == Quote.Down) {
1535 Quote.Count--;
1536 if (Quote.Count == 0) {
1537 styler.ColourTo(i, state);
1538 state = SCE_RB_DEFAULT;
1539 preferRE = false;
1540 }
1541 } else if (ch == Quote.Up) {
1542 Quote.Count++;
1543 } else if (ch == '#' && isInterpolableLiteral(state)) {
1544 if (chNext == '{') {
1545 if (innerExpr.canEnter()) {
1546 // process #{ ... }
1547 styler.ColourTo(i - 1, state);
1548 styler.ColourTo(i + 1, SCE_RB_OPERATOR);
1549 innerExpr.enter(state, Quote);
1550 preferRE = true;
1551 // Skip one
1552 advance_char(i, ch, chNext, chNext2);
1553 }
1554 } else if (chNext == '@' || chNext == '$') {
1555 InterpolateVariable(styler, state, i, ch, chNext, chNext2);
1556 }
1557 }
1558 }
1559
1560 if (state == SCE_RB_ERROR) {
1561 break;
1562 }
1563 chPrev = ch;
1564 }
1565 if (state == SCE_RB_WORD) {
1566 // We've ended on a word, possibly at EOF, and need to
1567 // classify it.
1568 ClassifyWordRb(styler.GetStartSegment(), lengthDoc - 1, '\0', keywords, styler, prevWord);
1569 } else {
1570 styler.ColourTo(lengthDoc - 1, state);
1571 }
1572 }
1573
1574 // Helper functions for folding, disambiguation keywords
1575 // Assert that there are no high-bit chars
1576
1577 void getPrevWord(Sci_Position pos, char *prevWord, Accessor &styler, int word_state) {
1578 Sci_Position i;
1579 styler.Flush();
1580 for (i = pos - 1; i > 0; i--) {
1581 if (actual_style(styler.StyleAt(i)) != word_state) {
1582 i++;
1583 break;
1584 }
1585 }
1586 if (i < pos - MAX_KEYWORD_LENGTH) // overflow
1587 i = pos - MAX_KEYWORD_LENGTH;
1588 char *dst = prevWord;
1589 for (; i <= pos; i++) {
1590 *dst++ = styler[i];
1591 }
1592 *dst = 0;
1593 }
1594
1595 bool keywordIsAmbiguous(const char *prevWord) noexcept {
1596 // Order from most likely used to least likely
1597 // Lots of ways to do a loop in Ruby besides 'while/until'
1598 if (!strcmp(prevWord, "if")
1599 || !strcmp(prevWord, "do")
1600 || !strcmp(prevWord, "while")
1601 || !strcmp(prevWord, "unless")
1602 || !strcmp(prevWord, "until")
1603 || !strcmp(prevWord, "for")) {
1604 return true;
1605 } else {
1606 return false;
1607 }
1608 }
1609
1610 // Demote keywords in the following conditions:
1611 // if, while, unless, until modify a statement
1612 // do after a while or until, as a noise word (like then after if)
1613
1614 bool keywordIsModifier(const char *word, Sci_Position pos, Accessor &styler) {
1615 if (word[0] == 'd' && word[1] == 'o' && !word[2]) {
1616 return keywordDoStartsLoop(pos, styler);
1617 }
1618 char ch;
1619 int style = SCE_RB_DEFAULT;
1620 Sci_Position lineStart = styler.GetLine(pos);
1621 Sci_Position lineStartPosn = styler.LineStart(lineStart);
1622 // We want to step backwards until we don't care about the current
1623 // position. But first move lineStartPosn back behind any
1624 // continuations immediately above word.
1625 while (lineStartPosn > 0) {
1626 ch = styler[lineStartPosn-1];
1627 if (ch == '\n' || ch == '\r') {
1628 const char chPrev = styler.SafeGetCharAt(lineStartPosn-2);
1629 const char chPrev2 = styler.SafeGetCharAt(lineStartPosn-3);
1630 lineStart = styler.GetLine(lineStartPosn-1);
1631 // If we find a continuation line, include it in our analysis.
1632 if (chPrev == '\\') {
1633 lineStartPosn = styler.LineStart(lineStart);
1634 } else if (ch == '\n' && chPrev == '\r' && chPrev2 == '\\') {
1635 lineStartPosn = styler.LineStart(lineStart);
1636 } else {
1637 break;
1638 }
1639 } else {
1640 break;
1641 }
1642 }
1643
1644 styler.Flush();
1645 while (--pos >= lineStartPosn) {
1646 style = actual_style(styler.StyleAt(pos));
1647 if (style == SCE_RB_DEFAULT) {
1648 ch = styler[pos];
1649 if (IsASpaceOrTab(ch)) {
1650 //continue
1651 } else if (ch == '\r' || ch == '\n') {
1652 // Scintilla's LineStart() and GetLine() routines aren't
1653 // platform-independent, so if we have text prepared with
1654 // a different system we can't rely on it.
1655
1656 // Also, lineStartPosn may have been moved to more than one
1657 // line above word's line while pushing past continuations.
1658 const char chPrev = styler.SafeGetCharAt(pos - 1);
1659 const char chPrev2 = styler.SafeGetCharAt(pos - 2);
1660 if (chPrev == '\\') {
1661 pos-=1; // gloss over the "\\"
1662 //continue
1663 } else if (ch == '\n' && chPrev == '\r' && chPrev2 == '\\') {
1664 pos-=2; // gloss over the "\\\r"
1665 //continue
1666 } else {
1667 return false;
1668 }
1669 }
1670 } else {
1671 break;
1672 }
1673 }
1674 if (pos < lineStartPosn) {
1675 return false;
1676 }
1677 // First things where the action is unambiguous
1678 switch (style) {
1679 case SCE_RB_DEFAULT:
1680 case SCE_RB_COMMENTLINE:
1681 case SCE_RB_POD:
1682 case SCE_RB_CLASSNAME:
1683 case SCE_RB_DEFNAME:
1684 case SCE_RB_MODULE_NAME:
1685 return false;
1686 case SCE_RB_OPERATOR:
1687 break;
1688 case SCE_RB_WORD:
1689 // Watch out for uses of 'else if'
1690 //XXX: Make a list of other keywords where 'if' isn't a modifier
1691 // and can appear legitimately
1692 // Formulate this to avoid warnings from most compilers
1693 if (strcmp(word, "if") == 0) {
1694 char prevWord[MAX_KEYWORD_LENGTH + 1];
1695 getPrevWord(pos, prevWord, styler, SCE_RB_WORD);
1696 return strcmp(prevWord, "else") != 0;
1697 }
1698 return true;
1699 default:
1700 return true;
1701 }
1702 // Assume that if the keyword follows an operator,
1703 // usually it's a block assignment, like
1704 // a << if x then y else z
1705
1706 ch = styler[pos];
1707 switch (ch) {
1708 case ')':
1709 case ']':
1710 case '}':
1711 return true;
1712 default:
1713 return false;
1714 }
1715 }
1716
1717 #define WHILE_BACKWARDS "elihw"
1718 #define UNTIL_BACKWARDS "litnu"
1719 #define FOR_BACKWARDS "rof"
1720
1721 // Nothing fancy -- look to see if we follow a while/until somewhere
1722 // on the current line
1723
1724 bool keywordDoStartsLoop(Sci_Position pos, Accessor &styler) {
1725 const Sci_Position lineStart = styler.GetLine(pos);
1726 const Sci_Position lineStartPosn = styler.LineStart(lineStart);
1727 styler.Flush();
1728 while (--pos >= lineStartPosn) {
1729 const int style = actual_style(styler.StyleAt(pos));
1730 if (style == SCE_RB_DEFAULT) {
1731 const char ch = styler[pos];
1732 if (ch == '\r' || ch == '\n') {
1733 // Scintilla's LineStart() and GetLine() routines aren't
1734 // platform-independent, so if we have text prepared with
1735 // a different system we can't rely on it.
1736 return false;
1737 }
1738 } else if (style == SCE_RB_WORD) {
1739 // Check for while or until, but write the word in backwards
1740 char prevWord[MAX_KEYWORD_LENGTH + 1]; // 1 byte for zero
1741 char *dst = prevWord;
1742 int wordLen = 0;
1743 Sci_Position start_word;
1744 for (start_word = pos;
1745 start_word >= lineStartPosn && actual_style(styler.StyleAt(start_word)) == SCE_RB_WORD;
1746 start_word--) {
1747 if (++wordLen < MAX_KEYWORD_LENGTH) {
1748 *dst++ = styler[start_word];
1749 }
1750 }
1751 *dst = 0;
1752 // Did we see our keyword?
1753 if (!strcmp(prevWord, WHILE_BACKWARDS)
1754 || !strcmp(prevWord, UNTIL_BACKWARDS)
1755 || !strcmp(prevWord, FOR_BACKWARDS)) {
1756 return true;
1757 }
1758 // We can move pos to the beginning of the keyword, and then
1759 // accept another decrement, as we can never have two contiguous
1760 // keywords:
1761 // word1 word2
1762 // ^
1763 // <- move to start_word
1764 // ^
1765 // <- loop decrement
1766 // ^ # pointing to end of word1 is fine
1767 pos = start_word;
1768 }
1769 }
1770 return false;
1771 }
1772
1773 bool IsCommentLine(Sci_Position line, Accessor &styler) {
1774 const Sci_Position pos = styler.LineStart(line);
1775 const Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
1776 for (Sci_Position i = pos; i < eol_pos; i++) {
1777 const char ch = styler[i];
1778 if (ch == '#')
1779 return true;
1780 else if (ch != ' ' && ch != '\t')
1781 return false;
1782 }
1783 return false;
1784 }
1785
1786 /*
1787 * Folding Ruby
1788 *
1789 * The language is quite complex to analyze without a full parse.
1790 * For example, this line shouldn't affect fold level:
1791 *
1792 * print "hello" if feeling_friendly?
1793 *
1794 * Neither should this:
1795 *
1796 * print "hello" \
1797 * if feeling_friendly?
1798 *
1799 *
1800 * But this should:
1801 *
1802 * if feeling_friendly? #++
1803 * print "hello" \
1804 * print "goodbye"
1805 * end #--
1806 *
1807 * So we cheat, by actually looking at the existing indentation
1808 * levels for each line, and just echoing it back. Like Python.
1809 * Then if we get better at it, we'll take braces into consideration,
1810 * which always affect folding levels.
1811
1812 * How the keywords should work:
1813 * No effect:
1814 * __FILE__ __LINE__ BEGIN END alias and
1815 * defined? false in nil not or self super then
1816 * true undef
1817
1818 * Always increment:
1819 * begin class def do for module when {
1820 *
1821 * Always decrement:
1822 * end }
1823 *
1824 * Increment if these start a statement
1825 * if unless until while -- do nothing if they're modifiers
1826
1827 * These end a block if there's no modifier, but don't bother
1828 * break next redo retry return yield
1829 *
1830 * These temporarily de-indent, but re-indent
1831 * case else elsif ensure rescue
1832 *
1833 * This means that the folder reflects indentation rather
1834 * than setting it. The language-service updates indentation
1835 * when users type return and finishes entering de-denters.
1836 *
1837 * Later offer to fold POD, here-docs, strings, and blocks of comments
1838 */
1839
1840 void FoldRbDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *[], Accessor &styler) {
1841 const bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
1842 const bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
1843
1844 synchronizeDocStart(startPos, length, initStyle, styler, false);
1845 const Sci_PositionU endPos = startPos + length;
1846 int visibleChars = 0;
1847 Sci_Position lineCurrent = styler.GetLine(startPos);
1848 int levelPrev = startPos == 0 ? 0 : (styler.LevelAt(lineCurrent)
1849 & SC_FOLDLEVELNUMBERMASK
1850 & ~SC_FOLDLEVELBASE);
1851 int levelCurrent = levelPrev;
1852 char chPrev = '\0';
1853 char chNext = styler[startPos];
1854 int styleNext = styler.StyleAt(startPos);
1855 int stylePrev = startPos <= 1 ? SCE_RB_DEFAULT : styler.StyleAt(startPos - 1);
1856 // detect endless method definition to fix up code folding
1857 enum class MethodDefinition {
1858 None,
1859 Define,
1860 Operator,
1861 Name,
1862 Argument,
1863 };
1864 MethodDefinition method_definition = MethodDefinition::None;
1865 int argument_paren_count = 0;
1866 bool heredocOpen = false;
1867
1868 for (Sci_PositionU i = startPos; i < endPos; i++) {
1869 const char ch = chNext;
1870 chNext = styler.SafeGetCharAt(i + 1);
1871 const int style = styleNext;
1872 styleNext = styler.StyleAt(i + 1);
1873 const bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
1874
1875 /*Mutiline comment patch*/
1876 if (foldComment && atEOL && IsCommentLine(lineCurrent, styler)) {
1877 if (!IsCommentLine(lineCurrent - 1, styler)
1878 && IsCommentLine(lineCurrent + 1, styler))
1879 levelCurrent++;
1880 else if (IsCommentLine(lineCurrent - 1, styler)
1881 && !IsCommentLine(lineCurrent + 1, styler))
1882 levelCurrent--;
1883 }
1884
1885 if (style == SCE_RB_COMMENTLINE) {
1886 if (foldComment && stylePrev != SCE_RB_COMMENTLINE) {
1887 if (chNext == '{') {
1888 levelCurrent++;
1889 } else if (chNext == '}' && levelCurrent > 0) {
1890 levelCurrent--;
1891 }
1892 }
1893 } else if (style == SCE_RB_OPERATOR) {
1894 if (AnyOf(ch, '[', '{', '(')) {
1895 levelCurrent++;
1896 } else if (AnyOf(ch, ']', '}', ')')) {
1897 // Don't decrement below 0
1898 if (levelCurrent > 0)
1899 levelCurrent--;
1900 }
1901 } else if (style == SCE_RB_WORD && styleNext != SCE_RB_WORD) {
1902 // Look at the keyword on the left and decide what to do
1903 char prevWord[MAX_KEYWORD_LENGTH + 1]; // 1 byte for zero
1904 prevWord[0] = 0;
1905 getPrevWord(i, prevWord, styler, SCE_RB_WORD);
1906 if (!strcmp(prevWord, "end")) {
1907 // Don't decrement below 0
1908 if (levelCurrent > 0)
1909 levelCurrent--;
1910 } else if (!strcmp(prevWord, "def")) {
1911 levelCurrent++;
1912 method_definition = MethodDefinition::Define;
1913 } else if (!strcmp(prevWord, "if")
1914 || !strcmp(prevWord, "class")
1915 || !strcmp(prevWord, "module")
1916 || !strcmp(prevWord, "begin")
1917 || !strcmp(prevWord, "case")
1918 || !strcmp(prevWord, "do")
1919 || !strcmp(prevWord, "while")
1920 || !strcmp(prevWord, "unless")
1921 || !strcmp(prevWord, "until")
1922 || !strcmp(prevWord, "for")
1923 ) {
1924 levelCurrent++;
1925 }
1926 } else if (style == SCE_RB_HERE_DELIM && !heredocOpen) {
1927 if (stylePrev == SCE_RB_OPERATOR && chPrev == '<' && styler.SafeGetCharAt(i - 2) == '<') {
1928 levelCurrent++;
1929 heredocOpen = true;
1930 } else if (styleNext != SCE_RB_HERE_DELIM) {
1931 levelCurrent--;
1932 }
1933 } else if (style == SCE_RB_STRING_QW || style == SCE_RB_STRING_W) {
1934 if (stylePrev != style) {
1935 levelCurrent++;
1936 }
1937 if (styleNext != style) {
1938 levelCurrent--;
1939 }
1940 }
1941 if (method_definition != MethodDefinition::None) {
1942 switch (method_definition) {
1943 case MethodDefinition::Define:
1944 if (style == SCE_RB_OPERATOR) {
1945 method_definition = MethodDefinition::Operator;
1946 } else if (style == SCE_RB_DEFNAME || style == SCE_RB_WORD_DEMOTED || style == SCE_RB_CLASSNAME || style == SCE_RB_IDENTIFIER) {
1947 method_definition = MethodDefinition::Name;
1948 } else if (!(style == SCE_RB_WORD || IsASpaceOrTab(ch))) {
1949 method_definition = MethodDefinition::None;
1950 }
1951 if (method_definition <= MethodDefinition::Define) {
1952 break;
1953 }
1954 // fall through for unary operator or single letter name
1955 [[fallthrough]];
1956 case MethodDefinition::Operator:
1957 case MethodDefinition::Name:
1958 if (isEOLChar(chNext) || chNext == '#') {
1959 method_definition = MethodDefinition::None;
1960 } else if (chNext == '(' || chNext <= ' ') {
1961 // setter method cannot be defined in an endless method definition.
1962 if (ch == '=' && (method_definition == MethodDefinition::Name || chPrev == ']')) {
1963 method_definition = MethodDefinition::None;
1964 } else {
1965 method_definition = MethodDefinition::Argument;
1966 argument_paren_count = 0;
1967 }
1968 }
1969 break;
1970 case MethodDefinition::Argument:
1971 if (style == SCE_RB_OPERATOR) {
1972 if (ch == '(') {
1973 ++argument_paren_count;
1974 } else if (ch == ')') {
1975 --argument_paren_count;
1976 } else if (argument_paren_count == 0) {
1977 method_definition = MethodDefinition::None;
1978 if (ch == '=' && levelCurrent > 0) {
1979 levelCurrent--;
1980 }
1981 }
1982 } else if (argument_paren_count == 0 && !IsASpaceOrTab(ch)) {
1983 // '=' must be first character after method name or right parenthesis
1984 method_definition = MethodDefinition::None;
1985 }
1986 break;
1987 default:
1988 break;
1989 }
1990 }
1991 if (atEOL || (i == endPos - 1)) {
1992 int lev = levelPrev;
1993 if (visibleChars == 0 && foldCompact)
1994 lev |= SC_FOLDLEVELWHITEFLAG;
1995 if ((levelCurrent > levelPrev) && (visibleChars > 0))
1996 lev |= SC_FOLDLEVELHEADERFLAG;
1997 styler.SetLevel(lineCurrent, lev|SC_FOLDLEVELBASE);
1998 lineCurrent++;
1999 levelPrev = levelCurrent;
2000 visibleChars = 0;
2001 method_definition = MethodDefinition::None;
2002 argument_paren_count = 0;
2003 heredocOpen = false;
2004 } else if (!isspacechar(ch)) {
2005 visibleChars++;
2006 }
2007 chPrev = ch;
2008 stylePrev = style;
2009 }
2010 }
2011
2012 const char *const rubyWordListDesc[] = {
2013 "Keywords",
2014 nullptr
2015 };
2016
2017 }
2018
2019 extern const LexerModule lmRuby(SCLEX_RUBY, ColouriseRbDoc, "ruby", FoldRbDoc, rubyWordListDesc);