| 1 |
function wordRegexp(words) { |
| 2 |
return new RegExp("^((" + words.join(")|(") + "))\\b"); |
| 3 |
} |
| 4 |
|
| 5 |
var wordOperators = wordRegexp(["and", "or", "not", "is"]); |
| 6 |
var commonKeywords = ["as", "assert", "break", "class", "continue", |
| 7 |
"def", "del", "elif", "else", "except", "finally", |
| 8 |
"for", "from", "global", "if", "import", |
| 9 |
"lambda", "pass", "raise", "return", |
| 10 |
"try", "while", "with", "yield", "in", "False", "True"]; |
| 11 |
var commonBuiltins = ["abs", "all", "any", "bin", "bool", "bytearray", "callable", "chr", |
| 12 |
"classmethod", "compile", "complex", "delattr", "dict", "dir", "divmod", |
| 13 |
"enumerate", "eval", "filter", "float", "format", "frozenset", |
| 14 |
"getattr", "globals", "hasattr", "hash", "help", "hex", "id", |
| 15 |
"input", "int", "isinstance", "issubclass", "iter", "len", |
| 16 |
"list", "locals", "map", "max", "memoryview", "min", "next", |
| 17 |
"object", "oct", "open", "ord", "pow", "property", "range", |
| 18 |
"repr", "reversed", "round", "set", "setattr", "slice", |
| 19 |
"sorted", "staticmethod", "str", "sum", "super", "tuple", |
| 20 |
"type", "vars", "zip", "__import__", "NotImplemented", |
| 21 |
"Ellipsis", "__debug__"]; |
| 22 |
|
| 23 |
function top(state) { |
| 24 |
return state.scopes[state.scopes.length - 1]; |
| 25 |
} |
| 26 |
|
| 27 |
export function mkPython(parserConf) { |
| 28 |
var ERRORCLASS = "error"; |
| 29 |
|
| 30 |
var delimiters = parserConf.delimiters || parserConf.singleDelimiters || /^[\(\)\[\]\{\}@,:`=;\.\\]/; |
| 31 |
// (Backwards-compatibility with old, cumbersome config system) |
| 32 |
var operators = [parserConf.singleOperators, parserConf.doubleOperators, parserConf.doubleDelimiters, parserConf.tripleDelimiters, |
| 33 |
parserConf.operators || /^([-+*/%\/&|^]=?|[<>=]+|\/\/=?|\*\*=?|!=|[~!@]|\.\.\.)/] |
| 34 |
for (var i = 0; i < operators.length; i++) if (!operators[i]) operators.splice(i--, 1) |
| 35 |
|
| 36 |
var hangingIndent = parserConf.hangingIndent; |
| 37 |
|
| 38 |
var myKeywords = commonKeywords, myBuiltins = commonBuiltins; |
| 39 |
if (parserConf.extra_keywords != undefined) |
| 40 |
myKeywords = myKeywords.concat(parserConf.extra_keywords); |
| 41 |
|
| 42 |
if (parserConf.extra_builtins != undefined) |
| 43 |
myBuiltins = myBuiltins.concat(parserConf.extra_builtins); |
| 44 |
|
| 45 |
var py3 = !(parserConf.version && Number(parserConf.version) < 3) |
| 46 |
if (py3) { |
| 47 |
// since http://legacy.python.org/dev/peps/pep-0465/ @ is also an operator |
| 48 |
var identifiers = parserConf.identifiers|| /^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*/; |
| 49 |
myKeywords = myKeywords.concat(["nonlocal", "None", "aiter", "anext", "async", "await", "breakpoint", "match", "case"]); |
| 50 |
myBuiltins = myBuiltins.concat(["ascii", "bytes", "exec", "print"]); |
| 51 |
var stringPrefixes = new RegExp("^(([rbuf]|(br)|(rb)|(fr)|(rf))?('{3}|\"{3}|['\"]))", "i"); |
| 52 |
} else { |
| 53 |
var identifiers = parserConf.identifiers|| /^[_A-Za-z][_A-Za-z0-9]*/; |
| 54 |
myKeywords = myKeywords.concat(["exec", "print"]); |
| 55 |
myBuiltins = myBuiltins.concat(["apply", "basestring", "buffer", "cmp", "coerce", "execfile", |
| 56 |
"file", "intern", "long", "raw_input", "reduce", "reload", |
| 57 |
"unichr", "unicode", "xrange", "None"]); |
| 58 |
var stringPrefixes = new RegExp("^(([rubf]|(ur)|(br))?('{3}|\"{3}|['\"]))", "i"); |
| 59 |
} |
| 60 |
var keywords = wordRegexp(myKeywords); |
| 61 |
var builtins = wordRegexp(myBuiltins); |
| 62 |
|
| 63 |
// tokenizers |
| 64 |
function tokenBase(stream, state) { |
| 65 |
var sol = stream.sol() && state.lastToken != "\\" |
| 66 |
if (sol) state.indent = stream.indentation() |
| 67 |
// Handle scope changes |
| 68 |
if (sol && top(state).type == "py") { |
| 69 |
var scopeOffset = top(state).offset; |
| 70 |
if (stream.eatSpace()) { |
| 71 |
var lineOffset = stream.indentation(); |
| 72 |
if (lineOffset > scopeOffset) |
| 73 |
pushPyScope(stream, state); |
| 74 |
else if (lineOffset < scopeOffset && dedent(stream, state) && stream.peek() != "#") |
| 75 |
state.errorToken = true; |
| 76 |
return null; |
| 77 |
} else { |
| 78 |
var style = tokenBaseInner(stream, state); |
| 79 |
if (scopeOffset > 0 && dedent(stream, state)) |
| 80 |
style += " " + ERRORCLASS; |
| 81 |
return style; |
| 82 |
} |
| 83 |
} |
| 84 |
return tokenBaseInner(stream, state); |
| 85 |
} |
| 86 |
|
| 87 |
function tokenBaseInner(stream, state, inFormat) { |
| 88 |
if (stream.eatSpace()) return null; |
| 89 |
|
| 90 |
// Handle Comments |
| 91 |
if (!inFormat && stream.match(/^#.*/)) return "comment"; |
| 92 |
|
| 93 |
// Handle Number Literals |
| 94 |
if (stream.match(/^[0-9\.]/, false)) { |
| 95 |
var floatLiteral = false; |
| 96 |
// Floats |
| 97 |
if (stream.match(/^[\d_]*\.\d+(e[\+\-]?\d+)?/i)) { floatLiteral = true; } |
| 98 |
if (stream.match(/^[\d_]+\.\d*/)) { floatLiteral = true; } |
| 99 |
if (stream.match(/^\.\d+/)) { floatLiteral = true; } |
| 100 |
if (floatLiteral) { |
| 101 |
// Float literals may be "imaginary" |
| 102 |
stream.eat(/J/i); |
| 103 |
return "number"; |
| 104 |
} |
| 105 |
// Integers |
| 106 |
var intLiteral = false; |
| 107 |
// Hex |
| 108 |
if (stream.match(/^0x[0-9a-f_]+/i)) intLiteral = true; |
| 109 |
// Binary |
| 110 |
if (stream.match(/^0b[01_]+/i)) intLiteral = true; |
| 111 |
// Octal |
| 112 |
if (stream.match(/^0o[0-7_]+/i)) intLiteral = true; |
| 113 |
// Decimal |
| 114 |
if (stream.match(/^[1-9][\d_]*(e[\+\-]?[\d_]+)?/)) { |
| 115 |
// Decimal literals may be "imaginary" |
| 116 |
stream.eat(/J/i); |
| 117 |
// TODO - Can you have imaginary longs? |
| 118 |
intLiteral = true; |
| 119 |
} |
| 120 |
// Zero by itself with no other piece of number. |
| 121 |
if (stream.match(/^0(?![\dx])/i)) intLiteral = true; |
| 122 |
if (intLiteral) { |
| 123 |
// Integer literals may be "long" |
| 124 |
stream.eat(/L/i); |
| 125 |
return "number"; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// Handle Strings |
| 130 |
if (stream.match(stringPrefixes)) { |
| 131 |
var isFmtString = stream.current().toLowerCase().indexOf('f') !== -1; |
| 132 |
if (!isFmtString) { |
| 133 |
state.tokenize = tokenStringFactory(stream.current(), state.tokenize); |
| 134 |
return state.tokenize(stream, state); |
| 135 |
} else { |
| 136 |
state.tokenize = formatStringFactory(stream.current(), state.tokenize); |
| 137 |
return state.tokenize(stream, state); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
for (var i = 0; i < operators.length; i++) |
| 142 |
if (stream.match(operators[i])) return "operator" |
| 143 |
|
| 144 |
if (stream.match(delimiters)) return "punctuation"; |
| 145 |
|
| 146 |
if (state.lastToken == "." && stream.match(identifiers)) |
| 147 |
return "property"; |
| 148 |
|
| 149 |
if (stream.match(keywords) || stream.match(wordOperators)) |
| 150 |
return "keyword"; |
| 151 |
|
| 152 |
if (stream.match(builtins)) |
| 153 |
return "builtin"; |
| 154 |
|
| 155 |
if (stream.match(/^(self|cls)\b/)) |
| 156 |
return "self"; |
| 157 |
|
| 158 |
if (stream.match(identifiers)) { |
| 159 |
if (state.lastToken == "def" || state.lastToken == "class") |
| 160 |
return "def"; |
| 161 |
return "variable"; |
| 162 |
} |
| 163 |
|
| 164 |
// Handle non-detected items |
| 165 |
stream.next(); |
| 166 |
return inFormat ? null :ERRORCLASS; |
| 167 |
} |
| 168 |
|
| 169 |
function formatStringFactory(delimiter, tokenOuter) { |
| 170 |
while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0) |
| 171 |
delimiter = delimiter.substr(1); |
| 172 |
|
| 173 |
var singleline = delimiter.length == 1; |
| 174 |
var OUTCLASS = "string"; |
| 175 |
|
| 176 |
function tokenNestedExpr(depth) { |
| 177 |
return function(stream, state) { |
| 178 |
var inner = tokenBaseInner(stream, state, true) |
| 179 |
if (inner == "punctuation") { |
| 180 |
if (stream.current() == "{") { |
| 181 |
state.tokenize = tokenNestedExpr(depth + 1) |
| 182 |
} else if (stream.current() == "}") { |
| 183 |
if (depth > 1) state.tokenize = tokenNestedExpr(depth - 1) |
| 184 |
else state.tokenize = tokenString |
| 185 |
} |
| 186 |
} |
| 187 |
return inner |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
function tokenString(stream, state) { |
| 192 |
while (!stream.eol()) { |
| 193 |
stream.eatWhile(/[^'"\{\}\\]/); |
| 194 |
if (stream.eat("\\")) { |
| 195 |
stream.next(); |
| 196 |
if (singleline && stream.eol()) |
| 197 |
return OUTCLASS; |
| 198 |
} else if (stream.match(delimiter)) { |
| 199 |
state.tokenize = tokenOuter; |
| 200 |
return OUTCLASS; |
| 201 |
} else if (stream.match('{{')) { |
| 202 |
// ignore {{ in f-str |
| 203 |
return OUTCLASS; |
| 204 |
} else if (stream.match('{', false)) { |
| 205 |
// switch to nested mode |
| 206 |
state.tokenize = tokenNestedExpr(0) |
| 207 |
if (stream.current()) return OUTCLASS; |
| 208 |
else return state.tokenize(stream, state) |
| 209 |
} else if (stream.match('}}')) { |
| 210 |
return OUTCLASS; |
| 211 |
} else if (stream.match('}')) { |
| 212 |
// single } in f-string is an error |
| 213 |
return ERRORCLASS; |
| 214 |
} else { |
| 215 |
stream.eat(/['"]/); |
| 216 |
} |
| 217 |
} |
| 218 |
if (singleline) { |
| 219 |
if (parserConf.singleLineStringErrors) |
| 220 |
return ERRORCLASS; |
| 221 |
else |
| 222 |
state.tokenize = tokenOuter; |
| 223 |
} |
| 224 |
return OUTCLASS; |
| 225 |
} |
| 226 |
tokenString.isString = true; |
| 227 |
return tokenString; |
| 228 |
} |
| 229 |
|
| 230 |
function tokenStringFactory(delimiter, tokenOuter) { |
| 231 |
while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0) |
| 232 |
delimiter = delimiter.substr(1); |
| 233 |
|
| 234 |
var singleline = delimiter.length == 1; |
| 235 |
var OUTCLASS = "string"; |
| 236 |
|
| 237 |
function tokenString(stream, state) { |
| 238 |
while (!stream.eol()) { |
| 239 |
stream.eatWhile(/[^'"\\]/); |
| 240 |
if (stream.eat("\\")) { |
| 241 |
stream.next(); |
| 242 |
if (singleline && stream.eol()) |
| 243 |
return OUTCLASS; |
| 244 |
} else if (stream.match(delimiter)) { |
| 245 |
state.tokenize = tokenOuter; |
| 246 |
return OUTCLASS; |
| 247 |
} else { |
| 248 |
stream.eat(/['"]/); |
| 249 |
} |
| 250 |
} |
| 251 |
if (singleline) { |
| 252 |
if (parserConf.singleLineStringErrors) |
| 253 |
return ERRORCLASS; |
| 254 |
else |
| 255 |
state.tokenize = tokenOuter; |
| 256 |
} |
| 257 |
return OUTCLASS; |
| 258 |
} |
| 259 |
tokenString.isString = true; |
| 260 |
return tokenString; |
| 261 |
} |
| 262 |
|
| 263 |
function pushPyScope(stream, state) { |
| 264 |
while (top(state).type != "py") state.scopes.pop() |
| 265 |
state.scopes.push({offset: top(state).offset + stream.indentUnit, |
| 266 |
type: "py", |
| 267 |
align: null}) |
| 268 |
} |
| 269 |
|
| 270 |
function pushBracketScope(stream, state, type) { |
| 271 |
var align = stream.match(/^[\s\[\{\(]*(?:#|$)/, false) ? null : stream.column() + 1 |
| 272 |
state.scopes.push({offset: state.indent + (hangingIndent || stream.indentUnit), |
| 273 |
type: type, |
| 274 |
align: align}) |
| 275 |
} |
| 276 |
|
| 277 |
function dedent(stream, state) { |
| 278 |
var indented = stream.indentation(); |
| 279 |
while (state.scopes.length > 1 && top(state).offset > indented) { |
| 280 |
if (top(state).type != "py") return true; |
| 281 |
state.scopes.pop(); |
| 282 |
} |
| 283 |
return top(state).offset != indented; |
| 284 |
} |
| 285 |
|
| 286 |
function tokenLexer(stream, state) { |
| 287 |
if (stream.sol()) { |
| 288 |
state.beginningOfLine = true; |
| 289 |
state.dedent = false; |
| 290 |
} |
| 291 |
|
| 292 |
var style = state.tokenize(stream, state); |
| 293 |
var current = stream.current(); |
| 294 |
|
| 295 |
// Handle decorators |
| 296 |
if (state.beginningOfLine && current == "@") |
| 297 |
return stream.match(identifiers, false) ? "meta" : py3 ? "operator" : ERRORCLASS; |
| 298 |
|
| 299 |
if (/\S/.test(current)) state.beginningOfLine = false; |
| 300 |
|
| 301 |
if ((style == "variable" || style == "builtin") |
| 302 |
&& state.lastToken == "meta") |
| 303 |
style = "meta"; |
| 304 |
|
| 305 |
// Handle scope changes. |
| 306 |
if (current == "pass" || current == "return") |
| 307 |
state.dedent = true; |
| 308 |
|
| 309 |
if (current == "lambda") state.lambda = true; |
| 310 |
if (current == ":" && !state.lambda && top(state).type == "py" && stream.match(/^\s*(?:#|$)/, false)) |
| 311 |
pushPyScope(stream, state); |
| 312 |
|
| 313 |
if (current.length == 1 && !/string|comment/.test(style)) { |
| 314 |
var delimiter_index = "[({".indexOf(current); |
| 315 |
if (delimiter_index != -1) |
| 316 |
pushBracketScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1)); |
| 317 |
|
| 318 |
delimiter_index = "])}".indexOf(current); |
| 319 |
if (delimiter_index != -1) { |
| 320 |
if (top(state).type == current) state.indent = state.scopes.pop().offset - (hangingIndent || stream.indentUnit) |
| 321 |
else return ERRORCLASS; |
| 322 |
} |
| 323 |
} |
| 324 |
if (state.dedent && stream.eol() && top(state).type == "py" && state.scopes.length > 1) |
| 325 |
state.scopes.pop(); |
| 326 |
|
| 327 |
return style; |
| 328 |
} |
| 329 |
|
| 330 |
return { |
| 331 |
name: "python", |
| 332 |
|
| 333 |
startState: function() { |
| 334 |
return { |
| 335 |
tokenize: tokenBase, |
| 336 |
scopes: [{offset: 0, type: "py", align: null}], |
| 337 |
indent: 0, |
| 338 |
lastToken: null, |
| 339 |
lambda: false, |
| 340 |
dedent: 0 |
| 341 |
}; |
| 342 |
}, |
| 343 |
|
| 344 |
token: function(stream, state) { |
| 345 |
var addErr = state.errorToken; |
| 346 |
if (addErr) state.errorToken = false; |
| 347 |
var style = tokenLexer(stream, state); |
| 348 |
|
| 349 |
if (style && style != "comment") |
| 350 |
state.lastToken = (style == "keyword" || style == "punctuation") ? stream.current() : style; |
| 351 |
if (style == "punctuation") style = null; |
| 352 |
|
| 353 |
if (stream.eol() && state.lambda) |
| 354 |
state.lambda = false; |
| 355 |
return addErr ? ERRORCLASS : style; |
| 356 |
}, |
| 357 |
|
| 358 |
indent: function(state, textAfter, cx) { |
| 359 |
if (state.tokenize != tokenBase) |
| 360 |
return state.tokenize.isString ? null : 0; |
| 361 |
|
| 362 |
var scope = top(state) |
| 363 |
var closing = scope.type == textAfter.charAt(0) || |
| 364 |
scope.type == "py" && !state.dedent && /^(else:|elif |except |finally:)/.test(textAfter) |
| 365 |
if (scope.align != null) |
| 366 |
return scope.align - (closing ? 1 : 0) |
| 367 |
else |
| 368 |
return scope.offset - (closing ? hangingIndent || cx.unit : 0) |
| 369 |
}, |
| 370 |
|
| 371 |
languageData: { |
| 372 |
autocomplete: commonKeywords.concat(commonBuiltins).concat(["exec", "print"]), |
| 373 |
indentOnInput: /^\s*([\}\]\)]|else:|elif |except |finally:)$/, |
| 374 |
commentTokens: {line: "#"}, |
| 375 |
closeBrackets: {brackets: ["(", "[", "{", "'", '"', "'''", '"""']} |
| 376 |
} |
| 377 |
}; |
| 378 |
}; |
| 379 |
|
| 380 |
var words = function(str) { return str.split(" "); }; |
| 381 |
|
| 382 |
export const python = mkPython({}) |
| 383 |
|
| 384 |
export const cython = mkPython({ |
| 385 |
extra_keywords: words("by cdef cimport cpdef ctypedef enum except "+ |
| 386 |
"extern gil include nogil property public "+ |
| 387 |
"readonly struct union DEF IF ELIF ELSE") |
| 388 |
}) |