mode-python.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. ace.define("ace/mode/python_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_highlight_rules"], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  5. var PythonHighlightRules = function() {
  6. var keywords = (
  7. "and|as|assert|break|class|continue|def|del|elif|else|except|exec|" +
  8. "finally|for|from|global|if|import|in|is|lambda|not|or|pass|print|" +
  9. "raise|return|try|while|with|yield|async|await|nonlocal"
  10. );
  11. var builtinConstants = (
  12. "True|False|None|NotImplemented|Ellipsis|__debug__"
  13. );
  14. var builtinFunctions = (
  15. "abs|divmod|input|open|staticmethod|all|enumerate|int|ord|str|any|" +
  16. "eval|isinstance|pow|sum|basestring|execfile|issubclass|print|super|" +
  17. "binfile|bin|iter|property|tuple|bool|filter|len|range|type|bytearray|" +
  18. "float|list|raw_input|unichr|callable|format|locals|reduce|unicode|" +
  19. "chr|frozenset|long|reload|vars|classmethod|getattr|map|repr|xrange|" +
  20. "cmp|globals|max|reversed|zip|compile|hasattr|memoryview|round|" +
  21. "__import__|complex|hash|min|apply|delattr|help|next|setattr|set|" +
  22. "buffer|dict|hex|object|slice|coerce|dir|id|oct|sorted|intern|" +
  23. "ascii|breakpoint|bytes"
  24. );
  25. var keywordMapper = this.createKeywordMapper({
  26. "invalid.deprecated": "debugger",
  27. "support.function": builtinFunctions,
  28. "variable.language": "self|cls",
  29. "constant.language": builtinConstants,
  30. "keyword": keywords
  31. }, "identifier");
  32. var strPre = "[uU]?";
  33. var strRawPre = "[rR]";
  34. var strFormatPre = "[fF]";
  35. var strRawFormatPre = "(?:[rR][fF]|[fF][rR])";
  36. var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
  37. var octInteger = "(?:0[oO]?[0-7]+)";
  38. var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
  39. var binInteger = "(?:0[bB][01]+)";
  40. var integer = "(?:" + decimalInteger + "|" + octInteger + "|" + hexInteger + "|" + binInteger + ")";
  41. var exponent = "(?:[eE][+-]?\\d+)";
  42. var fraction = "(?:\\.\\d+)";
  43. var intPart = "(?:\\d+)";
  44. var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
  45. var exponentFloat = "(?:(?:" + pointFloat + "|" + intPart + ")" + exponent + ")";
  46. var floatNumber = "(?:" + exponentFloat + "|" + pointFloat + ")";
  47. var stringEscape = "\\\\(x[0-9A-Fa-f]{2}|[0-7]{3}|[\\\\abfnrtv'\"]|U[0-9A-Fa-f]{8}|u[0-9A-Fa-f]{4})";
  48. this.$rules = {
  49. "start" : [ {
  50. token : "comment",
  51. regex : "#.*$"
  52. }, {
  53. token : "string", // multi line """ string start
  54. regex : strPre + '"{3}',
  55. next : "qqstring3"
  56. }, {
  57. token : "string", // " string
  58. regex : strPre + '"(?=.)',
  59. next : "qqstring"
  60. }, {
  61. token : "string", // multi line ''' string start
  62. regex : strPre + "'{3}",
  63. next : "qstring3"
  64. }, {
  65. token : "string", // ' string
  66. regex : strPre + "'(?=.)",
  67. next : "qstring"
  68. }, {
  69. token: "string",
  70. regex: strRawPre + '"{3}',
  71. next: "rawqqstring3"
  72. }, {
  73. token: "string",
  74. regex: strRawPre + '"(?=.)',
  75. next: "rawqqstring"
  76. }, {
  77. token: "string",
  78. regex: strRawPre + "'{3}",
  79. next: "rawqstring3"
  80. }, {
  81. token: "string",
  82. regex: strRawPre + "'(?=.)",
  83. next: "rawqstring"
  84. }, {
  85. token: "string",
  86. regex: strFormatPre + '"{3}',
  87. next: "fqqstring3"
  88. }, {
  89. token: "string",
  90. regex: strFormatPre + '"(?=.)',
  91. next: "fqqstring"
  92. }, {
  93. token: "string",
  94. regex: strFormatPre + "'{3}",
  95. next: "fqstring3"
  96. }, {
  97. token: "string",
  98. regex: strFormatPre + "'(?=.)",
  99. next: "fqstring"
  100. },{
  101. token: "string",
  102. regex: strRawFormatPre + '"{3}',
  103. next: "rfqqstring3"
  104. }, {
  105. token: "string",
  106. regex: strRawFormatPre + '"(?=.)',
  107. next: "rfqqstring"
  108. }, {
  109. token: "string",
  110. regex: strRawFormatPre + "'{3}",
  111. next: "rfqstring3"
  112. }, {
  113. token: "string",
  114. regex: strRawFormatPre + "'(?=.)",
  115. next: "rfqstring"
  116. }, {
  117. token: "keyword.operator",
  118. regex: "\\+|\\-|\\*|\\*\\*|\\/|\\/\\/|%|@|<<|>>|&|\\||\\^|~|<|>|<=|=>|==|!=|<>|="
  119. }, {
  120. token: "punctuation",
  121. regex: ",|:|;|\\->|\\+=|\\-=|\\*=|\\/=|\\/\\/=|%=|@=|&=|\\|=|^=|>>=|<<=|\\*\\*="
  122. }, {
  123. token: "paren.lparen",
  124. regex: "[\\[\\(\\{]"
  125. }, {
  126. token: "paren.rparen",
  127. regex: "[\\]\\)\\}]"
  128. }, {
  129. token: "text",
  130. regex: "\\s+"
  131. }, {
  132. include: "constants"
  133. }],
  134. "qqstring3": [{
  135. token: "constant.language.escape",
  136. regex: stringEscape
  137. }, {
  138. token: "string", // multi line """ string end
  139. regex: '"{3}',
  140. next: "start"
  141. }, {
  142. defaultToken: "string"
  143. }],
  144. "qstring3": [{
  145. token: "constant.language.escape",
  146. regex: stringEscape
  147. }, {
  148. token: "string", // multi line ''' string end
  149. regex: "'{3}",
  150. next: "start"
  151. }, {
  152. defaultToken: "string"
  153. }],
  154. "qqstring": [{
  155. token: "constant.language.escape",
  156. regex: stringEscape
  157. }, {
  158. token: "string",
  159. regex: "\\\\$",
  160. next: "qqstring"
  161. }, {
  162. token: "string",
  163. regex: '"|$',
  164. next: "start"
  165. }, {
  166. defaultToken: "string"
  167. }],
  168. "qstring": [{
  169. token: "constant.language.escape",
  170. regex: stringEscape
  171. }, {
  172. token: "string",
  173. regex: "\\\\$",
  174. next: "qstring"
  175. }, {
  176. token: "string",
  177. regex: "'|$",
  178. next: "start"
  179. }, {
  180. defaultToken: "string"
  181. }],
  182. "rawqqstring3": [{
  183. token: "string", // multi line """ string end
  184. regex: '"{3}',
  185. next: "start"
  186. }, {
  187. defaultToken: "string"
  188. }],
  189. "rawqstring3": [{
  190. token: "string", // multi line ''' string end
  191. regex: "'{3}",
  192. next: "start"
  193. }, {
  194. defaultToken: "string"
  195. }],
  196. "rawqqstring": [{
  197. token: "string",
  198. regex: "\\\\$",
  199. next: "rawqqstring"
  200. }, {
  201. token: "string",
  202. regex: '"|$',
  203. next: "start"
  204. }, {
  205. defaultToken: "string"
  206. }],
  207. "rawqstring": [{
  208. token: "string",
  209. regex: "\\\\$",
  210. next: "rawqstring"
  211. }, {
  212. token: "string",
  213. regex: "'|$",
  214. next: "start"
  215. }, {
  216. defaultToken: "string"
  217. }],
  218. "fqqstring3": [{
  219. token: "constant.language.escape",
  220. regex: stringEscape
  221. }, {
  222. token: "string", // multi line """ string end
  223. regex: '"{3}',
  224. next: "start"
  225. }, {
  226. token: "paren.lparen",
  227. regex: "{",
  228. push: "fqstringParRules"
  229. }, {
  230. defaultToken: "string"
  231. }],
  232. "fqstring3": [{
  233. token: "constant.language.escape",
  234. regex: stringEscape
  235. }, {
  236. token: "string", // multi line ''' string end
  237. regex: "'{3}",
  238. next: "start"
  239. }, {
  240. token: "paren.lparen",
  241. regex: "{",
  242. push: "fqstringParRules"
  243. }, {
  244. defaultToken: "string"
  245. }],
  246. "fqqstring": [{
  247. token: "constant.language.escape",
  248. regex: stringEscape
  249. }, {
  250. token: "string",
  251. regex: "\\\\$",
  252. next: "fqqstring"
  253. }, {
  254. token: "string",
  255. regex: '"|$',
  256. next: "start"
  257. }, {
  258. token: "paren.lparen",
  259. regex: "{",
  260. push: "fqstringParRules"
  261. }, {
  262. defaultToken: "string"
  263. }],
  264. "fqstring": [{
  265. token: "constant.language.escape",
  266. regex: stringEscape
  267. }, {
  268. token: "string",
  269. regex: "'|$",
  270. next: "start"
  271. }, {
  272. token: "paren.lparen",
  273. regex: "{",
  274. push: "fqstringParRules"
  275. }, {
  276. defaultToken: "string"
  277. }],
  278. "rfqqstring3": [{
  279. token: "string", // multi line """ string end
  280. regex: '"{3}',
  281. next: "start"
  282. }, {
  283. token: "paren.lparen",
  284. regex: "{",
  285. push: "fqstringParRules"
  286. }, {
  287. defaultToken: "string"
  288. }],
  289. "rfqstring3": [{
  290. token: "string", // multi line ''' string end
  291. regex: "'{3}",
  292. next: "start"
  293. }, {
  294. token: "paren.lparen",
  295. regex: "{",
  296. push: "fqstringParRules"
  297. }, {
  298. defaultToken: "string"
  299. }],
  300. "rfqqstring": [{
  301. token: "string",
  302. regex: "\\\\$",
  303. next: "rfqqstring"
  304. }, {
  305. token: "string",
  306. regex: '"|$',
  307. next: "start"
  308. }, {
  309. token: "paren.lparen",
  310. regex: "{",
  311. push: "fqstringParRules"
  312. }, {
  313. defaultToken: "string"
  314. }],
  315. "rfqstring": [{
  316. token: "string",
  317. regex: "'|$",
  318. next: "start"
  319. }, {
  320. token: "paren.lparen",
  321. regex: "{",
  322. push: "fqstringParRules"
  323. }, {
  324. defaultToken: "string"
  325. }],
  326. "fqstringParRules": [{//TODO: nested {}
  327. token: "paren.lparen",
  328. regex: "[\\[\\(]"
  329. }, {
  330. token: "paren.rparen",
  331. regex: "[\\]\\)]"
  332. }, {
  333. token: "string",
  334. regex: "\\s+"
  335. }, {
  336. token: "string",
  337. regex: "'[^']*'"
  338. }, {
  339. token: "string",
  340. regex: '"[^"]*"'
  341. }, {
  342. token: "function.support",
  343. regex: "(!s|!r|!a)"
  344. }, {
  345. include: "constants"
  346. },{
  347. token: 'paren.rparen',
  348. regex: "}",
  349. next: 'pop'
  350. },{
  351. token: 'paren.lparen',
  352. regex: "{",
  353. push: "fqstringParRules"
  354. }],
  355. "constants": [{
  356. token: "constant.numeric", // imaginary
  357. regex: "(?:" + floatNumber + "|\\d+)[jJ]\\b"
  358. }, {
  359. token: "constant.numeric", // float
  360. regex: floatNumber
  361. }, {
  362. token: "constant.numeric", // long integer
  363. regex: integer + "[lL]\\b"
  364. }, {
  365. token: "constant.numeric", // integer
  366. regex: integer + "\\b"
  367. }, {
  368. token: ["punctuation", "function.support"],// method
  369. regex: "(\\.)([a-zA-Z_]+)\\b"
  370. }, {
  371. token: keywordMapper,
  372. regex: "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  373. }]
  374. };
  375. this.normalizeRules();
  376. };
  377. oop.inherits(PythonHighlightRules, TextHighlightRules);
  378. exports.PythonHighlightRules = PythonHighlightRules;
  379. });
  380. ace.define("ace/mode/folding/pythonic",["require","exports","module","ace/lib/oop","ace/mode/folding/fold_mode"], function(require, exports, module) {
  381. "use strict";
  382. var oop = require("../../lib/oop");
  383. var BaseFoldMode = require("./fold_mode").FoldMode;
  384. var FoldMode = exports.FoldMode = function(markers) {
  385. this.foldingStartMarker = new RegExp("([\\[{])(?:\\s*)$|(" + markers + ")(?:\\s*)(?:#.*)?$");
  386. };
  387. oop.inherits(FoldMode, BaseFoldMode);
  388. (function() {
  389. this.getFoldWidgetRange = function(session, foldStyle, row) {
  390. var line = session.getLine(row);
  391. var match = line.match(this.foldingStartMarker);
  392. if (match) {
  393. if (match[1])
  394. return this.openingBracketBlock(session, match[1], row, match.index);
  395. if (match[2])
  396. return this.indentationBlock(session, row, match.index + match[2].length);
  397. return this.indentationBlock(session, row);
  398. }
  399. };
  400. }).call(FoldMode.prototype);
  401. });
  402. ace.define("ace/mode/python",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/python_highlight_rules","ace/mode/folding/pythonic","ace/range"], function(require, exports, module) {
  403. "use strict";
  404. var oop = require("../lib/oop");
  405. var TextMode = require("./text").Mode;
  406. var PythonHighlightRules = require("./python_highlight_rules").PythonHighlightRules;
  407. var PythonFoldMode = require("./folding/pythonic").FoldMode;
  408. var Range = require("../range").Range;
  409. var Mode = function() {
  410. this.HighlightRules = PythonHighlightRules;
  411. this.foldingRules = new PythonFoldMode("\\:");
  412. this.$behaviour = this.$defaultBehaviour;
  413. };
  414. oop.inherits(Mode, TextMode);
  415. (function() {
  416. this.lineCommentStart = "#";
  417. this.getNextLineIndent = function(state, line, tab) {
  418. var indent = this.$getIndent(line);
  419. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  420. var tokens = tokenizedLine.tokens;
  421. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  422. return indent;
  423. }
  424. if (state == "start") {
  425. var match = line.match(/^.*[\{\(\[:]\s*$/);
  426. if (match) {
  427. indent += tab;
  428. }
  429. }
  430. return indent;
  431. };
  432. var outdents = {
  433. "pass": 1,
  434. "return": 1,
  435. "raise": 1,
  436. "break": 1,
  437. "continue": 1
  438. };
  439. this.checkOutdent = function(state, line, input) {
  440. if (input !== "\r\n" && input !== "\r" && input !== "\n")
  441. return false;
  442. var tokens = this.getTokenizer().getLineTokens(line.trim(), state).tokens;
  443. if (!tokens)
  444. return false;
  445. do {
  446. var last = tokens.pop();
  447. } while (last && (last.type == "comment" || (last.type == "text" && last.value.match(/^\s+$/))));
  448. if (!last)
  449. return false;
  450. return (last.type == "keyword" && outdents[last.value]);
  451. };
  452. this.autoOutdent = function(state, doc, row) {
  453. row += 1;
  454. var indent = this.$getIndent(doc.getLine(row));
  455. var tab = doc.getTabString();
  456. if (indent.slice(-tab.length) == tab)
  457. doc.remove(new Range(row, indent.length-tab.length, row, indent.length));
  458. };
  459. this.$id = "ace/mode/python";
  460. this.snippetFileId = "ace/snippets/python";
  461. }).call(Mode.prototype);
  462. exports.Mode = Mode;
  463. }); (function() {
  464. ace.require(["ace/mode/python"], function(m) {
  465. if (typeof module == "object" && typeof exports == "object" && module) {
  466. module.exports = m;
  467. }
  468. });
  469. })();