2 For extra ASP classic objects, initialize CodeMirror instance with this option:
6 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
11 CodeMirror.defineMode("vbscript", function(conf, parserConf) {
12 var ERRORCLASS = 'error';
14 function wordRegexp(words) {
15 return new RegExp("^((" + words.join(")|(") + "))\\b", "i");
18 var singleOperators = new RegExp("^[\\+\\-\\*/&\\\\\\^<>=]");
19 var doubleOperators = new RegExp("^((<>)|(<=)|(>=))");
20 var singleDelimiters = new RegExp('^[\\.,]');
21 var brakets = new RegExp('^[\\(\\)]');
22 var identifiers = new RegExp("^[A-Za-z][_A-Za-z0-9]*");
24 var openingKeywords = ['class','sub','select','while','if','function', 'property', 'with', 'for'];
25 var middleKeywords = ['else','elseif','case'];
26 var endKeywords = ['next','loop','wend'];
28 var wordOperators = wordRegexp(['and', 'or', 'not', 'xor', 'is', 'mod', 'eqv', 'imp']);
29 var commonkeywords = ['dim', 'redim', 'then', 'until', 'randomize',
30 'byval','byref','new','property', 'exit', 'in',
31 'const','private', 'public',
32 'get','set','let', 'stop', 'on error resume next', 'on error goto 0', 'option explicit', 'call', 'me'];
34 //This list was from: http://msdn.microsoft.com/en-us/library/f8tbc79x(v=vs.84).aspx
35 var atomWords = ['true', 'false', 'nothing', 'empty', 'null'];
36 //This list was from: http://msdn.microsoft.com/en-us/library/3ca8tfek(v=vs.84).aspx
37 var builtinFuncsWords = ['abs', 'array', 'asc', 'atn', 'cbool', 'cbyte', 'ccur', 'cdate', 'cdbl', 'chr', 'cint', 'clng', 'cos', 'csng', 'cstr', 'date', 'dateadd', 'datediff', 'datepart',
38 'dateserial', 'datevalue', 'day', 'escape', 'eval', 'execute', 'exp', 'filter', 'formatcurrency', 'formatdatetime', 'formatnumber', 'formatpercent', 'getlocale', 'getobject',
39 'getref', 'hex', 'hour', 'inputbox', 'instr', 'instrrev', 'int', 'fix', 'isarray', 'isdate', 'isempty', 'isnull', 'isnumeric', 'isobject', 'join', 'lbound', 'lcase', 'left',
40 'len', 'loadpicture', 'log', 'ltrim', 'rtrim', 'trim', 'maths', 'mid', 'minute', 'month', 'monthname', 'msgbox', 'now', 'oct', 'replace', 'rgb', 'right', 'rnd', 'round',
41 'scriptengine', 'scriptenginebuildversion', 'scriptenginemajorversion', 'scriptengineminorversion', 'second', 'setlocale', 'sgn', 'sin', 'space', 'split', 'sqr', 'strcomp',
42 'string', 'strreverse', 'tan', 'time', 'timer', 'timeserial', 'timevalue', 'typename', 'ubound', 'ucase', 'unescape', 'vartype', 'weekday', 'weekdayname', 'year'];
44 //This list was from: http://msdn.microsoft.com/en-us/library/ydz4cfk3(v=vs.84).aspx
45 var builtinConsts = ['vbBlack', 'vbRed', 'vbGreen', 'vbYellow', 'vbBlue', 'vbMagenta', 'vbCyan', 'vbWhite', 'vbBinaryCompare', 'vbTextCompare',
46 'vbSunday', 'vbMonday', 'vbTuesday', 'vbWednesday', 'vbThursday', 'vbFriday', 'vbSaturday', 'vbUseSystemDayOfWeek', 'vbFirstJan1', 'vbFirstFourDays', 'vbFirstFullWeek',
47 'vbGeneralDate', 'vbLongDate', 'vbShortDate', 'vbLongTime', 'vbShortTime', 'vbObjectError',
48 'vbOKOnly', 'vbOKCancel', 'vbAbortRetryIgnore', 'vbYesNoCancel', 'vbYesNo', 'vbRetryCancel', 'vbCritical', 'vbQuestion', 'vbExclamation', 'vbInformation', 'vbDefaultButton1', 'vbDefaultButton2',
49 'vbDefaultButton3', 'vbDefaultButton4', 'vbApplicationModal', 'vbSystemModal', 'vbOK', 'vbCancel', 'vbAbort', 'vbRetry', 'vbIgnore', 'vbYes', 'vbNo',
50 'vbCr', 'VbCrLf', 'vbFormFeed', 'vbLf', 'vbNewLine', 'vbNullChar', 'vbNullString', 'vbTab', 'vbVerticalTab', 'vbUseDefault', 'vbTrue', 'vbFalse',
51 'vbEmpty', 'vbNull', 'vbInteger', 'vbLong', 'vbSingle', 'vbDouble', 'vbCurrency', 'vbDate', 'vbString', 'vbObject', 'vbError', 'vbBoolean', 'vbVariant', 'vbDataObject', 'vbDecimal', 'vbByte', 'vbArray'];
52 //This list was from: http://msdn.microsoft.com/en-us/library/hkc375ea(v=vs.84).aspx
53 var builtinObjsWords = ['WScript', 'err', 'debug', 'RegExp'];
54 var knownProperties = ['description', 'firstindex', 'global', 'helpcontext', 'helpfile', 'ignorecase', 'length', 'number', 'pattern', 'source', 'value', 'count'];
55 var knownMethods = ['clear', 'execute', 'raise', 'replace', 'test', 'write', 'writeline', 'close', 'open', 'state', 'eof', 'update', 'addnew', 'end', 'createobject', 'quit'];
57 var aspBuiltinObjsWords = ['server', 'response', 'request', 'session', 'application'];
58 var aspKnownProperties = ['buffer', 'cachecontrol', 'charset', 'contenttype', 'expires', 'expiresabsolute', 'isclientconnected', 'pics', 'status', //response
59 'clientcertificate', 'cookies', 'form', 'querystring', 'servervariables', 'totalbytes', //request
60 'contents', 'staticobjects', //application
61 'codepage', 'lcid', 'sessionid', 'timeout', //session
62 'scripttimeout']; //server
63 var aspKnownMethods = ['addheader', 'appendtolog', 'binarywrite', 'end', 'flush', 'redirect', //response
64 'binaryread', //request
65 'remove', 'removeall', 'lock', 'unlock', //application
67 'getlasterror', 'htmlencode', 'mappath', 'transfer', 'urlencode']; //server
69 var knownWords = knownMethods.concat(knownProperties);
71 builtinObjsWords = builtinObjsWords.concat(builtinConsts);
74 builtinObjsWords = builtinObjsWords.concat(aspBuiltinObjsWords);
75 knownWords = knownWords.concat(aspKnownMethods, aspKnownProperties);
78 var keywords = wordRegexp(commonkeywords);
79 var atoms = wordRegexp(atomWords);
80 var builtinFuncs = wordRegexp(builtinFuncsWords);
81 var builtinObjs = wordRegexp(builtinObjsWords);
82 var known = wordRegexp(knownWords);
83 var stringPrefixes = '"';
85 var opening = wordRegexp(openingKeywords);
86 var middle = wordRegexp(middleKeywords);
87 var closing = wordRegexp(endKeywords);
88 var doubleClosing = wordRegexp(['end']);
89 var doOpening = wordRegexp(['do']);
90 var noIndentWords = wordRegexp(['on error resume next', 'exit']);
91 var comment = wordRegexp(['rem']);
94 function indent(_stream, state) {
95 state.currentIndent++;
98 function dedent(_stream, state) {
99 state.currentIndent--;
102 function tokenBase(stream, state) {
103 if (stream.eatSpace()) {
108 var ch = stream.peek();
115 if (stream.match(comment)){
121 // Handle Number Literals
122 if (stream.match(/^((&H)|(&O))?[0-9\.]/i, false) && !stream.match(/^((&H)|(&O))?[0-9\.]+[a-z_]/i, false)) {
123 var floatLiteral = false;
125 if (stream.match(/^\d*\.\d+/i)) { floatLiteral = true; }
126 else if (stream.match(/^\d+\.\d*/)) { floatLiteral = true; }
127 else if (stream.match(/^\.\d+/)) { floatLiteral = true; }
130 // Float literals may be "imaginary"
135 var intLiteral = false;
137 if (stream.match(/^&H[0-9a-f]+/i)) { intLiteral = true; }
139 else if (stream.match(/^&O[0-7]+/i)) { intLiteral = true; }
141 else if (stream.match(/^[1-9]\d*F?/)) {
142 // Decimal literals may be "imaginary"
144 // TODO - Can you have imaginary longs?
147 // Zero by itself with no other piece of number.
148 else if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
150 // Integer literals may be "long"
157 if (stream.match(stringPrefixes)) {
158 state.tokenize = tokenStringFactory(stream.current());
159 return state.tokenize(stream, state);
162 // Handle operators and Delimiters
163 if (stream.match(doubleOperators)
164 || stream.match(singleOperators)
165 || stream.match(wordOperators)) {
168 if (stream.match(singleDelimiters)) {
172 if (stream.match(brakets)) {
176 if (stream.match(noIndentWords)) {
177 state.doInCurrentLine = true;
182 if (stream.match(doOpening)) {
183 indent(stream,state);
184 state.doInCurrentLine = true;
188 if (stream.match(opening)) {
189 if (! state.doInCurrentLine)
190 indent(stream,state);
192 state.doInCurrentLine = false;
196 if (stream.match(middle)) {
201 if (stream.match(doubleClosing)) {
202 dedent(stream,state);
203 dedent(stream,state);
207 if (stream.match(closing)) {
208 if (! state.doInCurrentLine)
209 dedent(stream,state);
211 state.doInCurrentLine = false;
216 if (stream.match(keywords)) {
220 if (stream.match(atoms)) {
224 if (stream.match(known)) {
228 if (stream.match(builtinFuncs)) {
232 if (stream.match(builtinObjs)){
236 if (stream.match(identifiers)) {
240 // Handle non-detected items
245 function tokenStringFactory(delimiter) {
246 var singleline = delimiter.length == 1;
247 var OUTCLASS = 'string';
249 return function(stream, state) {
250 while (!stream.eol()) {
251 stream.eatWhile(/[^'"]/);
252 if (stream.match(delimiter)) {
253 state.tokenize = tokenBase;
260 if (parserConf.singleLineStringErrors) {
263 state.tokenize = tokenBase;
271 function tokenLexer(stream, state) {
272 var style = state.tokenize(stream, state);
273 var current = stream.current();
275 // Handle '.' connected identifiers
276 if (current === '.') {
277 style = state.tokenize(stream, state);
279 current = stream.current();
280 if (style.substr(0, 8) === 'variable' || style==='builtin' || style==='keyword'){//|| knownWords.indexOf(current.substring(1)) > -1) {
281 if (style === 'builtin' || style === 'keyword') style='variable';
282 if (knownWords.indexOf(current.substr(1)) > -1) style='variable-2';
294 electricChars:"dDpPtTfFeE ",
295 startState: function() {
301 doInCurrentLine: false,
308 token: function(stream, state) {
310 state.currentIndent += state.nextLineIndent;
311 state.nextLineIndent = 0;
312 state.doInCurrentLine = 0;
314 var style = tokenLexer(stream, state);
316 state.lastToken = {style:style, content: stream.current()};
318 if (style==='space') style=null;
323 indent: function(state, textAfter) {
324 var trueText = textAfter.replace(/^\s+|\s+$/g, '') ;
325 if (trueText.match(closing) || trueText.match(doubleClosing) || trueText.match(middle)) return conf.indentUnit*(state.currentIndent-1);
326 if(state.currentIndent < 0) return 0;
327 return state.currentIndent * conf.indentUnit;
334 CodeMirror.defineMIME("text/vbscript", "vbscript");