python.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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-compatiblity 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)|(fr))?('{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) {
  90. if (stream.eatSpace()) return null;
  91. // Handle Comments
  92. if (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());
  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 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 tokenFString(stream, state) {
  165. // inside f-str Expression
  166. if (stream.match(delimiter)) {
  167. // expression ends pre-maturally, but very common in editing
  168. // Could show error to remind users to close brace here
  169. state.tokenize = tokenString
  170. return OUTCLASS;
  171. } else if (stream.match('{')) {
  172. // starting brace, if not eaten below
  173. return "punctuation";
  174. } else if (stream.match('}')) {
  175. // return to regular inside string state
  176. state.tokenize = tokenString
  177. return "punctuation";
  178. } else {
  179. // use tokenBaseInner to parse the expression
  180. return tokenBaseInner(stream, state);
  181. }
  182. }
  183. function tokenString(stream, state) {
  184. while (!stream.eol()) {
  185. stream.eatWhile(/[^'"\{\}\\]/);
  186. if (stream.eat("\\")) {
  187. stream.next();
  188. if (singleline && stream.eol())
  189. return OUTCLASS;
  190. } else if (stream.match(delimiter)) {
  191. state.tokenize = tokenOuter;
  192. return OUTCLASS;
  193. } else if (stream.match('{{')) {
  194. // ignore {{ in f-str
  195. return OUTCLASS;
  196. } else if (stream.match('{', false)) {
  197. // switch to nested mode
  198. state.tokenize = tokenFString
  199. if (stream.current()) {
  200. return OUTCLASS;
  201. } else {
  202. // need to return something, so eat the starting {
  203. stream.next();
  204. return "punctuation";
  205. }
  206. } else if (stream.match('}}')) {
  207. return OUTCLASS;
  208. } else if (stream.match('}')) {
  209. // single } in f-string is an error
  210. return ERRORCLASS;
  211. } else {
  212. stream.eat(/['"]/);
  213. }
  214. }
  215. if (singleline) {
  216. if (parserConf.singleLineStringErrors)
  217. return ERRORCLASS;
  218. else
  219. state.tokenize = tokenOuter;
  220. }
  221. return OUTCLASS;
  222. }
  223. tokenString.isString = true;
  224. return tokenString;
  225. }
  226. function tokenStringFactory(delimiter) {
  227. while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0)
  228. delimiter = delimiter.substr(1);
  229. var singleline = delimiter.length == 1;
  230. var OUTCLASS = "string";
  231. function tokenString(stream, state) {
  232. while (!stream.eol()) {
  233. stream.eatWhile(/[^'"\\]/);
  234. if (stream.eat("\\")) {
  235. stream.next();
  236. if (singleline && stream.eol())
  237. return OUTCLASS;
  238. } else if (stream.match(delimiter)) {
  239. state.tokenize = tokenBase;
  240. return OUTCLASS;
  241. } else {
  242. stream.eat(/['"]/);
  243. }
  244. }
  245. if (singleline) {
  246. if (parserConf.singleLineStringErrors)
  247. return ERRORCLASS;
  248. else
  249. state.tokenize = tokenBase;
  250. }
  251. return OUTCLASS;
  252. }
  253. tokenString.isString = true;
  254. return tokenString;
  255. }
  256. function pushPyScope(state) {
  257. while (top(state).type != "py") state.scopes.pop()
  258. state.scopes.push({offset: top(state).offset + conf.indentUnit,
  259. type: "py",
  260. align: null})
  261. }
  262. function pushBracketScope(stream, state, type) {
  263. var align = stream.match(/^([\s\[\{\(]|#.*)*$/, false) ? null : stream.column() + 1
  264. state.scopes.push({offset: state.indent + hangingIndent,
  265. type: type,
  266. align: align})
  267. }
  268. function dedent(stream, state) {
  269. var indented = stream.indentation();
  270. while (state.scopes.length > 1 && top(state).offset > indented) {
  271. if (top(state).type != "py") return true;
  272. state.scopes.pop();
  273. }
  274. return top(state).offset != indented;
  275. }
  276. function tokenLexer(stream, state) {
  277. if (stream.sol()) state.beginningOfLine = true;
  278. var style = state.tokenize(stream, state);
  279. var current = stream.current();
  280. // Handle decorators
  281. if (state.beginningOfLine && current == "@")
  282. return stream.match(identifiers, false) ? "meta" : py3 ? "operator" : ERRORCLASS;
  283. if (/\S/.test(current)) state.beginningOfLine = false;
  284. if ((style == "variable" || style == "builtin")
  285. && state.lastToken == "meta")
  286. style = "meta";
  287. // Handle scope changes.
  288. if (current == "pass" || current == "return")
  289. state.dedent += 1;
  290. if (current == "lambda") state.lambda = true;
  291. if (current == ":" && !state.lambda && top(state).type == "py")
  292. pushPyScope(state);
  293. if (current.length == 1 && !/string|comment/.test(style)) {
  294. var delimiter_index = "[({".indexOf(current);
  295. if (delimiter_index != -1)
  296. pushBracketScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1));
  297. delimiter_index = "])}".indexOf(current);
  298. if (delimiter_index != -1) {
  299. if (top(state).type == current) state.indent = state.scopes.pop().offset - hangingIndent
  300. else return ERRORCLASS;
  301. }
  302. }
  303. if (state.dedent > 0 && stream.eol() && top(state).type == "py") {
  304. if (state.scopes.length > 1) state.scopes.pop();
  305. state.dedent -= 1;
  306. }
  307. return style;
  308. }
  309. var external = {
  310. startState: function(basecolumn) {
  311. return {
  312. tokenize: tokenBase,
  313. scopes: [{offset: basecolumn || 0, type: "py", align: null}],
  314. indent: basecolumn || 0,
  315. lastToken: null,
  316. lambda: false,
  317. dedent: 0
  318. };
  319. },
  320. token: function(stream, state) {
  321. var addErr = state.errorToken;
  322. if (addErr) state.errorToken = false;
  323. var style = tokenLexer(stream, state);
  324. if (style && style != "comment")
  325. state.lastToken = (style == "keyword" || style == "punctuation") ? stream.current() : style;
  326. if (style == "punctuation") style = null;
  327. if (stream.eol() && state.lambda)
  328. state.lambda = false;
  329. return addErr ? style + " " + ERRORCLASS : style;
  330. },
  331. indent: function(state, textAfter) {
  332. if (state.tokenize != tokenBase)
  333. return state.tokenize.isString ? CodeMirror.Pass : 0;
  334. var scope = top(state), closing = scope.type == textAfter.charAt(0)
  335. if (scope.align != null)
  336. return scope.align - (closing ? 1 : 0)
  337. else
  338. return scope.offset - (closing ? hangingIndent : 0)
  339. },
  340. electricInput: /^\s*[\}\]\)]$/,
  341. closeBrackets: {triples: "'\""},
  342. lineComment: "#",
  343. fold: "indent"
  344. };
  345. return external;
  346. });
  347. CodeMirror.defineMIME("text/x-python", "python");
  348. var words = function(str) { return str.split(" "); };
  349. CodeMirror.defineMIME("text/x-cython", {
  350. name: "python",
  351. extra_keywords: words("by cdef cimport cpdef ctypedef enum except "+
  352. "extern gil include nogil property public "+
  353. "readonly struct union DEF IF ELIF ELSE")
  354. });
  355. });