mode-python.js 8.5 KB

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