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