Debian Codegraph @

scintilla/lexilla/lexers/LexCoffeeScript.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexCoffeeScript.cxx
3 ** Lexer for CoffeeScript.
4 **/
5 // Copyright 1998-2011 by Neil Hodgson <[email protected]>
6 // Based on the Scintilla C++ Lexer
7 // Written by Eric Promislow <[email protected]> in 2011 for the Komodo IDE
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
20 #include <algorithm>
21
22 #include "ILexer.h"
23 #include "Scintilla.h"
24 #include "SciLexer.h"
25
26 #include "WordList.h"
27 #include "LexAccessor.h"
28 #include "Accessor.h"
29 #include "StyleContext.h"
30 #include "CharacterSet.h"
31 #include "LexerModule.h"
32
33 using namespace Lexilla;
34
35 static bool IsSpaceEquiv(int state) {
36 return (state == SCE_COFFEESCRIPT_DEFAULT
37 || state == SCE_COFFEESCRIPT_COMMENTLINE
38 || state == SCE_COFFEESCRIPT_COMMENTBLOCK
39 || state == SCE_COFFEESCRIPT_VERBOSE_REGEX
40 || state == SCE_COFFEESCRIPT_VERBOSE_REGEX_COMMENT
41 || state == SCE_COFFEESCRIPT_WORD
42 || state == SCE_COFFEESCRIPT_REGEX);
43 }
44
45 // Store the current lexer state and brace count prior to starting a new
46 // `#{}` interpolation level.
47 // Based on LexRuby.cxx.
48 static void enterInnerExpression(int *p_inner_string_types,
49 int *p_inner_expn_brace_counts,
50 int& inner_string_count,
51 int state,
52 int& brace_counts
53 ) {
54 p_inner_string_types[inner_string_count] = state;
55 p_inner_expn_brace_counts[inner_string_count] = brace_counts;
56 brace_counts = 0;
57 ++inner_string_count;
58 }
59
60 // Restore the lexer state and brace count for the previous `#{}` interpolation
61 // level upon returning to it.
62 // Note the previous lexer state is the return value and needs to be restored
63 // manually by the StyleContext.
64 // Based on LexRuby.cxx.
65 static int exitInnerExpression(int *p_inner_string_types,
66 int *p_inner_expn_brace_counts,
67 int& inner_string_count,
68 int& brace_counts
69 ) {
70 --inner_string_count;
71 brace_counts = p_inner_expn_brace_counts[inner_string_count];
72 return p_inner_string_types[inner_string_count];
73 }
74
75 // Preconditions: sc.currentPos points to a character after '+' or '-'.
76 // The test for pos reaching 0 should be redundant,
77 // and is in only for safety measures.
78 // Limitation: this code will give the incorrect answer for code like
79 // a = b+++/ptn/...
80 // Putting a space between the '++' post-inc operator and the '+' binary op
81 // fixes this, and is highly recommended for readability anyway.
82 static bool FollowsPostfixOperator(StyleContext &sc, Accessor &styler) {
83 Sci_Position pos = (Sci_Position) sc.currentPos;
84 while (--pos > 0) {
85 char ch = styler[pos];
86 if (ch == '+' || ch == '-') {
87 return styler[pos - 1] == ch;
88 }
89 }
90 return false;
91 }
92
93 static bool followsKeyword(StyleContext &sc, Accessor &styler) {
94 Sci_Position pos = (Sci_Position) sc.currentPos;
95 Sci_Position currentLine = styler.GetLine(pos);
96 Sci_Position lineStartPos = styler.LineStart(currentLine);
97 while (--pos > lineStartPos) {
98 char ch = styler.SafeGetCharAt(pos);
99 if (ch != ' ' && ch != '\t') {
100 break;
101 }
102 }
103 styler.Flush();
104 return styler.StyleAt(pos) == SCE_COFFEESCRIPT_WORD;
105 }
106
107 #if defined(__clang__)
108 #if __has_warning("-Wunused-but-set-variable")
109 // Disable warning for visibleChars
110 #pragma clang diagnostic ignored "-Wunused-but-set-variable"
111 #endif
112 #endif
113
114 static void ColouriseCoffeeScriptDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
115 Accessor &styler) {
116
117 WordList &keywords = *keywordlists[0];
118 WordList &keywords2 = *keywordlists[1];
119 WordList &keywords4 = *keywordlists[3];
120
121 CharacterSet setOKBeforeRE(CharacterSet::setNone, "([{=,:;!%^&*|?~+-");
122 CharacterSet setCouldBePostOp(CharacterSet::setNone, "+-");
123
124 CharacterSet setWordStart(CharacterSet::setAlpha, "_$@", 0x80, true);
125 CharacterSet setWord(CharacterSet::setAlphaNum, "._$", 0x80, true);
126
127 int chPrevNonWhite = ' ';
128 int visibleChars = 0;
129
130 // String/Regex interpolation variables, based on LexRuby.cxx.
131 // In most cases a value of 2 should be ample for the code the user is
132 // likely to enter. For example,
133 // "Filling the #{container} with #{liquid}..."
134 // from the CoffeeScript homepage nests to a level of 2
135 // If the user actually hits a 6th occurrence of '#{' in a double-quoted
136 // string (including regexes), it will stay as a string. The problem with
137 // this is that quotes might flip, a 7th '#{' will look like a comment,
138 // and code-folding might be wrong.
139 #define INNER_STRINGS_MAX_COUNT 5
140 // These vars track our instances of "...#{,,,'..#{,,,}...',,,}..."
141 int inner_string_types[INNER_STRINGS_MAX_COUNT];
142 // Track # braces when we push a new #{ thing
143 int inner_expn_brace_counts[INNER_STRINGS_MAX_COUNT];
144 int inner_string_count = 0;
145 int brace_counts = 0; // Number of #{ ... } things within an expression
146 for (int i = 0; i < INNER_STRINGS_MAX_COUNT; i++) {
147 inner_string_types[i] = 0;
148 inner_expn_brace_counts[i] = 0;
149 }
150
151 // look back to set chPrevNonWhite properly for better regex colouring
152 Sci_Position endPos = startPos + length;
153 if (startPos > 0 && IsSpaceEquiv(initStyle)) {
154 Sci_PositionU back = startPos;
155 styler.Flush();
156 while (back > 0 && IsSpaceEquiv(styler.StyleAt(--back)))
157 ;
158 if (styler.StyleAt(back) == SCE_COFFEESCRIPT_OPERATOR) {
159 chPrevNonWhite = styler.SafeGetCharAt(back);
160 }
161 if (startPos != back) {
162 initStyle = styler.StyleAt(back);
163 if (IsSpaceEquiv(initStyle)) {
164 initStyle = SCE_COFFEESCRIPT_DEFAULT;
165 }
166 }
167 startPos = back;
168 }
169
170 StyleContext sc(startPos, endPos - startPos, initStyle, styler);
171
172 for (; sc.More();) {
173
174 if (sc.atLineStart) {
175 // Reset states to beginning of colourise so no surprises
176 // if different sets of lines lexed.
177 visibleChars = 0;
178 }
179
180 // Determine if the current state should terminate.
181 switch (sc.state) {
182 case SCE_COFFEESCRIPT_OPERATOR:
183 sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
184 break;
185 case SCE_COFFEESCRIPT_NUMBER:
186 // We accept almost anything because of hex. and number suffixes
187 if (!setWord.Contains(sc.ch) || sc.Match('.', '.')) {
188 sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
189 }
190 break;
191 case SCE_COFFEESCRIPT_IDENTIFIER:
192 if (!setWord.Contains(sc.ch) || (sc.ch == '.') || (sc.ch == '$')) {
193 char s[1000];
194 sc.GetCurrent(s, sizeof(s));
195 if (keywords.InList(s)) {
196 sc.ChangeState(SCE_COFFEESCRIPT_WORD);
197 } else if (keywords2.InList(s)) {
198 sc.ChangeState(SCE_COFFEESCRIPT_WORD2);
199 } else if (keywords4.InList(s)) {
200 sc.ChangeState(SCE_COFFEESCRIPT_GLOBALCLASS);
201 } else if (sc.LengthCurrent() > 0 && s[0] == '@') {
202 sc.ChangeState(SCE_COFFEESCRIPT_INSTANCEPROPERTY);
203 }
204 sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
205 }
206 break;
207 case SCE_COFFEESCRIPT_WORD:
208 case SCE_COFFEESCRIPT_WORD2:
209 case SCE_COFFEESCRIPT_GLOBALCLASS:
210 case SCE_COFFEESCRIPT_INSTANCEPROPERTY:
211 if (!setWord.Contains(sc.ch)) {
212 sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
213 }
214 break;
215 case SCE_COFFEESCRIPT_COMMENTLINE:
216 if (sc.atLineStart) {
217 sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
218 }
219 break;
220 case SCE_COFFEESCRIPT_STRING:
221 if (sc.ch == '\\') {
222 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
223 sc.Forward();
224 }
225 } else if (sc.ch == '\"') {
226 sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
227 } else if (sc.ch == '#' && sc.chNext == '{' && inner_string_count < INNER_STRINGS_MAX_COUNT) {
228 // process interpolated code #{ ... }
229 enterInnerExpression(inner_string_types,
230 inner_expn_brace_counts,
231 inner_string_count,
232 sc.state,
233 brace_counts);
234 sc.SetState(SCE_COFFEESCRIPT_OPERATOR);
235 sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
236 }
237 break;
238 case SCE_COFFEESCRIPT_CHARACTER:
239 if (sc.ch == '\\') {
240 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
241 sc.Forward();
242 }
243 } else if (sc.ch == '\'') {
244 sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
245 }
246 break;
247 case SCE_COFFEESCRIPT_REGEX:
248 if (sc.atLineStart) {
249 sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
250 } else if (sc.ch == '/') {
251 sc.Forward();
252 while ((sc.ch < 0x80) && islower(sc.ch))
253 sc.Forward(); // gobble regex flags
254 sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
255 } else if (sc.ch == '\\') {
256 // Gobble up the quoted character
257 if (sc.chNext == '\\' || sc.chNext == '/') {
258 sc.Forward();
259 }
260 }
261 break;
262 case SCE_COFFEESCRIPT_STRINGEOL:
263 if (sc.atLineStart) {
264 sc.SetState(SCE_COFFEESCRIPT_DEFAULT);
265 }
266 break;
267 case SCE_COFFEESCRIPT_COMMENTBLOCK:
268 if (sc.Match("###")) {
269 sc.Forward();
270 sc.Forward();
271 sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
272 } else if (sc.ch == '\\') {
273 sc.Forward();
274 }
275 break;
276 case SCE_COFFEESCRIPT_VERBOSE_REGEX:
277 if (sc.Match("///")) {
278 sc.Forward();
279 sc.Forward();
280 sc.ForwardSetState(SCE_COFFEESCRIPT_DEFAULT);
281 } else if (sc.Match('#')) {
282 sc.SetState(SCE_COFFEESCRIPT_VERBOSE_REGEX_COMMENT);
283 } else if (sc.ch == '\\') {
284 sc.Forward();
285 }
286 break;
287 case SCE_COFFEESCRIPT_VERBOSE_REGEX_COMMENT:
288 if (sc.atLineStart) {
289 sc.SetState(SCE_COFFEESCRIPT_VERBOSE_REGEX);
290 }
291 break;
292 }
293
294 // Determine if a new state should be entered.
295 if (sc.state == SCE_COFFEESCRIPT_DEFAULT) {
296 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
297 sc.SetState(SCE_COFFEESCRIPT_NUMBER);
298 } else if (setWordStart.Contains(sc.ch)) {
299 sc.SetState(SCE_COFFEESCRIPT_IDENTIFIER);
300 } else if (sc.Match("///")) {
301 sc.SetState(SCE_COFFEESCRIPT_VERBOSE_REGEX);
302 sc.Forward();
303 sc.Forward();
304 } else if (sc.ch == '/'
305 && (setOKBeforeRE.Contains(chPrevNonWhite)
306 || followsKeyword(sc, styler))
307 && (!setCouldBePostOp.Contains(chPrevNonWhite)
308 || !FollowsPostfixOperator(sc, styler))) {
309 sc.SetState(SCE_COFFEESCRIPT_REGEX); // JavaScript's RegEx
310 } else if (sc.ch == '\"') {
311 sc.SetState(SCE_COFFEESCRIPT_STRING);
312 } else if (sc.ch == '\'') {
313 sc.SetState(SCE_COFFEESCRIPT_CHARACTER);
314 } else if (sc.ch == '#') {
315 if (sc.Match("###")) {
316 sc.SetState(SCE_COFFEESCRIPT_COMMENTBLOCK);
317 sc.Forward();
318 sc.Forward();
319 } else {
320 sc.SetState(SCE_COFFEESCRIPT_COMMENTLINE);
321 }
322 } else if (isoperator(static_cast<char>(sc.ch))) {
323 sc.SetState(SCE_COFFEESCRIPT_OPERATOR);
324 // Handle '..' and '...' operators correctly.
325 if (sc.ch == '.') {
326 for (int i = 0; i < 2 && sc.chNext == '.'; i++, sc.Forward()) ;
327 } else if (sc.ch == '{') {
328 ++brace_counts;
329 } else if (sc.ch == '}' && --brace_counts <= 0 && inner_string_count > 0) {
330 // Return to previous state before #{ ... }
331 sc.ForwardSetState(exitInnerExpression(inner_string_types,
332 inner_expn_brace_counts,
333 inner_string_count,
334 brace_counts));
335 continue; // skip sc.Forward() at loop end
336 }
337 }
338 }
339
340 if (!IsASpace(sc.ch) && !IsSpaceEquiv(sc.state)) {
341 chPrevNonWhite = sc.ch;
342 visibleChars++;
343 }
344 sc.Forward();
345 }
346 sc.Complete();
347 }
348
349 static bool IsCommentLine(Sci_Position line, Accessor &styler) {
350 Sci_Position pos = styler.LineStart(line);
351 Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
352 for (Sci_Position i = pos; i < eol_pos; i++) {
353 char ch = styler[i];
354 if (ch == '#')
355 return true;
356 else if (ch != ' ' && ch != '\t')
357 return false;
358 }
359 return false;
360 }
361
362 static void FoldCoffeeScriptDoc(Sci_PositionU startPos, Sci_Position length, int,
363 WordList *[], Accessor &styler) {
364 // A simplified version of FoldPyDoc
365 const Sci_Position maxPos = startPos + length;
366 const Sci_Position maxLines = styler.GetLine(maxPos - 1); // Requested last line
367 const Sci_Position docLines = styler.GetLine(styler.Length() - 1); // Available last line
368
369 // property fold.coffeescript.comment
370 // Set to 1 to allow folding of comment blocks in CoffeeScript.
371 const bool foldComment = styler.GetPropertyInt("fold.coffeescript.comment") != 0;
372
373 const bool foldCompact = styler.GetPropertyInt("fold.compact") != 0;
374
375 // Backtrack to previous non-blank line so we can determine indent level
376 // for any white space lines
377 // and so we can fix any preceding fold level (which is why we go back
378 // at least one line in all cases)
379 int spaceFlags = 0;
380 Sci_Position lineCurrent = styler.GetLine(startPos);
381 int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
382 while (lineCurrent > 0) {
383 lineCurrent--;
384 indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
385 if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)
386 && !IsCommentLine(lineCurrent, styler))
387 break;
388 }
389 int indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
390
391 // Set up initial loop state
392 int prevComment = 0;
393 if (lineCurrent >= 1)
394 prevComment = foldComment && IsCommentLine(lineCurrent - 1, styler);
395
396 // Process all characters to end of requested range
397 // or comment that hangs over the end of the range. Cap processing in all cases
398 // to end of document (in case of comment at end).
399 while ((lineCurrent <= docLines) && ((lineCurrent <= maxLines) || prevComment)) {
400
401 // Gather info
402 int lev = indentCurrent;
403 Sci_Position lineNext = lineCurrent + 1;
404 int indentNext = indentCurrent;
405 if (lineNext <= docLines) {
406 // Information about next line is only available if not at end of document
407 indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
408 }
409 const int comment = foldComment && IsCommentLine(lineCurrent, styler);
410 const int comment_start = (comment && !prevComment && (lineNext <= docLines) &&
411 IsCommentLine(lineNext, styler) && (lev > SC_FOLDLEVELBASE));
412 const int comment_continue = (comment && prevComment);
413 if (!comment)
414 indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
415 if (indentNext & SC_FOLDLEVELWHITEFLAG)
416 indentNext = SC_FOLDLEVELWHITEFLAG | indentCurrentLevel;
417
418 if (comment_start) {
419 // Place fold point at start of a block of comments
420 lev |= SC_FOLDLEVELHEADERFLAG;
421 } else if (comment_continue) {
422 // Add level to rest of lines in the block
423 lev = lev + 1;
424 }
425
426 // Skip past any blank lines for next indent level info; we skip also
427 // comments (all comments, not just those starting in column 0)
428 // which effectively folds them into surrounding code rather
429 // than screwing up folding.
430
431 while ((lineNext < docLines) &&
432 ((indentNext & SC_FOLDLEVELWHITEFLAG) ||
433 (lineNext <= docLines && IsCommentLine(lineNext, styler)))) {
434
435 lineNext++;
436 indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
437 }
438
439 const int levelAfterComments = indentNext & SC_FOLDLEVELNUMBERMASK;
440 const int levelBeforeComments = std::max(indentCurrentLevel,levelAfterComments);
441
442 // Now set all the indent levels on the lines we skipped
443 // Do this from end to start. Once we encounter one line
444 // which is indented more than the line after the end of
445 // the comment-block, use the level of the block before
446
447 Sci_Position skipLine = lineNext;
448 int skipLevel = levelAfterComments;
449
450 while (--skipLine > lineCurrent) {
451 int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, NULL);
452
453 if (foldCompact) {
454 if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
455 skipLevel = levelBeforeComments;
456
457 int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
458
459 styler.SetLevel(skipLine, skipLevel | whiteFlag);
460 } else {
461 if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments &&
462 !(skipLineIndent & SC_FOLDLEVELWHITEFLAG) &&
463 !IsCommentLine(skipLine, styler))
464 skipLevel = levelBeforeComments;
465
466 styler.SetLevel(skipLine, skipLevel);
467 }
468 }
469
470 // Set fold header on non-comment line
471 if (!comment && !(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
472 if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK))
473 lev |= SC_FOLDLEVELHEADERFLAG;
474 }
475
476 // Keep track of block comment state of previous line
477 prevComment = comment_start || comment_continue;
478
479 // Set fold level for this line and move to next line
480 styler.SetLevel(lineCurrent, lev);
481 indentCurrent = indentNext;
482 lineCurrent = lineNext;
483 }
484 }
485
486 static const char *const csWordLists[] = {
487 "Keywords",
488 "Secondary keywords",
489 "Unused",
490 "Global classes",
491 0,
492 };
493
494 extern const LexerModule lmCoffeeScript(SCLEX_COFFEESCRIPT, ColouriseCoffeeScriptDoc, "coffeescript", FoldCoffeeScriptDoc, csWordLists);