Debian Codegraph @

scintilla/lexilla/lexers/LexMatlab.cxx

raw
1 // Scintilla source code edit control
2 // Encoding: UTF-8
3 /** @file LexMatlab.cxx
4 ** Lexer for Matlab.
5 ** Written by José Fonseca
6 **
7 ** Changes by Christoph Dalitz 2003/12/04:
8 ** - added support for Octave
9 ** - Strings can now be included both in single or double quotes
10 **
11 ** Changes by John Donoghue 2012/04/02
12 ** - added block comment (and nested block comments)
13 ** - added ... displayed as a comment
14 ** - removed unused IsAWord functions
15 ** - added some comments
16 **
17 ** Changes by John Donoghue 2014/08/01
18 ** - fix allowed transpose ' after {} operator
19 **
20 ** Changes by John Donoghue 2016/11/15
21 ** - update matlab code folding
22 **
23 ** Changes by John Donoghue 2017/01/18
24 ** - update matlab block comment detection
25 **
26 ** Changes by Andrey Smolyakov 2022/04/15
27 ** - add support for "arguments" block and class definition syntax
28 **/
29 // Copyright 1998-2001 by Neil Hodgson <[email protected]>
30 // The License.txt file describes the conditions under which this software may be distributed.
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <assert.h>
37 #include <ctype.h>
38
39 #include <string>
40 #include <string_view>
41
42 #include "ILexer.h"
43 #include "Scintilla.h"
44 #include "SciLexer.h"
45
46 #include "WordList.h"
47 #include "LexAccessor.h"
48 #include "Accessor.h"
49 #include "StyleContext.h"
50 #include "CharacterSet.h"
51 #include "LexerModule.h"
52
53 using namespace Lexilla;
54
55 static bool IsMatlabCommentChar(int c) {
56 return (c == '%') ;
57 }
58
59 static bool IsOctaveCommentChar(int c) {
60 return (c == '%' || c == '#') ;
61 }
62
63 static inline int LowerCase(int c) {
64 if (c >= 'A' && c <= 'Z')
65 return 'a' + c - 'A';
66 return c;
67 }
68
69 static int CheckKeywordFoldPoint(char *str) {
70 if (strcmp ("if", str) == 0 ||
71 strcmp ("for", str) == 0 ||
72 strcmp ("switch", str) == 0 ||
73 strcmp ("while", str) == 0 ||
74 strcmp ("try", str) == 0 ||
75 strcmp ("do", str) == 0 ||
76 strcmp ("parfor", str) == 0 ||
77 strcmp ("classdef", str) == 0 ||
78 strcmp ("spmd", str) == 0 ||
79 strcmp ("arguments", str) == 0 ||
80 strcmp ("methods", str) == 0 ||
81 strcmp ("properties", str) == 0 ||
82 strcmp ("events", str) == 0 ||
83 strcmp ("function", str) == 0)
84 return 1;
85 if (strncmp("end", str, 3) == 0 ||
86 strcmp("until", str) == 0)
87 return -1;
88 return 0;
89 }
90
91 static bool IsSpaceToEOL(Sci_Position startPos, Accessor &styler) {
92 Sci_Position line = styler.GetLine(startPos);
93 Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
94 for (Sci_Position i = startPos; i < eol_pos; i++) {
95 char ch = styler[i];
96 if(!IsASpace(ch)) return false;
97 }
98 return true;
99 }
100
101 #define MATLAB_STATE_FOLD_LVL_OFFSET 8
102 #define MATLAB_STATE_FOLD_LVL_MASK (0xFF00)
103 #define MATLAB_STATE_FLAGS_OFFSET 16
104 #define MATLAB_STATE_COMM_DEPTH_OFFSET 0
105 #define MATLAB_STATE_COMM_DEPTH_MASK (0xFF)
106 #define MATLAB_STATE_EXPECTING_ARG_BLOCK (1 << MATLAB_STATE_FLAGS_OFFSET)
107 #define MATLAB_STATE_IN_CLASS_SCOPE (1 <<(MATLAB_STATE_FLAGS_OFFSET+1))
108 #define MATLAB_STATE_IN_ARGUMENTS_SCOPE (1 <<(MATLAB_STATE_FLAGS_OFFSET+2))
109
110 static int ComposeLineState(int commentDepth,
111 int foldingLevel,
112 int expectingArgumentsBlock,
113 int inClassScope,
114 int inArgumentsScope) {
115
116 return ((commentDepth << MATLAB_STATE_COMM_DEPTH_OFFSET)
117 & MATLAB_STATE_COMM_DEPTH_MASK) |
118 ((foldingLevel << MATLAB_STATE_FOLD_LVL_OFFSET)
119 & MATLAB_STATE_FOLD_LVL_MASK) |
120 (expectingArgumentsBlock
121 & MATLAB_STATE_EXPECTING_ARG_BLOCK) |
122 (inClassScope
123 & MATLAB_STATE_IN_CLASS_SCOPE) |
124 (inArgumentsScope
125 & MATLAB_STATE_IN_ARGUMENTS_SCOPE);
126 }
127
128 static void ColouriseMatlabOctaveDoc(
129 Sci_PositionU startPos, Sci_Position length, int initStyle,
130 WordList *keywordlists[], Accessor &styler,
131 bool (*IsCommentChar)(int),
132 bool ismatlab) {
133
134 WordList &keywords = *keywordlists[0];
135
136 styler.StartAt(startPos);
137
138 // boolean for when the ' is allowed to be transpose vs the start/end
139 // of a string
140 bool transpose = false;
141
142 // count of brackets as boolean for when end could be an operator not a keyword
143 int allow_end_op = 0;
144
145 // approximate position of first non space character in a line
146 int nonSpaceColumn = -1;
147 // approximate column position of the current character in a line
148 int column = 0;
149
150 // This line contains a function declaration
151 bool funcDeclarationLine = false;
152 // We've just seen "function" keyword, so now we may expect the "arguments"
153 // keyword opening the corresponding code block
154 int expectingArgumentsBlock = 0;
155 // We saw "arguments" keyword, but not the closing "end"
156 int inArgumentsScope = 0;
157 // Current line's folding level
158 int foldingLevel = 0;
159 // Current line in in class scope
160 int inClassScope = 0;
161
162 // use the line state of each line to store the block comment depth
163 Sci_Position curLine = styler.GetLine(startPos);
164 int commentDepth = 0;
165 // Restore the previous line's state, if there was such a line
166 if (curLine > 0) {
167 int prevState = styler.GetLineState(curLine-1);
168 commentDepth = (prevState & MATLAB_STATE_COMM_DEPTH_MASK)
169 >> MATLAB_STATE_COMM_DEPTH_OFFSET;
170 foldingLevel = (prevState & MATLAB_STATE_FOLD_LVL_MASK)
171 >> MATLAB_STATE_FOLD_LVL_OFFSET;
172 expectingArgumentsBlock = prevState & MATLAB_STATE_EXPECTING_ARG_BLOCK;
173 inClassScope = prevState & MATLAB_STATE_IN_CLASS_SCOPE;
174 inArgumentsScope = prevState & MATLAB_STATE_IN_ARGUMENTS_SCOPE;
175 }
176
177
178 StyleContext sc(startPos, length, initStyle, styler);
179
180 for (; sc.More(); sc.Forward(), column++) {
181
182 if(sc.atLineStart) {
183 // set the line state to the current commentDepth
184 curLine = styler.GetLine(sc.currentPos);
185 styler.SetLineState(curLine, ComposeLineState(
186 commentDepth, foldingLevel, expectingArgumentsBlock, inClassScope, inArgumentsScope));
187
188 // reset the column to 0, nonSpace to -1 (not set)
189 column = 0;
190 nonSpaceColumn = -1;
191
192 // Reset the flag
193 funcDeclarationLine = false;
194 }
195
196 // Semicolon ends function declaration
197 // This condition is for one line functions support
198 if (sc.chPrev == ';') {
199 funcDeclarationLine = false;
200 }
201
202 // Only comments allowed between the function declaration and the
203 // arguments code block
204 if (expectingArgumentsBlock && !(funcDeclarationLine || inArgumentsScope)) {
205 if ((sc.state != SCE_MATLAB_KEYWORD) &&
206 (sc.state != SCE_MATLAB_COMMENT) &&
207 (sc.state != SCE_MATLAB_DEFAULT) &&
208 !(sc.state == SCE_MATLAB_OPERATOR && sc.chPrev == ';')) {
209 expectingArgumentsBlock = 0;
210 styler.SetLineState(curLine, ComposeLineState(
211 commentDepth, foldingLevel, expectingArgumentsBlock, inClassScope, inArgumentsScope));
212 }
213 }
214
215 // We've just left the class scope
216 if ((foldingLevel ==0) && inClassScope) {
217 inClassScope = 0;
218 }
219
220 // save the column position of first non space character in a line
221 if((nonSpaceColumn == -1) && (! IsASpace(sc.ch))) {
222 nonSpaceColumn = column;
223 }
224
225 // check for end of states
226 if (sc.state == SCE_MATLAB_OPERATOR) {
227 if (sc.chPrev == '.') {
228 if (sc.ch == '*' || sc.ch == '/' || sc.ch == '\\' || sc.ch == '^') {
229 sc.ForwardSetState(SCE_MATLAB_DEFAULT);
230 transpose = false;
231 } else if (sc.ch == '\'') {
232 sc.ForwardSetState(SCE_MATLAB_DEFAULT);
233 transpose = true;
234 } else if(sc.ch == '.' && sc.chNext == '.') {
235 // we werent an operator, but a '...'
236 sc.ChangeState(SCE_MATLAB_COMMENT);
237 transpose = false;
238 } else {
239 sc.SetState(SCE_MATLAB_DEFAULT);
240 }
241 } else {
242 sc.SetState(SCE_MATLAB_DEFAULT);
243 }
244 } else if (sc.state == SCE_MATLAB_KEYWORD) {
245 if (!isalnum(sc.ch) && sc.ch != '_') {
246 char s[100];
247 sc.GetCurrent(s, sizeof(s));
248 bool notKeyword = false;
249 transpose = false;
250
251 if (keywords.InList(s)) {
252
253 expectingArgumentsBlock = (funcDeclarationLine || inArgumentsScope) ? expectingArgumentsBlock : 0;
254
255 if (strcmp ("end", s) == 0 && allow_end_op) {
256 sc.ChangeState(SCE_MATLAB_NUMBER);
257 notKeyword = true;
258 } else if (strcmp("end", s) == 0 && !allow_end_op) {
259 inArgumentsScope = 0;
260 } else if (strcmp("function", s) == 0) {
261 // Need this flag to handle "arguments" block correctly
262 funcDeclarationLine = true;
263 expectingArgumentsBlock = ismatlab ? MATLAB_STATE_EXPECTING_ARG_BLOCK : 0;
264 } else if (strcmp("classdef", s) == 0) {
265 // Need this flag to process "events", "methods" and "properties" blocks
266 inClassScope = MATLAB_STATE_IN_CLASS_SCOPE;
267 }
268 } else {
269 // "arguments" is a keyword here, despite not being in the keywords list
270 if (expectingArgumentsBlock && !(funcDeclarationLine || inArgumentsScope) && (strcmp("arguments", s) == 0)) {
271 // We've entered an "arguments" block
272 inArgumentsScope = MATLAB_STATE_IN_ARGUMENTS_SCOPE;
273 } else {
274 // Found an identifier or a keyword after the function declaration
275 // No need to wait for the arguments block anymore
276 expectingArgumentsBlock = (funcDeclarationLine || inArgumentsScope) ? expectingArgumentsBlock : 0;
277
278 // "properties", "methods" and "events" are not keywords if they're declared
279 // inside some function in methods block
280 // To avoid tracking possible nested functions scopes, lexer considers everything
281 // beyond level 2 of folding to be in a scope of some function declared in the
282 // methods block. It is ok for the valid syntax: classes can only be declared in
283 // a separate file, function - only in methods block. However, in case of the invalid
284 // syntax lexer may erroneously ignore a keyword.
285 if (!((inClassScope) && (foldingLevel <= 2) && (
286 strcmp("properties", s) == 0 ||
287 strcmp("methods", s) == 0 ||
288 strcmp("events", s) == 0 ))) {
289 sc.ChangeState(SCE_MATLAB_IDENTIFIER);
290 transpose = true;
291 notKeyword = true;
292 }
293 }
294 }
295
296 sc.SetState(SCE_MATLAB_DEFAULT);
297 if (!notKeyword) {
298 foldingLevel += CheckKeywordFoldPoint(s);
299 }
300 }
301
302 styler.SetLineState(curLine, ComposeLineState(
303 commentDepth, foldingLevel, expectingArgumentsBlock, inClassScope, inArgumentsScope));
304 } else if (sc.state == SCE_MATLAB_NUMBER) {
305 if (!isdigit(sc.ch) && sc.ch != '.'
306 && !(sc.ch == 'e' || sc.ch == 'E')
307 && !((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E'))
308 && !(((sc.ch == 'x' || sc.ch == 'X') && sc.chPrev == '0') || (sc.ch >= 'a' && sc.ch <= 'f') || (sc.ch >= 'A' && sc.ch <= 'F'))
309 && !(sc.ch == 's' || sc.ch == 'S' || sc.ch == 'u' || sc.ch == 'U')
310 && !(sc.ch == 'i' || sc.ch == 'I' || sc.ch == 'j' || sc.ch == 'J')
311 && !(sc.ch == '_')) {
312 sc.SetState(SCE_MATLAB_DEFAULT);
313 transpose = true;
314 }
315 } else if (sc.state == SCE_MATLAB_STRING) {
316 if (sc.ch == '\'') {
317 if (sc.chNext == '\'') {
318 sc.Forward();
319 } else {
320 sc.ForwardSetState(SCE_MATLAB_DEFAULT);
321 }
322 } else if (sc.MatchLineEnd()) {
323 sc.SetState(SCE_MATLAB_DEFAULT);
324 }
325 } else if (sc.state == SCE_MATLAB_DOUBLEQUOTESTRING) {
326 if (sc.ch == '\\' && !ismatlab) {
327 sc.Forward(); // skip escape sequence, new line and others after backlash
328 } else if (sc.ch == '\"') {
329 sc.ForwardSetState(SCE_MATLAB_DEFAULT);
330 } else if (sc.MatchLineEnd()) {
331 sc.SetState(SCE_MATLAB_DEFAULT);
332 }
333 } else if (sc.state == SCE_MATLAB_COMMAND) {
334 if (sc.atLineEnd) {
335 sc.SetState(SCE_MATLAB_DEFAULT);
336 transpose = false;
337 }
338 } else if (sc.state == SCE_MATLAB_COMMENT) {
339 // end or start of a nested a block comment?
340 if( IsCommentChar(sc.ch) && sc.chNext == '}' && nonSpaceColumn == column && IsSpaceToEOL(sc.currentPos+2, styler)) {
341 if(commentDepth > 0) commentDepth --;
342
343 curLine = styler.GetLine(sc.currentPos);
344 styler.SetLineState(curLine, ComposeLineState(
345 commentDepth, foldingLevel, expectingArgumentsBlock, inClassScope, inArgumentsScope));
346 sc.Forward();
347
348 if (commentDepth == 0) {
349 sc.ForwardSetState(SCE_MATLAB_DEFAULT);
350 transpose = false;
351 }
352 } else if( IsCommentChar(sc.ch) && sc.chNext == '{' && nonSpaceColumn == column && IsSpaceToEOL(sc.currentPos+2, styler)) {
353 commentDepth ++;
354
355 curLine = styler.GetLine(sc.currentPos);
356 styler.SetLineState(curLine, ComposeLineState(
357 commentDepth, foldingLevel, expectingArgumentsBlock, inClassScope, inArgumentsScope));
358 sc.Forward();
359 transpose = false;
360
361 } else if(commentDepth == 0) {
362 // single line comment
363 if (sc.atLineEnd || sc.ch == '\r' || sc.ch == '\n') {
364 sc.SetState(SCE_MATLAB_DEFAULT);
365 transpose = false;
366 }
367 }
368 }
369
370 // check start of a new state
371 if (sc.state == SCE_MATLAB_DEFAULT) {
372 if (IsCommentChar(sc.ch)) {
373 // ncrement depth if we are a block comment
374 if(sc.chNext == '{' && nonSpaceColumn == column) {
375 if(IsSpaceToEOL(sc.currentPos+2, styler)) {
376 commentDepth ++;
377 }
378 }
379 curLine = styler.GetLine(sc.currentPos);
380 styler.SetLineState(curLine, ComposeLineState(
381 commentDepth, foldingLevel, expectingArgumentsBlock, inClassScope, inArgumentsScope));
382 sc.SetState(SCE_MATLAB_COMMENT);
383 } else if (sc.ch == '!' && sc.chNext != '=' ) {
384 if(ismatlab) {
385 sc.SetState(SCE_MATLAB_COMMAND);
386 } else {
387 sc.SetState(SCE_MATLAB_OPERATOR);
388 }
389 } else if (sc.ch == '\'') {
390 if (transpose) {
391 sc.SetState(SCE_MATLAB_OPERATOR);
392 } else {
393 sc.SetState(SCE_MATLAB_STRING);
394 }
395 } else if (sc.ch == '"') {
396 sc.SetState(SCE_MATLAB_DOUBLEQUOTESTRING);
397 } else if (isdigit(sc.ch) || (sc.ch == '.' && isdigit(sc.chNext))) {
398 sc.SetState(SCE_MATLAB_NUMBER);
399 } else if (isalpha(sc.ch)) {
400 sc.SetState(SCE_MATLAB_KEYWORD);
401 } else if (isoperator(static_cast<char>(sc.ch)) || sc.ch == '@' || sc.ch == '\\') {
402 if (sc.ch == '(' || sc.ch == '[' || sc.ch == '{') {
403 allow_end_op ++;
404 } else if ((sc.ch == ')' || sc.ch == ']' || sc.ch == '}') && (allow_end_op > 0)) {
405 allow_end_op --;
406 }
407
408 if (sc.ch == ')' || sc.ch == ']' || sc.ch == '}') {
409 transpose = true;
410 } else {
411 transpose = false;
412 }
413 sc.SetState(SCE_MATLAB_OPERATOR);
414 } else {
415 transpose = false;
416 }
417 }
418 }
419 sc.Complete();
420 }
421
422 static void ColouriseMatlabDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
423 WordList *keywordlists[], Accessor &styler) {
424 ColouriseMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsMatlabCommentChar, true);
425 }
426
427 static void ColouriseOctaveDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
428 WordList *keywordlists[], Accessor &styler) {
429 ColouriseMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsOctaveCommentChar, false);
430 }
431
432 static void FoldMatlabOctaveDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
433 WordList *[], Accessor &styler,
434 bool (*IsComment)(int ch)) {
435
436 if (styler.GetPropertyInt("fold") == 0)
437 return;
438
439 const bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
440 const bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
441
442 Sci_PositionU endPos = startPos + length;
443 int visibleChars = 0;
444 Sci_Position lineCurrent = styler.GetLine(startPos);
445 int levelCurrent = SC_FOLDLEVELBASE;
446 if (lineCurrent > 0)
447 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
448 int levelNext = levelCurrent;
449 char chNext = styler[startPos];
450 int styleNext = styler.StyleAt(startPos);
451 int style = initStyle;
452 char word[100];
453 int wordlen = 0;
454 for (Sci_PositionU i = startPos; i < endPos; i++) {
455 char ch = chNext;
456 chNext = styler.SafeGetCharAt(i + 1);
457 style = styleNext;
458 styleNext = styler.StyleAt(i + 1);
459 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
460
461 // a line that starts with a comment
462 if (foldComment && style == SCE_MATLAB_COMMENT && IsComment(ch) && visibleChars == 0) {
463 // start/end of block comment
464 if (chNext == '{' && IsSpaceToEOL(i+2, styler))
465 levelNext ++;
466 if (chNext == '}' && IsSpaceToEOL(i+2, styler))
467 levelNext --;
468 }
469 // keyword
470 if(style == SCE_MATLAB_KEYWORD) {
471 word[wordlen++] = static_cast<char>(LowerCase(ch));
472 if (wordlen == 100) { // prevent overflow
473 word[0] = '\0';
474 wordlen = 1;
475 }
476 if (styleNext != SCE_MATLAB_KEYWORD) {
477 word[wordlen] = '\0';
478 wordlen = 0;
479
480 levelNext += CheckKeywordFoldPoint(word);
481 }
482 }
483 if (!IsASpace(ch))
484 visibleChars++;
485 if (atEOL || (i == endPos-1)) {
486 int levelUse = levelCurrent;
487 int lev = levelUse | levelNext << 16;
488 if (visibleChars == 0 && foldCompact)
489 lev |= SC_FOLDLEVELWHITEFLAG;
490 if (levelUse < levelNext)
491 lev |= SC_FOLDLEVELHEADERFLAG;
492 if (lev != styler.LevelAt(lineCurrent)) {
493 styler.SetLevel(lineCurrent, lev);
494 }
495 lineCurrent++;
496 levelCurrent = levelNext;
497 if (atEOL && (i == static_cast<Sci_PositionU>(styler.Length() - 1))) {
498 // There is an empty line at end of file so give it same level and empty
499 styler.SetLevel(lineCurrent, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG);
500 }
501 visibleChars = 0;
502 }
503 }
504 }
505
506 static void FoldMatlabDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
507 WordList *keywordlists[], Accessor &styler) {
508 FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsMatlabCommentChar);
509 }
510
511 static void FoldOctaveDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
512 WordList *keywordlists[], Accessor &styler) {
513 FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsOctaveCommentChar);
514 }
515
516 static const char * const matlabWordListDesc[] = {
517 "Keywords",
518 0
519 };
520
521 static const char * const octaveWordListDesc[] = {
522 "Keywords",
523 0
524 };
525
526 extern const LexerModule lmMatlab(SCLEX_MATLAB, ColouriseMatlabDoc, "matlab", FoldMatlabDoc, matlabWordListDesc);
527
528 extern const LexerModule lmOctave(SCLEX_OCTAVE, ColouriseOctaveDoc, "octave", FoldOctaveDoc, octaveWordListDesc);