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 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
27 // The License.txt file describes the conditions under which this software may be distributed.
28 
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <assert.h>
34 #include <ctype.h>
35 
36 #include "ILexer.h"
37 #include "Scintilla.h"
38 #include "SciLexer.h"
39 
40 #include "WordList.h"
41 #include "LexAccessor.h"
42 #include "Accessor.h"
43 #include "StyleContext.h"
44 #include "CharacterSet.h"
45 #include "LexerModule.h"
46 
47 using namespace Scintilla;
48 
IsMatlabCommentChar(int c)49 static bool IsMatlabCommentChar(int c) {
50 	return (c == '%') ;
51 }
52 
IsOctaveCommentChar(int c)53 static bool IsOctaveCommentChar(int c) {
54 	return (c == '%' || c == '#') ;
55 }
56 
LowerCase(int c)57 static inline int LowerCase(int c) {
58 	if (c >= 'A' && c <= 'Z')
59 		return 'a' + c - 'A';
60 	return c;
61 }
62 
CheckKeywordFoldPoint(char * str)63 static int CheckKeywordFoldPoint(char *str) {
64 	if (strcmp ("if", str) == 0 ||
65 		strcmp ("for", str) == 0 ||
66 		strcmp ("switch", str) == 0 ||
67 		strcmp ("while", str) == 0 ||
68 		strcmp ("try", str) == 0 ||
69 		strcmp ("do", str) == 0 ||
70 		strcmp ("parfor", str) == 0 ||
71 		strcmp ("function", str) == 0)
72 		return 1;
73 	if (strncmp("end", str, 3) == 0 ||
74 		strcmp("until", str) == 0)
75 		return -1;
76 	return 0;
77 }
78 
IsSpaceToEOL(Sci_Position startPos,Accessor & styler)79 static bool IsSpaceToEOL(Sci_Position startPos, Accessor &styler) {
80 	Sci_Position line = styler.GetLine(startPos);
81 	Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
82 	for (Sci_Position i = startPos; i < eol_pos; i++) {
83 		char ch = styler[i];
84 		if(!IsASpace(ch)) return false;
85 	}
86 	return true;
87 }
88 
ColouriseMatlabOctaveDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * keywordlists[],Accessor & styler,bool (* IsCommentChar)(int),bool ismatlab)89 static void ColouriseMatlabOctaveDoc(
90             Sci_PositionU startPos, Sci_Position length, int initStyle,
91             WordList *keywordlists[], Accessor &styler,
92             bool (*IsCommentChar)(int),
93             bool ismatlab) {
94 
95 	WordList &keywords = *keywordlists[0];
96 
97 	styler.StartAt(startPos);
98 
99 	// boolean for when the ' is allowed to be transpose vs the start/end
100 	// of a string
101 	bool transpose = false;
102 
103 	// count of brackets as boolean for when end could be an operator not a keyword
104 	int allow_end_op = 0;
105 
106 	// approximate position of first non space character in a line
107 	int nonSpaceColumn = -1;
108 	// approximate column position of the current character in a line
109 	int column = 0;
110 
111         // use the line state of each line to store the block comment depth
112 	Sci_Position curLine = styler.GetLine(startPos);
113         int commentDepth = curLine > 0 ? styler.GetLineState(curLine-1) : 0;
114 
115 
116 	StyleContext sc(startPos, length, initStyle, styler);
117 
118 	for (; sc.More(); sc.Forward(), column++) {
119 
120                	if(sc.atLineStart) {
121 			// set the line state to the current commentDepth
122 			curLine = styler.GetLine(sc.currentPos);
123                         styler.SetLineState(curLine, commentDepth);
124 
125 			// reset the column to 0, nonSpace to -1 (not set)
126 			column = 0;
127 			nonSpaceColumn = -1;
128 		}
129 
130 		// save the column position of first non space character in a line
131 		if((nonSpaceColumn == -1) && (! IsASpace(sc.ch)))
132 		{
133 			nonSpaceColumn = column;
134 		}
135 
136 		// check for end of states
137 		if (sc.state == SCE_MATLAB_OPERATOR) {
138 			if (sc.chPrev == '.') {
139 				if (sc.ch == '*' || sc.ch == '/' || sc.ch == '\\' || sc.ch == '^') {
140 					sc.ForwardSetState(SCE_MATLAB_DEFAULT);
141 					transpose = false;
142 				} else if (sc.ch == '\'') {
143 					sc.ForwardSetState(SCE_MATLAB_DEFAULT);
144 					transpose = true;
145                                 } else if(sc.ch == '.' && sc.chNext == '.') {
146                                         // we werent an operator, but a '...'
147                                         sc.ChangeState(SCE_MATLAB_COMMENT);
148                                         transpose = false;
149 				} else {
150 					sc.SetState(SCE_MATLAB_DEFAULT);
151 				}
152 			} else {
153 				sc.SetState(SCE_MATLAB_DEFAULT);
154 			}
155 		} else if (sc.state == SCE_MATLAB_KEYWORD) {
156 			if (!isalnum(sc.ch) && sc.ch != '_') {
157 				char s[100];
158 				sc.GetCurrent(s, sizeof(s));
159 
160 				if (keywords.InList(s)) {
161 					if (strcmp ("end", s) == 0 && allow_end_op) {
162 						sc.ChangeState(SCE_MATLAB_NUMBER);
163 					}
164 					sc.SetState(SCE_MATLAB_DEFAULT);
165 					transpose = false;
166 				} else {
167 					sc.ChangeState(SCE_MATLAB_IDENTIFIER);
168 					sc.SetState(SCE_MATLAB_DEFAULT);
169 					transpose = true;
170 				}
171 			}
172 		} else if (sc.state == SCE_MATLAB_NUMBER) {
173 			if (!isdigit(sc.ch) && sc.ch != '.'
174 			        && !(sc.ch == 'e' || sc.ch == 'E')
175 			        && !((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E'))) {
176 				sc.SetState(SCE_MATLAB_DEFAULT);
177 				transpose = true;
178 			}
179 		} else if (sc.state == SCE_MATLAB_STRING) {
180 			if (sc.ch == '\'') {
181 				if (sc.chNext == '\'') {
182  					sc.Forward();
183 				} else {
184 					sc.ForwardSetState(SCE_MATLAB_DEFAULT);
185  				}
186 			}
187 		} else if (sc.state == SCE_MATLAB_DOUBLEQUOTESTRING) {
188 			if (sc.ch == '\\') {
189 				if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
190 					sc.Forward();
191 				}
192 			} else if (sc.ch == '\"') {
193 				sc.ForwardSetState(SCE_MATLAB_DEFAULT);
194 			}
195 		} else if (sc.state == SCE_MATLAB_COMMAND) {
196 			if (sc.atLineEnd) {
197 				sc.SetState(SCE_MATLAB_DEFAULT);
198 				transpose = false;
199 			}
200 		} else if (sc.state == SCE_MATLAB_COMMENT) {
201 			// end or start of a nested a block comment?
202 			if( IsCommentChar(sc.ch) && sc.chNext == '}' && nonSpaceColumn == column && IsSpaceToEOL(sc.currentPos+2, styler)) {
203                            	if(commentDepth > 0) commentDepth --;
204 
205 				curLine = styler.GetLine(sc.currentPos);
206 				styler.SetLineState(curLine, commentDepth);
207 				sc.Forward();
208 
209 				if (commentDepth == 0) {
210 					sc.ForwardSetState(SCE_D_DEFAULT);
211 					transpose = false;
212 				}
213                         }
214                         else if( IsCommentChar(sc.ch) && sc.chNext == '{' && nonSpaceColumn == column && IsSpaceToEOL(sc.currentPos+2, styler))
215                         {
216  				commentDepth ++;
217 
218 				curLine = styler.GetLine(sc.currentPos);
219 				styler.SetLineState(curLine, commentDepth);
220 				sc.Forward();
221 				transpose = false;
222 
223                         } else if(commentDepth == 0) {
224 				// single line comment
225 				if (sc.atLineEnd || sc.ch == '\r' || sc.ch == '\n') {
226 					sc.SetState(SCE_MATLAB_DEFAULT);
227 					transpose = false;
228 				}
229 			}
230 		}
231 
232 		// check start of a new state
233 		if (sc.state == SCE_MATLAB_DEFAULT) {
234 			if (IsCommentChar(sc.ch)) {
235 				// ncrement depth if we are a block comment
236 				if(sc.chNext == '{' && nonSpaceColumn == column) {
237 					if(IsSpaceToEOL(sc.currentPos+2, styler)) {
238 						commentDepth ++;
239 					}
240 				}
241 				curLine = styler.GetLine(sc.currentPos);
242 				styler.SetLineState(curLine, commentDepth);
243 				sc.SetState(SCE_MATLAB_COMMENT);
244 			} else if (sc.ch == '!' && sc.chNext != '=' ) {
245 				if(ismatlab) {
246 					sc.SetState(SCE_MATLAB_COMMAND);
247 				} else {
248 					sc.SetState(SCE_MATLAB_OPERATOR);
249 				}
250 			} else if (sc.ch == '\'') {
251 				if (transpose) {
252 					sc.SetState(SCE_MATLAB_OPERATOR);
253 				} else {
254 					sc.SetState(SCE_MATLAB_STRING);
255 				}
256 			} else if (sc.ch == '"') {
257 				sc.SetState(SCE_MATLAB_DOUBLEQUOTESTRING);
258 			} else if (isdigit(sc.ch) || (sc.ch == '.' && isdigit(sc.chNext))) {
259 				sc.SetState(SCE_MATLAB_NUMBER);
260 			} else if (isalpha(sc.ch)) {
261 				sc.SetState(SCE_MATLAB_KEYWORD);
262 			} else if (isoperator(static_cast<char>(sc.ch)) || sc.ch == '@' || sc.ch == '\\') {
263 				if (sc.ch == '(' || sc.ch == '[' || sc.ch == '{') {
264 					allow_end_op ++;
265 				} else if ((sc.ch == ')' || sc.ch == ']' || sc.ch == '}') && (allow_end_op > 0)) {
266 					allow_end_op --;
267 				}
268 
269 				if (sc.ch == ')' || sc.ch == ']' || sc.ch == '}') {
270 					transpose = true;
271 				} else {
272 					transpose = false;
273 				}
274 				sc.SetState(SCE_MATLAB_OPERATOR);
275 			} else {
276 				transpose = false;
277 			}
278 		}
279 	}
280 	sc.Complete();
281 }
282 
ColouriseMatlabDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * keywordlists[],Accessor & styler)283 static void ColouriseMatlabDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
284                                WordList *keywordlists[], Accessor &styler) {
285 	ColouriseMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsMatlabCommentChar, true);
286 }
287 
ColouriseOctaveDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * keywordlists[],Accessor & styler)288 static void ColouriseOctaveDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
289                                WordList *keywordlists[], Accessor &styler) {
290 	ColouriseMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsOctaveCommentChar, false);
291 }
292 
FoldMatlabOctaveDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * [],Accessor & styler,bool (* IsComment)(int ch))293 static void FoldMatlabOctaveDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
294                                 WordList *[], Accessor &styler,
295                                 bool (*IsComment)(int ch)) {
296 
297 	if (styler.GetPropertyInt("fold") == 0)
298 		return;
299 
300 	const bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
301 	const bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
302 
303 	Sci_PositionU endPos = startPos + length;
304 	int visibleChars = 0;
305 	Sci_Position lineCurrent = styler.GetLine(startPos);
306 	int levelCurrent = SC_FOLDLEVELBASE;
307 	if (lineCurrent > 0)
308 		levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
309 	int levelNext = levelCurrent;
310 	char chNext = styler[startPos];
311 	int styleNext = styler.StyleAt(startPos);
312 	int style = initStyle;
313 	char word[100];
314 	int wordlen = 0;
315 	for (Sci_PositionU i = startPos; i < endPos; i++) {
316 		char ch = chNext;
317 		chNext = styler.SafeGetCharAt(i + 1);
318 		style = styleNext;
319 		styleNext = styler.StyleAt(i + 1);
320 		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
321 
322 		// a line that starts with a comment
323 		if (foldComment && style == SCE_MATLAB_COMMENT && IsComment(ch) && visibleChars == 0) {
324 			// start/end of block comment
325 			if (chNext == '{' && IsSpaceToEOL(i+2, styler))
326 				levelNext ++;
327 			if (chNext == '}' && IsSpaceToEOL(i+2, styler))
328 				levelNext --;
329 		}
330 		// keyword
331 		if(style == SCE_MATLAB_KEYWORD) {
332 			word[wordlen++] = static_cast<char>(LowerCase(ch));
333 			if (wordlen == 100) {  // prevent overflow
334 				word[0] = '\0';
335 				wordlen = 1;
336 			}
337 			if (styleNext !=  SCE_MATLAB_KEYWORD) {
338 				word[wordlen] = '\0';
339 				wordlen = 0;
340 
341 				levelNext += CheckKeywordFoldPoint(word);
342  			}
343 		}
344 		if (!IsASpace(ch))
345 			visibleChars++;
346 		if (atEOL || (i == endPos-1)) {
347 			int levelUse = levelCurrent;
348 			int lev = levelUse | levelNext << 16;
349 			if (visibleChars == 0 && foldCompact)
350 				lev |= SC_FOLDLEVELWHITEFLAG;
351 			if (levelUse < levelNext)
352 				lev |= SC_FOLDLEVELHEADERFLAG;
353 			if (lev != styler.LevelAt(lineCurrent)) {
354 				styler.SetLevel(lineCurrent, lev);
355 			}
356 			lineCurrent++;
357 			levelCurrent = levelNext;
358 			if (atEOL && (i == static_cast<Sci_PositionU>(styler.Length() - 1))) {
359 				// There is an empty line at end of file so give it same level and empty
360 				styler.SetLevel(lineCurrent, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG);
361 			}
362 			visibleChars = 0;
363 		}
364 	}
365 }
366 
FoldMatlabDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * keywordlists[],Accessor & styler)367 static void FoldMatlabDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
368                           WordList *keywordlists[], Accessor &styler) {
369 	FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsMatlabCommentChar);
370 }
371 
FoldOctaveDoc(Sci_PositionU startPos,Sci_Position length,int initStyle,WordList * keywordlists[],Accessor & styler)372 static void FoldOctaveDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
373                           WordList *keywordlists[], Accessor &styler) {
374 	FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsOctaveCommentChar);
375 }
376 
377 static const char * const matlabWordListDesc[] = {
378 	"Keywords",
379 	0
380 };
381 
382 static const char * const octaveWordListDesc[] = {
383 	"Keywords",
384 	0
385 };
386 
387 LexerModule lmMatlab(SCLEX_MATLAB, ColouriseMatlabDoc, "matlab", FoldMatlabDoc, matlabWordListDesc);
388 
389 LexerModule lmOctave(SCLEX_OCTAVE, ColouriseOctaveDoc, "octave", FoldOctaveDoc, octaveWordListDesc);
390