LexMatlab.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Scintilla source code edit control
  2. /** @file LexMatlab.cxx
  3. ** Lexer for Matlab.
  4. ** Written by José Fonseca
  5. **
  6. ** Changes by Christoph Dalitz 2003/12/04:
  7. ** - added support for Octave
  8. ** - Strings can now be included both in single or double quotes
  9. **
  10. ** Changes by John Donoghue 2012/04/02
  11. ** - added block comment (and nested block comments)
  12. ** - added ... displayed as a comment
  13. ** - removed unused IsAWord functions
  14. ** - added some comments
  15. **
  16. ** Changes by John Donoghue 2014/08/01
  17. ** - fix allowed transpose ' after {} operator
  18. **
  19. ** Changes by John Donoghue 2016/11/15
  20. ** - update matlab code folding
  21. **/
  22. // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
  23. // The License.txt file describes the conditions under which this software may be distributed.
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <stdarg.h>
  28. #include <assert.h>
  29. #include <ctype.h>
  30. #include "ILexer.h"
  31. #include "Scintilla.h"
  32. #include "SciLexer.h"
  33. #include "WordList.h"
  34. #include "LexAccessor.h"
  35. #include "Accessor.h"
  36. #include "StyleContext.h"
  37. #include "CharacterSet.h"
  38. #include "LexerModule.h"
  39. #ifdef SCI_NAMESPACE
  40. using namespace Scintilla;
  41. #endif
  42. static bool IsMatlabCommentChar(int c) {
  43. return (c == '%') ;
  44. }
  45. static bool IsOctaveCommentChar(int c) {
  46. return (c == '%' || c == '#') ;
  47. }
  48. static inline int LowerCase(int c) {
  49. if (c >= 'A' && c <= 'Z')
  50. return 'a' + c - 'A';
  51. return c;
  52. }
  53. static int CheckKeywordFoldPoint(char *str) {
  54. if (strcmp ("if", str) == 0 ||
  55. strcmp ("for", str) == 0 ||
  56. strcmp ("switch", str) == 0 ||
  57. strcmp ("try", str) == 0 ||
  58. strcmp ("do", str) == 0 ||
  59. strcmp ("parfor", str) == 0 ||
  60. strcmp ("function", str) == 0)
  61. return 1;
  62. if (strncmp("end", str, 3) == 0 ||
  63. strcmp("until", str) == 0)
  64. return -1;
  65. return 0;
  66. }
  67. static void ColouriseMatlabOctaveDoc(
  68. Sci_PositionU startPos, Sci_Position length, int initStyle,
  69. WordList *keywordlists[], Accessor &styler,
  70. bool (*IsCommentChar)(int),
  71. bool ismatlab) {
  72. WordList &keywords = *keywordlists[0];
  73. styler.StartAt(startPos);
  74. // boolean for when the ' is allowed to be transpose vs the start/end
  75. // of a string
  76. bool transpose = false;
  77. // approximate position of first non space character in a line
  78. int nonSpaceColumn = -1;
  79. // approximate column position of the current character in a line
  80. int column = 0;
  81. // use the line state of each line to store the block comment depth
  82. Sci_Position curLine = styler.GetLine(startPos);
  83. int commentDepth = curLine > 0 ? styler.GetLineState(curLine-1) : 0;
  84. StyleContext sc(startPos, length, initStyle, styler);
  85. for (; sc.More(); sc.Forward(), column++) {
  86. if(sc.atLineStart) {
  87. // set the line state to the current commentDepth
  88. curLine = styler.GetLine(sc.currentPos);
  89. styler.SetLineState(curLine, commentDepth);
  90. // reset the column to 0, nonSpace to -1 (not set)
  91. column = 0;
  92. nonSpaceColumn = -1;
  93. }
  94. // save the column position of first non space character in a line
  95. if((nonSpaceColumn == -1) && (! IsASpace(sc.ch)))
  96. {
  97. nonSpaceColumn = column;
  98. }
  99. // check for end of states
  100. if (sc.state == SCE_MATLAB_OPERATOR) {
  101. if (sc.chPrev == '.') {
  102. if (sc.ch == '*' || sc.ch == '/' || sc.ch == '\\' || sc.ch == '^') {
  103. sc.ForwardSetState(SCE_MATLAB_DEFAULT);
  104. transpose = false;
  105. } else if (sc.ch == '\'') {
  106. sc.ForwardSetState(SCE_MATLAB_DEFAULT);
  107. transpose = true;
  108. } else if(sc.ch == '.' && sc.chNext == '.') {
  109. // we werent an operator, but a '...'
  110. sc.ChangeState(SCE_MATLAB_COMMENT);
  111. transpose = false;
  112. } else {
  113. sc.SetState(SCE_MATLAB_DEFAULT);
  114. }
  115. } else {
  116. sc.SetState(SCE_MATLAB_DEFAULT);
  117. }
  118. } else if (sc.state == SCE_MATLAB_KEYWORD) {
  119. if (!isalnum(sc.ch) && sc.ch != '_') {
  120. char s[100];
  121. sc.GetCurrentLowered(s, sizeof(s));
  122. if (keywords.InList(s)) {
  123. sc.SetState(SCE_MATLAB_DEFAULT);
  124. transpose = false;
  125. } else {
  126. sc.ChangeState(SCE_MATLAB_IDENTIFIER);
  127. sc.SetState(SCE_MATLAB_DEFAULT);
  128. transpose = true;
  129. }
  130. }
  131. } else if (sc.state == SCE_MATLAB_NUMBER) {
  132. if (!isdigit(sc.ch) && sc.ch != '.'
  133. && !(sc.ch == 'e' || sc.ch == 'E')
  134. && !((sc.ch == '+' || sc.ch == '-') && (sc.chPrev == 'e' || sc.chPrev == 'E'))) {
  135. sc.SetState(SCE_MATLAB_DEFAULT);
  136. transpose = true;
  137. }
  138. } else if (sc.state == SCE_MATLAB_STRING) {
  139. if (sc.ch == '\'') {
  140. if (sc.chNext == '\'') {
  141. sc.Forward();
  142. } else {
  143. sc.ForwardSetState(SCE_MATLAB_DEFAULT);
  144. }
  145. }
  146. } else if (sc.state == SCE_MATLAB_DOUBLEQUOTESTRING) {
  147. if (sc.ch == '\\') {
  148. if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
  149. sc.Forward();
  150. }
  151. } else if (sc.ch == '\"') {
  152. sc.ForwardSetState(SCE_MATLAB_DEFAULT);
  153. }
  154. } else if (sc.state == SCE_MATLAB_COMMAND) {
  155. if (sc.atLineEnd) {
  156. sc.SetState(SCE_MATLAB_DEFAULT);
  157. transpose = false;
  158. }
  159. } else if (sc.state == SCE_MATLAB_COMMENT) {
  160. // end or start of a nested a block comment?
  161. if( IsCommentChar(sc.ch) && sc.chNext == '}' && nonSpaceColumn == column) {
  162. if(commentDepth > 0) commentDepth --;
  163. curLine = styler.GetLine(sc.currentPos);
  164. styler.SetLineState(curLine, commentDepth);
  165. sc.Forward();
  166. if (commentDepth == 0) {
  167. sc.ForwardSetState(SCE_D_DEFAULT);
  168. transpose = false;
  169. }
  170. }
  171. else if( IsCommentChar(sc.ch) && sc.chNext == '{' && nonSpaceColumn == column)
  172. {
  173. commentDepth ++;
  174. curLine = styler.GetLine(sc.currentPos);
  175. styler.SetLineState(curLine, commentDepth);
  176. sc.Forward();
  177. transpose = false;
  178. } else if(commentDepth == 0) {
  179. // single line comment
  180. if (sc.atLineEnd || sc.ch == '\r' || sc.ch == '\n') {
  181. sc.SetState(SCE_MATLAB_DEFAULT);
  182. transpose = false;
  183. }
  184. }
  185. }
  186. // check start of a new state
  187. if (sc.state == SCE_MATLAB_DEFAULT) {
  188. if (IsCommentChar(sc.ch)) {
  189. // ncrement depth if we are a block comment
  190. if(sc.chNext == '{' && nonSpaceColumn == column)
  191. commentDepth ++;
  192. curLine = styler.GetLine(sc.currentPos);
  193. styler.SetLineState(curLine, commentDepth);
  194. sc.SetState(SCE_MATLAB_COMMENT);
  195. } else if (sc.ch == '!' && sc.chNext != '=' ) {
  196. if(ismatlab) {
  197. sc.SetState(SCE_MATLAB_COMMAND);
  198. } else {
  199. sc.SetState(SCE_MATLAB_OPERATOR);
  200. }
  201. } else if (sc.ch == '\'') {
  202. if (transpose) {
  203. sc.SetState(SCE_MATLAB_OPERATOR);
  204. } else {
  205. sc.SetState(SCE_MATLAB_STRING);
  206. }
  207. } else if (sc.ch == '"') {
  208. sc.SetState(SCE_MATLAB_DOUBLEQUOTESTRING);
  209. } else if (isdigit(sc.ch) || (sc.ch == '.' && isdigit(sc.chNext))) {
  210. sc.SetState(SCE_MATLAB_NUMBER);
  211. } else if (isalpha(sc.ch)) {
  212. sc.SetState(SCE_MATLAB_KEYWORD);
  213. } else if (isoperator(static_cast<char>(sc.ch)) || sc.ch == '@' || sc.ch == '\\') {
  214. if (sc.ch == ')' || sc.ch == ']' || sc.ch == '}') {
  215. transpose = true;
  216. } else {
  217. transpose = false;
  218. }
  219. sc.SetState(SCE_MATLAB_OPERATOR);
  220. } else {
  221. transpose = false;
  222. }
  223. }
  224. }
  225. sc.Complete();
  226. }
  227. static void ColouriseMatlabDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  228. WordList *keywordlists[], Accessor &styler) {
  229. ColouriseMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsMatlabCommentChar, true);
  230. }
  231. static void ColouriseOctaveDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  232. WordList *keywordlists[], Accessor &styler) {
  233. ColouriseMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsOctaveCommentChar, false);
  234. }
  235. static void FoldMatlabOctaveDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  236. WordList *[], Accessor &styler,
  237. bool (*IsComment)(int ch)) {
  238. Sci_PositionU endPos = startPos + length;
  239. int visibleChars = 0;
  240. Sci_Position lineCurrent = styler.GetLine(startPos);
  241. int levelCurrent = SC_FOLDLEVELBASE;
  242. if (lineCurrent > 0)
  243. levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
  244. int levelNext = levelCurrent;
  245. char chNext = styler[startPos];
  246. int styleNext = styler.StyleAt(startPos);
  247. int style = initStyle;
  248. char word[100];
  249. int wordlen = 0;
  250. for (Sci_PositionU i = startPos; i < endPos; i++) {
  251. char ch = chNext;
  252. chNext = styler.SafeGetCharAt(i + 1);
  253. style = styleNext;
  254. styleNext = styler.StyleAt(i + 1);
  255. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  256. // a line that starts with a comment
  257. if (style == SCE_MATLAB_COMMENT && IsComment(ch) && visibleChars == 0) {
  258. // start/end of block comment
  259. if (chNext == '{')
  260. levelNext ++;
  261. if (chNext == '}')
  262. levelNext --;
  263. }
  264. // keyword
  265. if(style == SCE_MATLAB_KEYWORD) {
  266. word[wordlen++] = static_cast<char>(LowerCase(ch));
  267. if (wordlen == 100) { // prevent overflow
  268. word[0] = '\0';
  269. wordlen = 1;
  270. }
  271. if (styleNext != SCE_MATLAB_KEYWORD) {
  272. word[wordlen] = '\0';
  273. wordlen = 0;
  274. levelNext += CheckKeywordFoldPoint(word);
  275. }
  276. }
  277. if (!IsASpace(ch))
  278. visibleChars++;
  279. if (atEOL || (i == endPos-1)) {
  280. int levelUse = levelCurrent;
  281. int lev = levelUse | levelNext << 16;
  282. if (visibleChars == 0)
  283. lev |= SC_FOLDLEVELWHITEFLAG;
  284. if (levelUse < levelNext)
  285. lev |= SC_FOLDLEVELHEADERFLAG;
  286. if (lev != styler.LevelAt(lineCurrent)) {
  287. styler.SetLevel(lineCurrent, lev);
  288. }
  289. lineCurrent++;
  290. levelCurrent = levelNext;
  291. if (atEOL && (i == static_cast<Sci_PositionU>(styler.Length() - 1))) {
  292. // There is an empty line at end of file so give it same level and empty
  293. styler.SetLevel(lineCurrent, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG);
  294. }
  295. visibleChars = 0;
  296. }
  297. }
  298. }
  299. static void FoldMatlabDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  300. WordList *keywordlists[], Accessor &styler) {
  301. FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsMatlabCommentChar);
  302. }
  303. static void FoldOctaveDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  304. WordList *keywordlists[], Accessor &styler) {
  305. FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsOctaveCommentChar);
  306. }
  307. static const char * const matlabWordListDesc[] = {
  308. "Keywords",
  309. 0
  310. };
  311. static const char * const octaveWordListDesc[] = {
  312. "Keywords",
  313. 0
  314. };
  315. LexerModule lmMatlab(SCLEX_MATLAB, ColouriseMatlabDoc, "matlab", FoldMatlabDoc, matlabWordListDesc);
  316. LexerModule lmOctave(SCLEX_OCTAVE, ColouriseOctaveDoc, "octave", FoldOctaveDoc, octaveWordListDesc);