Debian Codegraph @

scintilla/lexilla/lexers/LexRust.cxx

raw
1 /** @file LexRust.cxx
2 ** Lexer for Rust.
3 **
4 ** Copyright (c) 2013 by SiegeLord <[email protected]>
5 ** Converted to lexer object and added further folding features/properties by "Udo Lechner" <dlchnr(at)gmx(dot)net>
6 **/
7 // Copyright 1998-2005 by Neil Hodgson <[email protected]>
8 // The License.txt file describes the conditions under which this software may be distributed.
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 #include <assert.h>
15 #include <ctype.h>
16
17 #include <string>
18 #include <string_view>
19 #include <map>
20 #include <functional>
21
22 #include "ILexer.h"
23 #include "Scintilla.h"
24 #include "SciLexer.h"
25
26 #include "PropSetSimple.h"
27 #include "WordList.h"
28 #include "LexAccessor.h"
29 #include "Accessor.h"
30 #include "StyleContext.h"
31 #include "CharacterSet.h"
32 #include "LexerModule.h"
33 #include "OptionSet.h"
34 #include "DefaultLexer.h"
35
36 using namespace Scintilla;
37 using namespace Lexilla;
38
39 namespace {
40
41 constexpr int NUM_RUST_KEYWORD_LISTS = 7;
42 constexpr int MAX_RUST_IDENT_CHARS = 1023;
43
44
45 enum class StringType : int {
46 STRING = SCE_RUST_STRING,
47 BYTESTRING = SCE_RUST_BYTESTRING,
48 CSTRING = SCE_RUST_CSTRING,
49
50 RAW_STRING = SCE_RUST_STRINGR,
51 RAW_BYTESTRING = SCE_RUST_BYTESTRINGR,
52 RAW_CSTRING = SCE_RUST_CSTRINGR
53 };
54
55 static bool IsStreamCommentStyle(int style) noexcept {
56 return style == SCE_RUST_COMMENTBLOCK ||
57 style == SCE_RUST_COMMENTBLOCKDOC;
58 }
59
60 // Options used for LexerRust
61 struct OptionsRust {
62 bool fold;
63 bool foldSyntaxBased;
64 bool foldComment;
65 bool foldCommentMultiline;
66 bool foldCommentExplicit;
67 std::string foldExplicitStart;
68 std::string foldExplicitEnd;
69 bool foldExplicitAnywhere;
70 bool foldCompact;
71 int foldAtElseInt; // This variable is not used
72 bool foldAtElse;
73 OptionsRust() {
74 fold = false;
75 foldSyntaxBased = true;
76 foldComment = false;
77 foldCommentMultiline = true;
78 foldCommentExplicit = true;
79 foldExplicitStart = "";
80 foldExplicitEnd = "";
81 foldExplicitAnywhere = false;
82 foldCompact = true;
83 foldAtElseInt = -1;
84 foldAtElse = false;
85 }
86 };
87
88 static const char * const rustWordLists[NUM_RUST_KEYWORD_LISTS + 1] = {
89 "Primary keywords and identifiers",
90 "Built in types",
91 "Other keywords",
92 "Keywords 4",
93 "Keywords 5",
94 "Keywords 6",
95 "Keywords 7",
96 0,
97 };
98
99 struct OptionSetRust : public OptionSet<OptionsRust> {
100 OptionSetRust() {
101 DefineProperty("fold", &OptionsRust::fold);
102
103 DefineProperty("fold.comment", &OptionsRust::foldComment);
104
105 DefineProperty("fold.compact", &OptionsRust::foldCompact);
106
107 DefineProperty("fold.at.else", &OptionsRust::foldAtElse);
108
109 DefineProperty("fold.rust.syntax.based", &OptionsRust::foldSyntaxBased,
110 "Set this property to 0 to disable syntax based folding.");
111
112 DefineProperty("fold.rust.comment.multiline", &OptionsRust::foldCommentMultiline,
113 "Set this property to 0 to disable folding multi-line comments when fold.comment=1.");
114
115 DefineProperty("fold.rust.comment.explicit", &OptionsRust::foldCommentExplicit,
116 "Set this property to 0 to disable folding explicit fold points when fold.comment=1.");
117
118 DefineProperty("fold.rust.explicit.start", &OptionsRust::foldExplicitStart,
119 "The string to use for explicit fold start points, replacing the standard //{.");
120
121 DefineProperty("fold.rust.explicit.end", &OptionsRust::foldExplicitEnd,
122 "The string to use for explicit fold end points, replacing the standard //}.");
123
124 DefineProperty("fold.rust.explicit.anywhere", &OptionsRust::foldExplicitAnywhere,
125 "Set this property to 1 to enable explicit fold points anywhere, not just in line comments.");
126
127 DefineProperty("lexer.rust.fold.at.else", &OptionsRust::foldAtElseInt,
128 "This option enables Rust folding on a \"} else {\" line of an if statement.");
129
130 DefineWordListSets(rustWordLists);
131 }
132 };
133
134 class LexerRust : public DefaultLexer {
135 WordList keywords[NUM_RUST_KEYWORD_LISTS];
136 OptionsRust options;
137 OptionSetRust osRust;
138 public:
139 LexerRust() : DefaultLexer("rust", SCLEX_RUST) {
140 }
141 virtual ~LexerRust() {
142 }
143 void SCI_METHOD Release() override {
144 delete this;
145 }
146 int SCI_METHOD Version() const override {
147 return lvRelease5;
148 }
149 const char * SCI_METHOD PropertyNames() override {
150 return osRust.PropertyNames();
151 }
152 int SCI_METHOD PropertyType(const char *name) override {
153 return osRust.PropertyType(name);
154 }
155 const char * SCI_METHOD DescribeProperty(const char *name) override {
156 return osRust.DescribeProperty(name);
157 }
158 Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
159 const char * SCI_METHOD PropertyGet(const char *key) override {
160 return osRust.PropertyGet(key);
161 }
162 const char * SCI_METHOD DescribeWordListSets() override {
163 return osRust.DescribeWordListSets();
164 }
165 Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
166 void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
167 void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
168 void * SCI_METHOD PrivateCall(int, void *) override {
169 return 0;
170 }
171 static ILexer5 *LexerFactoryRust() {
172 return new LexerRust();
173 }
174 };
175
176 Sci_Position SCI_METHOD LexerRust::PropertySet(const char *key, const char *val) {
177 if (osRust.PropertySet(&options, key, val)) {
178 return 0;
179 }
180 return -1;
181 }
182
183 Sci_Position SCI_METHOD LexerRust::WordListSet(int n, const char *wl) {
184 Sci_Position firstModification = -1;
185 if (n < NUM_RUST_KEYWORD_LISTS) {
186 WordList *wordListN = &keywords[n];
187 if (wordListN->Set(wl)) {
188 firstModification = 0;
189 }
190 }
191 return firstModification;
192 }
193
194 static bool IsWhitespace(int c) {
195 return c == ' ' || c == '\t' || c == '\r' || c == '\n';
196 }
197
198 /* This isn't quite right for Unicode identifiers */
199 static bool IsIdentifierStart(int ch) {
200 return (IsASCII(ch) && (isalpha(ch) || ch == '_')) || !IsASCII(ch);
201 }
202
203 /* This isn't quite right for Unicode identifiers */
204 static bool IsIdentifierContinue(int ch) {
205 return (IsASCII(ch) && (isalnum(ch) || ch == '_')) || !IsASCII(ch);
206 }
207
208 static void ScanWhitespace(Accessor& styler, Sci_Position& pos, Sci_Position max) {
209 while (IsWhitespace(styler.SafeGetCharAt(pos, '\0')) && pos < max) {
210 if (pos == styler.LineEnd(styler.GetLine(pos)))
211 styler.SetLineState(styler.GetLine(pos), 0);
212 pos++;
213 }
214 styler.ColourTo(pos-1, SCE_RUST_DEFAULT);
215 }
216
217 static void GrabString(char* s, Accessor& styler, Sci_Position start, Sci_Position len) {
218 for (Sci_Position ii = 0; ii < len; ii++)
219 s[ii] = styler[ii + start];
220 s[len] = '\0';
221 }
222
223 static void ScanRawIdentifier(Accessor& styler, Sci_Position& pos) {
224 const Sci_Position start = pos;
225 while (IsIdentifierContinue(styler.SafeGetCharAt(pos, '\0')))
226 pos++;
227
228 char s[MAX_RUST_IDENT_CHARS + 1];
229 Sci_Position len = pos - start;
230 len = len > MAX_RUST_IDENT_CHARS ? MAX_RUST_IDENT_CHARS : len;
231 GrabString(s, styler, start, len);
232 // restricted values https://doc.rust-lang.org/reference/identifiers.html#raw-identifiers
233 if (strcmp(s, "crate") != 0 && strcmp(s, "self") != 0 &&
234 strcmp(s, "super") != 0 && strcmp(s, "Self") != 0) {
235 styler.ColourTo(pos - 1, SCE_RUST_IDENTIFIER);
236 } else {
237 styler.ColourTo(pos - 1, SCE_RUST_LEXERROR);
238 }
239 }
240
241 static void ScanIdentifier(Accessor& styler, Sci_Position& pos, WordList *keywords) {
242 const Sci_Position start = pos;
243 while (IsIdentifierContinue(styler.SafeGetCharAt(pos, '\0')))
244 pos++;
245
246 if (styler.SafeGetCharAt(pos, '\0') == '!') {
247 pos++;
248 styler.ColourTo(pos - 1, SCE_RUST_MACRO);
249 } else {
250 char s[MAX_RUST_IDENT_CHARS + 1];
251 Sci_Position len = pos - start;
252 len = len > MAX_RUST_IDENT_CHARS ? MAX_RUST_IDENT_CHARS : len;
253 GrabString(s, styler, start, len);
254 bool keyword = false;
255 for (int ii = 0; ii < NUM_RUST_KEYWORD_LISTS; ii++) {
256 if (keywords[ii].InList(s)) {
257 styler.ColourTo(pos - 1, SCE_RUST_WORD + ii);
258 keyword = true;
259 break;
260 }
261 }
262 if (!keyword) {
263 styler.ColourTo(pos - 1, SCE_RUST_IDENTIFIER);
264 }
265 }
266 }
267
268 /* Scans a sequence of digits, returning true if it found any. */
269 static bool ScanDigits(Accessor& styler, Sci_Position& pos, int base) {
270 const Sci_Position old_pos = pos;
271 for (;;) {
272 const int c = styler.SafeGetCharAt(pos, '\0');
273 if (IsADigit(c, base) || c == '_')
274 pos++;
275 else
276 break;
277 }
278 return old_pos != pos;
279 }
280
281 /* Scans an integer and floating point literals. */
282 static void ScanNumber(Accessor& styler, Sci_Position& pos) {
283 int base = 10;
284 int c = styler.SafeGetCharAt(pos, '\0');
285 int n = styler.SafeGetCharAt(pos + 1, '\0');
286 bool error = false;
287 /* Scan the prefix, thus determining the base.
288 * 10 is default if there's no prefix. */
289 if (c == '0' && n == 'x') {
290 pos += 2;
291 base = 16;
292 } else if (c == '0' && n == 'b') {
293 pos += 2;
294 base = 2;
295 } else if (c == '0' && n == 'o') {
296 pos += 2;
297 base = 8;
298 }
299
300 /* Scan initial digits. The literal is malformed if there are none. */
301 error |= !ScanDigits(styler, pos, base);
302 /* See if there's an integer suffix. We mimic the Rust's lexer
303 * and munch it even if there was an error above. */
304 c = styler.SafeGetCharAt(pos, '\0');
305 if (c == 'u' || c == 'i') {
306 pos++;
307 c = styler.SafeGetCharAt(pos, '\0');
308 n = styler.SafeGetCharAt(pos + 1, '\0');
309 if (c == '8') {
310 pos++;
311 } else if (c == '1' && n == '6') {
312 pos += 2;
313 } else if (c == '3' && n == '2') {
314 pos += 2;
315 } else if (c == '6' && n == '4') {
316 pos += 2;
317 } else if (styler.Match(pos, "128")) {
318 pos += 3;
319 } else if (styler.Match(pos, "size")) {
320 pos += 4;
321 } else {
322 error = true;
323 }
324 /* See if it's a floating point literal. These literals have to be base 10.
325 */
326 } else if (!error) {
327 /* If there's a period, it's a floating point literal unless it's
328 * followed by an identifier (meaning this is a method call, e.g.
329 * `1.foo()`) or another period, in which case it's a range (e.g. 1..2)
330 */
331 n = styler.SafeGetCharAt(pos + 1, '\0');
332 if (c == '.' && !(IsIdentifierStart(n) || n == '.')) {
333 error |= base != 10;
334 pos++;
335 /* It's ok to have no digits after the period. */
336 ScanDigits(styler, pos, 10);
337 }
338
339 /* Look for the exponentiation. */
340 c = styler.SafeGetCharAt(pos, '\0');
341 if (c == 'e' || c == 'E') {
342 error |= base != 10;
343 pos++;
344 c = styler.SafeGetCharAt(pos, '\0');
345 if (c == '-' || c == '+')
346 pos++;
347 /* It is invalid to have no digits in the exponent. */
348 error |= !ScanDigits(styler, pos, 10);
349 }
350
351 /* Scan the floating point suffix. */
352 c = styler.SafeGetCharAt(pos, '\0');
353 if (c == 'f') {
354 error |= base != 10;
355 pos++;
356 c = styler.SafeGetCharAt(pos, '\0');
357 n = styler.SafeGetCharAt(pos + 1, '\0');
358 if (c == '3' && n == '2') {
359 pos += 2;
360 } else if (c == '6' && n == '4') {
361 pos += 2;
362 } else {
363 error = true;
364 }
365 }
366 }
367
368 if (error)
369 styler.ColourTo(pos - 1, SCE_RUST_LEXERROR);
370 else
371 styler.ColourTo(pos - 1, SCE_RUST_NUMBER);
372 }
373
374 static bool IsOneCharOperator(int c) {
375 return c == ';' || c == ',' || c == '(' || c == ')'
376 || c == '{' || c == '}' || c == '[' || c == ']'
377 || c == '@' || c == '#' || c == '~' || c == '+'
378 || c == '*' || c == '/' || c == '^' || c == '%'
379 || c == '.' || c == ':' || c == '!' || c == '<'
380 || c == '>' || c == '=' || c == '-' || c == '&'
381 || c == '|' || c == '$' || c == '?';
382 }
383
384 static bool IsTwoCharOperator(int c, int n) {
385 return (c == '.' && n == '.') || (c == ':' && n == ':')
386 || (c == '!' && n == '=') || (c == '<' && n == '<')
387 || (c == '<' && n == '=') || (c == '>' && n == '>')
388 || (c == '>' && n == '=') || (c == '=' && n == '=')
389 || (c == '=' && n == '>') || (c == '-' && n == '>')
390 || (c == '&' && n == '&') || (c == '|' && n == '|')
391 || (c == '-' && n == '=') || (c == '&' && n == '=')
392 || (c == '|' && n == '=') || (c == '+' && n == '=')
393 || (c == '*' && n == '=') || (c == '/' && n == '=')
394 || (c == '^' && n == '=') || (c == '%' && n == '=');
395 }
396
397 static bool IsThreeCharOperator(int c, int n, int n2) {
398 return (c == '<' && n == '<' && n2 == '=')
399 || (c == '>' && n == '>' && n2 == '=');
400 }
401
402 static bool IsValidCharacterEscape(int c) {
403 return c == 'n' || c == 'r' || c == 't' || c == '\\'
404 || c == '\'' || c == '"' || c == '0';
405 }
406
407 static bool IsValidStringEscape(int c) {
408 return IsValidCharacterEscape(c) || c == '\n' || c == '\r';
409 }
410
411 static bool ScanNumericEscape(Accessor &styler, Sci_Position& pos, Sci_Position num_digits, bool stop_asap) {
412 for (;;) {
413 const int c = styler.SafeGetCharAt(pos, '\0');
414 if (!IsADigit(c, 16))
415 break;
416 num_digits--;
417 pos++;
418 if (num_digits == 0 && stop_asap)
419 return true;
420 }
421 if (num_digits == 0) {
422 return true;
423 } else {
424 return false;
425 }
426 }
427
428 /* This is overly permissive for character literals in order to accept UTF-8 encoded
429 * character literals. */
430 static void ScanCharacterLiteralOrLifetime(Accessor &styler, Sci_Position& pos, bool ascii_only) {
431 pos++;
432 int c = styler.SafeGetCharAt(pos, '\0');
433 int n = styler.SafeGetCharAt(pos + 1, '\0');
434 bool done = false;
435 bool valid_lifetime = !ascii_only && IsIdentifierStart(c);
436 bool valid_char = true;
437 bool first = true;
438 while (!done) {
439 switch (c) {
440 case '\\':
441 done = true;
442 if (IsValidCharacterEscape(n)) {
443 pos += 2;
444 } else if (n == 'x') {
445 pos += 2;
446 valid_char = ScanNumericEscape(styler, pos, 2, false);
447 } else if (n == 'u' && !ascii_only) {
448 pos += 2;
449 if (styler.SafeGetCharAt(pos, '\0') != '{') {
450 // old-style
451 valid_char = ScanNumericEscape(styler, pos, 4, false);
452 } else {
453 int n_digits = 0;
454 while (IsADigit(styler.SafeGetCharAt(++pos, '\0'), 16) && n_digits++ < 6) {
455 }
456 if (n_digits > 0 && styler.SafeGetCharAt(pos, '\0') == '}')
457 pos++;
458 else
459 valid_char = false;
460 }
461 } else if (n == 'U' && !ascii_only) {
462 pos += 2;
463 valid_char = ScanNumericEscape(styler, pos, 8, false);
464 } else {
465 valid_char = false;
466 }
467 break;
468 case '\'':
469 valid_char = !first;
470 done = true;
471 break;
472 case '\t':
473 case '\n':
474 case '\r':
475 case '\0':
476 valid_char = false;
477 done = true;
478 break;
479 default:
480 if (ascii_only && !IsASCII((char)c)) {
481 done = true;
482 valid_char = false;
483 } else if (!IsIdentifierContinue(c) && !first) {
484 done = true;
485 } else {
486 pos++;
487 }
488 break;
489 }
490 c = styler.SafeGetCharAt(pos, '\0');
491 n = styler.SafeGetCharAt(pos + 1, '\0');
492
493 first = false;
494 }
495 if (styler.SafeGetCharAt(pos, '\0') == '\'') {
496 valid_lifetime = false;
497 } else {
498 valid_char = false;
499 }
500 if (valid_lifetime) {
501 styler.ColourTo(pos - 1, SCE_RUST_LIFETIME);
502 } else if (valid_char) {
503 pos++;
504 styler.ColourTo(pos - 1, ascii_only ? SCE_RUST_BYTECHARACTER : SCE_RUST_CHARACTER);
505 } else {
506 styler.ColourTo(pos - 1, SCE_RUST_LEXERROR);
507 }
508 }
509
510 enum CommentState {
511 UnknownComment,
512 DocComment,
513 NotDocComment
514 };
515
516 /*
517 * The rule for block-doc comments is as follows: /xxN and /x! (where x is an asterisk, N is a non-asterisk) start doc comments.
518 * Otherwise it's a regular comment.
519 */
520 static void ResumeBlockComment(Accessor &styler, Sci_Position& pos, Sci_Position max, CommentState state, int level) {
521 int c = styler.SafeGetCharAt(pos, '\0');
522 bool maybe_doc_comment = false;
523 if (c == '*') {
524 const int n = styler.SafeGetCharAt(pos + 1, '\0');
525 if (n != '*' && n != '/') {
526 maybe_doc_comment = true;
527 }
528 } else if (c == '!') {
529 maybe_doc_comment = true;
530 }
531
532 for (;;) {
533 const int n = styler.SafeGetCharAt(pos + 1, '\0');
534 if (pos == styler.LineEnd(styler.GetLine(pos)))
535 styler.SetLineState(styler.GetLine(pos), level);
536 if (c == '*') {
537 pos++;
538 if (n == '/') {
539 pos++;
540 level--;
541 if (level == 0) {
542 styler.SetLineState(styler.GetLine(pos), 0);
543 if (state == DocComment || (state == UnknownComment && maybe_doc_comment))
544 styler.ColourTo(pos - 1, SCE_RUST_COMMENTBLOCKDOC);
545 else
546 styler.ColourTo(pos - 1, SCE_RUST_COMMENTBLOCK);
547 break;
548 }
549 }
550 } else if (c == '/') {
551 pos++;
552 if (n == '*') {
553 pos++;
554 level++;
555 }
556 }
557 else if (pos < max) {
558 pos++;
559 }
560 if (pos >= max) {
561 if (state == DocComment || (state == UnknownComment && maybe_doc_comment))
562 styler.ColourTo(pos - 1, SCE_RUST_COMMENTBLOCKDOC);
563 else
564 styler.ColourTo(pos - 1, SCE_RUST_COMMENTBLOCK);
565 break;
566 }
567 c = styler.SafeGetCharAt(pos, '\0');
568 }
569 }
570
571 /*
572 * The rule for line-doc comments is as follows... ///N and //! (where N is a non slash) start doc comments.
573 * Otherwise it's a normal line comment.
574 */
575 static void ResumeLineComment(Accessor &styler, Sci_Position& pos, Sci_Position max, CommentState state) {
576 bool maybe_doc_comment = false;
577 int c = styler.SafeGetCharAt(pos, '\0');
578 if (c == '/') {
579 if (pos < max) {
580 pos++;
581 c = styler.SafeGetCharAt(pos, '\0');
582 if (c != '/') {
583 maybe_doc_comment = true;
584 }
585 }
586 } else if (c == '!') {
587 maybe_doc_comment = true;
588 }
589
590 pos = styler.LineEnd(styler.GetLine(pos));
591 styler.SetLineState(styler.GetLine(pos), SCE_RUST_DEFAULT);
592
593 if (state == DocComment || (state == UnknownComment && maybe_doc_comment))
594 styler.ColourTo(pos - 1, SCE_RUST_COMMENTLINEDOC);
595 else
596 styler.ColourTo(pos - 1, SCE_RUST_COMMENTLINE);
597 }
598
599 static void ScanComments(Accessor &styler, Sci_Position& pos, Sci_Position max) {
600 pos++;
601 const int c = styler.SafeGetCharAt(pos, '\0');
602 pos++;
603 if (c == '/')
604 ResumeLineComment(styler, pos, max, UnknownComment);
605 else if (c == '*')
606 ResumeBlockComment(styler, pos, max, UnknownComment, 1);
607 }
608
609 static void ResumeString(Accessor &styler, Sci_Position& pos, Sci_Position max, StringType string_type) {
610 int c = styler.SafeGetCharAt(pos, '\0');
611 bool error = false;
612 while (c != '"' && !error) {
613 if (pos >= max) {
614 error = true;
615 break;
616 }
617 if (pos == styler.LineEnd(styler.GetLine(pos)))
618 styler.SetLineState(styler.GetLine(pos), 0);
619 if (c == '\\') {
620 const int n = styler.SafeGetCharAt(pos + 1, '\0');
621 if (IsValidStringEscape(n)) {
622 pos += 2;
623 } else if (n == 'x') {
624 pos += 2;
625 error = !ScanNumericEscape(styler, pos, 2, true);
626 } else if (n == 'u' && (string_type != StringType::BYTESTRING)) {
627 pos += 2;
628 if (styler.SafeGetCharAt(pos, '\0') != '{') {
629 // old-style
630 error = !ScanNumericEscape(styler, pos, 4, true);
631 } else {
632 int n_digits = 0;
633 while (IsADigit(styler.SafeGetCharAt(++pos, '\0'), 16) && n_digits++ < 6) {
634 }
635 if (n_digits > 0 && styler.SafeGetCharAt(pos, '\0') == '}')
636 pos++;
637 else
638 error = true;
639 }
640 } else if (n == 'U' && (string_type != StringType::BYTESTRING)) {
641 pos += 2;
642 error = !ScanNumericEscape(styler, pos, 8, true);
643 } else {
644 pos += 1;
645 error = true;
646 }
647 } else {
648 if (string_type == StringType::BYTESTRING && !IsASCII((char)c))
649 error = true;
650 else
651 pos++;
652 }
653 c = styler.SafeGetCharAt(pos, '\0');
654 }
655 if (!error)
656 pos++;
657
658 styler.ColourTo(pos - 1, static_cast<int>(string_type));
659 }
660
661 static void ResumeRawString(Accessor &styler, Sci_Position& pos, Sci_Position max, int num_hashes, StringType string_type) {
662 for (;;) {
663 if (pos == styler.LineEnd(styler.GetLine(pos)))
664 styler.SetLineState(styler.GetLine(pos), num_hashes);
665
666 const int c = styler.SafeGetCharAt(pos, '\0');
667 if (c == '"') {
668 pos++;
669 int trailing_num_hashes = 0;
670 while (styler.SafeGetCharAt(pos, '\0') == '#' && trailing_num_hashes < num_hashes) {
671 trailing_num_hashes++;
672 pos++;
673 }
674 if (trailing_num_hashes == num_hashes) {
675 styler.SetLineState(styler.GetLine(pos), 0);
676 break;
677 }
678 } else if (pos >= max) {
679 break;
680 } else {
681 if ((string_type == StringType::RAW_BYTESTRING) && !IsASCII((char)c))
682 break;
683 pos++;
684 }
685 }
686
687 styler.ColourTo(pos - 1, static_cast<int>(string_type));
688 }
689
690 static void ScanRawString(Accessor &styler, Sci_Position& pos, Sci_Position max, StringType string_type) {
691 pos++;
692 int num_hashes = 0;
693 while (styler.SafeGetCharAt(pos, '\0') == '#') {
694 num_hashes++;
695 pos++;
696 }
697 if (styler.SafeGetCharAt(pos, '\0') != '"') {
698 styler.ColourTo(pos - 1, SCE_RUST_LEXERROR);
699 } else {
700 pos++;
701 ResumeRawString(styler, pos, max, num_hashes, string_type);
702 }
703 }
704
705 void SCI_METHOD LexerRust::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
706 PropSetSimple props;
707 Accessor styler(pAccess, &props);
708 Sci_Position pos = startPos;
709 const Sci_Position max = pos + length;
710
711 styler.StartAt(pos);
712 styler.StartSegment(pos);
713
714 if (initStyle == SCE_RUST_COMMENTBLOCK || initStyle == SCE_RUST_COMMENTBLOCKDOC) {
715 ResumeBlockComment(styler, pos, max, initStyle == SCE_RUST_COMMENTBLOCKDOC ? DocComment : NotDocComment, styler.GetLineState(styler.GetLine(pos) - 1));
716 } else if (initStyle == SCE_RUST_COMMENTLINE || initStyle == SCE_RUST_COMMENTLINEDOC) {
717 ResumeLineComment(styler, pos, max, initStyle == SCE_RUST_COMMENTLINEDOC ? DocComment : NotDocComment);
718 } else if (initStyle == SCE_RUST_STRING) {
719 ResumeString(styler, pos, max, StringType::STRING);
720 } else if (initStyle == SCE_RUST_BYTESTRING) {
721 ResumeString(styler, pos, max, StringType::BYTESTRING);
722 } else if (initStyle == SCE_RUST_STRINGR) {
723 ResumeRawString(styler, pos, max, styler.GetLineState(styler.GetLine(pos) - 1), StringType::RAW_STRING);
724 } else if (initStyle == SCE_RUST_BYTESTRINGR) {
725 ResumeRawString(styler, pos, max, styler.GetLineState(styler.GetLine(pos) - 1), StringType::RAW_BYTESTRING);
726 } else if (initStyle == SCE_RUST_CSTRING) {
727 ResumeRawString(styler, pos, max, styler.GetLineState(styler.GetLine(pos) - 1), StringType::CSTRING);
728 } else if (initStyle == SCE_RUST_CSTRINGR) {
729 ResumeRawString(styler, pos, max, styler.GetLineState(styler.GetLine(pos) - 1), StringType::RAW_CSTRING);
730 }
731 while (pos < max) {
732 const int c = styler.SafeGetCharAt(pos, '\0');
733 const int n = styler.SafeGetCharAt(pos + 1, '\0');
734 const int n2 = styler.SafeGetCharAt(pos + 2, '\0');
735
736 if (pos == 0 && c == '#' && n == '!' && n2 != '[') {
737 pos += 2;
738 ResumeLineComment(styler, pos, max, NotDocComment);
739 } else if (IsWhitespace(c)) {
740 ScanWhitespace(styler, pos, max);
741 } else if (c == '/' && (n == '/' || n == '*')) {
742 ScanComments(styler, pos, max);
743 } else if (c == 'r' && (n == '#' && IsIdentifierStart(n2))) {
744 pos += 2;
745 ScanRawIdentifier(styler, pos);
746 } else if (c == 'r' && (n == '#' || n == '"')) {
747 ScanRawString(styler, pos, max, StringType::RAW_STRING);
748 } else if (c == 'b' && n == 'r' && (n2 == '#' || n2 == '"')) {
749 pos++;
750 ScanRawString(styler, pos, max, StringType::RAW_BYTESTRING);
751 } else if (c == 'b' && n == '"') {
752 pos += 2;
753 ResumeString(styler, pos, max, StringType::BYTESTRING);
754 } else if (c == 'c' && n == 'r' && (n2 == '#' || n2 == '"')) {
755 pos++;
756 ScanRawString(styler, pos, max, StringType::RAW_CSTRING);
757 } else if (c == 'c' && n == '"') {
758 pos += 2;
759 ResumeString(styler, pos, max, StringType::CSTRING);
760 } else if (c == 'b' && n == '\'') {
761 pos++;
762 ScanCharacterLiteralOrLifetime(styler, pos, true);
763 } else if (IsIdentifierStart(c)) {
764 ScanIdentifier(styler, pos, keywords);
765 } else if (IsADigit(c)) {
766 ScanNumber(styler, pos);
767 } else if (IsThreeCharOperator(c, n, n2)) {
768 pos += 3;
769 styler.ColourTo(pos - 1, SCE_RUST_OPERATOR);
770 } else if (IsTwoCharOperator(c, n)) {
771 pos += 2;
772 styler.ColourTo(pos - 1, SCE_RUST_OPERATOR);
773 } else if (IsOneCharOperator(c)) {
774 pos++;
775 styler.ColourTo(pos - 1, SCE_RUST_OPERATOR);
776 } else if (c == '\'') {
777 ScanCharacterLiteralOrLifetime(styler, pos, false);
778 } else if (c == '"') {
779 pos++;
780 ResumeString(styler, pos, max, StringType::STRING);
781 } else {
782 pos++;
783 styler.ColourTo(pos - 1, SCE_RUST_LEXERROR);
784 }
785 }
786 styler.ColourTo(pos - 1, SCE_RUST_DEFAULT);
787 styler.Flush();
788 }
789
790 void SCI_METHOD LexerRust::Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
791
792 if (!options.fold)
793 return;
794
795 LexAccessor styler(pAccess);
796
797 const Sci_PositionU endPos = startPos + length;
798 int visibleChars = 0;
799 bool inLineComment = false;
800 Sci_Position lineCurrent = styler.GetLine(startPos);
801 int levelCurrent = SC_FOLDLEVELBASE;
802 if (lineCurrent > 0)
803 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
804 Sci_PositionU lineStartNext = styler.LineStart(lineCurrent+1);
805 int levelMinCurrent = levelCurrent;
806 int levelNext = levelCurrent;
807 char chNext = styler[startPos];
808 int styleNext = styler.StyleAt(startPos);
809 int style = initStyle;
810 const bool userDefinedFoldMarkers = !options.foldExplicitStart.empty() && !options.foldExplicitEnd.empty();
811 for (Sci_PositionU i = startPos; i < endPos; i++) {
812 const char ch = chNext;
813 chNext = styler.SafeGetCharAt(i + 1);
814 const int stylePrev = style;
815 style = styleNext;
816 styleNext = styler.StyleAt(i + 1);
817 const bool atEOL = i == (lineStartNext-1);
818 if ((style == SCE_RUST_COMMENTLINE) || (style == SCE_RUST_COMMENTLINEDOC))
819 inLineComment = true;
820 if (options.foldComment && options.foldCommentMultiline && IsStreamCommentStyle(style) && !inLineComment) {
821 if (!IsStreamCommentStyle(stylePrev)) {
822 levelNext++;
823 } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
824 // Comments don't end at end of line and the next character may be unstyled.
825 levelNext--;
826 }
827 }
828 if (options.foldComment && options.foldCommentExplicit && ((style == SCE_RUST_COMMENTLINE) || options.foldExplicitAnywhere)) {
829 if (userDefinedFoldMarkers) {
830 if (styler.Match(i, options.foldExplicitStart.c_str())) {
831 levelNext++;
832 } else if (styler.Match(i, options.foldExplicitEnd.c_str())) {
833 levelNext--;
834 }
835 } else {
836 if ((ch == '/') && (chNext == '/')) {
837 const char chNext2 = styler.SafeGetCharAt(i + 2);
838 if (chNext2 == '{') {
839 levelNext++;
840 } else if (chNext2 == '}') {
841 levelNext--;
842 }
843 }
844 }
845 }
846 if (options.foldSyntaxBased && (style == SCE_RUST_OPERATOR)) {
847 if (ch == '{') {
848 // Measure the minimum before a '{' to allow
849 // folding on "} else {"
850 if (levelMinCurrent > levelNext) {
851 levelMinCurrent = levelNext;
852 }
853 levelNext++;
854 } else if (ch == '}') {
855 levelNext--;
856 }
857 }
858 if (!IsASpace(ch))
859 visibleChars++;
860 if (atEOL || (i == endPos-1)) {
861 int levelUse = levelCurrent;
862 if (options.foldSyntaxBased && options.foldAtElse) {
863 levelUse = levelMinCurrent;
864 }
865 int lev = levelUse | levelNext << 16;
866 if (visibleChars == 0 && options.foldCompact)
867 lev |= SC_FOLDLEVELWHITEFLAG;
868 if (levelUse < levelNext)
869 lev |= SC_FOLDLEVELHEADERFLAG;
870 if (lev != styler.LevelAt(lineCurrent)) {
871 styler.SetLevel(lineCurrent, lev);
872 }
873 lineCurrent++;
874 lineStartNext = styler.LineStart(lineCurrent+1);
875 levelCurrent = levelNext;
876 levelMinCurrent = levelCurrent;
877 if (atEOL && (i == static_cast<Sci_PositionU>(styler.Length()-1))) {
878 // There is an empty line at end of file so give it same level and empty
879 styler.SetLevel(lineCurrent, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG);
880 }
881 visibleChars = 0;
882 inLineComment = false;
883 }
884 }
885 }
886
887 }
888
889 extern const LexerModule lmRust(SCLEX_RUST, LexerRust::LexerFactoryRust, "rust", rustWordLists);