vb.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("vb", function(conf, parserConf) {
  13. var ERRORCLASS = 'error';
  14. function wordRegexp(words) {
  15. return new RegExp("^((" + words.join(")|(") + "))\\b", "i");
  16. }
  17. var singleOperators = new RegExp("^[\\+\\-\\*/%&\\\\|\\^~<>!]");
  18. var singleDelimiters = new RegExp('^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]');
  19. var doubleOperators = new RegExp("^((==)|(<>)|(<=)|(>=)|(<>)|(<<)|(>>)|(//)|(\\*\\*))");
  20. var doubleDelimiters = new RegExp("^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))");
  21. var tripleDelimiters = new RegExp("^((//=)|(>>=)|(<<=)|(\\*\\*=))");
  22. var identifiers = new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
  23. var openingKeywords = ['class','module', 'sub','enum','select','while','if','function', 'get','set','property', 'try', 'structure', 'synclock', 'using', 'with'];
  24. var middleKeywords = ['else','elseif','case', 'catch', 'finally'];
  25. var endKeywords = ['next','loop'];
  26. var operatorKeywords = ['and', "andalso", 'or', 'orelse', 'xor', 'in', 'not', 'is', 'isnot', 'like'];
  27. var wordOperators = wordRegexp(operatorKeywords);
  28. var commonKeywords = ["#const", "#else", "#elseif", "#end", "#if", "#region", "addhandler", "addressof", "alias", "as", "byref", "byval", "cbool", "cbyte", "cchar", "cdate", "cdbl", "cdec", "cint", "clng", "cobj", "compare", "const", "continue", "csbyte", "cshort", "csng", "cstr", "cuint", "culng", "cushort", "declare", "default", "delegate", "dim", "directcast", "each", "erase", "error", "event", "exit", "explicit", "false", "for", "friend", "gettype", "goto", "handles", "implements", "imports", "infer", "inherits", "interface", "isfalse", "istrue", "lib", "me", "mod", "mustinherit", "mustoverride", "my", "mybase", "myclass", "namespace", "narrowing", "new", "nothing", "notinheritable", "notoverridable", "of", "off", "on", "operator", "option", "optional", "out", "overloads", "overridable", "overrides", "paramarray", "partial", "private", "protected", "public", "raiseevent", "readonly", "redim", "removehandler", "resume", "return", "shadows", "shared", "static", "step", "stop", "strict", "then", "throw", "to", "true", "trycast", "typeof", "until", "until", "when", "widening", "withevents", "writeonly"];
  29. var commontypes = ['object', 'boolean', 'char', 'string', 'byte', 'sbyte', 'short', 'ushort', 'int16', 'uint16', 'integer', 'uinteger', 'int32', 'uint32', 'long', 'ulong', 'int64', 'uint64', 'decimal', 'single', 'double', 'float', 'date', 'datetime', 'intptr', 'uintptr'];
  30. var keywords = wordRegexp(commonKeywords);
  31. var types = wordRegexp(commontypes);
  32. var stringPrefixes = '"';
  33. var opening = wordRegexp(openingKeywords);
  34. var middle = wordRegexp(middleKeywords);
  35. var closing = wordRegexp(endKeywords);
  36. var doubleClosing = wordRegexp(['end']);
  37. var doOpening = wordRegexp(['do']);
  38. var indentInfo = null;
  39. CodeMirror.registerHelper("hintWords", "vb", openingKeywords.concat(middleKeywords).concat(endKeywords)
  40. .concat(operatorKeywords).concat(commonKeywords).concat(commontypes));
  41. function indent(_stream, state) {
  42. state.currentIndent++;
  43. }
  44. function dedent(_stream, state) {
  45. state.currentIndent--;
  46. }
  47. // tokenizers
  48. function tokenBase(stream, state) {
  49. if (stream.eatSpace()) {
  50. return null;
  51. }
  52. var ch = stream.peek();
  53. // Handle Comments
  54. if (ch === "'") {
  55. stream.skipToEnd();
  56. return 'comment';
  57. }
  58. // Handle Number Literals
  59. if (stream.match(/^((&H)|(&O))?[0-9\.a-f]/i, false)) {
  60. var floatLiteral = false;
  61. // Floats
  62. if (stream.match(/^\d*\.\d+F?/i)) { floatLiteral = true; }
  63. else if (stream.match(/^\d+\.\d*F?/)) { floatLiteral = true; }
  64. else if (stream.match(/^\.\d+F?/)) { floatLiteral = true; }
  65. if (floatLiteral) {
  66. // Float literals may be "imaginary"
  67. stream.eat(/J/i);
  68. return 'number';
  69. }
  70. // Integers
  71. var intLiteral = false;
  72. // Hex
  73. if (stream.match(/^&H[0-9a-f]+/i)) { intLiteral = true; }
  74. // Octal
  75. else if (stream.match(/^&O[0-7]+/i)) { intLiteral = true; }
  76. // Decimal
  77. else if (stream.match(/^[1-9]\d*F?/)) {
  78. // Decimal literals may be "imaginary"
  79. stream.eat(/J/i);
  80. // TODO - Can you have imaginary longs?
  81. intLiteral = true;
  82. }
  83. // Zero by itself with no other piece of number.
  84. else if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
  85. if (intLiteral) {
  86. // Integer literals may be "long"
  87. stream.eat(/L/i);
  88. return 'number';
  89. }
  90. }
  91. // Handle Strings
  92. if (stream.match(stringPrefixes)) {
  93. state.tokenize = tokenStringFactory(stream.current());
  94. return state.tokenize(stream, state);
  95. }
  96. // Handle operators and Delimiters
  97. if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters)) {
  98. return null;
  99. }
  100. if (stream.match(doubleOperators)
  101. || stream.match(singleOperators)
  102. || stream.match(wordOperators)) {
  103. return 'operator';
  104. }
  105. if (stream.match(singleDelimiters)) {
  106. return null;
  107. }
  108. if (stream.match(doOpening)) {
  109. indent(stream,state);
  110. state.doInCurrentLine = true;
  111. return 'keyword';
  112. }
  113. if (stream.match(opening)) {
  114. if (! state.doInCurrentLine)
  115. indent(stream,state);
  116. else
  117. state.doInCurrentLine = false;
  118. return 'keyword';
  119. }
  120. if (stream.match(middle)) {
  121. return 'keyword';
  122. }
  123. if (stream.match(doubleClosing)) {
  124. dedent(stream,state);
  125. dedent(stream,state);
  126. return 'keyword';
  127. }
  128. if (stream.match(closing)) {
  129. dedent(stream,state);
  130. return 'keyword';
  131. }
  132. if (stream.match(types)) {
  133. return 'keyword';
  134. }
  135. if (stream.match(keywords)) {
  136. return 'keyword';
  137. }
  138. if (stream.match(identifiers)) {
  139. return 'variable';
  140. }
  141. // Handle non-detected items
  142. stream.next();
  143. return ERRORCLASS;
  144. }
  145. function tokenStringFactory(delimiter) {
  146. var singleline = delimiter.length == 1;
  147. var OUTCLASS = 'string';
  148. return function(stream, state) {
  149. while (!stream.eol()) {
  150. stream.eatWhile(/[^'"]/);
  151. if (stream.match(delimiter)) {
  152. state.tokenize = tokenBase;
  153. return OUTCLASS;
  154. } else {
  155. stream.eat(/['"]/);
  156. }
  157. }
  158. if (singleline) {
  159. if (parserConf.singleLineStringErrors) {
  160. return ERRORCLASS;
  161. } else {
  162. state.tokenize = tokenBase;
  163. }
  164. }
  165. return OUTCLASS;
  166. };
  167. }
  168. function tokenLexer(stream, state) {
  169. var style = state.tokenize(stream, state);
  170. var current = stream.current();
  171. // Handle '.' connected identifiers
  172. if (current === '.') {
  173. style = state.tokenize(stream, state);
  174. if (style === 'variable') {
  175. return 'variable';
  176. } else {
  177. return ERRORCLASS;
  178. }
  179. }
  180. var delimiter_index = '[({'.indexOf(current);
  181. if (delimiter_index !== -1) {
  182. indent(stream, state );
  183. }
  184. if (indentInfo === 'dedent') {
  185. if (dedent(stream, state)) {
  186. return ERRORCLASS;
  187. }
  188. }
  189. delimiter_index = '])}'.indexOf(current);
  190. if (delimiter_index !== -1) {
  191. if (dedent(stream, state)) {
  192. return ERRORCLASS;
  193. }
  194. }
  195. return style;
  196. }
  197. var external = {
  198. electricChars:"dDpPtTfFeE ",
  199. startState: function() {
  200. return {
  201. tokenize: tokenBase,
  202. lastToken: null,
  203. currentIndent: 0,
  204. nextLineIndent: 0,
  205. doInCurrentLine: false
  206. };
  207. },
  208. token: function(stream, state) {
  209. if (stream.sol()) {
  210. state.currentIndent += state.nextLineIndent;
  211. state.nextLineIndent = 0;
  212. state.doInCurrentLine = 0;
  213. }
  214. var style = tokenLexer(stream, state);
  215. state.lastToken = {style:style, content: stream.current()};
  216. return style;
  217. },
  218. indent: function(state, textAfter) {
  219. var trueText = textAfter.replace(/^\s+|\s+$/g, '') ;
  220. if (trueText.match(closing) || trueText.match(doubleClosing) || trueText.match(middle)) return conf.indentUnit*(state.currentIndent-1);
  221. if(state.currentIndent < 0) return 0;
  222. return state.currentIndent * conf.indentUnit;
  223. },
  224. lineComment: "'"
  225. };
  226. return external;
  227. });
  228. CodeMirror.defineMIME("text/x-vb", "vb");
  229. });