mode-python.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. ace.define("ace/mode/python_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"
  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|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|set|apply|delattr|help|next|setattr|" +
  22. "buffer|dict|hex|object|slice|coerce|dir|id|oct|sorted|intern"
  23. );
  24. var keywordMapper = this.createKeywordMapper({
  25. "invalid.deprecated": "debugger",
  26. "support.function": builtinFunctions,
  27. "variable.language": "self|cls",
  28. "constant.language": builtinConstants,
  29. "keyword": keywords
  30. }, "identifier");
  31. var strPre = "(?:r|u|ur|R|U|UR|Ur|uR)?";
  32. var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
  33. var octInteger = "(?:0[oO]?[0-7]+)";
  34. var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
  35. var binInteger = "(?:0[bB][01]+)";
  36. var integer = "(?:" + decimalInteger + "|" + octInteger + "|" + hexInteger + "|" + binInteger + ")";
  37. var exponent = "(?:[eE][+-]?\\d+)";
  38. var fraction = "(?:\\.\\d+)";
  39. var intPart = "(?:\\d+)";
  40. var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
  41. var exponentFloat = "(?:(?:" + pointFloat + "|" + intPart + ")" + exponent + ")";
  42. var floatNumber = "(?:" + exponentFloat + "|" + pointFloat + ")";
  43. var stringEscape = "\\\\(x[0-9A-Fa-f]{2}|[0-7]{3}|[\\\\abfnrtv'\"]|U[0-9A-Fa-f]{8}|u[0-9A-Fa-f]{4})";
  44. this.$rules = {
  45. "start" : [ {
  46. token : "comment",
  47. regex : "#.*$"
  48. }, {
  49. token : "string", // multi line """ string start
  50. regex : strPre + '"{3}',
  51. next : "qqstring3"
  52. }, {
  53. token : "string", // " string
  54. regex : strPre + '"(?=.)',
  55. next : "qqstring"
  56. }, {
  57. token : "string", // multi line ''' string start
  58. regex : strPre + "'{3}",
  59. next : "qstring3"
  60. }, {
  61. token : "string", // ' string
  62. regex : strPre + "'(?=.)",
  63. next : "qstring"
  64. }, {
  65. token : "constant.numeric", // imaginary
  66. regex : "(?:" + floatNumber + "|\\d+)[jJ]\\b"
  67. }, {
  68. token : "constant.numeric", // float
  69. regex : floatNumber
  70. }, {
  71. token : "constant.numeric", // long integer
  72. regex : integer + "[lL]\\b"
  73. }, {
  74. token : "constant.numeric", // integer
  75. regex : integer + "\\b"
  76. }, {
  77. token : keywordMapper,
  78. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  79. }, {
  80. token : "keyword.operator",
  81. regex : "\\+|\\-|\\*|\\*\\*|\\/|\\/\\/|%|<<|>>|&|\\||\\^|~|<|>|<=|=>|==|!=|<>|="
  82. }, {
  83. token : "paren.lparen",
  84. regex : "[\\[\\(\\{]"
  85. }, {
  86. token : "paren.rparen",
  87. regex : "[\\]\\)\\}]"
  88. }, {
  89. token : "text",
  90. regex : "\\s+"
  91. } ],
  92. "qqstring3" : [ {
  93. token : "constant.language.escape",
  94. regex : stringEscape
  95. }, {
  96. token : "string", // multi line """ string end
  97. regex : '"{3}',
  98. next : "start"
  99. }, {
  100. defaultToken : "string"
  101. } ],
  102. "qstring3" : [ {
  103. token : "constant.language.escape",
  104. regex : stringEscape
  105. }, {
  106. token : "string", // multi line ''' string end
  107. regex : "'{3}",
  108. next : "start"
  109. }, {
  110. defaultToken : "string"
  111. } ],
  112. "qqstring" : [{
  113. token : "constant.language.escape",
  114. regex : stringEscape
  115. }, {
  116. token : "string",
  117. regex : "\\\\$",
  118. next : "qqstring"
  119. }, {
  120. token : "string",
  121. regex : '"|$',
  122. next : "start"
  123. }, {
  124. defaultToken: "string"
  125. }],
  126. "qstring" : [{
  127. token : "constant.language.escape",
  128. regex : stringEscape
  129. }, {
  130. token : "string",
  131. regex : "\\\\$",
  132. next : "qstring"
  133. }, {
  134. token : "string",
  135. regex : "'|$",
  136. next : "start"
  137. }, {
  138. defaultToken: "string"
  139. }]
  140. };
  141. };
  142. oop.inherits(PythonHighlightRules, TextHighlightRules);
  143. exports.PythonHighlightRules = PythonHighlightRules;
  144. });
  145. ace.define("ace/mode/folding/pythonic",[], function(require, exports, module) {
  146. "use strict";
  147. var oop = require("../../lib/oop");
  148. var BaseFoldMode = require("./fold_mode").FoldMode;
  149. var FoldMode = exports.FoldMode = function(markers) {
  150. this.foldingStartMarker = new RegExp("([\\[{])(?:\\s*)$|(" + markers + ")(?:\\s*)(?:#.*)?$");
  151. };
  152. oop.inherits(FoldMode, BaseFoldMode);
  153. (function() {
  154. this.getFoldWidgetRange = function(session, foldStyle, row) {
  155. var line = session.getLine(row);
  156. var match = line.match(this.foldingStartMarker);
  157. if (match) {
  158. if (match[1])
  159. return this.openingBracketBlock(session, match[1], row, match.index);
  160. if (match[2])
  161. return this.indentationBlock(session, row, match.index + match[2].length);
  162. return this.indentationBlock(session, row);
  163. }
  164. };
  165. }).call(FoldMode.prototype);
  166. });
  167. ace.define("ace/mode/python",[], function(require, exports, module) {
  168. "use strict";
  169. var oop = require("../lib/oop");
  170. var TextMode = require("./text").Mode;
  171. var PythonHighlightRules = require("./python_highlight_rules").PythonHighlightRules;
  172. var PythonFoldMode = require("./folding/pythonic").FoldMode;
  173. var Range = require("../range").Range;
  174. var Mode = function() {
  175. this.HighlightRules = PythonHighlightRules;
  176. this.foldingRules = new PythonFoldMode("\\:");
  177. this.$behaviour = this.$defaultBehaviour;
  178. };
  179. oop.inherits(Mode, TextMode);
  180. (function() {
  181. this.lineCommentStart = "#";
  182. this.getNextLineIndent = function(state, line, tab) {
  183. var indent = this.$getIndent(line);
  184. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  185. var tokens = tokenizedLine.tokens;
  186. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  187. return indent;
  188. }
  189. if (state == "start") {
  190. var match = line.match(/^.*[\{\(\[:]\s*$/);
  191. if (match) {
  192. indent += tab;
  193. }
  194. }
  195. return indent;
  196. };
  197. var outdents = {
  198. "pass": 1,
  199. "return": 1,
  200. "raise": 1,
  201. "break": 1,
  202. "continue": 1
  203. };
  204. this.checkOutdent = function(state, line, input) {
  205. if (input !== "\r\n" && input !== "\r" && input !== "\n")
  206. return false;
  207. var tokens = this.getTokenizer().getLineTokens(line.trim(), state).tokens;
  208. if (!tokens)
  209. return false;
  210. do {
  211. var last = tokens.pop();
  212. } while (last && (last.type == "comment" || (last.type == "text" && last.value.match(/^\s+$/))));
  213. if (!last)
  214. return false;
  215. return (last.type == "keyword" && outdents[last.value]);
  216. };
  217. this.autoOutdent = function(state, doc, row) {
  218. row += 1;
  219. var indent = this.$getIndent(doc.getLine(row));
  220. var tab = doc.getTabString();
  221. if (indent.slice(-tab.length) == tab)
  222. doc.remove(new Range(row, indent.length-tab.length, row, indent.length));
  223. };
  224. this.$id = "ace/mode/python";
  225. }).call(Mode.prototype);
  226. exports.Mode = Mode;
  227. });
  228. (function() {
  229. ace.require(["ace/mode/python"], function(m) {
  230. if (typeof module == "object" && typeof exports == "object" && module) {
  231. module.exports = m;
  232. }
  233. });
  234. })();