mode-python-uncompressed.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is Ajax.org Code Editor (ACE).
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Ajax.org B.V.
  18. * Portions created by the Initial Developer are Copyright (C) 2010
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * Fabian Jakobs <fabian AT ajax DOT org>
  23. * Colin Gourlay <colin DOT j DOT gourlay AT gmail DOT com>
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either the GNU General Public License Version 2 or later (the "GPL"), or
  27. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. define('ace/mode/python', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text', 'ace/tokenizer', 'ace/mode/python_highlight_rules', 'ace/mode/folding/pythonic', 'ace/range'], function(require, exports, module) {
  39. "use strict";
  40. var oop = require("../lib/oop");
  41. var TextMode = require("./text").Mode;
  42. var Tokenizer = require("../tokenizer").Tokenizer;
  43. var PythonHighlightRules = require("./python_highlight_rules").PythonHighlightRules;
  44. var PythonFoldMode = require("./folding/pythonic").FoldMode;
  45. var Range = require("../range").Range;
  46. var Mode = function() {
  47. this.$tokenizer = new Tokenizer(new PythonHighlightRules().getRules());
  48. this.foldingRules = new PythonFoldMode("\\:");
  49. };
  50. oop.inherits(Mode, TextMode);
  51. (function() {
  52. this.toggleCommentLines = function(state, doc, startRow, endRow) {
  53. var outdent = true;
  54. var re = /^(\s*)#/;
  55. for (var i=startRow; i<= endRow; i++) {
  56. if (!re.test(doc.getLine(i))) {
  57. outdent = false;
  58. break;
  59. }
  60. }
  61. if (outdent) {
  62. var deleteRange = new Range(0, 0, 0, 0);
  63. for (var i=startRow; i<= endRow; i++)
  64. {
  65. var line = doc.getLine(i);
  66. var m = line.match(re);
  67. deleteRange.start.row = i;
  68. deleteRange.end.row = i;
  69. deleteRange.end.column = m[0].length;
  70. doc.replace(deleteRange, m[1]);
  71. }
  72. }
  73. else {
  74. doc.indentRows(startRow, endRow, "#");
  75. }
  76. };
  77. this.getNextLineIndent = function(state, line, tab) {
  78. var indent = this.$getIndent(line);
  79. var tokenizedLine = this.$tokenizer.getLineTokens(line, state);
  80. var tokens = tokenizedLine.tokens;
  81. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  82. return indent;
  83. }
  84. if (state == "start") {
  85. var match = line.match(/^.*[\{\(\[\:]\s*$/);
  86. if (match) {
  87. indent += tab;
  88. }
  89. }
  90. return indent;
  91. };
  92. var outdents = {
  93. "pass": 1,
  94. "return": 1,
  95. "raise": 1,
  96. "break": 1,
  97. "continue": 1
  98. };
  99. this.checkOutdent = function(state, line, input) {
  100. if (input !== "\r\n" && input !== "\r" && input !== "\n")
  101. return false;
  102. var tokens = this.$tokenizer.getLineTokens(line.trim(), state).tokens;
  103. if (!tokens)
  104. return false;
  105. // ignore trailing comments
  106. do {
  107. var last = tokens.pop();
  108. } while (last && (last.type == "comment" || (last.type == "text" && last.value.match(/^\s+$/))));
  109. if (!last)
  110. return false;
  111. return (last.type == "keyword" && outdents[last.value]);
  112. };
  113. this.autoOutdent = function(state, doc, row) {
  114. // outdenting in python is slightly different because it always applies
  115. // to the next line and only of a new line is inserted
  116. row += 1;
  117. var indent = this.$getIndent(doc.getLine(row));
  118. var tab = doc.getTabString();
  119. if (indent.slice(-tab.length) == tab)
  120. doc.remove(new Range(row, indent.length-tab.length, row, indent.length));
  121. };
  122. }).call(Mode.prototype);
  123. exports.Mode = Mode;
  124. });
  125. /* ***** BEGIN LICENSE BLOCK *****
  126. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  127. *
  128. * The contents of this file are subject to the Mozilla Public License Version
  129. * 1.1 (the "License"); you may not use this file except in compliance with
  130. * the License. You may obtain a copy of the License at
  131. * http://www.mozilla.org/MPL/
  132. *
  133. * Software distributed under the License is distributed on an "AS IS" basis,
  134. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  135. * for the specific language governing rights and limitations under the
  136. * License.
  137. *
  138. * The Original Code is Ajax.org Code Editor (ACE).
  139. *
  140. * The Initial Developer of the Original Code is
  141. * Ajax.org B.V.
  142. * Portions created by the Initial Developer are Copyright (C) 2010
  143. * the Initial Developer. All Rights Reserved.
  144. *
  145. * Contributor(s):
  146. * Fabian Jakobs <fabian AT ajax DOT org>
  147. * Colin Gourlay <colin DOT j DOT gourlay AT gmail DOT com>
  148. *
  149. * Alternatively, the contents of this file may be used under the terms of
  150. * either the GNU General Public License Version 2 or later (the "GPL"), or
  151. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  152. * in which case the provisions of the GPL or the LGPL are applicable instead
  153. * of those above. If you wish to allow use of your version of this file only
  154. * under the terms of either the GPL or the LGPL, and not to allow others to
  155. * use your version of this file under the terms of the MPL, indicate your
  156. * decision by deleting the provisions above and replace them with the notice
  157. * and other provisions required by the GPL or the LGPL. If you do not delete
  158. * the provisions above, a recipient may use your version of this file under
  159. * the terms of any one of the MPL, the GPL or the LGPL.
  160. *
  161. * ***** END LICENSE BLOCK *****
  162. *
  163. * TODO: python delimiters
  164. */
  165. define('ace/mode/python_highlight_rules', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/lib/lang', 'ace/mode/text_highlight_rules'], function(require, exports, module) {
  166. "use strict";
  167. var oop = require("../lib/oop");
  168. var lang = require("../lib/lang");
  169. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  170. var PythonHighlightRules = function() {
  171. var keywords = lang.arrayToMap(
  172. ("and|as|assert|break|class|continue|def|del|elif|else|except|exec|" +
  173. "finally|for|from|global|if|import|in|is|lambda|not|or|pass|print|" +
  174. "raise|return|try|while|with|yield").split("|")
  175. );
  176. var builtinConstants = lang.arrayToMap(
  177. ("True|False|None|NotImplemented|Ellipsis|__debug__").split("|")
  178. );
  179. var builtinFunctions = lang.arrayToMap(
  180. ("abs|divmod|input|open|staticmethod|all|enumerate|int|ord|str|any|" +
  181. "eval|isinstance|pow|sum|basestring|execfile|issubclass|print|super|" +
  182. "binfile|iter|property|tuple|bool|filter|len|range|type|bytearray|" +
  183. "float|list|raw_input|unichr|callable|format|locals|reduce|unicode|" +
  184. "chr|frozenset|long|reload|vars|classmethod|getattr|map|repr|xrange|" +
  185. "cmp|globals|max|reversed|zip|compile|hasattr|memoryview|round|" +
  186. "__import__|complex|hash|min|set|apply|delattr|help|next|setattr|" +
  187. "buffer|dict|hex|object|slice|coerce|dir|id|oct|sorted|intern").split("|")
  188. );
  189. var futureReserved = lang.arrayToMap(
  190. ("").split("|")
  191. );
  192. var strPre = "(?:r|u|ur|R|U|UR|Ur|uR)?";
  193. var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
  194. var octInteger = "(?:0[oO]?[0-7]+)";
  195. var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
  196. var binInteger = "(?:0[bB][01]+)";
  197. var integer = "(?:" + decimalInteger + "|" + octInteger + "|" + hexInteger + "|" + binInteger + ")";
  198. var exponent = "(?:[eE][+-]?\\d+)";
  199. var fraction = "(?:\\.\\d+)";
  200. var intPart = "(?:\\d+)";
  201. var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
  202. var exponentFloat = "(?:(?:" + pointFloat + "|" + intPart + ")" + exponent + ")";
  203. var floatNumber = "(?:" + exponentFloat + "|" + pointFloat + ")";
  204. this.$rules = {
  205. "start" : [ {
  206. token : "comment",
  207. regex : "#.*$"
  208. }, {
  209. token : "string", // """ string
  210. regex : strPre + '"{3}(?:[^\\\\]|\\\\.)*?"{3}'
  211. }, {
  212. token : "string", // multi line """ string start
  213. merge : true,
  214. regex : strPre + '"{3}.*$',
  215. next : "qqstring"
  216. }, {
  217. token : "string", // " string
  218. regex : strPre + '"(?:[^\\\\]|\\\\.)*?"'
  219. }, {
  220. token : "string", // ''' string
  221. regex : strPre + "'{3}(?:[^\\\\]|\\\\.)*?'{3}"
  222. }, {
  223. token : "string", // multi line ''' string start
  224. merge : true,
  225. regex : strPre + "'{3}.*$",
  226. next : "qstring"
  227. }, {
  228. token : "string", // ' string
  229. regex : strPre + "'(?:[^\\\\]|\\\\.)*?'"
  230. }, {
  231. token : "constant.numeric", // imaginary
  232. regex : "(?:" + floatNumber + "|\\d+)[jJ]\\b"
  233. }, {
  234. token : "constant.numeric", // float
  235. regex : floatNumber
  236. }, {
  237. token : "constant.numeric", // long integer
  238. regex : integer + "[lL]\\b"
  239. }, {
  240. token : "constant.numeric", // integer
  241. regex : integer + "\\b"
  242. }, {
  243. token : function(value) {
  244. if (keywords.hasOwnProperty(value))
  245. return "keyword";
  246. else if (builtinConstants.hasOwnProperty(value))
  247. return "constant.language";
  248. else if (futureReserved.hasOwnProperty(value))
  249. return "invalid.illegal";
  250. else if (builtinFunctions.hasOwnProperty(value))
  251. return "support.function";
  252. else if (value == "debugger")
  253. return "invalid.deprecated";
  254. else
  255. return "identifier";
  256. },
  257. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  258. }, {
  259. token : "keyword.operator",
  260. regex : "\\+|\\-|\\*|\\*\\*|\\/|\\/\\/|%|<<|>>|&|\\||\\^|~|<|>|<=|=>|==|!=|<>|="
  261. }, {
  262. token : "lparen.paren",
  263. regex : "[\\[\\(\\{]"
  264. }, {
  265. token : "paren.rparen",
  266. regex : "[\\]\\)\\}]"
  267. }, {
  268. token : "text",
  269. regex : "\\s+"
  270. } ],
  271. "qqstring" : [ {
  272. token : "string", // multi line """ string end
  273. regex : '(?:[^\\\\]|\\\\.)*?"{3}',
  274. next : "start"
  275. }, {
  276. token : "string",
  277. merge : true,
  278. regex : '.+'
  279. } ],
  280. "qstring" : [ {
  281. token : "string", // multi line ''' string end
  282. regex : "(?:[^\\\\]|\\\\.)*?'{3}",
  283. next : "start"
  284. }, {
  285. token : "string",
  286. merge : true,
  287. regex : '.+'
  288. } ]
  289. };
  290. };
  291. oop.inherits(PythonHighlightRules, TextHighlightRules);
  292. exports.PythonHighlightRules = PythonHighlightRules;
  293. });
  294. /* ***** BEGIN LICENSE BLOCK *****
  295. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  296. *
  297. * The contents of this file are subject to the Mozilla Public License Version
  298. * 1.1 (the "License"); you may not use this file except in compliance with
  299. * the License. You may obtain a copy of the License at
  300. * http://www.mozilla.org/MPL/
  301. *
  302. * Software distributed under the License is distributed on an "AS IS" basis,
  303. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  304. * for the specific language governing rights and limitations under the
  305. * License.
  306. *
  307. * The Original Code is Ajax.org Code Editor (ACE).
  308. *
  309. * The Initial Developer of the Original Code is
  310. * Ajax.org B.V.
  311. * Portions created by the Initial Developer are Copyright (C) 2010
  312. * the Initial Developer. All Rights Reserved.
  313. *
  314. * Contributor(s):
  315. * Fabian Jakobs <fabian AT ajax DOT org>
  316. *
  317. * Alternatively, the contents of this file may be used under the terms of
  318. * either the GNU General Public License Version 2 or later (the "GPL"), or
  319. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  320. * in which case the provisions of the GPL or the LGPL are applicable instead
  321. * of those above. If you wish to allow use of your version of this file only
  322. * under the terms of either the GPL or the LGPL, and not to allow others to
  323. * use your version of this file under the terms of the MPL, indicate your
  324. * decision by deleting the provisions above and replace them with the notice
  325. * and other provisions required by the GPL or the LGPL. If you do not delete
  326. * the provisions above, a recipient may use your version of this file under
  327. * the terms of any one of the MPL, the GPL or the LGPL.
  328. *
  329. * ***** END LICENSE BLOCK ***** */
  330. define('ace/mode/folding/pythonic', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/folding/fold_mode'], function(require, exports, module) {
  331. "use strict";
  332. var oop = require("../../lib/oop");
  333. var BaseFoldMode = require("./fold_mode").FoldMode;
  334. var FoldMode = exports.FoldMode = function(markers) {
  335. this.foldingStartMarker = new RegExp("(?:([\\[{])|(" + markers + "))(?:\\s*)(?:#.*)?$");
  336. };
  337. oop.inherits(FoldMode, BaseFoldMode);
  338. (function() {
  339. this.getFoldWidgetRange = function(session, foldStyle, row) {
  340. var line = session.getLine(row);
  341. var match = line.match(this.foldingStartMarker);
  342. if (match) {
  343. if (match[1])
  344. return this.openingBracketBlock(session, match[1], row, match.index);
  345. if (match[2])
  346. return this.indentationBlock(session, row, match.index + match[2].length);
  347. return this.indentationBlock(session, row);
  348. }
  349. }
  350. }).call(FoldMode.prototype);
  351. });/* ***** BEGIN LICENSE BLOCK *****
  352. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  353. *
  354. * The contents of this file are subject to the Mozilla Public License Version
  355. * 1.1 (the "License"); you may not use this file except in compliance with
  356. * the License. You may obtain a copy of the License at
  357. * http://www.mozilla.org/MPL/
  358. *
  359. * Software distributed under the License is distributed on an "AS IS" basis,
  360. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  361. * for the specific language governing rights and limitations under the
  362. * License.
  363. *
  364. * The Original Code is Ajax.org Code Editor (ACE).
  365. *
  366. * The Initial Developer of the Original Code is
  367. * Ajax.org B.V.
  368. * Portions created by the Initial Developer are Copyright (C) 2010
  369. * the Initial Developer. All Rights Reserved.
  370. *
  371. * Contributor(s):
  372. * Fabian Jakobs <fabian AT ajax DOT org>
  373. *
  374. * Alternatively, the contents of this file may be used under the terms of
  375. * either the GNU General Public License Version 2 or later (the "GPL"), or
  376. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  377. * in which case the provisions of the GPL or the LGPL are applicable instead
  378. * of those above. If you wish to allow use of your version of this file only
  379. * under the terms of either the GPL or the LGPL, and not to allow others to
  380. * use your version of this file under the terms of the MPL, indicate your
  381. * decision by deleting the provisions above and replace them with the notice
  382. * and other provisions required by the GPL or the LGPL. If you do not delete
  383. * the provisions above, a recipient may use your version of this file under
  384. * the terms of any one of the MPL, the GPL or the LGPL.
  385. *
  386. * ***** END LICENSE BLOCK ***** */
  387. define('ace/mode/folding/fold_mode', ['require', 'exports', 'module' , 'ace/range'], function(require, exports, module) {
  388. "use strict";
  389. var Range = require("../../range").Range;
  390. var FoldMode = exports.FoldMode = function() {};
  391. (function() {
  392. this.foldingStartMarker = null;
  393. this.foldingStopMarker = null;
  394. // must return "" if there's no fold, to enable caching
  395. this.getFoldWidget = function(session, foldStyle, row) {
  396. var line = session.getLine(row);
  397. if (this.foldingStartMarker.test(line))
  398. return "start";
  399. if (foldStyle == "markbeginend"
  400. && this.foldingStopMarker
  401. && this.foldingStopMarker.test(line))
  402. return "end";
  403. return "";
  404. };
  405. this.getFoldWidgetRange = function(session, foldStyle, row) {
  406. return null;
  407. };
  408. this.indentationBlock = function(session, row, column) {
  409. var re = /^\s*/;
  410. var startRow = row;
  411. var endRow = row;
  412. var line = session.getLine(row);
  413. var startColumn = column || line.length;
  414. var startLevel = line.match(re)[0].length;
  415. var maxRow = session.getLength()
  416. while (++row < maxRow) {
  417. line = session.getLine(row);
  418. var level = line.match(re)[0].length;
  419. if (level == line.length)
  420. continue;
  421. if (level <= startLevel)
  422. break;
  423. endRow = row;
  424. }
  425. if (endRow > startRow) {
  426. var endColumn = session.getLine(endRow).length;
  427. return new Range(startRow, startColumn, endRow, endColumn);
  428. }
  429. };
  430. this.openingBracketBlock = function(session, bracket, row, column) {
  431. var start = {row: row, column: column + 1};
  432. var end = session.$findClosingBracket(bracket, start);
  433. if (!end)
  434. return;
  435. var fw = session.foldWidgets[end.row];
  436. if (fw == null)
  437. fw = this.getFoldWidget(session, end.row);
  438. if (fw == "start") {
  439. end.row --;
  440. end.column = session.getLine(end.row).length;
  441. }
  442. return Range.fromPoints(start, end);
  443. };
  444. }).call(FoldMode.prototype);
  445. });