vb.js 8.5 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'];
  24. var middleKeywords = ['else','elseif','case', 'catch'];
  25. var endKeywords = ['next','loop'];
  26. var operatorKeywords = ['and', 'or', 'not', 'xor', 'in'];
  27. var wordOperators = wordRegexp(operatorKeywords);
  28. var commonKeywords = ['as', 'dim', 'break', 'continue','optional', 'then', 'until',
  29. 'goto', 'byval','byref','new','handles','property', 'return',
  30. 'const','private', 'protected', 'friend', 'public', 'shared', 'static', 'true','false'];
  31. var commontypes = ['integer','string','double','decimal','boolean','short','char', 'float','single'];
  32. var keywords = wordRegexp(commonKeywords);
  33. var types = wordRegexp(commontypes);
  34. var stringPrefixes = '"';
  35. var opening = wordRegexp(openingKeywords);
  36. var middle = wordRegexp(middleKeywords);
  37. var closing = wordRegexp(endKeywords);
  38. var doubleClosing = wordRegexp(['end']);
  39. var doOpening = wordRegexp(['do']);
  40. var indentInfo = null;
  41. CodeMirror.registerHelper("hintWords", "vb", openingKeywords.concat(middleKeywords).concat(endKeywords)
  42. .concat(operatorKeywords).concat(commonKeywords).concat(commontypes));
  43. function indent(_stream, state) {
  44. state.currentIndent++;
  45. }
  46. function dedent(_stream, state) {
  47. state.currentIndent--;
  48. }
  49. // tokenizers
  50. function tokenBase(stream, state) {
  51. if (stream.eatSpace()) {
  52. return null;
  53. }
  54. var ch = stream.peek();
  55. // Handle Comments
  56. if (ch === "'") {
  57. stream.skipToEnd();
  58. return 'comment';
  59. }
  60. // Handle Number Literals
  61. if (stream.match(/^((&H)|(&O))?[0-9\.a-f]/i, false)) {
  62. var floatLiteral = false;
  63. // Floats
  64. if (stream.match(/^\d*\.\d+F?/i)) { floatLiteral = true; }
  65. else if (stream.match(/^\d+\.\d*F?/)) { floatLiteral = true; }
  66. else if (stream.match(/^\.\d+F?/)) { floatLiteral = true; }
  67. if (floatLiteral) {
  68. // Float literals may be "imaginary"
  69. stream.eat(/J/i);
  70. return 'number';
  71. }
  72. // Integers
  73. var intLiteral = false;
  74. // Hex
  75. if (stream.match(/^&H[0-9a-f]+/i)) { intLiteral = true; }
  76. // Octal
  77. else if (stream.match(/^&O[0-7]+/i)) { intLiteral = true; }
  78. // Decimal
  79. else if (stream.match(/^[1-9]\d*F?/)) {
  80. // Decimal literals may be "imaginary"
  81. stream.eat(/J/i);
  82. // TODO - Can you have imaginary longs?
  83. intLiteral = true;
  84. }
  85. // Zero by itself with no other piece of number.
  86. else if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
  87. if (intLiteral) {
  88. // Integer literals may be "long"
  89. stream.eat(/L/i);
  90. return 'number';
  91. }
  92. }
  93. // Handle Strings
  94. if (stream.match(stringPrefixes)) {
  95. state.tokenize = tokenStringFactory(stream.current());
  96. return state.tokenize(stream, state);
  97. }
  98. // Handle operators and Delimiters
  99. if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters)) {
  100. return null;
  101. }
  102. if (stream.match(doubleOperators)
  103. || stream.match(singleOperators)
  104. || stream.match(wordOperators)) {
  105. return 'operator';
  106. }
  107. if (stream.match(singleDelimiters)) {
  108. return null;
  109. }
  110. if (stream.match(doOpening)) {
  111. indent(stream,state);
  112. state.doInCurrentLine = true;
  113. return 'keyword';
  114. }
  115. if (stream.match(opening)) {
  116. if (! state.doInCurrentLine)
  117. indent(stream,state);
  118. else
  119. state.doInCurrentLine = false;
  120. return 'keyword';
  121. }
  122. if (stream.match(middle)) {
  123. return 'keyword';
  124. }
  125. if (stream.match(doubleClosing)) {
  126. dedent(stream,state);
  127. dedent(stream,state);
  128. return 'keyword';
  129. }
  130. if (stream.match(closing)) {
  131. dedent(stream,state);
  132. return 'keyword';
  133. }
  134. if (stream.match(types)) {
  135. return 'keyword';
  136. }
  137. if (stream.match(keywords)) {
  138. return 'keyword';
  139. }
  140. if (stream.match(identifiers)) {
  141. return 'variable';
  142. }
  143. // Handle non-detected items
  144. stream.next();
  145. return ERRORCLASS;
  146. }
  147. function tokenStringFactory(delimiter) {
  148. var singleline = delimiter.length == 1;
  149. var OUTCLASS = 'string';
  150. return function(stream, state) {
  151. while (!stream.eol()) {
  152. stream.eatWhile(/[^'"]/);
  153. if (stream.match(delimiter)) {
  154. state.tokenize = tokenBase;
  155. return OUTCLASS;
  156. } else {
  157. stream.eat(/['"]/);
  158. }
  159. }
  160. if (singleline) {
  161. if (parserConf.singleLineStringErrors) {
  162. return ERRORCLASS;
  163. } else {
  164. state.tokenize = tokenBase;
  165. }
  166. }
  167. return OUTCLASS;
  168. };
  169. }
  170. function tokenLexer(stream, state) {
  171. var style = state.tokenize(stream, state);
  172. var current = stream.current();
  173. // Handle '.' connected identifiers
  174. if (current === '.') {
  175. style = state.tokenize(stream, state);
  176. if (style === 'variable') {
  177. return 'variable';
  178. } else {
  179. return ERRORCLASS;
  180. }
  181. }
  182. var delimiter_index = '[({'.indexOf(current);
  183. if (delimiter_index !== -1) {
  184. indent(stream, state );
  185. }
  186. if (indentInfo === 'dedent') {
  187. if (dedent(stream, state)) {
  188. return ERRORCLASS;
  189. }
  190. }
  191. delimiter_index = '])}'.indexOf(current);
  192. if (delimiter_index !== -1) {
  193. if (dedent(stream, state)) {
  194. return ERRORCLASS;
  195. }
  196. }
  197. return style;
  198. }
  199. var external = {
  200. electricChars:"dDpPtTfFeE ",
  201. startState: function() {
  202. return {
  203. tokenize: tokenBase,
  204. lastToken: null,
  205. currentIndent: 0,
  206. nextLineIndent: 0,
  207. doInCurrentLine: false
  208. };
  209. },
  210. token: function(stream, state) {
  211. if (stream.sol()) {
  212. state.currentIndent += state.nextLineIndent;
  213. state.nextLineIndent = 0;
  214. state.doInCurrentLine = 0;
  215. }
  216. var style = tokenLexer(stream, state);
  217. state.lastToken = {style:style, content: stream.current()};
  218. return style;
  219. },
  220. indent: function(state, textAfter) {
  221. var trueText = textAfter.replace(/^\s+|\s+$/g, '') ;
  222. if (trueText.match(closing) || trueText.match(doubleClosing) || trueText.match(middle)) return conf.indentUnit*(state.currentIndent-1);
  223. if(state.currentIndent < 0) return 0;
  224. return state.currentIndent * conf.indentUnit;
  225. },
  226. lineComment: "'"
  227. };
  228. return external;
  229. });
  230. CodeMirror.defineMIME("text/x-vb", "vb");
  231. });