You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vb.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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. current = stream.current();
  177. if (style === 'variable') {
  178. return 'variable';
  179. } else {
  180. return ERRORCLASS;
  181. }
  182. }
  183. var delimiter_index = '[({'.indexOf(current);
  184. if (delimiter_index !== -1) {
  185. indent(stream, state );
  186. }
  187. if (indentInfo === 'dedent') {
  188. if (dedent(stream, state)) {
  189. return ERRORCLASS;
  190. }
  191. }
  192. delimiter_index = '])}'.indexOf(current);
  193. if (delimiter_index !== -1) {
  194. if (dedent(stream, state)) {
  195. return ERRORCLASS;
  196. }
  197. }
  198. return style;
  199. }
  200. var external = {
  201. electricChars:"dDpPtTfFeE ",
  202. startState: function() {
  203. return {
  204. tokenize: tokenBase,
  205. lastToken: null,
  206. currentIndent: 0,
  207. nextLineIndent: 0,
  208. doInCurrentLine: false
  209. };
  210. },
  211. token: function(stream, state) {
  212. if (stream.sol()) {
  213. state.currentIndent += state.nextLineIndent;
  214. state.nextLineIndent = 0;
  215. state.doInCurrentLine = 0;
  216. }
  217. var style = tokenLexer(stream, state);
  218. state.lastToken = {style:style, content: stream.current()};
  219. return style;
  220. },
  221. indent: function(state, textAfter) {
  222. var trueText = textAfter.replace(/^\s+|\s+$/g, '') ;
  223. if (trueText.match(closing) || trueText.match(doubleClosing) || trueText.match(middle)) return conf.indentUnit*(state.currentIndent-1);
  224. if(state.currentIndent < 0) return 0;
  225. return state.currentIndent * conf.indentUnit;
  226. },
  227. lineComment: "'"
  228. };
  229. return external;
  230. });
  231. CodeMirror.defineMIME("text/x-vb", "vb");
  232. });