Debian Codegraph @

scintilla/lexilla/lexers/LexDart.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexDart.cxx
3 ** Lexer for Dart.
4 **/
5 // Based on Zufu Liu's Notepad4 Dart lexer
6 // Modified for Scintilla by Jiri Techet, 2024
7 // The License.txt file describes the conditions under which this software may be distributed.
8
9 #include <cassert>
10 #include <cstring>
11
12 #include <string>
13 #include <string_view>
14 #include <vector>
15 #include <map>
16 #include <algorithm>
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 #include "OptionSet.h"
29 #include "DefaultLexer.h"
30
31 using namespace Scintilla;
32 using namespace Lexilla;
33
34 namespace {
35 // Use an unnamed namespace to protect the functions and classes from name conflicts
36
37 constexpr bool IsEOLChar(int ch) noexcept {
38 return ch == '\r' || ch == '\n';
39 }
40
41 constexpr bool IsAGraphic(int ch) noexcept {
42 // excludes C0 control characters and whitespace
43 return ch > 32 && ch < 127;
44 }
45
46 constexpr bool IsIdentifierChar(int ch) noexcept {
47 return IsAlphaNumeric(ch) || ch == '_';
48 }
49
50 constexpr bool IsIdentifierStart(int ch) noexcept {
51 return IsUpperOrLowerCase(ch) || ch == '_';
52 }
53
54 constexpr bool IsNumberContinue(int chPrev, int ch, int chNext) noexcept {
55 return ((ch == '+' || ch == '-') && (chPrev == 'e' || chPrev == 'E'))
56 || (ch == '.' && chNext != '.');
57 }
58
59 constexpr bool IsNumberStart(int ch, int chNext) noexcept {
60 return IsADigit(ch) || (ch == '.' && IsADigit(chNext));
61 }
62
63 constexpr bool IsDecimalNumber(int chPrev, int ch, int chNext) noexcept {
64 return IsIdentifierChar(ch) || IsNumberContinue(chPrev, ch, chNext);
65 }
66
67 struct EscapeSequence {
68 int outerState = SCE_DART_DEFAULT;
69 int digitsLeft = 0;
70 bool brace = false;
71
72 // highlight any character as escape sequence.
73 bool resetEscapeState(int state, int chNext) noexcept {
74 if (IsEOLChar(chNext)) {
75 return false;
76 }
77 outerState = state;
78 brace = false;
79 digitsLeft = (chNext == 'x')? 3 : ((chNext == 'u') ? 5 : 1);
80 return true;
81 }
82 bool atEscapeEnd(int ch) noexcept {
83 --digitsLeft;
84 return digitsLeft <= 0 || !IsAHeXDigit(ch);
85 }
86 };
87
88 constexpr bool IsDartIdentifierStart(int ch) noexcept {
89 return IsIdentifierStart(ch) || ch == '$';
90 }
91
92 constexpr bool IsDartIdentifierChar(int ch) noexcept {
93 return IsIdentifierChar(ch) || ch == '$';
94 }
95
96 constexpr bool IsDefinableOperator(int ch) noexcept {
97 // https://github.com/dart-lang/sdk/blob/main/sdk/lib/core/symbol.dart
98 return AnyOf(ch, '+', '-', '*', '/', '%', '~', '&', '|',
99 '^', '<', '>', '=', '[', ']');
100 }
101
102 constexpr bool IsSpaceEquiv(int state) noexcept {
103 return state == SCE_DART_DEFAULT ||
104 state == SCE_DART_COMMENTLINE ||
105 state == SCE_DART_COMMENTLINEDOC ||
106 state == SCE_DART_COMMENTBLOCK ||
107 state == SCE_DART_COMMENTBLOCKDOC;
108 }
109
110 constexpr bool IsTripleString(int state) noexcept {
111 return state == SCE_DART_TRIPLE_STRING_SQ ||
112 state == SCE_DART_TRIPLE_STRING_DQ ||
113 state == SCE_DART_TRIPLE_RAWSTRING_SQ ||
114 state == SCE_DART_TRIPLE_RAWSTRING_DQ;
115 }
116
117 constexpr bool IsDoubleQuoted(int state) noexcept {
118 return state == SCE_DART_STRING_DQ ||
119 state == SCE_DART_RAWSTRING_DQ ||
120 state == SCE_DART_TRIPLE_STRING_DQ ||
121 state == SCE_DART_TRIPLE_RAWSTRING_DQ;
122 }
123
124 constexpr bool IsRaw(int state) noexcept {
125 return state == SCE_DART_RAWSTRING_SQ ||
126 state == SCE_DART_RAWSTRING_DQ ||
127 state == SCE_DART_TRIPLE_RAWSTRING_SQ ||
128 state == SCE_DART_TRIPLE_RAWSTRING_DQ;
129 }
130
131 constexpr int GetStringQuote(int state) noexcept {
132 return IsDoubleQuoted(state) ? '\"' : '\'';
133 }
134
135 enum {
136 DartLineStateMaskLineComment = 1, // line comment
137 DartLineStateMaskImport = (1 << 1), // import
138 DartLineStateMaskInterpolation = (1 << 2), // string interpolation
139 };
140
141 // string interpolating state
142 struct InterpolatingState {
143 int state;
144 int braceCount;
145 };
146
147 struct FoldLineState {
148 int lineComment;
149 int packageImport;
150 constexpr explicit FoldLineState(int lineState) noexcept:
151 lineComment(lineState & DartLineStateMaskLineComment),
152 packageImport((lineState >> 1) & 1) {
153 }
154 };
155
156 // Options used for LexerDart
157 struct OptionsDart {
158 bool fold = false;
159 };
160
161 const char * const dartWordListDesc[] = {
162 "Primary keywords",
163 "Secondary keywords",
164 "Tertiary keywords",
165 "Global type definitions",
166 nullptr
167 };
168
169 struct OptionSetDart : public OptionSet<OptionsDart> {
170 OptionSetDart() {
171 DefineProperty("fold", &OptionsDart::fold);
172
173 DefineWordListSets(dartWordListDesc);
174 }
175 };
176
177
178 LexicalClass lexicalClasses[] = {
179 // Lexer DART SCLEX_DART SCE_DART_:
180 0, "SCE_DART_DEFAULT", "default", "White space",
181 1, "SCE_DART_COMMENTLINE", "comment line", "Comment: //",
182 2, "SCE_DART_COMMENTLINEDOC", "comment line documentation", "Comment: ///",
183 3, "SCE_DART_COMMENTBLOCK", "comment", "Comment: /* */",
184 4, "SCE_DART_COMMENTBLOCKDOC", "comment documentation", "Comment: /** */",
185 5, "SCE_DART_STRING_SQ", "literal string", "Single quoted string",
186 6, "SCE_DART_STRING_DQ", "literal string", "Double quoted string",
187 7, "SCE_DART_TRIPLE_STRING_SQ", "literal string multiline", "Single quoted multiline string",
188 8, "SCE_DART_TRIPLE_STRING_DQ", "literal string multiline", "Double quoted multiline string",
189 9, "SCE_DART_RAWSTRING_SQ", "literal string raw", "Single quoted raw string",
190 10, "SCE_DART_RAWSTRING_DQ", "literal string raw", "Double quoted raw string",
191 11, "SCE_DART_TRIPLE_RAWSTRING_SQ", "literal string multiline raw", "Single quoted multiline raw string",
192 12, "SCE_DART_TRIPLE_RAWSTRING_DQ", "literal string multiline raw", "Double quoted multiline raw string",
193 13, "SCE_DART_ESCAPECHAR", "literal string escapesequence", "Escape sequence",
194 14, "SCE_DART_IDENTIFIER", "identifier", "Identifier",
195 15, "SCE_DART_IDENTIFIER_STRING", "identifier interpolated", "Identifier following $ inside strings",
196 16, "SCE_DART_OPERATOR", "operator", "Operator",
197 17, "SCE_DART_OPERATOR_STRING", "operator interpolated", "Braces following $ inside string",
198 18, "SCE_DART_SYMBOL_IDENTIFIER", "identifier symbol", "Symbol name introduced by #",
199 19, "SCE_DART_SYMBOL_OPERATOR", "operator symbol", "Operator introduced by #",
200 20, "SCE_DART_NUMBER", "literal numeric", "Number",
201 21, "SCE_DART_KEY", "key", "Keys preceding ':' and named parameters",
202 22, "SCE_DART_METADATA", "preprocessor", "Metadata introduced by @",
203 23, "SCE_DART_KW_PRIMARY", "keyword", "Primary keywords",
204 24, "SCE_DART_KW_SECONDARY", "identifier", "Secondary keywords",
205 25, "SCE_DART_KW_TERTIARY", "identifier", "Tertiary keywords",
206 26, "SCE_DART_KW_TYPE", "identifier", "Global types",
207 27, "SCE_DART_STRINGEOL", "error literal string", "End of line where string is not closed",
208 };
209
210 class LexerDart : public DefaultLexer {
211 WordList keywordsPrimary;
212 WordList keywordsSecondary;
213 WordList keywordsTertiary;
214 WordList keywordsTypes;
215 OptionsDart options;
216 OptionSetDart osDart;
217 public:
218 LexerDart(const char *languageName_, int language_) :
219 DefaultLexer(languageName_, language_, lexicalClasses, std::size(lexicalClasses)) {
220 }
221 // Deleted so LexerDart objects can not be copied.
222 LexerDart(const LexerDart &) = delete;
223 LexerDart(LexerDart &&) = delete;
224 void operator=(const LexerDart &) = delete;
225 void operator=(LexerDart &&) = delete;
226 ~LexerDart() override = default;
227
228 void SCI_METHOD Release() override {
229 delete this;
230 }
231 int SCI_METHOD Version() const override {
232 return lvRelease5;
233 }
234 const char *SCI_METHOD PropertyNames() override {
235 return osDart.PropertyNames();
236 }
237 int SCI_METHOD PropertyType(const char *name) override {
238 return osDart.PropertyType(name);
239 }
240 const char *SCI_METHOD DescribeProperty(const char *name) override {
241 return osDart.DescribeProperty(name);
242 }
243 Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
244 const char *SCI_METHOD PropertyGet(const char *key) override {
245 return osDart.PropertyGet(key);
246 }
247 const char *SCI_METHOD DescribeWordListSets() override {
248 return osDart.DescribeWordListSets();
249 }
250 Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
251
252 void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
253 void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
254
255 void *SCI_METHOD PrivateCall(int, void *) override {
256 return nullptr;
257 }
258
259 void BacktrackToStart(const LexAccessor &styler, int stateMask, Sci_PositionU &startPos, Sci_Position &lengthDoc, int &initStyle);
260 Sci_PositionU LookbackNonWhite(LexAccessor &styler, Sci_PositionU startPos, int &chPrevNonWhite, int &stylePrevNonWhite);
261
262 static ILexer5 *LexerFactoryDart() {
263 return new LexerDart("dart", SCLEX_DART);
264 }
265 };
266
267 Sci_Position SCI_METHOD LexerDart::PropertySet(const char *key, const char *val) {
268 if (osDart.PropertySet(&options, key, val)) {
269 return 0;
270 }
271 return -1;
272 }
273
274 Sci_Position SCI_METHOD LexerDart::WordListSet(int n, const char *wl) {
275 WordList *wordListN = nullptr;
276 switch (n) {
277 case 0:
278 wordListN = &keywordsPrimary;
279 break;
280 case 1:
281 wordListN = &keywordsSecondary;
282 break;
283 case 2:
284 wordListN = &keywordsTertiary;
285 break;
286 case 3:
287 wordListN = &keywordsTypes;
288 break;
289 default:
290 break;
291 }
292 Sci_Position firstModification = -1;
293 if (wordListN && wordListN->Set(wl, false)) {
294 firstModification = 0;
295 }
296 return firstModification;
297 }
298
299 void LexerDart::BacktrackToStart(const LexAccessor &styler, int stateMask, Sci_PositionU &startPos, Sci_Position &lengthDoc, int &initStyle) {
300 const Sci_Position currentLine = styler.GetLine(startPos);
301 if (currentLine != 0) {
302 Sci_Position line = currentLine - 1;
303 int lineState = styler.GetLineState(line);
304 while ((lineState & stateMask) != 0 && line != 0) {
305 --line;
306 lineState = styler.GetLineState(line);
307 }
308 if ((lineState & stateMask) == 0) {
309 ++line;
310 }
311 if (line != currentLine) {
312 const Sci_PositionU endPos = startPos + lengthDoc;
313 startPos = (line == 0) ? 0 : styler.LineStart(line);
314 lengthDoc = endPos - startPos;
315 initStyle = (startPos == 0) ? 0 : styler.StyleAt(startPos - 1);
316 }
317 }
318 }
319
320 Sci_PositionU LexerDart::LookbackNonWhite(LexAccessor &styler, Sci_PositionU startPos, int &chPrevNonWhite, int &stylePrevNonWhite) {
321 do {
322 --startPos;
323 const unsigned style = styler.StyleAt(startPos);
324 if (!IsSpaceEquiv(style)) {
325 stylePrevNonWhite = style;
326 chPrevNonWhite = static_cast<unsigned char>(styler[startPos]);
327 break;
328 }
329 } while (startPos != 0);
330 return startPos;
331 }
332
333 void LexerDart::Lex(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, IDocument *pAccess) {
334 Accessor styler(pAccess, nullptr);
335
336 int lineStateLineType = 0;
337 int commentLevel = 0; // nested block comment level
338
339 std::vector<InterpolatingState> interpolatingStack;
340
341 int visibleChars = 0;
342 int chBefore = 0;
343 int chPrevNonWhite = 0;
344 EscapeSequence escSeq;
345
346 if (initStyle == SCE_DART_STRINGEOL) {
347 initStyle = SCE_DART_DEFAULT;
348 }
349
350 if (startPos != 0) {
351 // backtrack to the line where interpolation starts
352 BacktrackToStart(styler, DartLineStateMaskInterpolation, startPos, lengthDoc, initStyle);
353 }
354
355 StyleContext sc(startPos, lengthDoc, initStyle, styler);
356 if (sc.currentLine > 0) {
357 const int lineState = styler.GetLineState(sc.currentLine - 1);
358 commentLevel = lineState >> 4;
359 }
360 if (startPos == 0) {
361 if (sc.Match('#', '!')) {
362 // Shell Shebang at beginning of file
363 sc.SetState(SCE_DART_COMMENTLINE);
364 sc.Forward();
365 lineStateLineType = DartLineStateMaskLineComment;
366 }
367 } else if (IsSpaceEquiv(initStyle)) {
368 LookbackNonWhite(styler, startPos, chPrevNonWhite, initStyle);
369 chBefore = chPrevNonWhite;
370 }
371
372 while (sc.More()) {
373 switch (sc.state) {
374 case SCE_DART_OPERATOR:
375 case SCE_DART_OPERATOR_STRING:
376 sc.SetState(SCE_DART_DEFAULT);
377 break;
378
379 case SCE_DART_NUMBER:
380 if (!IsDecimalNumber(sc.chPrev, sc.ch, sc.chNext)) {
381 sc.SetState(SCE_DART_DEFAULT);
382 }
383 break;
384
385 case SCE_DART_IDENTIFIER:
386 case SCE_DART_IDENTIFIER_STRING:
387 case SCE_DART_METADATA:
388 case SCE_DART_SYMBOL_IDENTIFIER:
389 if (!IsDartIdentifierChar(sc.ch) || (sc.ch == '$' && sc.state == SCE_DART_IDENTIFIER_STRING)) {
390 if (sc.state == SCE_DART_METADATA || sc.state == SCE_DART_SYMBOL_IDENTIFIER) {
391 if (sc.ch == '.') {
392 const int state = sc.state;
393 sc.SetState(SCE_DART_OPERATOR);
394 sc.ForwardSetState(state);
395 continue;
396 }
397 } else {
398 char s[64];
399 sc.GetCurrent(s, sizeof(s));
400 const int state = sc.state;
401 if (state == SCE_DART_IDENTIFIER_STRING) {
402 sc.SetState(escSeq.outerState);
403 continue;
404 } else if (keywordsPrimary.InList(s)) {
405 sc.ChangeState(SCE_DART_KW_PRIMARY);
406 if (strcmp(s, "import") == 0 || strcmp(s, "part") == 0) {
407 if (visibleChars == sc.LengthCurrent()) {
408 lineStateLineType = DartLineStateMaskImport;
409 }
410 }
411 } else if (keywordsSecondary.InList(s)) {
412 sc.ChangeState(SCE_DART_KW_SECONDARY);
413 } else if (keywordsTertiary.InList(s)) {
414 sc.ChangeState(SCE_DART_KW_TERTIARY);
415 } else if (keywordsTypes.InList(s)) {
416 sc.ChangeState(SCE_DART_KW_TYPE);
417 } else if (state == SCE_DART_IDENTIFIER && sc.ch == ':') {
418 if (chBefore == ',' || chBefore == '{' || chBefore == '(') {
419 sc.ChangeState(SCE_DART_KEY); // map key or named parameter
420 }
421 }
422 }
423
424 sc.SetState(SCE_DART_DEFAULT);
425 }
426 break;
427
428 case SCE_DART_SYMBOL_OPERATOR:
429 if (!IsDefinableOperator(sc.ch)) {
430 sc.SetState(SCE_DART_DEFAULT);
431 }
432 break;
433
434 case SCE_DART_COMMENTLINE:
435 case SCE_DART_COMMENTLINEDOC:
436 if (sc.atLineStart) {
437 sc.SetState(SCE_DART_DEFAULT);
438 }
439 break;
440
441 case SCE_DART_COMMENTBLOCK:
442 case SCE_DART_COMMENTBLOCKDOC:
443 if (sc.Match('*', '/')) {
444 sc.Forward();
445 --commentLevel;
446 if (commentLevel == 0) {
447 sc.ForwardSetState(SCE_DART_DEFAULT);
448 }
449 } else if (sc.Match('/', '*')) {
450 sc.Forward();
451 ++commentLevel;
452 }
453 break;
454
455 case SCE_DART_STRING_SQ:
456 case SCE_DART_STRING_DQ:
457 case SCE_DART_TRIPLE_STRING_SQ:
458 case SCE_DART_TRIPLE_STRING_DQ:
459 case SCE_DART_RAWSTRING_SQ:
460 case SCE_DART_RAWSTRING_DQ:
461 case SCE_DART_TRIPLE_RAWSTRING_SQ:
462 case SCE_DART_TRIPLE_RAWSTRING_DQ:
463 if (sc.atLineStart && !IsTripleString(sc.state)) {
464 sc.SetState(SCE_DART_DEFAULT);
465 } else if (sc.atLineEnd && !IsTripleString(sc.state)) {
466 sc.ChangeState(SCE_DART_STRINGEOL);
467 } else if (sc.ch == '\\' && !IsRaw(sc.state)) {
468 if (escSeq.resetEscapeState(sc.state, sc.chNext)) {
469 sc.SetState(SCE_DART_ESCAPECHAR);
470 sc.Forward();
471 if (sc.Match('u', '{')) {
472 escSeq.brace = true;
473 escSeq.digitsLeft = 7; // Unicode code point
474 sc.Forward();
475 }
476 }
477 } else if (sc.ch == '$' && !IsRaw(sc.state)) {
478 escSeq.outerState = sc.state;
479 sc.SetState(SCE_DART_OPERATOR_STRING);
480 sc.Forward();
481 if (sc.ch == '{') {
482 interpolatingStack.push_back({escSeq.outerState, 1});
483 } else if (sc.ch != '$' && IsDartIdentifierStart(sc.ch)) {
484 sc.SetState(SCE_DART_IDENTIFIER_STRING);
485 } else { // error
486 sc.SetState(escSeq.outerState);
487 continue;
488 }
489 } else if (sc.ch == GetStringQuote(sc.state) &&
490 (!IsTripleString(sc.state) || (sc.Match(IsDoubleQuoted(sc.state) ? R"(""")" : R"(''')")))) {
491 if (IsTripleString(sc.state)) {
492 sc.Forward(2);
493 }
494 sc.Forward();
495 sc.SetState(SCE_DART_DEFAULT);
496 }
497 break;
498
499 case SCE_DART_STRINGEOL:
500 if (sc.atLineStart) {
501 sc.SetState(SCE_DART_DEFAULT);
502 }
503 break;
504
505 case SCE_DART_ESCAPECHAR:
506 if (escSeq.atEscapeEnd(sc.ch)) {
507 if (escSeq.brace && sc.ch == '}') {
508 sc.Forward();
509 }
510 sc.SetState(escSeq.outerState);
511 continue;
512 }
513 break;
514 }
515
516 if (sc.state == SCE_DART_DEFAULT) {
517 if (sc.ch == '/' && (sc.chNext == '/' || sc.chNext == '*')) {
518 const int chNext = sc.chNext;
519 sc.SetState((chNext == '/') ? SCE_DART_COMMENTLINE : SCE_DART_COMMENTBLOCK);
520 sc.Forward(2);
521 if (sc.ch == chNext && sc.chNext != chNext) {
522 if (sc.state == SCE_DART_COMMENTLINE) {
523 sc.ChangeState(SCE_DART_COMMENTLINEDOC);
524 } else {
525 sc.ChangeState(SCE_DART_COMMENTBLOCKDOC);
526 }
527 }
528 if (chNext == '/') {
529 if (visibleChars == 0) {
530 lineStateLineType = DartLineStateMaskLineComment;
531 }
532 } else {
533 commentLevel = 1;
534 }
535 continue;
536 }
537 if (sc.ch == 'r' && (sc.chNext == '\'' || sc.chNext == '"')) {
538 sc.SetState((sc.chNext == '\'') ? SCE_DART_RAWSTRING_SQ : SCE_DART_RAWSTRING_DQ);
539 sc.Forward(2);
540 if (sc.chPrev == '\'' && sc.Match('\'', '\'')) {
541 sc.ChangeState(SCE_DART_TRIPLE_RAWSTRING_SQ);
542 sc.Forward(2);
543 } else if (sc.chPrev == '"' && sc.Match('"', '"')) {
544 sc.ChangeState(SCE_DART_TRIPLE_RAWSTRING_DQ);
545 sc.Forward(2);
546 }
547 continue;
548 }
549 if (sc.ch == '"') {
550 if (sc.Match(R"(""")")) {
551 sc.SetState(SCE_DART_TRIPLE_STRING_DQ);
552 sc.Forward(2);
553 } else {
554 chBefore = chPrevNonWhite;
555 sc.SetState(SCE_DART_STRING_DQ);
556 }
557 } else if (sc.ch == '\'') {
558 if (sc.Match(R"(''')")) {
559 sc.SetState(SCE_DART_TRIPLE_STRING_SQ);
560 sc.Forward(2);
561 } else {
562 chBefore = chPrevNonWhite;
563 sc.SetState(SCE_DART_STRING_SQ);
564 }
565 } else if (IsNumberStart(sc.ch, sc.chNext)) {
566 sc.SetState(SCE_DART_NUMBER);
567 } else if ((sc.ch == '@' || sc.ch == '#') && IsDartIdentifierStart(sc.chNext)) {
568 sc.SetState((sc.ch == '@') ? SCE_DART_METADATA : SCE_DART_SYMBOL_IDENTIFIER);
569 } else if (IsDartIdentifierStart(sc.ch)) {
570 chBefore = chPrevNonWhite;
571 sc.SetState(SCE_DART_IDENTIFIER);
572 } else if (sc.ch == '#' && IsDefinableOperator(sc.chNext)) {
573 sc.SetState(SCE_DART_SYMBOL_OPERATOR);
574 } else if (IsAGraphic(sc.ch)) {
575 sc.SetState(SCE_DART_OPERATOR);
576 if (!interpolatingStack.empty() && AnyOf(sc.ch, '{', '}')) {
577 InterpolatingState &current = interpolatingStack.back();
578 if (sc.ch == '{') {
579 current.braceCount += 1;
580 } else {
581 current.braceCount -= 1;
582 if (current.braceCount == 0) {
583 sc.ChangeState(SCE_DART_OPERATOR_STRING);
584 sc.ForwardSetState(current.state);
585 interpolatingStack.pop_back();
586 continue;
587 }
588 }
589 }
590 }
591 }
592
593 if (!isspacechar(sc.ch)) {
594 visibleChars++;
595 if (!IsSpaceEquiv(sc.state)) {
596 chPrevNonWhite = sc.ch;
597 }
598 }
599 if (sc.atLineEnd) {
600 int lineState = (commentLevel << 4) | lineStateLineType;
601 if (!interpolatingStack.empty()) {
602 lineState |= DartLineStateMaskInterpolation;
603 }
604 styler.SetLineState(sc.currentLine, lineState);
605 lineStateLineType = 0;
606 visibleChars = 0;
607 }
608 sc.Forward();
609 }
610
611 sc.Complete();
612 }
613
614 void LexerDart::Fold(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, IDocument *pAccess) {
615 if (!options.fold)
616 return;
617
618 Accessor styler(pAccess, nullptr);
619 const Sci_PositionU endPos = startPos + lengthDoc;
620 Sci_Position lineCurrent = styler.GetLine(startPos);
621 while (lineCurrent > 0) {
622 lineCurrent--;
623 startPos = styler.LineStart(lineCurrent);
624 initStyle = (startPos > 0) ? styler.StyleIndexAt(startPos) : 0;
625 if (initStyle != SCE_DART_COMMENTBLOCKDOC && initStyle != SCE_DART_COMMENTBLOCK &&
626 initStyle != SCE_DART_TRIPLE_RAWSTRING_SQ && initStyle != SCE_DART_TRIPLE_RAWSTRING_DQ &&
627 initStyle != SCE_DART_TRIPLE_STRING_SQ && initStyle != SCE_DART_TRIPLE_STRING_DQ)
628 break;
629 }
630 FoldLineState foldPrev(0);
631 int levelCurrent = SC_FOLDLEVELBASE;
632 if (lineCurrent > 0) {
633 levelCurrent = styler.LevelAt(lineCurrent - 1) >> 16;
634 foldPrev = FoldLineState(styler.GetLineState(lineCurrent - 1));
635 }
636
637 int levelNext = levelCurrent;
638 FoldLineState foldCurrent(styler.GetLineState(lineCurrent));
639 Sci_PositionU lineStartNext = styler.LineStart(lineCurrent + 1);
640 lineStartNext = std::min(lineStartNext, endPos);
641
642 char chNext = styler[startPos];
643 int styleNext = styler.StyleIndexAt(startPos);
644 int style = initStyle;
645
646 while (startPos < endPos) {
647 const char ch = chNext;
648 const int stylePrev = style;
649 style = styleNext;
650 chNext = styler[++startPos];
651 styleNext = styler.StyleIndexAt(startPos);
652
653 switch (style) {
654 case SCE_DART_COMMENTBLOCKDOC:
655 case SCE_DART_COMMENTBLOCK: {
656 const int level = (ch == '/' && chNext == '*') ? 1 : ((ch == '*' && chNext == '/') ? -1 : 0);
657 if (level != 0) {
658 levelNext += level;
659 startPos++;
660 chNext = styler[startPos];
661 styleNext = styler.StyleIndexAt(startPos);
662 }
663 } break;
664
665 case SCE_DART_TRIPLE_RAWSTRING_SQ:
666 case SCE_DART_TRIPLE_RAWSTRING_DQ:
667 case SCE_DART_TRIPLE_STRING_SQ:
668 case SCE_DART_TRIPLE_STRING_DQ:
669 if (style != stylePrev && !AnyOf(stylePrev, SCE_DART_ESCAPECHAR, SCE_DART_OPERATOR_STRING, SCE_DART_IDENTIFIER_STRING)) {
670 levelNext++;
671 }
672 if (style != styleNext && !AnyOf(styleNext, SCE_DART_ESCAPECHAR, SCE_DART_OPERATOR_STRING, SCE_DART_IDENTIFIER_STRING)) {
673 levelNext--;
674 }
675 break;
676
677 case SCE_DART_OPERATOR:
678 case SCE_DART_OPERATOR_STRING:
679 if (ch == '{' || ch == '[' || ch == '(') {
680 levelNext++;
681 } else if (ch == '}' || ch == ']' || ch == ')') {
682 levelNext--;
683 }
684 break;
685 }
686
687 if (startPos == lineStartNext) {
688 const FoldLineState foldNext(styler.GetLineState(lineCurrent + 1));
689 levelNext = std::max(levelNext, SC_FOLDLEVELBASE);
690 if (foldCurrent.lineComment) {
691 levelNext += foldNext.lineComment - foldPrev.lineComment;
692 } else if (foldCurrent.packageImport) {
693 levelNext += foldNext.packageImport - foldPrev.packageImport;
694 }
695
696 const int levelUse = levelCurrent;
697 int lev = levelUse | (levelNext << 16);
698 if (levelUse < levelNext) {
699 lev |= SC_FOLDLEVELHEADERFLAG;
700 }
701 styler.SetLevel(lineCurrent, lev);
702
703 lineCurrent++;
704 lineStartNext = styler.LineStart(lineCurrent + 1);
705 lineStartNext = std::min(lineStartNext, endPos);
706 levelCurrent = levelNext;
707 foldPrev = foldCurrent;
708 foldCurrent = foldNext;
709 }
710 }
711 }
712
713 } // unnamed namespace end
714
715 extern const LexerModule lmDart(SCLEX_DART, LexerDart::LexerFactoryDart, "dart", dartWordListDesc);