1
0

vb.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 wordOperators = wordRegexp(['and', 'or', 'not', 'xor', 'in']);
  27. var commonkeywords = ['as', 'dim', 'break', 'continue','optional', 'then', 'until',
  28. 'goto', 'byval','byref','new','handles','property', 'return',
  29. 'const','private', 'protected', 'friend', 'public', 'shared', 'static', 'true','false'];
  30. var commontypes = ['integer','string','double','decimal','boolean','short','char', 'float','single'];
  31. var keywords = wordRegexp(commonkeywords);
  32. var types = wordRegexp(commontypes);
  33. var stringPrefixes = '"';
  34. var opening = wordRegexp(openingKeywords);
  35. var middle = wordRegexp(middleKeywords);
  36. var closing = wordRegexp(endKeywords);
  37. var doubleClosing = wordRegexp(['end']);
  38. var doOpening = wordRegexp(['do']);
  39. var indentInfo = null;
  40. function indent(_stream, state) {
  41. state.currentIndent++;
  42. }
  43. function dedent(_stream, state) {
  44. state.currentIndent--;
  45. }
  46. // tokenizers
  47. function tokenBase(stream, state) {
  48. if (stream.eatSpace()) {
  49. return null;
  50. }
  51. var ch = stream.peek();
  52. // Handle Comments
  53. if (ch === "'") {
  54. stream.skipToEnd();
  55. return 'comment';
  56. }
  57. // Handle Number Literals
  58. if (stream.match(/^((&H)|(&O))?[0-9\.a-f]/i, false)) {
  59. var floatLiteral = false;
  60. // Floats
  61. if (stream.match(/^\d*\.\d+F?/i)) { floatLiteral = true; }
  62. else if (stream.match(/^\d+\.\d*F?/)) { floatLiteral = true; }
  63. else if (stream.match(/^\.\d+F?/)) { floatLiteral = true; }
  64. if (floatLiteral) {
  65. // Float literals may be "imaginary"
  66. stream.eat(/J/i);
  67. return 'number';
  68. }
  69. // Integers
  70. var intLiteral = false;
  71. // Hex
  72. if (stream.match(/^&H[0-9a-f]+/i)) { intLiteral = true; }
  73. // Octal
  74. else if (stream.match(/^&O[0-7]+/i)) { intLiteral = true; }
  75. // Decimal
  76. else if (stream.match(/^[1-9]\d*F?/)) {
  77. // Decimal literals may be "imaginary"
  78. stream.eat(/J/i);
  79. // TODO - Can you have imaginary longs?
  80. intLiteral = true;
  81. }
  82. // Zero by itself with no other piece of number.
  83. else if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
  84. if (intLiteral) {
  85. // Integer literals may be "long"
  86. stream.eat(/L/i);
  87. return 'number';
  88. }
  89. }
  90. // Handle Strings
  91. if (stream.match(stringPrefixes)) {
  92. state.tokenize = tokenStringFactory(stream.current());
  93. return state.tokenize(stream, state);
  94. }
  95. // Handle operators and Delimiters
  96. if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters)) {
  97. return null;
  98. }
  99. if (stream.match(doubleOperators)
  100. || stream.match(singleOperators)
  101. || stream.match(wordOperators)) {
  102. return 'operator';
  103. }
  104. if (stream.match(singleDelimiters)) {
  105. return null;
  106. }
  107. if (stream.match(doOpening)) {
  108. indent(stream,state);
  109. state.doInCurrentLine = true;
  110. return 'keyword';
  111. }
  112. if (stream.match(opening)) {
  113. if (! state.doInCurrentLine)
  114. indent(stream,state);
  115. else
  116. state.doInCurrentLine = false;
  117. return 'keyword';
  118. }
  119. if (stream.match(middle)) {
  120. return 'keyword';
  121. }
  122. if (stream.match(doubleClosing)) {
  123. dedent(stream,state);
  124. dedent(stream,state);
  125. return 'keyword';
  126. }
  127. if (stream.match(closing)) {
  128. dedent(stream,state);
  129. return 'keyword';
  130. }
  131. if (stream.match(types)) {
  132. return 'keyword';
  133. }
  134. if (stream.match(keywords)) {
  135. return 'keyword';
  136. }
  137. if (stream.match(identifiers)) {
  138. return 'variable';
  139. }
  140. // Handle non-detected items
  141. stream.next();
  142. return ERRORCLASS;
  143. }
  144. function tokenStringFactory(delimiter) {
  145. var singleline = delimiter.length == 1;
  146. var OUTCLASS = 'string';
  147. return function(stream, state) {
  148. while (!stream.eol()) {
  149. stream.eatWhile(/[^'"]/);
  150. if (stream.match(delimiter)) {
  151. state.tokenize = tokenBase;
  152. return OUTCLASS;
  153. } else {
  154. stream.eat(/['"]/);
  155. }
  156. }
  157. if (singleline) {
  158. if (parserConf.singleLineStringErrors) {
  159. return ERRORCLASS;
  160. } else {
  161. state.tokenize = tokenBase;
  162. }
  163. }
  164. return OUTCLASS;
  165. };
  166. }
  167. function tokenLexer(stream, state) {
  168. var style = state.tokenize(stream, state);
  169. var current = stream.current();
  170. // Handle '.' connected identifiers
  171. if (current === '.') {
  172. style = state.tokenize(stream, state);
  173. current = stream.current();
  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. };
  225. return external;
  226. });
  227. CodeMirror.defineMIME("text/x-vb", "vb");
  228. });