vb.js 7.8 KB

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