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

/Dependencies/wxWidgets/src/stc/scintilla/src/LexNimrod.cxx

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