LexBullant.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SciTE - Scintilla based Text Editor
  2. // LexBullant.cxx - lexer for Bullant
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdarg.h>
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #include "ILexer.h"
  10. #include "Scintilla.h"
  11. #include "SciLexer.h"
  12. #include "WordList.h"
  13. #include "LexAccessor.h"
  14. #include "Accessor.h"
  15. #include "StyleContext.h"
  16. #include "CharacterSet.h"
  17. #include "LexerModule.h"
  18. #ifdef SCI_NAMESPACE
  19. using namespace Scintilla;
  20. #endif
  21. static int classifyWordBullant(Sci_PositionU start, Sci_PositionU end, WordList &keywords, Accessor &styler) {
  22. char s[100];
  23. s[0] = '\0';
  24. for (Sci_PositionU i = 0; i < end - start + 1 && i < 30; i++) {
  25. s[i] = static_cast<char>(tolower(styler[start + i]));
  26. s[i + 1] = '\0';
  27. }
  28. int lev= 0;
  29. char chAttr = SCE_C_IDENTIFIER;
  30. if (isdigit(s[0]) || (s[0] == '.')){
  31. chAttr = SCE_C_NUMBER;
  32. }
  33. else {
  34. if (keywords.InList(s)) {
  35. chAttr = SCE_C_WORD;
  36. if (strcmp(s, "end") == 0)
  37. lev = -1;
  38. else if (strcmp(s, "method") == 0 ||
  39. strcmp(s, "case") == 0 ||
  40. strcmp(s, "class") == 0 ||
  41. strcmp(s, "debug") == 0 ||
  42. strcmp(s, "test") == 0 ||
  43. strcmp(s, "if") == 0 ||
  44. strcmp(s, "lock") == 0 ||
  45. strcmp(s, "transaction") == 0 ||
  46. strcmp(s, "trap") == 0 ||
  47. strcmp(s, "until") == 0 ||
  48. strcmp(s, "while") == 0)
  49. lev = 1;
  50. }
  51. }
  52. styler.ColourTo(end, chAttr);
  53. return lev;
  54. }
  55. static void ColouriseBullantDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
  56. Accessor &styler) {
  57. WordList &keywords = *keywordlists[0];
  58. styler.StartAt(startPos);
  59. bool fold = styler.GetPropertyInt("fold") != 0;
  60. Sci_Position lineCurrent = styler.GetLine(startPos);
  61. int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
  62. int levelCurrent = levelPrev;
  63. int state = initStyle;
  64. if (state == SCE_C_STRINGEOL) // Does not leak onto next line
  65. state = SCE_C_DEFAULT;
  66. char chPrev = ' ';
  67. char chNext = styler[startPos];
  68. Sci_PositionU lengthDoc = startPos + length;
  69. int visibleChars = 0;
  70. styler.StartSegment(startPos);
  71. int endFoundThisLine = 0;
  72. for (Sci_PositionU i = startPos; i < lengthDoc; i++) {
  73. char ch = chNext;
  74. chNext = styler.SafeGetCharAt(i + 1);
  75. if ((ch == '\r' && chNext != '\n') || (ch == '\n')) {
  76. // Trigger on CR only (Mac style) or either on LF from CR+LF (Dos/Win) or on LF alone (Unix)
  77. // Avoid triggering two times on Dos/Win
  78. // End of line
  79. endFoundThisLine = 0;
  80. if (state == SCE_C_STRINGEOL) {
  81. styler.ColourTo(i, state);
  82. state = SCE_C_DEFAULT;
  83. }
  84. if (fold) {
  85. int lev = levelPrev;
  86. if (visibleChars == 0)
  87. lev |= SC_FOLDLEVELWHITEFLAG;
  88. if ((levelCurrent > levelPrev) && (visibleChars > 0))
  89. lev |= SC_FOLDLEVELHEADERFLAG;
  90. styler.SetLevel(lineCurrent, lev);
  91. lineCurrent++;
  92. levelPrev = levelCurrent;
  93. }
  94. visibleChars = 0;
  95. /* int indentBlock = GetLineIndentation(lineCurrent);
  96. if (blockChange==1){
  97. lineCurrent++;
  98. int pos=SetLineIndentation(lineCurrent, indentBlock + indentSize);
  99. } else if (blockChange==-1) {
  100. indentBlock -= indentSize;
  101. if (indentBlock < 0)
  102. indentBlock = 0;
  103. SetLineIndentation(lineCurrent, indentBlock);
  104. lineCurrent++;
  105. }
  106. blockChange=0;
  107. */ }
  108. if (!(IsASCII(ch) && isspace(ch)))
  109. visibleChars++;
  110. if (styler.IsLeadByte(ch)) {
  111. chNext = styler.SafeGetCharAt(i + 2);
  112. chPrev = ' ';
  113. i += 1;
  114. continue;
  115. }
  116. if (state == SCE_C_DEFAULT) {
  117. if (iswordstart(ch)) {
  118. styler.ColourTo(i-1, state);
  119. state = SCE_C_IDENTIFIER;
  120. } else if (ch == '@' && chNext == 'o') {
  121. if ((styler.SafeGetCharAt(i+2) =='f') && (styler.SafeGetCharAt(i+3) == 'f')) {
  122. styler.ColourTo(i-1, state);
  123. state = SCE_C_COMMENT;
  124. }
  125. } else if (ch == '#') {
  126. styler.ColourTo(i-1, state);
  127. state = SCE_C_COMMENTLINE;
  128. } else if (ch == '\"') {
  129. styler.ColourTo(i-1, state);
  130. state = SCE_C_STRING;
  131. } else if (ch == '\'') {
  132. styler.ColourTo(i-1, state);
  133. state = SCE_C_CHARACTER;
  134. } else if (isoperator(ch)) {
  135. styler.ColourTo(i-1, state);
  136. styler.ColourTo(i, SCE_C_OPERATOR);
  137. }
  138. } else if (state == SCE_C_IDENTIFIER) {
  139. if (!iswordchar(ch)) {
  140. int levelChange = classifyWordBullant(styler.GetStartSegment(), i - 1, keywords, styler);
  141. state = SCE_C_DEFAULT;
  142. chNext = styler.SafeGetCharAt(i + 1);
  143. if (ch == '#') {
  144. state = SCE_C_COMMENTLINE;
  145. } else if (ch == '\"') {
  146. state = SCE_C_STRING;
  147. } else if (ch == '\'') {
  148. state = SCE_C_CHARACTER;
  149. } else if (isoperator(ch)) {
  150. styler.ColourTo(i, SCE_C_OPERATOR);
  151. }
  152. if (endFoundThisLine == 0)
  153. levelCurrent+=levelChange;
  154. if (levelChange == -1)
  155. endFoundThisLine=1;
  156. }
  157. } else if (state == SCE_C_COMMENT) {
  158. if (ch == '@' && chNext == 'o') {
  159. if (styler.SafeGetCharAt(i+2) == 'n') {
  160. styler.ColourTo(i+2, state);
  161. state = SCE_C_DEFAULT;
  162. i+=2;
  163. }
  164. }
  165. } else if (state == SCE_C_COMMENTLINE) {
  166. if (ch == '\r' || ch == '\n') {
  167. endFoundThisLine = 0;
  168. styler.ColourTo(i-1, state);
  169. state = SCE_C_DEFAULT;
  170. }
  171. } else if (state == SCE_C_STRING) {
  172. if (ch == '\\') {
  173. if (chNext == '\"' || chNext == '\'' || chNext == '\\') {
  174. i++;
  175. ch = chNext;
  176. chNext = styler.SafeGetCharAt(i + 1);
  177. }
  178. } else if (ch == '\"') {
  179. styler.ColourTo(i, state);
  180. state = SCE_C_DEFAULT;
  181. } else if (chNext == '\r' || chNext == '\n') {
  182. endFoundThisLine = 0;
  183. styler.ColourTo(i-1, SCE_C_STRINGEOL);
  184. state = SCE_C_STRINGEOL;
  185. }
  186. } else if (state == SCE_C_CHARACTER) {
  187. if ((ch == '\r' || ch == '\n') && (chPrev != '\\')) {
  188. endFoundThisLine = 0;
  189. styler.ColourTo(i-1, SCE_C_STRINGEOL);
  190. state = SCE_C_STRINGEOL;
  191. } else if (ch == '\\') {
  192. if (chNext == '\"' || chNext == '\'' || chNext == '\\') {
  193. i++;
  194. ch = chNext;
  195. chNext = styler.SafeGetCharAt(i + 1);
  196. }
  197. } else if (ch == '\'') {
  198. styler.ColourTo(i, state);
  199. state = SCE_C_DEFAULT;
  200. }
  201. }
  202. chPrev = ch;
  203. }
  204. styler.ColourTo(lengthDoc - 1, state);
  205. // Fill in the real level of the next line, keeping the current flags as they will be filled in later
  206. if (fold) {
  207. int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
  208. //styler.SetLevel(lineCurrent, levelCurrent | flagsNext);
  209. styler.SetLevel(lineCurrent, levelPrev | flagsNext);
  210. }
  211. }
  212. static const char * const bullantWordListDesc[] = {
  213. "Keywords",
  214. 0
  215. };
  216. LexerModule lmBullant(SCLEX_BULLANT, ColouriseBullantDoc, "bullant", 0, bullantWordListDesc);