vbscript.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. For extra ASP classic objects, initialize CodeMirror instance with this option:
  3. isASP: true
  4. E.G.:
  5. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  6. lineNumbers: true,
  7. isASP: true
  8. });
  9. */
  10. CodeMirror.defineMode("vbscript", 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 doubleOperators = new RegExp("^((<>)|(<=)|(>=))");
  17. var singleDelimiters = new RegExp('^[\\.,]');
  18. var brakets = new RegExp('^[\\(\\)]');
  19. var identifiers = new RegExp("^[A-Za-z][_A-Za-z0-9]*");
  20. var openingKeywords = ['class','sub','select','while','if','function', 'property', 'with', 'for'];
  21. var middleKeywords = ['else','elseif','case'];
  22. var endKeywords = ['next','loop','wend'];
  23. var wordOperators = wordRegexp(['and', 'or', 'not', 'xor', 'is', 'mod', 'eqv', 'imp']);
  24. var commonkeywords = ['dim', 'redim', 'then', 'until', 'randomize',
  25. 'byval','byref','new','property', 'exit', 'in',
  26. 'const','private', 'public',
  27. 'get','set','let', 'stop', 'on error resume next', 'on error goto 0', 'option explicit', 'call', 'me'];
  28. //This list was from: http://msdn.microsoft.com/en-us/library/f8tbc79x(v=vs.84).aspx
  29. var atomWords = ['true', 'false', 'nothing', 'empty', 'null'];
  30. //This list was from: http://msdn.microsoft.com/en-us/library/3ca8tfek(v=vs.84).aspx
  31. var builtinFuncsWords = ['abs', 'array', 'asc', 'atn', 'cbool', 'cbyte', 'ccur', 'cdate', 'cdbl', 'chr', 'cint', 'clng', 'cos', 'csng', 'cstr', 'date', 'dateadd', 'datediff', 'datepart',
  32. 'dateserial', 'datevalue', 'day', 'escape', 'eval', 'execute', 'exp', 'filter', 'formatcurrency', 'formatdatetime', 'formatnumber', 'formatpercent', 'getlocale', 'getobject',
  33. 'getref', 'hex', 'hour', 'inputbox', 'instr', 'instrrev', 'int', 'fix', 'isarray', 'isdate', 'isempty', 'isnull', 'isnumeric', 'isobject', 'join', 'lbound', 'lcase', 'left',
  34. 'len', 'loadpicture', 'log', 'ltrim', 'rtrim', 'trim', 'maths', 'mid', 'minute', 'month', 'monthname', 'msgbox', 'now', 'oct', 'replace', 'rgb', 'right', 'rnd', 'round',
  35. 'scriptengine', 'scriptenginebuildversion', 'scriptenginemajorversion', 'scriptengineminorversion', 'second', 'setlocale', 'sgn', 'sin', 'space', 'split', 'sqr', 'strcomp',
  36. 'string', 'strreverse', 'tan', 'time', 'timer', 'timeserial', 'timevalue', 'typename', 'ubound', 'ucase', 'unescape', 'vartype', 'weekday', 'weekdayname', 'year'];
  37. //This list was from: http://msdn.microsoft.com/en-us/library/ydz4cfk3(v=vs.84).aspx
  38. var builtinConsts = ['vbBlack', 'vbRed', 'vbGreen', 'vbYellow', 'vbBlue', 'vbMagenta', 'vbCyan', 'vbWhite', 'vbBinaryCompare', 'vbTextCompare',
  39. 'vbSunday', 'vbMonday', 'vbTuesday', 'vbWednesday', 'vbThursday', 'vbFriday', 'vbSaturday', 'vbUseSystemDayOfWeek', 'vbFirstJan1', 'vbFirstFourDays', 'vbFirstFullWeek',
  40. 'vbGeneralDate', 'vbLongDate', 'vbShortDate', 'vbLongTime', 'vbShortTime', 'vbObjectError',
  41. 'vbOKOnly', 'vbOKCancel', 'vbAbortRetryIgnore', 'vbYesNoCancel', 'vbYesNo', 'vbRetryCancel', 'vbCritical', 'vbQuestion', 'vbExclamation', 'vbInformation', 'vbDefaultButton1', 'vbDefaultButton2',
  42. 'vbDefaultButton3', 'vbDefaultButton4', 'vbApplicationModal', 'vbSystemModal', 'vbOK', 'vbCancel', 'vbAbort', 'vbRetry', 'vbIgnore', 'vbYes', 'vbNo',
  43. 'vbCr', 'VbCrLf', 'vbFormFeed', 'vbLf', 'vbNewLine', 'vbNullChar', 'vbNullString', 'vbTab', 'vbVerticalTab', 'vbUseDefault', 'vbTrue', 'vbFalse',
  44. 'vbEmpty', 'vbNull', 'vbInteger', 'vbLong', 'vbSingle', 'vbDouble', 'vbCurrency', 'vbDate', 'vbString', 'vbObject', 'vbError', 'vbBoolean', 'vbVariant', 'vbDataObject', 'vbDecimal', 'vbByte', 'vbArray'];
  45. //This list was from: http://msdn.microsoft.com/en-us/library/hkc375ea(v=vs.84).aspx
  46. var builtinObjsWords = ['WScript', 'err', 'debug', 'RegExp'];
  47. var knownProperties = ['description', 'firstindex', 'global', 'helpcontext', 'helpfile', 'ignorecase', 'length', 'number', 'pattern', 'source', 'value', 'count'];
  48. var knownMethods = ['clear', 'execute', 'raise', 'replace', 'test', 'write', 'writeline', 'close', 'open', 'state', 'eof', 'update', 'addnew', 'end', 'createobject', 'quit'];
  49. var aspBuiltinObjsWords = ['server', 'response', 'request', 'session', 'application'];
  50. var aspKnownProperties = ['buffer', 'cachecontrol', 'charset', 'contenttype', 'expires', 'expiresabsolute', 'isclientconnected', 'pics', 'status', //response
  51. 'clientcertificate', 'cookies', 'form', 'querystring', 'servervariables', 'totalbytes', //request
  52. 'contents', 'staticobjects', //application
  53. 'codepage', 'lcid', 'sessionid', 'timeout', //session
  54. 'scripttimeout']; //server
  55. var aspKnownMethods = ['addheader', 'appendtolog', 'binarywrite', 'end', 'flush', 'redirect', //response
  56. 'binaryread', //request
  57. 'remove', 'removeall', 'lock', 'unlock', //application
  58. 'abandon', //session
  59. 'getlasterror', 'htmlencode', 'mappath', 'transfer', 'urlencode']; //server
  60. var knownWords = knownMethods.concat(knownProperties);
  61. builtinObjsWords = builtinObjsWords.concat(builtinConsts);
  62. if (conf.isASP){
  63. builtinObjsWords = builtinObjsWords.concat(aspBuiltinObjsWords);
  64. knownWords = knownWords.concat(aspKnownMethods, aspKnownProperties);
  65. };
  66. var keywords = wordRegexp(commonkeywords);
  67. var atoms = wordRegexp(atomWords);
  68. var builtinFuncs = wordRegexp(builtinFuncsWords);
  69. var builtinObjs = wordRegexp(builtinObjsWords);
  70. var known = wordRegexp(knownWords);
  71. var stringPrefixes = '"';
  72. var opening = wordRegexp(openingKeywords);
  73. var middle = wordRegexp(middleKeywords);
  74. var closing = wordRegexp(endKeywords);
  75. var doubleClosing = wordRegexp(['end']);
  76. var doOpening = wordRegexp(['do']);
  77. var noIndentWords = wordRegexp(['on error resume next', 'exit']);
  78. var comment = wordRegexp(['rem']);
  79. function indent(_stream, state) {
  80. state.currentIndent++;
  81. }
  82. function dedent(_stream, state) {
  83. state.currentIndent--;
  84. }
  85. // tokenizers
  86. function tokenBase(stream, state) {
  87. if (stream.eatSpace()) {
  88. return 'space';
  89. //return null;
  90. }
  91. var ch = stream.peek();
  92. // Handle Comments
  93. if (ch === "'") {
  94. stream.skipToEnd();
  95. return 'comment';
  96. }
  97. if (stream.match(comment)){
  98. stream.skipToEnd();
  99. return 'comment';
  100. }
  101. // Handle Number Literals
  102. if (stream.match(/^((&H)|(&O))?[0-9\.]/i, false) && !stream.match(/^((&H)|(&O))?[0-9\.]+[a-z_]/i, false)) {
  103. var floatLiteral = false;
  104. // Floats
  105. if (stream.match(/^\d*\.\d+/i)) { floatLiteral = true; }
  106. else if (stream.match(/^\d+\.\d*/)) { floatLiteral = true; }
  107. else if (stream.match(/^\.\d+/)) { floatLiteral = true; }
  108. if (floatLiteral) {
  109. // Float literals may be "imaginary"
  110. stream.eat(/J/i);
  111. return 'number';
  112. }
  113. // Integers
  114. var intLiteral = false;
  115. // Hex
  116. if (stream.match(/^&H[0-9a-f]+/i)) { intLiteral = true; }
  117. // Octal
  118. else if (stream.match(/^&O[0-7]+/i)) { intLiteral = true; }
  119. // Decimal
  120. else if (stream.match(/^[1-9]\d*F?/)) {
  121. // Decimal literals may be "imaginary"
  122. stream.eat(/J/i);
  123. // TODO - Can you have imaginary longs?
  124. intLiteral = true;
  125. }
  126. // Zero by itself with no other piece of number.
  127. else if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
  128. if (intLiteral) {
  129. // Integer literals may be "long"
  130. stream.eat(/L/i);
  131. return 'number';
  132. }
  133. }
  134. // Handle Strings
  135. if (stream.match(stringPrefixes)) {
  136. state.tokenize = tokenStringFactory(stream.current());
  137. return state.tokenize(stream, state);
  138. }
  139. // Handle operators and Delimiters
  140. if (stream.match(doubleOperators)
  141. || stream.match(singleOperators)
  142. || stream.match(wordOperators)) {
  143. return 'operator';
  144. }
  145. if (stream.match(singleDelimiters)) {
  146. return null;
  147. }
  148. if (stream.match(brakets)) {
  149. return "bracket";
  150. }
  151. if (stream.match(noIndentWords)) {
  152. state.doInCurrentLine = true;
  153. return 'keyword';
  154. }
  155. if (stream.match(doOpening)) {
  156. indent(stream,state);
  157. state.doInCurrentLine = true;
  158. return 'keyword';
  159. }
  160. if (stream.match(opening)) {
  161. if (! state.doInCurrentLine)
  162. indent(stream,state);
  163. else
  164. state.doInCurrentLine = false;
  165. return 'keyword';
  166. }
  167. if (stream.match(middle)) {
  168. return 'keyword';
  169. }
  170. if (stream.match(doubleClosing)) {
  171. dedent(stream,state);
  172. dedent(stream,state);
  173. return 'keyword';
  174. }
  175. if (stream.match(closing)) {
  176. if (! state.doInCurrentLine)
  177. dedent(stream,state);
  178. else
  179. state.doInCurrentLine = false;
  180. return 'keyword';
  181. }
  182. if (stream.match(keywords)) {
  183. return 'keyword';
  184. }
  185. if (stream.match(atoms)) {
  186. return 'atom';
  187. }
  188. if (stream.match(known)) {
  189. return 'variable-2';
  190. }
  191. if (stream.match(builtinFuncs)) {
  192. return 'builtin';
  193. }
  194. if (stream.match(builtinObjs)){
  195. return 'variable-2';
  196. }
  197. if (stream.match(identifiers)) {
  198. return 'variable';
  199. }
  200. // Handle non-detected items
  201. stream.next();
  202. return ERRORCLASS;
  203. }
  204. function tokenStringFactory(delimiter) {
  205. var singleline = delimiter.length == 1;
  206. var OUTCLASS = 'string';
  207. return function(stream, state) {
  208. while (!stream.eol()) {
  209. stream.eatWhile(/[^'"]/);
  210. if (stream.match(delimiter)) {
  211. state.tokenize = tokenBase;
  212. return OUTCLASS;
  213. } else {
  214. stream.eat(/['"]/);
  215. }
  216. }
  217. if (singleline) {
  218. if (parserConf.singleLineStringErrors) {
  219. return ERRORCLASS;
  220. } else {
  221. state.tokenize = tokenBase;
  222. }
  223. }
  224. return OUTCLASS;
  225. };
  226. }
  227. function tokenLexer(stream, state) {
  228. var style = state.tokenize(stream, state);
  229. var current = stream.current();
  230. // Handle '.' connected identifiers
  231. if (current === '.') {
  232. style = state.tokenize(stream, state);
  233. current = stream.current();
  234. if (style.substr(0, 8) === 'variable' || style==='builtin' || style==='keyword'){//|| knownWords.indexOf(current.substring(1)) > -1) {
  235. if (style === 'builtin' || style === 'keyword') style='variable';
  236. if (knownWords.indexOf(current.substr(1)) > -1) style='variable-2';
  237. return style;
  238. } else {
  239. return ERRORCLASS;
  240. }
  241. }
  242. return style;
  243. }
  244. var external = {
  245. electricChars:"dDpPtTfFeE ",
  246. startState: function() {
  247. return {
  248. tokenize: tokenBase,
  249. lastToken: null,
  250. currentIndent: 0,
  251. nextLineIndent: 0,
  252. doInCurrentLine: false,
  253. ignoreKeyword: false
  254. };
  255. },
  256. token: function(stream, state) {
  257. if (stream.sol()) {
  258. state.currentIndent += state.nextLineIndent;
  259. state.nextLineIndent = 0;
  260. state.doInCurrentLine = 0;
  261. }
  262. var style = tokenLexer(stream, state);
  263. state.lastToken = {style:style, content: stream.current()};
  264. if (style==='space') style=null;
  265. return style;
  266. },
  267. indent: function(state, textAfter) {
  268. var trueText = textAfter.replace(/^\s+|\s+$/g, '') ;
  269. if (trueText.match(closing) || trueText.match(doubleClosing) || trueText.match(middle)) return conf.indentUnit*(state.currentIndent-1);
  270. if(state.currentIndent < 0) return 0;
  271. return state.currentIndent * conf.indentUnit;
  272. }
  273. };
  274. return external;
  275. });
  276. CodeMirror.defineMIME("text/vbscript", "vbscript");