LexVB.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // Scintilla source code edit control
  2. /** @file LexVB.cxx
  3. ** Lexer for Visual Basic and VBScript.
  4. **/
  5. // Copyright 1998-2005 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. // Internal state, highlighted as number
  26. #define SCE_B_FILENUMBER SCE_B_DEFAULT+100
  27. static bool IsVBComment(Accessor &styler, Sci_Position pos, Sci_Position len) {
  28. return len > 0 && styler[pos] == '\'';
  29. }
  30. static inline bool IsTypeCharacter(int ch) {
  31. return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$';
  32. }
  33. // Extended to accept accented characters
  34. static inline bool IsAWordChar(int ch) {
  35. return ch >= 0x80 ||
  36. (isalnum(ch) || ch == '.' || ch == '_');
  37. }
  38. static inline bool IsAWordStart(int ch) {
  39. return ch >= 0x80 ||
  40. (isalpha(ch) || ch == '_');
  41. }
  42. static inline bool IsANumberChar(int ch) {
  43. // Not exactly following number definition (several dots are seen as OK, etc.)
  44. // but probably enough in most cases.
  45. return (ch < 0x80) &&
  46. (isdigit(ch) || toupper(ch) == 'E' ||
  47. ch == '.' || ch == '-' || ch == '+');
  48. }
  49. static void ColouriseVBDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  50. WordList *keywordlists[], Accessor &styler, bool vbScriptSyntax) {
  51. WordList &keywords = *keywordlists[0];
  52. WordList &keywords2 = *keywordlists[1];
  53. WordList &keywords3 = *keywordlists[2];
  54. WordList &keywords4 = *keywordlists[3];
  55. styler.StartAt(startPos);
  56. int visibleChars = 0;
  57. int fileNbDigits = 0;
  58. // Do not leak onto next line
  59. if (initStyle == SCE_B_STRINGEOL || initStyle == SCE_B_COMMENT || initStyle == SCE_B_PREPROCESSOR) {
  60. initStyle = SCE_B_DEFAULT;
  61. }
  62. StyleContext sc(startPos, length, initStyle, styler);
  63. for (; sc.More(); sc.Forward()) {
  64. if (sc.state == SCE_B_OPERATOR) {
  65. sc.SetState(SCE_B_DEFAULT);
  66. } else if (sc.state == SCE_B_IDENTIFIER) {
  67. if (!IsAWordChar(sc.ch)) {
  68. // In Basic (except VBScript), a variable name or a function name
  69. // can end with a special character indicating the type of the value
  70. // held or returned.
  71. bool skipType = false;
  72. if (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
  73. sc.Forward(); // Skip it
  74. skipType = true;
  75. }
  76. if (sc.ch == ']') {
  77. sc.Forward();
  78. }
  79. char s[100];
  80. sc.GetCurrentLowered(s, sizeof(s));
  81. if (skipType) {
  82. s[strlen(s) - 1] = '\0';
  83. }
  84. if (strcmp(s, "rem") == 0) {
  85. sc.ChangeState(SCE_B_COMMENT);
  86. } else {
  87. if (keywords.InList(s)) {
  88. sc.ChangeState(SCE_B_KEYWORD);
  89. } else if (keywords2.InList(s)) {
  90. sc.ChangeState(SCE_B_KEYWORD2);
  91. } else if (keywords3.InList(s)) {
  92. sc.ChangeState(SCE_B_KEYWORD3);
  93. } else if (keywords4.InList(s)) {
  94. sc.ChangeState(SCE_B_KEYWORD4);
  95. } // Else, it is really an identifier...
  96. sc.SetState(SCE_B_DEFAULT);
  97. }
  98. }
  99. } else if (sc.state == SCE_B_NUMBER) {
  100. // We stop the number definition on non-numerical non-dot non-eE non-sign char
  101. // Also accepts A-F for hex. numbers
  102. if (!IsANumberChar(sc.ch) && !(tolower(sc.ch) >= 'a' && tolower(sc.ch) <= 'f')) {
  103. sc.SetState(SCE_B_DEFAULT);
  104. }
  105. } else if (sc.state == SCE_B_STRING) {
  106. // VB doubles quotes to preserve them, so just end this string
  107. // state now as a following quote will start again
  108. if (sc.ch == '\"') {
  109. if (sc.chNext == '\"') {
  110. sc.Forward();
  111. } else {
  112. if (tolower(sc.chNext) == 'c') {
  113. sc.Forward();
  114. }
  115. sc.ForwardSetState(SCE_B_DEFAULT);
  116. }
  117. } else if (sc.atLineEnd) {
  118. visibleChars = 0;
  119. sc.ChangeState(SCE_B_STRINGEOL);
  120. sc.ForwardSetState(SCE_B_DEFAULT);
  121. }
  122. } else if (sc.state == SCE_B_COMMENT) {
  123. if (sc.atLineEnd) {
  124. visibleChars = 0;
  125. sc.ForwardSetState(SCE_B_DEFAULT);
  126. }
  127. } else if (sc.state == SCE_B_PREPROCESSOR) {
  128. if (sc.atLineEnd) {
  129. visibleChars = 0;
  130. sc.ForwardSetState(SCE_B_DEFAULT);
  131. }
  132. } else if (sc.state == SCE_B_FILENUMBER) {
  133. if (IsADigit(sc.ch)) {
  134. fileNbDigits++;
  135. if (fileNbDigits > 3) {
  136. sc.ChangeState(SCE_B_DATE);
  137. }
  138. } else if (sc.ch == '\r' || sc.ch == '\n' || sc.ch == ',') {
  139. // Regular uses: Close #1; Put #1, ...; Get #1, ... etc.
  140. // Too bad if date is format #27, Oct, 2003# or something like that...
  141. // Use regular number state
  142. sc.ChangeState(SCE_B_NUMBER);
  143. sc.SetState(SCE_B_DEFAULT);
  144. } else if (sc.ch == '#') {
  145. sc.ChangeState(SCE_B_DATE);
  146. sc.ForwardSetState(SCE_B_DEFAULT);
  147. } else {
  148. sc.ChangeState(SCE_B_DATE);
  149. }
  150. if (sc.state != SCE_B_FILENUMBER) {
  151. fileNbDigits = 0;
  152. }
  153. } else if (sc.state == SCE_B_DATE) {
  154. if (sc.atLineEnd) {
  155. visibleChars = 0;
  156. sc.ChangeState(SCE_B_STRINGEOL);
  157. sc.ForwardSetState(SCE_B_DEFAULT);
  158. } else if (sc.ch == '#') {
  159. sc.ForwardSetState(SCE_B_DEFAULT);
  160. }
  161. }
  162. if (sc.state == SCE_B_DEFAULT) {
  163. if (sc.ch == '\'') {
  164. sc.SetState(SCE_B_COMMENT);
  165. } else if (sc.ch == '\"') {
  166. sc.SetState(SCE_B_STRING);
  167. } else if (sc.ch == '#' && visibleChars == 0) {
  168. // Preprocessor commands are alone on their line
  169. sc.SetState(SCE_B_PREPROCESSOR);
  170. } else if (sc.ch == '#') {
  171. // It can be a date literal, ending with #, or a file number, from 1 to 511
  172. // The date literal depends on the locale, so anything can go between #'s.
  173. // Can be #January 1, 1993# or #1 Jan 93# or #05/11/2003#, etc.
  174. // So we set the FILENUMBER state, and switch to DATE if it isn't a file number
  175. sc.SetState(SCE_B_FILENUMBER);
  176. } else if (sc.ch == '&' && tolower(sc.chNext) == 'h') {
  177. // Hexadecimal number
  178. sc.SetState(SCE_B_NUMBER);
  179. sc.Forward();
  180. } else if (sc.ch == '&' && tolower(sc.chNext) == 'o') {
  181. // Octal number
  182. sc.SetState(SCE_B_NUMBER);
  183. sc.Forward();
  184. } else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
  185. sc.SetState(SCE_B_NUMBER);
  186. } else if (IsAWordStart(sc.ch) || (sc.ch == '[')) {
  187. sc.SetState(SCE_B_IDENTIFIER);
  188. } else if (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) { // Integer division
  189. sc.SetState(SCE_B_OPERATOR);
  190. }
  191. }
  192. if (sc.atLineEnd) {
  193. visibleChars = 0;
  194. }
  195. if (!IsASpace(sc.ch)) {
  196. visibleChars++;
  197. }
  198. }
  199. if (sc.state == SCE_B_IDENTIFIER && !IsAWordChar(sc.ch)) {
  200. // In Basic (except VBScript), a variable name or a function name
  201. // can end with a special character indicating the type of the value
  202. // held or returned.
  203. bool skipType = false;
  204. if (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
  205. sc.Forward(); // Skip it
  206. skipType = true;
  207. }
  208. if (sc.ch == ']') {
  209. sc.Forward();
  210. }
  211. char s[100];
  212. sc.GetCurrentLowered(s, sizeof(s));
  213. if (skipType) {
  214. s[strlen(s) - 1] = '\0';
  215. }
  216. if (strcmp(s, "rem") == 0) {
  217. sc.ChangeState(SCE_B_COMMENT);
  218. } else {
  219. if (keywords.InList(s)) {
  220. sc.ChangeState(SCE_B_KEYWORD);
  221. } else if (keywords2.InList(s)) {
  222. sc.ChangeState(SCE_B_KEYWORD2);
  223. } else if (keywords3.InList(s)) {
  224. sc.ChangeState(SCE_B_KEYWORD3);
  225. } else if (keywords4.InList(s)) {
  226. sc.ChangeState(SCE_B_KEYWORD4);
  227. } // Else, it is really an identifier...
  228. sc.SetState(SCE_B_DEFAULT);
  229. }
  230. }
  231. sc.Complete();
  232. }
  233. static void FoldVBDoc(Sci_PositionU startPos, Sci_Position length, int,
  234. WordList *[], Accessor &styler) {
  235. Sci_Position endPos = startPos + length;
  236. // Backtrack to previous line in case need to fix its fold status
  237. Sci_Position lineCurrent = styler.GetLine(startPos);
  238. if (startPos > 0) {
  239. if (lineCurrent > 0) {
  240. lineCurrent--;
  241. startPos = styler.LineStart(lineCurrent);
  242. }
  243. }
  244. int spaceFlags = 0;
  245. int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsVBComment);
  246. char chNext = styler[startPos];
  247. for (Sci_Position i = startPos; i < endPos; i++) {
  248. char ch = chNext;
  249. chNext = styler.SafeGetCharAt(i + 1);
  250. if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
  251. int lev = indentCurrent;
  252. int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsVBComment);
  253. if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
  254. // Only non whitespace lines can be headers
  255. if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
  256. lev |= SC_FOLDLEVELHEADERFLAG;
  257. } else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
  258. // Line after is blank so check the next - maybe should continue further?
  259. int spaceFlags2 = 0;
  260. int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsVBComment);
  261. if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
  262. lev |= SC_FOLDLEVELHEADERFLAG;
  263. }
  264. }
  265. }
  266. indentCurrent = indentNext;
  267. styler.SetLevel(lineCurrent, lev);
  268. lineCurrent++;
  269. }
  270. }
  271. }
  272. static void ColouriseVBNetDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  273. WordList *keywordlists[], Accessor &styler) {
  274. ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, false);
  275. }
  276. static void ColouriseVBScriptDoc(Sci_PositionU startPos, Sci_Position length, int initStyle,
  277. WordList *keywordlists[], Accessor &styler) {
  278. ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, true);
  279. }
  280. static const char * const vbWordListDesc[] = {
  281. "Keywords",
  282. "user1",
  283. "user2",
  284. "user3",
  285. 0
  286. };
  287. LexerModule lmVB(SCLEX_VB, ColouriseVBNetDoc, "vb", FoldVBDoc, vbWordListDesc);
  288. LexerModule lmVBScript(SCLEX_VBSCRIPT, ColouriseVBScriptDoc, "vbscript", FoldVBDoc, vbWordListDesc);