| 1 |
function wordRegexp(words, end, pre) { |
| 2 |
if (typeof pre === "undefined") pre = ""; |
| 3 |
if (typeof end === "undefined") { end = "\\b"; } |
| 4 |
return new RegExp("^" + pre + "((" + words.join(")|(") + "))" + end); |
| 5 |
} |
| 6 |
|
| 7 |
var octChar = "\\\\[0-7]{1,3}"; |
| 8 |
var hexChar = "\\\\x[A-Fa-f0-9]{1,2}"; |
| 9 |
var sChar = "\\\\[abefnrtv0%?'\"\\\\]"; |
| 10 |
var uChar = "([^\\u0027\\u005C\\uD800-\\uDFFF]|[\\uD800-\\uDFFF][\\uDC00-\\uDFFF])"; |
| 11 |
|
| 12 |
var asciiOperatorsList = [ |
| 13 |
"[<>]:", "[<>=]=", "<<=?", ">>>?=?", "=>", "--?>", "<--[->]?", "\\/\\/", |
| 14 |
"\\.{2,3}", "[\\.\\\\%*+\\-<>!\\/^|&]=?", "\\?", "\\$", "~", ":" |
| 15 |
]; |
| 16 |
var operators = wordRegexp([ |
| 17 |
"[<>]:", "[<>=]=", "[!=]==", "<<=?", ">>>?=?", "=>?", "--?>", "<--[->]?", "\\/\\/", |
| 18 |
"[\\\\%*+\\-<>!\\/^|&\\u00F7\\u22BB]=?", "\\?", "\\$", "~", ":", |
| 19 |
"\\u00D7", "\\u2208", "\\u2209", "\\u220B", "\\u220C", "\\u2218", |
| 20 |
"\\u221A", "\\u221B", "\\u2229", "\\u222A", "\\u2260", "\\u2264", |
| 21 |
"\\u2265", "\\u2286", "\\u2288", "\\u228A", "\\u22C5", |
| 22 |
"\\b(in|isa)\\b(?!\.?\\()" |
| 23 |
], ""); |
| 24 |
var delimiters = /^[;,()[\]{}]/; |
| 25 |
var identifiers = /^[_A-Za-z\u00A1-\u2217\u2219-\uFFFF][\w\u00A1-\u2217\u2219-\uFFFF]*!*/; |
| 26 |
|
| 27 |
var chars = wordRegexp([octChar, hexChar, sChar, uChar], "'"); |
| 28 |
|
| 29 |
var openersList = ["begin", "function", "type", "struct", "immutable", "let", |
| 30 |
"macro", "for", "while", "quote", "if", "else", "elseif", "try", |
| 31 |
"finally", "catch", "do"]; |
| 32 |
|
| 33 |
var closersList = ["end", "else", "elseif", "catch", "finally"]; |
| 34 |
|
| 35 |
var keywordsList = ["if", "else", "elseif", "while", "for", "begin", "let", |
| 36 |
"end", "do", "try", "catch", "finally", "return", "break", "continue", |
| 37 |
"global", "local", "const", "export", "import", "importall", "using", |
| 38 |
"function", "where", "macro", "module", "baremodule", "struct", "type", |
| 39 |
"mutable", "immutable", "quote", "typealias", "abstract", "primitive", |
| 40 |
"bitstype"]; |
| 41 |
|
| 42 |
var builtinsList = ["true", "false", "nothing", "NaN", "Inf"]; |
| 43 |
|
| 44 |
var openers = wordRegexp(openersList); |
| 45 |
var closers = wordRegexp(closersList); |
| 46 |
var keywords = wordRegexp(keywordsList); |
| 47 |
var builtins = wordRegexp(builtinsList); |
| 48 |
|
| 49 |
var macro = /^@[_A-Za-z\u00A1-\uFFFF][\w\u00A1-\uFFFF]*!*/; |
| 50 |
var symbol = /^:[_A-Za-z\u00A1-\uFFFF][\w\u00A1-\uFFFF]*!*/; |
| 51 |
var stringPrefixes = /^(`|([_A-Za-z\u00A1-\uFFFF]*"("")?))/; |
| 52 |
|
| 53 |
var macroOperators = wordRegexp(asciiOperatorsList, "", "@"); |
| 54 |
var symbolOperators = wordRegexp(asciiOperatorsList, "", ":"); |
| 55 |
|
| 56 |
function inArray(state) { |
| 57 |
return (state.nestedArrays > 0); |
| 58 |
} |
| 59 |
|
| 60 |
function inGenerator(state) { |
| 61 |
return (state.nestedGenerators > 0); |
| 62 |
} |
| 63 |
|
| 64 |
function currentScope(state, n) { |
| 65 |
if (typeof(n) === "undefined") { n = 0; } |
| 66 |
if (state.scopes.length <= n) { |
| 67 |
return null; |
| 68 |
} |
| 69 |
return state.scopes[state.scopes.length - (n + 1)]; |
| 70 |
} |
| 71 |
|
| 72 |
// tokenizers |
| 73 |
function tokenBase(stream, state) { |
| 74 |
// Handle multiline comments |
| 75 |
if (stream.match('#=', false)) { |
| 76 |
state.tokenize = tokenComment; |
| 77 |
return state.tokenize(stream, state); |
| 78 |
} |
| 79 |
|
| 80 |
// Handle scope changes |
| 81 |
var leavingExpr = state.leavingExpr; |
| 82 |
if (stream.sol()) { |
| 83 |
leavingExpr = false; |
| 84 |
} |
| 85 |
state.leavingExpr = false; |
| 86 |
|
| 87 |
if (leavingExpr) { |
| 88 |
if (stream.match(/^'+/)) { |
| 89 |
return "operator"; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (stream.match(/\.{4,}/)) { |
| 94 |
return "error"; |
| 95 |
} else if (stream.match(/\.{1,3}/)) { |
| 96 |
return "operator"; |
| 97 |
} |
| 98 |
|
| 99 |
if (stream.eatSpace()) { |
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
var ch = stream.peek(); |
| 104 |
|
| 105 |
// Handle single line comments |
| 106 |
if (ch === '#') { |
| 107 |
stream.skipToEnd(); |
| 108 |
return "comment"; |
| 109 |
} |
| 110 |
|
| 111 |
if (ch === '[') { |
| 112 |
state.scopes.push('['); |
| 113 |
state.nestedArrays++; |
| 114 |
} |
| 115 |
|
| 116 |
if (ch === '(') { |
| 117 |
state.scopes.push('('); |
| 118 |
state.nestedGenerators++; |
| 119 |
} |
| 120 |
|
| 121 |
if (inArray(state) && ch === ']') { |
| 122 |
while (state.scopes.length && currentScope(state) !== "[") { state.scopes.pop(); } |
| 123 |
state.scopes.pop(); |
| 124 |
state.nestedArrays--; |
| 125 |
state.leavingExpr = true; |
| 126 |
} |
| 127 |
|
| 128 |
if (inGenerator(state) && ch === ')') { |
| 129 |
while (state.scopes.length && currentScope(state) !== "(") { state.scopes.pop(); } |
| 130 |
state.scopes.pop(); |
| 131 |
state.nestedGenerators--; |
| 132 |
state.leavingExpr = true; |
| 133 |
} |
| 134 |
|
| 135 |
if (inArray(state)) { |
| 136 |
if (state.lastToken == "end" && stream.match(':')) { |
| 137 |
return "operator"; |
| 138 |
} |
| 139 |
if (stream.match('end')) { |
| 140 |
return "number"; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
var match; |
| 145 |
if (match = stream.match(openers, false)) { |
| 146 |
state.scopes.push(match[0]); |
| 147 |
} |
| 148 |
|
| 149 |
if (stream.match(closers, false)) { |
| 150 |
state.scopes.pop(); |
| 151 |
} |
| 152 |
|
| 153 |
// Handle type annotations |
| 154 |
if (stream.match(/^::(?![:\$])/)) { |
| 155 |
state.tokenize = tokenAnnotation; |
| 156 |
return state.tokenize(stream, state); |
| 157 |
} |
| 158 |
|
| 159 |
// Handle symbols |
| 160 |
if (!leavingExpr && (stream.match(symbol) || stream.match(symbolOperators))) { |
| 161 |
return "builtin"; |
| 162 |
} |
| 163 |
|
| 164 |
// Handle parametric types |
| 165 |
//if (stream.match(/^{[^}]*}(?=\()/)) { |
| 166 |
// return "builtin"; |
| 167 |
//} |
| 168 |
|
| 169 |
// Handle operators and Delimiters |
| 170 |
if (stream.match(operators)) { |
| 171 |
return "operator"; |
| 172 |
} |
| 173 |
|
| 174 |
// Handle Number Literals |
| 175 |
if (stream.match(/^\.?\d/, false)) { |
| 176 |
var imMatcher = RegExp(/^im\b/); |
| 177 |
var numberLiteral = false; |
| 178 |
if (stream.match(/^0x\.[0-9a-f_]+p[\+\-]?[_\d]+/i)) { numberLiteral = true; } |
| 179 |
// Integers |
| 180 |
if (stream.match(/^0x[0-9a-f_]+/i)) { numberLiteral = true; } // Hex |
| 181 |
if (stream.match(/^0b[01_]+/i)) { numberLiteral = true; } // Binary |
| 182 |
if (stream.match(/^0o[0-7_]+/i)) { numberLiteral = true; } // Octal |
| 183 |
// Floats |
| 184 |
if (stream.match(/^(?:(?:\d[_\d]*)?\.(?!\.)(?:\d[_\d]*)?|\d[_\d]*\.(?!\.)(?:\d[_\d]*))?([Eef][\+\-]?[_\d]+)?/i)) { numberLiteral = true; } |
| 185 |
if (stream.match(/^\d[_\d]*(e[\+\-]?\d+)?/i)) { numberLiteral = true; } // Decimal |
| 186 |
if (numberLiteral) { |
| 187 |
// Integer literals may be "long" |
| 188 |
stream.match(imMatcher); |
| 189 |
state.leavingExpr = true; |
| 190 |
return "number"; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// Handle Chars |
| 195 |
if (stream.match("'")) { |
| 196 |
state.tokenize = tokenChar; |
| 197 |
return state.tokenize(stream, state); |
| 198 |
} |
| 199 |
|
| 200 |
// Handle Strings |
| 201 |
if (stream.match(stringPrefixes)) { |
| 202 |
state.tokenize = tokenStringFactory(stream.current()); |
| 203 |
return state.tokenize(stream, state); |
| 204 |
} |
| 205 |
|
| 206 |
if (stream.match(macro) || stream.match(macroOperators)) { |
| 207 |
return "meta"; |
| 208 |
} |
| 209 |
|
| 210 |
if (stream.match(delimiters)) { |
| 211 |
return null; |
| 212 |
} |
| 213 |
|
| 214 |
if (stream.match(keywords)) { |
| 215 |
return "keyword"; |
| 216 |
} |
| 217 |
|
| 218 |
if (stream.match(builtins)) { |
| 219 |
return "builtin"; |
| 220 |
} |
| 221 |
|
| 222 |
var isDefinition = state.isDefinition || state.lastToken == "function" || |
| 223 |
state.lastToken == "macro" || state.lastToken == "type" || |
| 224 |
state.lastToken == "struct" || state.lastToken == "immutable"; |
| 225 |
|
| 226 |
if (stream.match(identifiers)) { |
| 227 |
if (isDefinition) { |
| 228 |
if (stream.peek() === '.') { |
| 229 |
state.isDefinition = true; |
| 230 |
return "variable"; |
| 231 |
} |
| 232 |
state.isDefinition = false; |
| 233 |
return "def"; |
| 234 |
} |
| 235 |
state.leavingExpr = true; |
| 236 |
return "variable"; |
| 237 |
} |
| 238 |
|
| 239 |
// Handle non-detected items |
| 240 |
stream.next(); |
| 241 |
return "error"; |
| 242 |
} |
| 243 |
|
| 244 |
function tokenAnnotation(stream, state) { |
| 245 |
stream.match(/.*?(?=[,;{}()=\s]|$)/); |
| 246 |
if (stream.match('{')) { |
| 247 |
state.nestedParameters++; |
| 248 |
} else if (stream.match('}') && state.nestedParameters > 0) { |
| 249 |
state.nestedParameters--; |
| 250 |
} |
| 251 |
if (state.nestedParameters > 0) { |
| 252 |
stream.match(/.*?(?={|})/) || stream.next(); |
| 253 |
} else if (state.nestedParameters == 0) { |
| 254 |
state.tokenize = tokenBase; |
| 255 |
} |
| 256 |
return "builtin"; |
| 257 |
} |
| 258 |
|
| 259 |
function tokenComment(stream, state) { |
| 260 |
if (stream.match('#=')) { |
| 261 |
state.nestedComments++; |
| 262 |
} |
| 263 |
if (!stream.match(/.*?(?=(#=|=#))/)) { |
| 264 |
stream.skipToEnd(); |
| 265 |
} |
| 266 |
if (stream.match('=#')) { |
| 267 |
state.nestedComments--; |
| 268 |
if (state.nestedComments == 0) |
| 269 |
state.tokenize = tokenBase; |
| 270 |
} |
| 271 |
return "comment"; |
| 272 |
} |
| 273 |
|
| 274 |
function tokenChar(stream, state) { |
| 275 |
var isChar = false, match; |
| 276 |
if (stream.match(chars)) { |
| 277 |
isChar = true; |
| 278 |
} else if (match = stream.match(/\\u([a-f0-9]{1,4})(?=')/i)) { |
| 279 |
var value = parseInt(match[1], 16); |
| 280 |
if (value <= 55295 || value >= 57344) { // (U+0,U+D7FF), (U+E000,U+FFFF) |
| 281 |
isChar = true; |
| 282 |
stream.next(); |
| 283 |
} |
| 284 |
} else if (match = stream.match(/\\U([A-Fa-f0-9]{5,8})(?=')/)) { |
| 285 |
var value = parseInt(match[1], 16); |
| 286 |
if (value <= 1114111) { // U+10FFFF |
| 287 |
isChar = true; |
| 288 |
stream.next(); |
| 289 |
} |
| 290 |
} |
| 291 |
if (isChar) { |
| 292 |
state.leavingExpr = true; |
| 293 |
state.tokenize = tokenBase; |
| 294 |
return "string"; |
| 295 |
} |
| 296 |
if (!stream.match(/^[^']+(?=')/)) { stream.skipToEnd(); } |
| 297 |
if (stream.match("'")) { state.tokenize = tokenBase; } |
| 298 |
return "error"; |
| 299 |
} |
| 300 |
|
| 301 |
function tokenStringFactory(delimiter) { |
| 302 |
if (delimiter.substr(-3) === '"""') { |
| 303 |
delimiter = '"""'; |
| 304 |
} else if (delimiter.substr(-1) === '"') { |
| 305 |
delimiter = '"'; |
| 306 |
} |
| 307 |
function tokenString(stream, state) { |
| 308 |
if (stream.eat('\\')) { |
| 309 |
stream.next(); |
| 310 |
} else if (stream.match(delimiter)) { |
| 311 |
state.tokenize = tokenBase; |
| 312 |
state.leavingExpr = true; |
| 313 |
return "string"; |
| 314 |
} else { |
| 315 |
stream.eat(/[`"]/); |
| 316 |
} |
| 317 |
stream.eatWhile(/[^\\`"]/); |
| 318 |
return "string"; |
| 319 |
} |
| 320 |
return tokenString; |
| 321 |
} |
| 322 |
|
| 323 |
export const julia = { |
| 324 |
name: "julia", |
| 325 |
startState: function() { |
| 326 |
return { |
| 327 |
tokenize: tokenBase, |
| 328 |
scopes: [], |
| 329 |
lastToken: null, |
| 330 |
leavingExpr: false, |
| 331 |
isDefinition: false, |
| 332 |
nestedArrays: 0, |
| 333 |
nestedComments: 0, |
| 334 |
nestedGenerators: 0, |
| 335 |
nestedParameters: 0, |
| 336 |
firstParenPos: -1 |
| 337 |
}; |
| 338 |
}, |
| 339 |
|
| 340 |
token: function(stream, state) { |
| 341 |
var style = state.tokenize(stream, state); |
| 342 |
var current = stream.current(); |
| 343 |
|
| 344 |
if (current && style) { |
| 345 |
state.lastToken = current; |
| 346 |
} |
| 347 |
|
| 348 |
return style; |
| 349 |
}, |
| 350 |
|
| 351 |
indent: function(state, textAfter, cx) { |
| 352 |
var delta = 0; |
| 353 |
if ( textAfter === ']' || textAfter === ')' || /^end\b/.test(textAfter) || |
| 354 |
/^else/.test(textAfter) || /^catch\b/.test(textAfter) || /^elseif\b/.test(textAfter) || |
| 355 |
/^finally/.test(textAfter) ) { |
| 356 |
delta = -1; |
| 357 |
} |
| 358 |
return (state.scopes.length + delta) * cx.unit; |
| 359 |
}, |
| 360 |
|
| 361 |
languageData: { |
| 362 |
indentOnInput: /^\s*(end|else|catch|finally)\b$/, |
| 363 |
commentTokens: {line: "#", block: {open: "#=", close: "=#"}}, |
| 364 |
closeBrackets: {brackets: ["(", "[", "{", '"']}, |
| 365 |
autocomplete: keywordsList.concat(builtinsList) |
| 366 |
} |
| 367 |
}; |