Fix: merge conflict
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / mode / vbscript / vbscript.js
1 /*
2 For extra ASP classic objects, initialize CodeMirror instance with this option:
3     isASP: true
4
5 E.G.:
6     var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
7         lineNumbers: true,
8         isASP: true
9       });
10 */
11 CodeMirror.defineMode("vbscript", function(conf, parserConf) {
12     var ERRORCLASS = 'error';
13
14     function wordRegexp(words) {
15         return new RegExp("^((" + words.join(")|(") + "))\\b", "i");
16     }
17
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]*");
23
24     var openingKeywords = ['class','sub','select','while','if','function', 'property', 'with', 'for'];
25     var middleKeywords = ['else','elseif','case'];
26     var endKeywords = ['next','loop','wend'];
27
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'];
33
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'];
43
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'];
56
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
66                            'abandon', //session
67                            'getlasterror', 'htmlencode', 'mappath', 'transfer', 'urlencode']; //server
68
69     var knownWords = knownMethods.concat(knownProperties);
70
71     builtinObjsWords = builtinObjsWords.concat(builtinConsts);
72
73     if (conf.isASP){
74         builtinObjsWords = builtinObjsWords.concat(aspBuiltinObjsWords);
75         knownWords = knownWords.concat(aspKnownMethods, aspKnownProperties);
76     };
77
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 = '"';
84
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']);
92
93
94     function indent(_stream, state) {
95       state.currentIndent++;
96     }
97
98     function dedent(_stream, state) {
99       state.currentIndent--;
100     }
101     // tokenizers
102     function tokenBase(stream, state) {
103         if (stream.eatSpace()) {
104             return 'space';
105             //return null;
106         }
107
108         var ch = stream.peek();
109
110         // Handle Comments
111         if (ch === "'") {
112             stream.skipToEnd();
113             return 'comment';
114         }
115         if (stream.match(comment)){
116             stream.skipToEnd();
117             return 'comment';
118         }
119
120
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;
124             // Floats
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; }
128
129             if (floatLiteral) {
130                 // Float literals may be "imaginary"
131                 stream.eat(/J/i);
132                 return 'number';
133             }
134             // Integers
135             var intLiteral = false;
136             // Hex
137             if (stream.match(/^&H[0-9a-f]+/i)) { intLiteral = true; }
138             // Octal
139             else if (stream.match(/^&O[0-7]+/i)) { intLiteral = true; }
140             // Decimal
141             else if (stream.match(/^[1-9]\d*F?/)) {
142                 // Decimal literals may be "imaginary"
143                 stream.eat(/J/i);
144                 // TODO - Can you have imaginary longs?
145                 intLiteral = true;
146             }
147             // Zero by itself with no other piece of number.
148             else if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
149             if (intLiteral) {
150                 // Integer literals may be "long"
151                 stream.eat(/L/i);
152                 return 'number';
153             }
154         }
155
156         // Handle Strings
157         if (stream.match(stringPrefixes)) {
158             state.tokenize = tokenStringFactory(stream.current());
159             return state.tokenize(stream, state);
160         }
161
162         // Handle operators and Delimiters
163         if (stream.match(doubleOperators)
164             || stream.match(singleOperators)
165             || stream.match(wordOperators)) {
166             return 'operator';
167         }
168         if (stream.match(singleDelimiters)) {
169             return null;
170         }
171
172         if (stream.match(brakets)) {
173             return "bracket";
174         }
175
176         if (stream.match(noIndentWords)) {
177             state.doInCurrentLine = true;
178
179             return 'keyword';
180         }
181
182         if (stream.match(doOpening)) {
183             indent(stream,state);
184             state.doInCurrentLine = true;
185
186             return 'keyword';
187         }
188         if (stream.match(opening)) {
189             if (! state.doInCurrentLine)
190               indent(stream,state);
191             else
192               state.doInCurrentLine = false;
193
194             return 'keyword';
195         }
196         if (stream.match(middle)) {
197             return 'keyword';
198         }
199
200
201         if (stream.match(doubleClosing)) {
202             dedent(stream,state);
203             dedent(stream,state);
204
205             return 'keyword';
206         }
207         if (stream.match(closing)) {
208             if (! state.doInCurrentLine)
209               dedent(stream,state);
210             else
211               state.doInCurrentLine = false;
212
213             return 'keyword';
214         }
215
216         if (stream.match(keywords)) {
217             return 'keyword';
218         }
219
220         if (stream.match(atoms)) {
221             return 'atom';
222         }
223
224         if (stream.match(known)) {
225             return 'variable-2';
226         }
227
228         if (stream.match(builtinFuncs)) {
229             return 'builtin';
230         }
231
232         if (stream.match(builtinObjs)){
233             return 'variable-2';
234         }
235
236         if (stream.match(identifiers)) {
237             return 'variable';
238         }
239
240         // Handle non-detected items
241         stream.next();
242         return ERRORCLASS;
243     }
244
245     function tokenStringFactory(delimiter) {
246         var singleline = delimiter.length == 1;
247         var OUTCLASS = 'string';
248
249         return function(stream, state) {
250             while (!stream.eol()) {
251                 stream.eatWhile(/[^'"]/);
252                 if (stream.match(delimiter)) {
253                     state.tokenize = tokenBase;
254                     return OUTCLASS;
255                 } else {
256                     stream.eat(/['"]/);
257                 }
258             }
259             if (singleline) {
260                 if (parserConf.singleLineStringErrors) {
261                     return ERRORCLASS;
262                 } else {
263                     state.tokenize = tokenBase;
264                 }
265             }
266             return OUTCLASS;
267         };
268     }
269
270
271     function tokenLexer(stream, state) {
272         var style = state.tokenize(stream, state);
273         var current = stream.current();
274
275         // Handle '.' connected identifiers
276         if (current === '.') {
277             style = state.tokenize(stream, state);
278
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';
283
284                 return style;
285             } else {
286                 return ERRORCLASS;
287             }
288         }
289
290         return style;
291     }
292
293     var external = {
294         electricChars:"dDpPtTfFeE ",
295         startState: function() {
296             return {
297               tokenize: tokenBase,
298               lastToken: null,
299               currentIndent: 0,
300               nextLineIndent: 0,
301               doInCurrentLine: false,
302               ignoreKeyword: false
303
304
305           };
306         },
307
308         token: function(stream, state) {
309             if (stream.sol()) {
310               state.currentIndent += state.nextLineIndent;
311               state.nextLineIndent = 0;
312               state.doInCurrentLine = 0;
313             }
314             var style = tokenLexer(stream, state);
315
316             state.lastToken = {style:style, content: stream.current()};
317
318             if (style==='space') style=null;
319
320             return style;
321         },
322
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;
328         }
329
330     };
331     return external;
332 });
333
334 CodeMirror.defineMIME("text/vbscript", "vbscript");