vb.js 8.2 KB

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