python.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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(), 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 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)
  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()) state.beginningOfLine = true;
  268. var style = state.tokenize(stream, state);
  269. var current = stream.current();
  270. // Handle decorators
  271. if (state.beginningOfLine && current == "@")
  272. return stream.match(identifiers, false) ? "meta" : py3 ? "operator" : ERRORCLASS;
  273. if (/\S/.test(current)) state.beginningOfLine = false;
  274. if ((style == "variable" || style == "builtin")
  275. && state.lastToken == "meta")
  276. style = "meta";
  277. // Handle scope changes.
  278. if (current == "pass" || current == "return")
  279. state.dedent += 1;
  280. if (current == "lambda") state.lambda = true;
  281. if (current == ":" && !state.lambda && top(state).type == "py")
  282. pushPyScope(state);
  283. if (current.length == 1 && !/string|comment/.test(style)) {
  284. var delimiter_index = "[({".indexOf(current);
  285. if (delimiter_index != -1)
  286. pushBracketScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1));
  287. delimiter_index = "])}".indexOf(current);
  288. if (delimiter_index != -1) {
  289. if (top(state).type == current) state.indent = state.scopes.pop().offset - hangingIndent
  290. else return ERRORCLASS;
  291. }
  292. }
  293. if (state.dedent > 0 && stream.eol() && top(state).type == "py") {
  294. if (state.scopes.length > 1) state.scopes.pop();
  295. state.dedent -= 1;
  296. }
  297. return style;
  298. }
  299. var external = {
  300. startState: function(basecolumn) {
  301. return {
  302. tokenize: tokenBase,
  303. scopes: [{offset: basecolumn || 0, type: "py", align: null}],
  304. indent: basecolumn || 0,
  305. lastToken: null,
  306. lambda: false,
  307. dedent: 0
  308. };
  309. },
  310. token: function(stream, state) {
  311. var addErr = state.errorToken;
  312. if (addErr) state.errorToken = false;
  313. var style = tokenLexer(stream, state);
  314. if (style && style != "comment")
  315. state.lastToken = (style == "keyword" || style == "punctuation") ? stream.current() : style;
  316. if (style == "punctuation") style = null;
  317. if (stream.eol() && state.lambda)
  318. state.lambda = false;
  319. return addErr ? style + " " + ERRORCLASS : style;
  320. },
  321. indent: function(state, textAfter) {
  322. if (state.tokenize != tokenBase)
  323. return state.tokenize.isString ? CodeMirror.Pass : 0;
  324. var scope = top(state), closing = scope.type == textAfter.charAt(0)
  325. if (scope.align != null)
  326. return scope.align - (closing ? 1 : 0)
  327. else
  328. return scope.offset - (closing ? hangingIndent : 0)
  329. },
  330. electricInput: /^\s*[\}\]\)]$/,
  331. closeBrackets: {triples: "'\""},
  332. lineComment: "#",
  333. fold: "indent"
  334. };
  335. return external;
  336. });
  337. CodeMirror.defineMIME("text/x-python", "python");
  338. var words = function(str) { return str.split(" "); };
  339. CodeMirror.defineMIME("text/x-cython", {
  340. name: "python",
  341. extra_keywords: words("by cdef cimport cpdef ctypedef enum except "+
  342. "extern gil include nogil property public "+
  343. "readonly struct union DEF IF ELIF ELSE")
  344. });
  345. });