Debian Codegraph @

scintilla/lexilla/lexers/LexD.cxx

raw
1 /** @file LexD.cxx
2 ** Lexer for D.
3 **
4 ** Copyright (c) 2006 by Waldemar Augustyn <[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 "WordList.h"
27 #include "LexAccessor.h"
28 #include "StyleContext.h"
29 #include "CharacterSet.h"
30 #include "LexerModule.h"
31 #include "OptionSet.h"
32 #include "DefaultLexer.h"
33
34 using namespace Scintilla;
35 using namespace Lexilla;
36
37 /* Nested comments require keeping the value of the nesting level for every
38 position in the document. But since scintilla always styles line by line,
39 we only need to store one value per line. The non-negative number indicates
40 nesting level at the end of the line.
41 */
42
43 // Underscore, letter, digit and universal alphas from C99 Appendix D.
44
45 static bool IsWordStart(int ch) {
46 return (IsASCII(ch) && (isalpha(ch) || ch == '_')) || !IsASCII(ch);
47 }
48
49 static bool IsWord(int ch) {
50 return (IsASCII(ch) && (isalnum(ch) || ch == '_')) || !IsASCII(ch);
51 }
52
53 static bool IsDoxygen(int ch) {
54 if (IsASCII(ch) && islower(ch))
55 return true;
56 if (ch == '$' || ch == '@' || ch == '\\' ||
57 ch == '&' || ch == '#' || ch == '<' || ch == '>' ||
58 ch == '{' || ch == '}' || ch == '[' || ch == ']')
59 return true;
60 return false;
61 }
62
63 static bool IsStringSuffix(int ch) {
64 return ch == 'c' || ch == 'w' || ch == 'd';
65 }
66
67 static bool IsStreamCommentStyle(int style) {
68 return style == SCE_D_COMMENT ||
69 style == SCE_D_COMMENTDOC ||
70 style == SCE_D_COMMENTDOCKEYWORD ||
71 style == SCE_D_COMMENTDOCKEYWORDERROR;
72 }
73
74 // An individual named option for use in an OptionSet
75
76 // Options used for LexerD
77 struct OptionsD {
78 bool fold;
79 bool foldSyntaxBased;
80 bool foldComment;
81 bool foldCommentMultiline;
82 bool foldCommentExplicit;
83 std::string foldExplicitStart;
84 std::string foldExplicitEnd;
85 bool foldExplicitAnywhere;
86 bool foldCompact;
87 int foldAtElseInt;
88 bool foldAtElse;
89 OptionsD() {
90 fold = false;
91 foldSyntaxBased = true;
92 foldComment = false;
93 foldCommentMultiline = true;
94 foldCommentExplicit = true;
95 foldExplicitStart = "";
96 foldExplicitEnd = "";
97 foldExplicitAnywhere = false;
98 foldCompact = true;
99 foldAtElseInt = -1;
100 foldAtElse = false;
101 }
102 };
103
104 static const char * const dWordLists[] = {
105 "Primary keywords and identifiers",
106 "Secondary keywords and identifiers",
107 "Documentation comment keywords",
108 "Type definitions and aliases",
109 "Keywords 5",
110 "Keywords 6",
111 "Keywords 7",
112 0,
113 };
114
115 struct OptionSetD : public OptionSet<OptionsD> {
116 OptionSetD() {
117 DefineProperty("fold", &OptionsD::fold);
118
119 DefineProperty("fold.d.syntax.based", &OptionsD::foldSyntaxBased,
120 "Set this property to 0 to disable syntax based folding.");
121
122 DefineProperty("fold.comment", &OptionsD::foldComment);
123
124 DefineProperty("fold.d.comment.multiline", &OptionsD::foldCommentMultiline,
125 "Set this property to 0 to disable folding multi-line comments when fold.comment=1.");
126
127 DefineProperty("fold.d.comment.explicit", &OptionsD::foldCommentExplicit,
128 "Set this property to 0 to disable folding explicit fold points when fold.comment=1.");
129
130 DefineProperty("fold.d.explicit.start", &OptionsD::foldExplicitStart,
131 "The string to use for explicit fold start points, replacing the standard //{.");
132
133 DefineProperty("fold.d.explicit.end", &OptionsD::foldExplicitEnd,
134 "The string to use for explicit fold end points, replacing the standard //}.");
135
136 DefineProperty("fold.d.explicit.anywhere", &OptionsD::foldExplicitAnywhere,
137 "Set this property to 1 to enable explicit fold points anywhere, not just in line comments.");
138
139 DefineProperty("fold.compact", &OptionsD::foldCompact);
140
141 DefineProperty("lexer.d.fold.at.else", &OptionsD::foldAtElseInt,
142 "This option enables D folding on a \"} else {\" line of an if statement.");
143
144 DefineProperty("fold.at.else", &OptionsD::foldAtElse);
145
146 DefineWordListSets(dWordLists);
147 }
148 };
149
150 class LexerD : public DefaultLexer {
151 bool caseSensitive;
152 WordList keywords;
153 WordList keywords2;
154 WordList keywords3;
155 WordList keywords4;
156 WordList keywords5;
157 WordList keywords6;
158 WordList keywords7;
159 OptionsD options;
160 OptionSetD osD;
161 public:
162 LexerD(bool caseSensitive_) :
163 DefaultLexer("D", SCLEX_D),
164 caseSensitive(caseSensitive_) {
165 }
166 virtual ~LexerD() {
167 }
168 void SCI_METHOD Release() override {
169 delete this;
170 }
171 int SCI_METHOD Version() const override {
172 return lvRelease5;
173 }
174 const char * SCI_METHOD PropertyNames() override {
175 return osD.PropertyNames();
176 }
177 int SCI_METHOD PropertyType(const char *name) override {
178 return osD.PropertyType(name);
179 }
180 const char * SCI_METHOD DescribeProperty(const char *name) override {
181 return osD.DescribeProperty(name);
182 }
183 Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override;
184 const char * SCI_METHOD PropertyGet(const char *key) override {
185 return osD.PropertyGet(key);
186 }
187 const char * SCI_METHOD DescribeWordListSets() override {
188 return osD.DescribeWordListSets();
189 }
190 Sci_Position SCI_METHOD WordListSet(int n, const char *wl) override;
191 void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
192 void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
193
194 void * SCI_METHOD PrivateCall(int, void *) override {
195 return 0;
196 }
197
198 static ILexer5 *LexerFactoryD() {
199 return new LexerD(true);
200 }
201 };
202
203 Sci_Position SCI_METHOD LexerD::PropertySet(const char *key, const char *val) {
204 if (osD.PropertySet(&options, key, val)) {
205 return 0;
206 }
207 return -1;
208 }
209
210 Sci_Position SCI_METHOD LexerD::WordListSet(int n, const char *wl) {
211 WordList *wordListN = 0;
212 switch (n) {
213 case 0:
214 wordListN = &keywords;
215 break;
216 case 1:
217 wordListN = &keywords2;
218 break;
219 case 2:
220 wordListN = &keywords3;
221 break;
222 case 3:
223 wordListN = &keywords4;
224 break;
225 case 4:
226 wordListN = &keywords5;
227 break;
228 case 5:
229 wordListN = &keywords6;
230 break;
231 case 6:
232 wordListN = &keywords7;
233 break;
234 }
235 Sci_Position firstModification = -1;
236 if (wordListN) {
237 WordList wlNew;
238 wlNew.Set(wl);
239 if (*wordListN != wlNew) {
240 wordListN->Set(wl);
241 firstModification = 0;
242 }
243 }
244 return firstModification;
245 }
246
247 void SCI_METHOD LexerD::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
248 LexAccessor styler(pAccess);
249
250 int styleBeforeDCKeyword = SCE_D_DEFAULT;
251
252 StyleContext sc(startPos, length, initStyle, styler);
253
254 Sci_Position curLine = styler.GetLine(startPos);
255 int curNcLevel = curLine > 0? styler.GetLineState(curLine-1): 0;
256 bool numFloat = false; // Float literals have '+' and '-' signs
257 bool numHex = false;
258
259 for (; sc.More(); sc.Forward()) {
260
261 if (sc.atLineStart) {
262 curLine = styler.GetLine(sc.currentPos);
263 styler.SetLineState(curLine, curNcLevel);
264 }
265
266 // Determine if the current state should terminate.
267 switch (sc.state) {
268 case SCE_D_OPERATOR:
269 sc.SetState(SCE_D_DEFAULT);
270 break;
271 case SCE_D_NUMBER:
272 // We accept almost anything because of hex. and number suffixes
273 if (IsASCII(sc.ch) && (isalnum(sc.ch) || sc.ch == '_')) {
274 continue;
275 } else if (sc.ch == '.' && sc.chNext != '.' && !numFloat) {
276 // Don't parse 0..2 as number.
277 numFloat=true;
278 continue;
279 } else if ( ( sc.ch == '-' || sc.ch == '+' ) && ( /*sign and*/
280 ( !numHex && ( sc.chPrev == 'e' || sc.chPrev == 'E' ) ) || /*decimal or*/
281 ( sc.chPrev == 'p' || sc.chPrev == 'P' ) ) ) { /*hex*/
282 // Parse exponent sign in float literals: 2e+10 0x2e+10
283 continue;
284 } else {
285 sc.SetState(SCE_D_DEFAULT);
286 }
287 break;
288 case SCE_D_IDENTIFIER:
289 if (!IsWord(sc.ch)) {
290 char s[1000];
291 if (caseSensitive) {
292 sc.GetCurrent(s, sizeof(s));
293 } else {
294 sc.GetCurrentLowered(s, sizeof(s));
295 }
296 if (keywords.InList(s)) {
297 sc.ChangeState(SCE_D_WORD);
298 } else if (keywords2.InList(s)) {
299 sc.ChangeState(SCE_D_WORD2);
300 } else if (keywords4.InList(s)) {
301 sc.ChangeState(SCE_D_TYPEDEF);
302 } else if (keywords5.InList(s)) {
303 sc.ChangeState(SCE_D_WORD5);
304 } else if (keywords6.InList(s)) {
305 sc.ChangeState(SCE_D_WORD6);
306 } else if (keywords7.InList(s)) {
307 sc.ChangeState(SCE_D_WORD7);
308 }
309 sc.SetState(SCE_D_DEFAULT);
310 }
311 break;
312 case SCE_D_COMMENT:
313 if (sc.Match('*', '/')) {
314 sc.Forward();
315 sc.ForwardSetState(SCE_D_DEFAULT);
316 }
317 break;
318 case SCE_D_COMMENTDOC:
319 if (sc.Match('*', '/')) {
320 sc.Forward();
321 sc.ForwardSetState(SCE_D_DEFAULT);
322 } else if (sc.ch == '@' || sc.ch == '\\') { // JavaDoc and Doxygen support
323 // Verify that we have the conditions to mark a comment-doc-keyword
324 if ((IsASpace(sc.chPrev) || sc.chPrev == '*') && (!IsASpace(sc.chNext))) {
325 styleBeforeDCKeyword = SCE_D_COMMENTDOC;
326 sc.SetState(SCE_D_COMMENTDOCKEYWORD);
327 }
328 }
329 break;
330 case SCE_D_COMMENTLINE:
331 if (sc.atLineStart) {
332 sc.SetState(SCE_D_DEFAULT);
333 }
334 break;
335 case SCE_D_COMMENTLINEDOC:
336 if (sc.atLineStart) {
337 sc.SetState(SCE_D_DEFAULT);
338 } else if (sc.ch == '@' || sc.ch == '\\') { // JavaDoc and Doxygen support
339 // Verify that we have the conditions to mark a comment-doc-keyword
340 if ((IsASpace(sc.chPrev) || sc.chPrev == '/' || sc.chPrev == '!') && (!IsASpace(sc.chNext))) {
341 styleBeforeDCKeyword = SCE_D_COMMENTLINEDOC;
342 sc.SetState(SCE_D_COMMENTDOCKEYWORD);
343 }
344 }
345 break;
346 case SCE_D_COMMENTDOCKEYWORD:
347 if ((styleBeforeDCKeyword == SCE_D_COMMENTDOC) && sc.Match('*', '/')) {
348 sc.ChangeState(SCE_D_COMMENTDOCKEYWORDERROR);
349 sc.Forward();
350 sc.ForwardSetState(SCE_D_DEFAULT);
351 } else if (!IsDoxygen(sc.ch)) {
352 char s[100];
353 if (caseSensitive) {
354 sc.GetCurrent(s, sizeof(s));
355 } else {
356 sc.GetCurrentLowered(s, sizeof(s));
357 }
358 if (!IsASpace(sc.ch) || !keywords3.InList(s + 1)) {
359 sc.ChangeState(SCE_D_COMMENTDOCKEYWORDERROR);
360 }
361 sc.SetState(styleBeforeDCKeyword);
362 }
363 break;
364 case SCE_D_COMMENTNESTED:
365 if (sc.Match('+', '/')) {
366 if (curNcLevel > 0)
367 curNcLevel -= 1;
368 curLine = styler.GetLine(sc.currentPos);
369 styler.SetLineState(curLine, curNcLevel);
370 sc.Forward();
371 if (curNcLevel == 0) {
372 sc.ForwardSetState(SCE_D_DEFAULT);
373 }
374 } else if (sc.Match('/','+')) {
375 curNcLevel += 1;
376 curLine = styler.GetLine(sc.currentPos);
377 styler.SetLineState(curLine, curNcLevel);
378 sc.Forward();
379 }
380 break;
381 case SCE_D_STRING:
382 if (sc.ch == '\\') {
383 if (sc.chNext == '"' || sc.chNext == '\\') {
384 sc.Forward();
385 }
386 } else if (sc.ch == '"') {
387 if(IsStringSuffix(sc.chNext))
388 sc.Forward();
389 sc.ForwardSetState(SCE_D_DEFAULT);
390 }
391 break;
392 case SCE_D_CHARACTER:
393 if (sc.atLineEnd) {
394 sc.ChangeState(SCE_D_STRINGEOL);
395 } else if (sc.ch == '\\') {
396 if (sc.chNext == '\'' || sc.chNext == '\\') {
397 sc.Forward();
398 }
399 } else if (sc.ch == '\'') {
400 // Char has no suffixes
401 sc.ForwardSetState(SCE_D_DEFAULT);
402 }
403 break;
404 case SCE_D_STRINGEOL:
405 if (sc.atLineStart) {
406 sc.SetState(SCE_D_DEFAULT);
407 }
408 break;
409 case SCE_D_STRINGB:
410 if (sc.ch == '`') {
411 if(IsStringSuffix(sc.chNext))
412 sc.Forward();
413 sc.ForwardSetState(SCE_D_DEFAULT);
414 }
415 break;
416 case SCE_D_STRINGR:
417 if (sc.ch == '"') {
418 if(IsStringSuffix(sc.chNext))
419 sc.Forward();
420 sc.ForwardSetState(SCE_D_DEFAULT);
421 }
422 break;
423 }
424
425 // Determine if a new state should be entered.
426 if (sc.state == SCE_D_DEFAULT) {
427 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
428 sc.SetState(SCE_D_NUMBER);
429 numFloat = sc.ch == '.';
430 // Remember hex literal
431 numHex = sc.ch == '0' && ( sc.chNext == 'x' || sc.chNext == 'X' );
432 } else if ( (sc.ch == 'r' || sc.ch == 'x' || sc.ch == 'q')
433 && sc.chNext == '"' ) {
434 // Limited support for hex and delimited strings: parse as r""
435 sc.SetState(SCE_D_STRINGR);
436 sc.Forward();
437 } else if (IsWordStart(sc.ch) || sc.ch == '$') {
438 sc.SetState(SCE_D_IDENTIFIER);
439 } else if (sc.Match('/','+')) {
440 curNcLevel += 1;
441 curLine = styler.GetLine(sc.currentPos);
442 styler.SetLineState(curLine, curNcLevel);
443 sc.SetState(SCE_D_COMMENTNESTED);
444 sc.Forward();
445 } else if (sc.Match('/', '*')) {
446 if (sc.Match("/**") || sc.Match("/*!")) { // Support of Qt/Doxygen doc. style
447 sc.SetState(SCE_D_COMMENTDOC);
448 } else {
449 sc.SetState(SCE_D_COMMENT);
450 }
451 sc.Forward(); // Eat the * so it isn't used for the end of the comment
452 } else if (sc.Match('/', '/')) {
453 if ((sc.Match("///") && !sc.Match("////")) || sc.Match("//!"))
454 // Support of Qt/Doxygen doc. style
455 sc.SetState(SCE_D_COMMENTLINEDOC);
456 else
457 sc.SetState(SCE_D_COMMENTLINE);
458 } else if (sc.ch == '"') {
459 sc.SetState(SCE_D_STRING);
460 } else if (sc.ch == '\'') {
461 sc.SetState(SCE_D_CHARACTER);
462 } else if (sc.ch == '`') {
463 sc.SetState(SCE_D_STRINGB);
464 } else if (isoperator(static_cast<char>(sc.ch))) {
465 sc.SetState(SCE_D_OPERATOR);
466 if (sc.ch == '.' && sc.chNext == '.') sc.Forward(); // Range operator
467 }
468 }
469 }
470 sc.Complete();
471 }
472
473 // Store both the current line's fold level and the next lines in the
474 // level store to make it easy to pick up with each increment
475 // and to make it possible to fiddle the current level for "} else {".
476
477 void SCI_METHOD LexerD::Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) {
478
479 if (!options.fold)
480 return;
481
482 LexAccessor styler(pAccess);
483
484 Sci_PositionU endPos = startPos + length;
485 int visibleChars = 0;
486 Sci_Position lineCurrent = styler.GetLine(startPos);
487 int levelCurrent = SC_FOLDLEVELBASE;
488 if (lineCurrent > 0)
489 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
490 int levelMinCurrent = levelCurrent;
491 int levelNext = levelCurrent;
492 char chNext = styler[startPos];
493 int styleNext = styler.StyleAt(startPos);
494 int style = initStyle;
495 bool foldAtElse = options.foldAtElseInt >= 0 ? options.foldAtElseInt != 0 : options.foldAtElse;
496 const bool userDefinedFoldMarkers = !options.foldExplicitStart.empty() && !options.foldExplicitEnd.empty();
497 for (Sci_PositionU i = startPos; i < endPos; i++) {
498 char ch = chNext;
499 chNext = styler.SafeGetCharAt(i + 1);
500 int stylePrev = style;
501 style = styleNext;
502 styleNext = styler.StyleAt(i + 1);
503 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
504 if (options.foldComment && options.foldCommentMultiline && IsStreamCommentStyle(style)) {
505 if (!IsStreamCommentStyle(stylePrev)) {
506 levelNext++;
507 } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
508 // Comments don't end at end of line and the next character may be unstyled.
509 levelNext--;
510 }
511 }
512 if (options.foldComment && options.foldCommentExplicit && ((style == SCE_D_COMMENTLINE) || options.foldExplicitAnywhere)) {
513 if (userDefinedFoldMarkers) {
514 if (styler.Match(i, options.foldExplicitStart.c_str())) {
515 levelNext++;
516 } else if (styler.Match(i, options.foldExplicitEnd.c_str())) {
517 levelNext--;
518 }
519 } else {
520 if ((ch == '/') && (chNext == '/')) {
521 char chNext2 = styler.SafeGetCharAt(i + 2);
522 if (chNext2 == '{') {
523 levelNext++;
524 } else if (chNext2 == '}') {
525 levelNext--;
526 }
527 }
528 }
529 }
530 if (options.foldSyntaxBased && (style == SCE_D_OPERATOR)) {
531 if (ch == '{') {
532 // Measure the minimum before a '{' to allow
533 // folding on "} else {"
534 if (levelMinCurrent > levelNext) {
535 levelMinCurrent = levelNext;
536 }
537 levelNext++;
538 } else if (ch == '}') {
539 levelNext--;
540 }
541 }
542 if (atEOL || (i == endPos-1)) {
543 if (options.foldComment && options.foldCommentMultiline) { // Handle nested comments
544 int nc;
545 nc = styler.GetLineState(lineCurrent);
546 nc -= lineCurrent>0? styler.GetLineState(lineCurrent-1): 0;
547 levelNext += nc;
548 }
549 int levelUse = levelCurrent;
550 if (options.foldSyntaxBased && foldAtElse) {
551 levelUse = levelMinCurrent;
552 }
553 int lev = levelUse | levelNext << 16;
554 if (visibleChars == 0 && options.foldCompact)
555 lev |= SC_FOLDLEVELWHITEFLAG;
556 if (levelUse < levelNext)
557 lev |= SC_FOLDLEVELHEADERFLAG;
558 if (lev != styler.LevelAt(lineCurrent)) {
559 styler.SetLevel(lineCurrent, lev);
560 }
561 lineCurrent++;
562 levelCurrent = levelNext;
563 levelMinCurrent = levelCurrent;
564 visibleChars = 0;
565 }
566 if (!IsASpace(ch))
567 visibleChars++;
568 }
569 }
570
571 extern const LexerModule lmD(SCLEX_D, LexerD::LexerFactoryD, "d", dWordLists);