PageRenderTime 43ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/scintilla/lexers/LexNimrod.cxx

https://gitlab.com/cfuji/tortoisegit
C++ | 433 lines | 415 code | 9 blank | 9 comment | 22 complexity | fe3f3bbba7fc573a8e5cc39abe9d6c6f MD5 | raw file
  1. // Scintilla source code edit control
  2. // Nimrod lexer
  3. // (c) 2009 Andreas Rumpf
  4. /** @file LexNimrod.cxx
  5. ** Lexer for Nimrod.
  6. **/
  7. // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
  8. // The License.txt file describes the conditions under which this software may be distributed.
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #include <assert.h>
  14. #include <ctype.h>
  15. #include "ILexer.h"
  16. #include "Scintilla.h"
  17. #include "SciLexer.h"
  18. #include "WordList.h"
  19. #include "LexAccessor.h"
  20. #include "Accessor.h"
  21. #include "StyleContext.h"
  22. #include "CharacterSet.h"
  23. #include "LexerModule.h"
  24. #ifdef SCI_NAMESPACE
  25. using namespace Scintilla;
  26. #endif
  27. static inline bool IsAWordChar(int ch) {
  28. return (ch >= 0x80) || isalnum(ch) || ch == '_';
  29. }
  30. static Sci_Position tillEndOfTripleQuote(Accessor &styler, Sci_Position pos, Sci_Position max) {
  31. /* search for """ */
  32. for (;;) {
  33. if (styler.SafeGetCharAt(pos, '\0') == '\0') return pos;
  34. if (pos >= max) return pos;
  35. if (styler.Match(pos, "\"\"\"")) {
  36. return pos + 2;
  37. }
  38. pos++;
  39. }
  40. }
  41. #define CR 13 /* use both because Scite allows changing the line ending */
  42. #define LF 10
  43. static bool inline isNewLine(int ch) {
  44. return ch == CR || ch == LF;
  45. }
  46. static Sci_Position scanString(Accessor &styler, Sci_Position pos, Sci_Position max, bool rawMode) {
  47. for (;;) {
  48. if (pos >= max) return pos;
  49. char ch = styler.SafeGetCharAt(pos, '\0');
  50. if (ch == CR || ch == LF || ch == '\0') return pos;
  51. if (ch == '"') return pos;
  52. if (ch == '\\' && !rawMode) {
  53. pos += 2;
  54. } else {
  55. pos++;
  56. }
  57. }
  58. }
  59. static Sci_Position scanChar(Accessor &styler, Sci_Position pos, Sci_Position max) {
  60. for (;;) {
  61. if (pos >= max) return pos;
  62. char ch = styler.SafeGetCharAt(pos, '\0');
  63. if (ch == CR || ch == LF || ch == '\0') return pos;
  64. if (ch == '\'' && !isalnum(styler.SafeGetCharAt(pos+1, '\0')) )
  65. return pos;
  66. if (ch == '\\') {
  67. pos += 2;
  68. } else {
  69. pos++;
  70. }
  71. }
  72. }
  73. static Sci_Position scanIdent(Accessor &styler, Sci_Position pos, WordList &keywords) {
  74. char buf[100]; /* copy to lowercase and ignore underscores */
  75. Sci_Position i = 0;
  76. for (;;) {
  77. char ch = styler.SafeGetCharAt(pos, '\0');
  78. if (!IsAWordChar(ch)) break;
  79. if (ch != '_' && i < ((int)sizeof(buf))-1) {
  80. buf[i] = static_cast<char>(tolower(ch));
  81. i++;
  82. }
  83. pos++;
  84. }
  85. buf[i] = '\0';
  86. /* look for keyword */
  87. if (keywords.InList(buf)) {
  88. styler.ColourTo(pos-1, SCE_P_WORD);
  89. } else {
  90. styler.ColourTo(pos-1, SCE_P_IDENTIFIER);
  91. }
  92. return pos;
  93. }
  94. static Sci_Position scanNumber(Accessor &styler, Sci_Position pos) {
  95. char ch, ch2;
  96. ch = styler.SafeGetCharAt(pos, '\0');
  97. ch2 = styler.SafeGetCharAt(pos+1, '\0');
  98. if (ch == '0' && (ch2 == 'b' || ch2 == 'B')) {
  99. /* binary number: */
  100. pos += 2;
  101. for (;;) {
  102. ch = styler.SafeGetCharAt(pos, '\0');
  103. if (ch == '_' || (ch >= '0' && ch <= '1')) ++pos;
  104. else break;
  105. }
  106. } else if (ch == '0' &&
  107. (ch2 == 'o' || ch2 == 'O' || ch2 == 'c' || ch2 == 'C')) {
  108. /* octal number: */
  109. pos += 2;
  110. for (;;) {
  111. ch = styler.SafeGetCharAt(pos, '\0');
  112. if (ch == '_' || (ch >= '0' && ch <= '7')) ++pos;
  113. else break;
  114. }
  115. } else if (ch == '0' && (ch2 == 'x' || ch2 == 'X')) {
  116. /* hexadecimal number: */
  117. pos += 2;
  118. for (;;) {
  119. ch = styler.SafeGetCharAt(pos, '\0');
  120. if (ch == '_' || (ch >= '0' && ch <= '9')
  121. || (ch >= 'a' && ch <= 'f')
  122. || (ch >= 'A' && ch <= 'F')) ++pos;
  123. else break;
  124. }
  125. } else {
  126. // skip decimal part:
  127. for (;;) {
  128. ch = styler.SafeGetCharAt(pos, '\0');
  129. if (ch == '_' || (ch >= '0' && ch <= '9')) ++pos;
  130. else break;
  131. }
  132. ch2 = styler.SafeGetCharAt(pos+1, '\0');
  133. if (ch == '.' && ch2 >= '0' && ch2 <= '9') {
  134. ++pos; // skip '.'
  135. for (;;) {
  136. ch = styler.SafeGetCharAt(pos, '\0');
  137. if (ch == '_' || (ch >= '0' && ch <= '9')) ++pos;
  138. else break;
  139. }
  140. }
  141. if (ch == 'e' || ch == 'E') {
  142. ++pos;
  143. ch = styler.SafeGetCharAt(pos, '\0');
  144. if (ch == '-' || ch == '+') ++pos;
  145. for (;;) {
  146. ch = styler.SafeGetCharAt(pos, '\0');
  147. if (ch == '_' || (ch >= '0' && ch <= '9')) ++pos;
  148. else break;
  149. }
  150. }
  151. }
  152. if (ch == '\'') {
  153. /* a type suffix: */
  154. pos++;
  155. for (;;) {
  156. ch = styler.SafeGetCharAt(pos);
  157. if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z')
  158. || (ch >= 'a' && ch <= 'z') || ch == '_') ++pos;
  159. else break;
  160. }
  161. }
  162. styler.ColourTo(pos-1, SCE_P_NUMBER);
  163. return pos;
  164. }
  165. /* rewritten from scratch, because I couldn't get rid of the bugs...
  166. (A character based approach sucks!)
  167. */
  168. static void ColouriseNimrodDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  169. WordList *keywordlists[], Accessor &styler) {
  170. Sci_Position pos = startPos;
  171. Sci_Position max = startPos + length;
  172. char ch;
  173. WordList &keywords = *keywordlists[0];
  174. styler.StartAt(startPos);
  175. styler.StartSegment(startPos);
  176. switch (initStyle) {
  177. /* check where we are: */
  178. case SCE_P_TRIPLEDOUBLE:
  179. pos = tillEndOfTripleQuote(styler, pos, max);
  180. styler.ColourTo(pos, SCE_P_TRIPLEDOUBLE);
  181. pos++;
  182. break;
  183. default: /* nothing to do: */
  184. break;
  185. }
  186. while (pos < max) {
  187. ch = styler.SafeGetCharAt(pos, '\0');
  188. switch (ch) {
  189. case '\0': return;
  190. case '#': {
  191. bool doccomment = (styler.SafeGetCharAt(pos+1) == '#');
  192. while (pos < max && !isNewLine(styler.SafeGetCharAt(pos, LF))) pos++;
  193. if (doccomment)
  194. styler.ColourTo(pos, SCE_C_COMMENTLINEDOC);
  195. else
  196. styler.ColourTo(pos, SCE_P_COMMENTLINE);
  197. } break;
  198. case 'r': case 'R': {
  199. if (styler.SafeGetCharAt(pos+1) == '"') {
  200. pos = scanString(styler, pos+2, max, true);
  201. styler.ColourTo(pos, SCE_P_STRING);
  202. pos++;
  203. } else {
  204. pos = scanIdent(styler, pos, keywords);
  205. }
  206. } break;
  207. case '"':
  208. if (styler.Match(pos+1, "\"\"")) {
  209. pos = tillEndOfTripleQuote(styler, pos+3, max);
  210. styler.ColourTo(pos, SCE_P_TRIPLEDOUBLE);
  211. } else {
  212. pos = scanString(styler, pos+1, max, false);
  213. styler.ColourTo(pos, SCE_P_STRING);
  214. }
  215. pos++;
  216. break;
  217. case '\'':
  218. pos = scanChar(styler, pos+1, max);
  219. styler.ColourTo(pos, SCE_P_CHARACTER);
  220. pos++;
  221. break;
  222. default: // identifers, numbers, operators, whitespace
  223. if (ch >= '0' && ch <= '9') {
  224. pos = scanNumber(styler, pos);
  225. } else if (IsAWordChar(ch)) {
  226. pos = scanIdent(styler, pos, keywords);
  227. } else if (ch == '`') {
  228. pos++;
  229. while (pos < max) {
  230. ch = styler.SafeGetCharAt(pos, LF);
  231. if (ch == '`') {
  232. ++pos;
  233. break;
  234. }
  235. if (ch == CR || ch == LF) break;
  236. ++pos;
  237. }
  238. styler.ColourTo(pos, SCE_P_IDENTIFIER);
  239. } else if (strchr("()[]{}:=;-\\/&%$!+<>|^?,.*~@", ch)) {
  240. styler.ColourTo(pos, SCE_P_OPERATOR);
  241. pos++;
  242. } else {
  243. styler.ColourTo(pos, SCE_P_DEFAULT);
  244. pos++;
  245. }
  246. break;
  247. }
  248. }
  249. }
  250. static bool IsCommentLine(Sci_Position line, Accessor &styler) {
  251. Sci_Position pos = styler.LineStart(line);
  252. Sci_Position eol_pos = styler.LineStart(line + 1) - 1;
  253. for (Sci_Position i = pos; i < eol_pos; i++) {
  254. char ch = styler[i];
  255. if (ch == '#')
  256. return true;
  257. else if (ch != ' ' && ch != '\t')
  258. return false;
  259. }
  260. return false;
  261. }
  262. static bool IsQuoteLine(Sci_Position line, Accessor &styler) {
  263. int style = styler.StyleAt(styler.LineStart(line)) & 31;
  264. return ((style == SCE_P_TRIPLE) || (style == SCE_P_TRIPLEDOUBLE));
  265. }
  266. static void FoldNimrodDoc(Sci_PositionU startPos, Sci_Position length,
  267. int /*initStyle - unused*/,
  268. WordList *[], Accessor &styler) {
  269. const Sci_Position maxPos = startPos + length;
  270. const Sci_Position maxLines = styler.GetLine(maxPos - 1); // Requested last line
  271. const Sci_Position docLines = styler.GetLine(styler.Length() - 1); // Available last line
  272. const bool foldComment = styler.GetPropertyInt("fold.comment.nimrod") != 0;
  273. const bool foldQuotes = styler.GetPropertyInt("fold.quotes.nimrod") != 0;
  274. // Backtrack to previous non-blank line so we can determine indent level
  275. // for any white space lines (needed esp. within triple quoted strings)
  276. // and so we can fix any preceding fold level (which is why we go back
  277. // at least one line in all cases)
  278. int spaceFlags = 0;
  279. Sci_Position lineCurrent = styler.GetLine(startPos);
  280. int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
  281. while (lineCurrent > 0) {
  282. lineCurrent--;
  283. indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, NULL);
  284. if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG) &&
  285. (!IsCommentLine(lineCurrent, styler)) &&
  286. (!IsQuoteLine(lineCurrent, styler)))
  287. break;
  288. }
  289. int indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
  290. // Set up initial loop state
  291. startPos = styler.LineStart(lineCurrent);
  292. int prev_state = SCE_P_DEFAULT & 31;
  293. if (lineCurrent >= 1)
  294. prev_state = styler.StyleAt(startPos - 1) & 31;
  295. int prevQuote = foldQuotes && ((prev_state == SCE_P_TRIPLE) ||
  296. (prev_state == SCE_P_TRIPLEDOUBLE));
  297. int prevComment = 0;
  298. if (lineCurrent >= 1)
  299. prevComment = foldComment && IsCommentLine(lineCurrent - 1, styler);
  300. // Process all characters to end of requested range or end of any triple quote
  301. // or comment that hangs over the end of the range. Cap processing in all cases
  302. // to end of document (in case of unclosed quote or comment at end).
  303. while ((lineCurrent <= docLines) && ((lineCurrent <= maxLines) ||
  304. prevQuote || prevComment)) {
  305. // Gather info
  306. int lev = indentCurrent;
  307. Sci_Position lineNext = lineCurrent + 1;
  308. int indentNext = indentCurrent;
  309. int quote = false;
  310. if (lineNext <= docLines) {
  311. // Information about next line is only available if not at end of document
  312. indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
  313. int style = styler.StyleAt(styler.LineStart(lineNext)) & 31;
  314. quote = foldQuotes && ((style == SCE_P_TRIPLE) || (style == SCE_P_TRIPLEDOUBLE));
  315. }
  316. const int quote_start = (quote && !prevQuote);
  317. const int quote_continue = (quote && prevQuote);
  318. const int comment = foldComment && IsCommentLine(lineCurrent, styler);
  319. const int comment_start = (comment && !prevComment && (lineNext <= docLines) &&
  320. IsCommentLine(lineNext, styler) &&
  321. (lev > SC_FOLDLEVELBASE));
  322. const int comment_continue = (comment && prevComment);
  323. if ((!quote || !prevQuote) && !comment)
  324. indentCurrentLevel = indentCurrent & SC_FOLDLEVELNUMBERMASK;
  325. if (quote)
  326. indentNext = indentCurrentLevel;
  327. if (indentNext & SC_FOLDLEVELWHITEFLAG)
  328. indentNext = SC_FOLDLEVELWHITEFLAG | indentCurrentLevel;
  329. if (quote_start) {
  330. // Place fold point at start of triple quoted string
  331. lev |= SC_FOLDLEVELHEADERFLAG;
  332. } else if (quote_continue || prevQuote) {
  333. // Add level to rest of lines in the string
  334. lev = lev + 1;
  335. } else if (comment_start) {
  336. // Place fold point at start of a block of comments
  337. lev |= SC_FOLDLEVELHEADERFLAG;
  338. } else if (comment_continue) {
  339. // Add level to rest of lines in the block
  340. lev = lev + 1;
  341. }
  342. // Skip past any blank lines for next indent level info; we skip also
  343. // comments (all comments, not just those starting in column 0)
  344. // which effectively folds them into surrounding code rather
  345. // than screwing up folding.
  346. while (!quote &&
  347. (lineNext < docLines) &&
  348. ((indentNext & SC_FOLDLEVELWHITEFLAG) ||
  349. (lineNext <= docLines && IsCommentLine(lineNext, styler)))) {
  350. lineNext++;
  351. indentNext = styler.IndentAmount(lineNext, &spaceFlags, NULL);
  352. }
  353. const int levelAfterComments = indentNext & SC_FOLDLEVELNUMBERMASK;
  354. const int levelBeforeComments =
  355. Maximum(indentCurrentLevel,levelAfterComments);
  356. // Now set all the indent levels on the lines we skipped
  357. // Do this from end to start. Once we encounter one line
  358. // which is indented more than the line after the end of
  359. // the comment-block, use the level of the block before
  360. Sci_Position skipLine = lineNext;
  361. int skipLevel = levelAfterComments;
  362. while (--skipLine > lineCurrent) {
  363. int skipLineIndent = styler.IndentAmount(skipLine, &spaceFlags, NULL);
  364. if ((skipLineIndent & SC_FOLDLEVELNUMBERMASK) > levelAfterComments)
  365. skipLevel = levelBeforeComments;
  366. int whiteFlag = skipLineIndent & SC_FOLDLEVELWHITEFLAG;
  367. styler.SetLevel(skipLine, skipLevel | whiteFlag);
  368. }
  369. // Set fold header on non-quote/non-comment line
  370. if (!quote && !comment && !(indentCurrent & SC_FOLDLEVELWHITEFLAG) ) {
  371. if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) <
  372. (indentNext & SC_FOLDLEVELNUMBERMASK))
  373. lev |= SC_FOLDLEVELHEADERFLAG;
  374. }
  375. // Keep track of triple quote and block comment state of previous line
  376. prevQuote = quote;
  377. prevComment = comment_start || comment_continue;
  378. // Set fold level for this line and move to next line
  379. styler.SetLevel(lineCurrent, lev);
  380. indentCurrent = indentNext;
  381. lineCurrent = lineNext;
  382. }
  383. // NOTE: Cannot set level of last line here because indentCurrent doesn't have
  384. // header flag set; the loop above is crafted to take care of this case!
  385. //styler.SetLevel(lineCurrent, indentCurrent);
  386. }
  387. static const char * const nimrodWordListDesc[] = {
  388. "Keywords",
  389. 0
  390. };
  391. LexerModule lmNimrod(SCLEX_NIMROD, ColouriseNimrodDoc, "nimrod", FoldNimrodDoc,
  392. nimrodWordListDesc);