vbscript.js 13 KB

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