LexEiffel.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Scintilla source code edit control
  2. /** @file LexEiffel.cxx
  3. ** Lexer for Eiffel.
  4. **/
  5. // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
  6. // The License.txt file describes the conditions under which this software may be distributed.
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <assert.h>
  12. #include <ctype.h>
  13. #include "ILexer.h"
  14. #include "Scintilla.h"
  15. #include "SciLexer.h"
  16. #include "WordList.h"
  17. #include "LexAccessor.h"
  18. #include "Accessor.h"
  19. #include "StyleContext.h"
  20. #include "CharacterSet.h"
  21. #include "LexerModule.h"
  22. #ifdef SCI_NAMESPACE
  23. using namespace Scintilla;
  24. #endif
  25. static inline bool isEiffelOperator(unsigned int ch) {
  26. // '.' left out as it is used to make up numbers
  27. return ch == '*' || ch == '/' || ch == '\\' || ch == '-' || ch == '+' ||
  28. ch == '(' || ch == ')' || ch == '=' ||
  29. ch == '{' || ch == '}' || ch == '~' ||
  30. ch == '[' || ch == ']' || ch == ';' ||
  31. ch == '<' || ch == '>' || ch == ',' ||
  32. ch == '.' || ch == '^' || ch == '%' || ch == ':' ||
  33. ch == '!' || ch == '@' || ch == '?';
  34. }
  35. static inline bool IsAWordChar(unsigned int ch) {
  36. return (ch < 0x80) && (isalnum(ch) || ch == '_');
  37. }
  38. static inline bool IsAWordStart(unsigned int ch) {
  39. return (ch < 0x80) && (isalnum(ch) || ch == '_');
  40. }
  41. static void ColouriseEiffelDoc(Sci_PositionU startPos,
  42. Sci_Position length,
  43. int initStyle,
  44. WordList *keywordlists[],
  45. Accessor &styler) {
  46. WordList &keywords = *keywordlists[0];
  47. StyleContext sc(startPos, length, initStyle, styler);
  48. for (; sc.More(); sc.Forward()) {
  49. if (sc.state == SCE_EIFFEL_STRINGEOL) {
  50. if (sc.ch != '\r' && sc.ch != '\n') {
  51. sc.SetState(SCE_EIFFEL_DEFAULT);
  52. }
  53. } else if (sc.state == SCE_EIFFEL_OPERATOR) {
  54. sc.SetState(SCE_EIFFEL_DEFAULT);
  55. } else if (sc.state == SCE_EIFFEL_WORD) {
  56. if (!IsAWordChar(sc.ch)) {
  57. char s[100];
  58. sc.GetCurrentLowered(s, sizeof(s));
  59. if (!keywords.InList(s)) {
  60. sc.ChangeState(SCE_EIFFEL_IDENTIFIER);
  61. }
  62. sc.SetState(SCE_EIFFEL_DEFAULT);
  63. }
  64. } else if (sc.state == SCE_EIFFEL_NUMBER) {
  65. if (!IsAWordChar(sc.ch)) {
  66. sc.SetState(SCE_EIFFEL_DEFAULT);
  67. }
  68. } else if (sc.state == SCE_EIFFEL_COMMENTLINE) {
  69. if (sc.ch == '\r' || sc.ch == '\n') {
  70. sc.SetState(SCE_EIFFEL_DEFAULT);
  71. }
  72. } else if (sc.state == SCE_EIFFEL_STRING) {
  73. if (sc.ch == '%') {
  74. sc.Forward();
  75. } else if (sc.ch == '\"') {
  76. sc.Forward();
  77. sc.SetState(SCE_EIFFEL_DEFAULT);
  78. }
  79. } else if (sc.state == SCE_EIFFEL_CHARACTER) {
  80. if (sc.ch == '\r' || sc.ch == '\n') {
  81. sc.SetState(SCE_EIFFEL_STRINGEOL);
  82. } else if (sc.ch == '%') {
  83. sc.Forward();
  84. } else if (sc.ch == '\'') {
  85. sc.Forward();
  86. sc.SetState(SCE_EIFFEL_DEFAULT);
  87. }
  88. }
  89. if (sc.state == SCE_EIFFEL_DEFAULT) {
  90. if (sc.ch == '-' && sc.chNext == '-') {
  91. sc.SetState(SCE_EIFFEL_COMMENTLINE);
  92. } else if (sc.ch == '\"') {
  93. sc.SetState(SCE_EIFFEL_STRING);
  94. } else if (sc.ch == '\'') {
  95. sc.SetState(SCE_EIFFEL_CHARACTER);
  96. } else if (IsADigit(sc.ch) || (sc.ch == '.')) {
  97. sc.SetState(SCE_EIFFEL_NUMBER);
  98. } else if (IsAWordStart(sc.ch)) {
  99. sc.SetState(SCE_EIFFEL_WORD);
  100. } else if (isEiffelOperator(sc.ch)) {
  101. sc.SetState(SCE_EIFFEL_OPERATOR);
  102. }
  103. }
  104. }
  105. sc.Complete();
  106. }
  107. static bool IsEiffelComment(Accessor &styler, Sci_Position pos, Sci_Position len) {
  108. return len>1 && styler[pos]=='-' && styler[pos+1]=='-';
  109. }
  110. static void FoldEiffelDocIndent(Sci_PositionU startPos, Sci_Position length, int,
  111. WordList *[], Accessor &styler) {
  112. Sci_Position lengthDoc = startPos + length;
  113. // Backtrack to previous line in case need to fix its fold status
  114. Sci_Position lineCurrent = styler.GetLine(startPos);
  115. if (startPos > 0) {
  116. if (lineCurrent > 0) {
  117. lineCurrent--;
  118. startPos = styler.LineStart(lineCurrent);
  119. }
  120. }
  121. int spaceFlags = 0;
  122. int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsEiffelComment);
  123. char chNext = styler[startPos];
  124. for (Sci_Position i = startPos; i < lengthDoc; i++) {
  125. char ch = chNext;
  126. chNext = styler.SafeGetCharAt(i + 1);
  127. if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == lengthDoc)) {
  128. int lev = indentCurrent;
  129. int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsEiffelComment);
  130. if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
  131. // Only non whitespace lines can be headers
  132. if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
  133. lev |= SC_FOLDLEVELHEADERFLAG;
  134. } else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
  135. // Line after is blank so check the next - maybe should continue further?
  136. int spaceFlags2 = 0;
  137. int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsEiffelComment);
  138. if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
  139. lev |= SC_FOLDLEVELHEADERFLAG;
  140. }
  141. }
  142. }
  143. indentCurrent = indentNext;
  144. styler.SetLevel(lineCurrent, lev);
  145. lineCurrent++;
  146. }
  147. }
  148. }
  149. static void FoldEiffelDocKeyWords(Sci_PositionU startPos, Sci_Position length, int /* initStyle */, WordList *[],
  150. Accessor &styler) {
  151. Sci_PositionU lengthDoc = startPos + length;
  152. int visibleChars = 0;
  153. Sci_Position lineCurrent = styler.GetLine(startPos);
  154. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  155. int levelCurrent = levelPrev;
  156. char chNext = styler[startPos];
  157. int stylePrev = 0;
  158. int styleNext = styler.StyleAt(startPos);
  159. // lastDeferred should be determined by looking back to last keyword in case
  160. // the "deferred" is on a line before "class"
  161. bool lastDeferred = false;
  162. for (Sci_PositionU i = startPos; i < lengthDoc; i++) {
  163. char ch = chNext;
  164. chNext = styler.SafeGetCharAt(i + 1);
  165. int style = styleNext;
  166. styleNext = styler.StyleAt(i + 1);
  167. bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
  168. if ((stylePrev != SCE_EIFFEL_WORD) && (style == SCE_EIFFEL_WORD)) {
  169. char s[20];
  170. Sci_PositionU j = 0;
  171. while ((j < (sizeof(s) - 1)) && (iswordchar(styler[i + j]))) {
  172. s[j] = styler[i + j];
  173. j++;
  174. }
  175. s[j] = '\0';
  176. if (
  177. (strcmp(s, "check") == 0) ||
  178. (strcmp(s, "debug") == 0) ||
  179. (strcmp(s, "deferred") == 0) ||
  180. (strcmp(s, "do") == 0) ||
  181. (strcmp(s, "from") == 0) ||
  182. (strcmp(s, "if") == 0) ||
  183. (strcmp(s, "inspect") == 0) ||
  184. (strcmp(s, "once") == 0)
  185. )
  186. levelCurrent++;
  187. if (!lastDeferred && (strcmp(s, "class") == 0))
  188. levelCurrent++;
  189. if (strcmp(s, "end") == 0)
  190. levelCurrent--;
  191. lastDeferred = strcmp(s, "deferred") == 0;
  192. }
  193. if (atEOL) {
  194. int lev = levelPrev;
  195. if (visibleChars == 0)
  196. lev |= SC_FOLDLEVELWHITEFLAG;
  197. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  198. lev |= SC_FOLDLEVELHEADERFLAG;
  199. if (lev != styler.LevelAt(lineCurrent)) {
  200. styler.SetLevel(lineCurrent, lev);
  201. }
  202. lineCurrent++;
  203. levelPrev = levelCurrent;
  204. visibleChars = 0;
  205. }
  206. if (!isspacechar(ch))
  207. visibleChars++;
  208. stylePrev = style;
  209. }
  210. // Fill in the real level of the next line, keeping the current flags as they will be filled in later
  211. int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
  212. styler.SetLevel(lineCurrent, levelPrev | flagsNext);
  213. }
  214. static const char * const eiffelWordListDesc[] = {
  215. "Keywords",
  216. 0
  217. };
  218. LexerModule lmEiffel(SCLEX_EIFFEL, ColouriseEiffelDoc, "eiffel", FoldEiffelDocIndent, eiffelWordListDesc);
  219. LexerModule lmEiffelkw(SCLEX_EIFFELKW, ColouriseEiffelDoc, "eiffelkw", FoldEiffelDocKeyWords, eiffelWordListDesc);