You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

python.js 15KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. function wordRegexp(words) {
  13. return new RegExp("^((" + words.join(")|(") + "))\\b");
  14. }
  15. var wordOperators = wordRegexp(["and", "or", "not", "is"]);
  16. var commonKeywords = ["as", "assert", "break", "class", "continue",
  17. "def", "del", "elif", "else", "except", "finally",
  18. "for", "from", "global", "if", "import",
  19. "lambda", "pass", "raise", "return",
  20. "try", "while", "with", "yield", "in"];
  21. var commonBuiltins = ["abs", "all", "any", "bin", "bool", "bytearray", "callable", "chr",
  22. "classmethod", "compile", "complex", "delattr", "dict", "dir", "divmod",
  23. "enumerate", "eval", "filter", "float", "format", "frozenset",
  24. "getattr", "globals", "hasattr", "hash", "help", "hex", "id",
  25. "input", "int", "isinstance", "issubclass", "iter", "len",
  26. "list", "locals", "map", "max", "memoryview", "min", "next",
  27. "object", "oct", "open", "ord", "pow", "property", "range",
  28. "repr", "reversed", "round", "set", "setattr", "slice",
  29. "sorted", "staticmethod", "str", "sum", "super", "tuple",
  30. "type", "vars", "zip", "__import__", "NotImplemented",
  31. "Ellipsis", "__debug__"];
  32. CodeMirror.registerHelper("hintWords", "python", commonKeywords.concat(commonBuiltins));
  33. function top(state) {
  34. return state.scopes[state.scopes.length - 1];
  35. }
  36. CodeMirror.defineMode("python", function(conf, parserConf) {
  37. var ERRORCLASS = "error";
  38. var delimiters = parserConf.delimiters || parserConf.singleDelimiters || /^[\(\)\[\]\{\}@,:`=;\.\\]/;
  39. // (Backwards-compatibility with old, cumbersome config system)
  40. var operators = [parserConf.singleOperators, parserConf.doubleOperators, parserConf.doubleDelimiters, parserConf.tripleDelimiters,
  41. parserConf.operators || /^([-+*/%\/&|^]=?|[<>=]+|\/\/=?|\*\*=?|!=|[~!@]|\.\.\.)/]
  42. for (var i = 0; i < operators.length; i++) if (!operators[i]) operators.splice(i--, 1)
  43. var hangingIndent = parserConf.hangingIndent || conf.indentUnit;
  44. var myKeywords = commonKeywords, myBuiltins = commonBuiltins;
  45. if (parserConf.extra_keywords != undefined)
  46. myKeywords = myKeywords.concat(parserConf.extra_keywords);
  47. if (parserConf.extra_builtins != undefined)
  48. myBuiltins = myBuiltins.concat(parserConf.extra_builtins);
  49. var py3 = !(parserConf.version && Number(parserConf.version) < 3)
  50. if (py3) {
  51. // since http://legacy.python.org/dev/peps/pep-0465/ @ is also an operator
  52. var identifiers = parserConf.identifiers|| /^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*/;
  53. myKeywords = myKeywords.concat(["nonlocal", "False", "True", "None", "async", "await"]);
  54. myBuiltins = myBuiltins.concat(["ascii", "bytes", "exec", "print"]);
  55. var stringPrefixes = new RegExp("^(([rbuf]|(br)|(rb)|(fr)|(rf))?('{3}|\"{3}|['\"]))", "i");
  56. } else {
  57. var identifiers = parserConf.identifiers|| /^[_A-Za-z][_A-Za-z0-9]*/;
  58. myKeywords = myKeywords.concat(["exec", "print"]);
  59. myBuiltins = myBuiltins.concat(["apply", "basestring", "buffer", "cmp", "coerce", "execfile",
  60. "file", "intern", "long", "raw_input", "reduce", "reload",
  61. "unichr", "unicode", "xrange", "False", "True", "None"]);
  62. var stringPrefixes = new RegExp("^(([rubf]|(ur)|(br))?('{3}|\"{3}|['\"]))", "i");
  63. }
  64. var keywords = wordRegexp(myKeywords);
  65. var builtins = wordRegexp(myBuiltins);
  66. // tokenizers
  67. function tokenBase(stream, state) {
  68. var sol = stream.sol() && state.lastToken != "\\"
  69. if (sol) state.indent = stream.indentation()
  70. // Handle scope changes
  71. if (sol && top(state).type == "py") {
  72. var scopeOffset = top(state).offset;
  73. if (stream.eatSpace()) {
  74. var lineOffset = stream.indentation();
  75. if (lineOffset > scopeOffset)
  76. pushPyScope(state);
  77. else if (lineOffset < scopeOffset && dedent(stream, state) && stream.peek() != "#")
  78. state.errorToken = true;
  79. return null;
  80. } else {
  81. var style = tokenBaseInner(stream, state);
  82. if (scopeOffset > 0 && dedent(stream, state))
  83. style += " " + ERRORCLASS;
  84. return style;
  85. }
  86. }
  87. return tokenBaseInner(stream, state);
  88. }
  89. function tokenBaseInner(stream, state, inFormat) {
  90. if (stream.eatSpace()) return null;
  91. // Handle Comments
  92. if (!inFormat && stream.match(/^#.*/)) return "comment";
  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. // Handle Strings
  129. if (stream.match(stringPrefixes)) {
  130. var isFmtString = stream.current().toLowerCase().indexOf('f') !== -1;
  131. if (!isFmtString) {
  132. state.tokenize = tokenStringFactory(stream.current(), state.tokenize);
  133. return state.tokenize(stream, state);
  134. } else {
  135. state.tokenize = formatStringFactory(stream.current(), state.tokenize);
  136. return state.tokenize(stream, state);
  137. }
  138. }
  139. for (var i = 0; i < operators.length; i++)
  140. if (stream.match(operators[i])) return "operator"
  141. if (stream.match(delimiters)) return "punctuation";
  142. if (state.lastToken == "." && stream.match(identifiers))
  143. return "property";
  144. if (stream.match(keywords) || stream.match(wordOperators))
  145. return "keyword";
  146. if (stream.match(builtins))
  147. return "builtin";
  148. if (stream.match(/^(self|cls)\b/))
  149. return "variable-2";
  150. if (stream.match(identifiers)) {
  151. if (state.lastToken == "def" || state.lastToken == "class")
  152. return "def";
  153. return "variable";
  154. }
  155. // Handle non-detected items
  156. stream.next();
  157. return inFormat ? null :ERRORCLASS;
  158. }
  159. function formatStringFactory(delimiter, tokenOuter) {
  160. while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0)
  161. delimiter = delimiter.substr(1);
  162. var singleline = delimiter.length == 1;
  163. var OUTCLASS = "string";
  164. function tokenNestedExpr(depth) {
  165. return function(stream, state) {
  166. var inner = tokenBaseInner(stream, state, true)
  167. if (inner == "punctuation") {
  168. if (stream.current() == "{") {
  169. state.tokenize = tokenNestedExpr(depth + 1)
  170. } else if (stream.current() == "}") {
  171. if (depth > 1) state.tokenize = tokenNestedExpr(depth - 1)
  172. else state.tokenize = tokenString
  173. }
  174. }
  175. return inner
  176. }
  177. }
  178. function tokenString(stream, state) {
  179. while (!stream.eol()) {
  180. stream.eatWhile(/[^'"\{\}\\]/);
  181. if (stream.eat("\\")) {
  182. stream.next();
  183. if (singleline && stream.eol())
  184. return OUTCLASS;
  185. } else if (stream.match(delimiter)) {
  186. state.tokenize = tokenOuter;
  187. return OUTCLASS;
  188. } else if (stream.match('{{')) {
  189. // ignore {{ in f-str
  190. return OUTCLASS;
  191. } else if (stream.match('{', false)) {
  192. // switch to nested mode
  193. state.tokenize = tokenNestedExpr(0)
  194. if (stream.current()) return OUTCLASS;
  195. else return state.tokenize(stream, state)
  196. } else if (stream.match('}}')) {
  197. return OUTCLASS;
  198. } else if (stream.match('}')) {
  199. // single } in f-string is an error
  200. return ERRORCLASS;
  201. } else {
  202. stream.eat(/['"]/);
  203. }
  204. }
  205. if (singleline) {
  206. if (parserConf.singleLineStringErrors)
  207. return ERRORCLASS;
  208. else
  209. state.tokenize = tokenOuter;
  210. }
  211. return OUTCLASS;
  212. }
  213. tokenString.isString = true;
  214. return tokenString;
  215. }
  216. function tokenStringFactory(delimiter, tokenOuter) {
  217. while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0)
  218. delimiter = delimiter.substr(1);
  219. var singleline = delimiter.length == 1;
  220. var OUTCLASS = "string";
  221. function tokenString(stream, state) {
  222. while (!stream.eol()) {
  223. stream.eatWhile(/[^'"\\]/);
  224. if (stream.eat("\\")) {
  225. stream.next();
  226. if (singleline && stream.eol())
  227. return OUTCLASS;
  228. } else if (stream.match(delimiter)) {
  229. state.tokenize = tokenOuter;
  230. return OUTCLASS;
  231. } else {
  232. stream.eat(/['"]/);
  233. }
  234. }
  235. if (singleline) {
  236. if (parserConf.singleLineStringErrors)
  237. return ERRORCLASS;
  238. else
  239. state.tokenize = tokenOuter;
  240. }
  241. return OUTCLASS;
  242. }
  243. tokenString.isString = true;
  244. return tokenString;
  245. }
  246. function pushPyScope(state) {
  247. while (top(state).type != "py") state.scopes.pop()
  248. state.scopes.push({offset: top(state).offset + conf.indentUnit,
  249. type: "py",
  250. align: null})
  251. }
  252. function pushBracketScope(stream, state, type) {
  253. var align = stream.match(/^[\s\[\{\(]*(?:#|$)/, false) ? null : stream.column() + 1
  254. state.scopes.push({offset: state.indent + hangingIndent,
  255. type: type,
  256. align: align})
  257. }
  258. function dedent(stream, state) {
  259. var indented = stream.indentation();
  260. while (state.scopes.length > 1 && top(state).offset > indented) {
  261. if (top(state).type != "py") return true;
  262. state.scopes.pop();
  263. }
  264. return top(state).offset != indented;
  265. }
  266. function tokenLexer(stream, state) {
  267. if (stream.sol()) {
  268. state.beginningOfLine = true;
  269. state.dedent = false;
  270. }
  271. var style = state.tokenize(stream, state);
  272. var current = stream.current();
  273. // Handle decorators
  274. if (state.beginningOfLine && current == "@")
  275. return stream.match(identifiers, false) ? "meta" : py3 ? "operator" : ERRORCLASS;
  276. if (/\S/.test(current)) state.beginningOfLine = false;
  277. if ((style == "variable" || style == "builtin")
  278. && state.lastToken == "meta")
  279. style = "meta";
  280. // Handle scope changes.
  281. if (current == "pass" || current == "return")
  282. state.dedent = true;
  283. if (current == "lambda") state.lambda = true;
  284. if (current == ":" && !state.lambda && top(state).type == "py" && stream.match(/^\s*(?:#|$)/, false))
  285. pushPyScope(state);
  286. if (current.length == 1 && !/string|comment/.test(style)) {
  287. var delimiter_index = "[({".indexOf(current);
  288. if (delimiter_index != -1)
  289. pushBracketScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1));
  290. delimiter_index = "])}".indexOf(current);
  291. if (delimiter_index != -1) {
  292. if (top(state).type == current) state.indent = state.scopes.pop().offset - hangingIndent
  293. else return ERRORCLASS;
  294. }
  295. }
  296. if (state.dedent && stream.eol() && top(state).type == "py" && state.scopes.length > 1)
  297. state.scopes.pop();
  298. return style;
  299. }
  300. var external = {
  301. startState: function(basecolumn) {
  302. return {
  303. tokenize: tokenBase,
  304. scopes: [{offset: basecolumn || 0, type: "py", align: null}],
  305. indent: basecolumn || 0,
  306. lastToken: null,
  307. lambda: false,
  308. dedent: 0
  309. };
  310. },
  311. token: function(stream, state) {
  312. var addErr = state.errorToken;
  313. if (addErr) state.errorToken = false;
  314. var style = tokenLexer(stream, state);
  315. if (style && style != "comment")
  316. state.lastToken = (style == "keyword" || style == "punctuation") ? stream.current() : style;
  317. if (style == "punctuation") style = null;
  318. if (stream.eol() && state.lambda)
  319. state.lambda = false;
  320. return addErr ? style + " " + ERRORCLASS : style;
  321. },
  322. indent: function(state, textAfter) {
  323. if (state.tokenize != tokenBase)
  324. return state.tokenize.isString ? CodeMirror.Pass : 0;
  325. var scope = top(state)
  326. var closing = scope.type == textAfter.charAt(0) ||
  327. scope.type == "py" && !state.dedent && /^(else:|elif |except |finally:)/.test(textAfter)
  328. if (scope.align != null)
  329. return scope.align - (closing ? 1 : 0)
  330. else
  331. return scope.offset - (closing ? hangingIndent : 0)
  332. },
  333. electricInput: /^\s*([\}\]\)]|else:|elif |except |finally:)$/,
  334. closeBrackets: {triples: "'\""},
  335. lineComment: "#",
  336. fold: "indent"
  337. };
  338. return external;
  339. });
  340. CodeMirror.defineMIME("text/x-python", "python");
  341. var words = function(str) { return str.split(" "); };
  342. CodeMirror.defineMIME("text/x-cython", {
  343. name: "python",
  344. extra_keywords: words("by cdef cimport cpdef ctypedef enum except "+
  345. "extern gil include nogil property public "+
  346. "readonly struct union DEF IF ELIF ELSE")
  347. });
  348. });