vbscript.js 13 KB

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