PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ExtLibs/wxWidgets/src/stc/scintilla/src/LexVB.cxx

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