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