python_highlight_rules.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. /*
  31. * TODO: python delimiters
  32. */
  33. define(function(require, exports, module) {
  34. "use strict";
  35. var oop = require("../lib/oop");
  36. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  37. var PythonHighlightRules = function() {
  38. var keywords = (
  39. "and|as|assert|break|class|continue|def|del|elif|else|except|exec|" +
  40. "finally|for|from|global|if|import|in|is|lambda|not|or|pass|print|" +
  41. "raise|return|try|while|with|yield"
  42. );
  43. var builtinConstants = (
  44. "True|False|None|NotImplemented|Ellipsis|__debug__"
  45. );
  46. var builtinFunctions = (
  47. "abs|divmod|input|open|staticmethod|all|enumerate|int|ord|str|any|" +
  48. "eval|isinstance|pow|sum|basestring|execfile|issubclass|print|super|" +
  49. "binfile|iter|property|tuple|bool|filter|len|range|type|bytearray|" +
  50. "float|list|raw_input|unichr|callable|format|locals|reduce|unicode|" +
  51. "chr|frozenset|long|reload|vars|classmethod|getattr|map|repr|xrange|" +
  52. "cmp|globals|max|reversed|zip|compile|hasattr|memoryview|round|" +
  53. "__import__|complex|hash|min|set|apply|delattr|help|next|setattr|" +
  54. "buffer|dict|hex|object|slice|coerce|dir|id|oct|sorted|intern"
  55. );
  56. //var futureReserved = "";
  57. var keywordMapper = this.createKeywordMapper({
  58. "invalid.deprecated": "debugger",
  59. "support.function": builtinFunctions,
  60. //"invalid.illegal": futureReserved,
  61. "constant.language": builtinConstants,
  62. "keyword": keywords
  63. }, "identifier");
  64. var strPre = "(?:r|u|ur|R|U|UR|Ur|uR)?";
  65. var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
  66. var octInteger = "(?:0[oO]?[0-7]+)";
  67. var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
  68. var binInteger = "(?:0[bB][01]+)";
  69. var integer = "(?:" + decimalInteger + "|" + octInteger + "|" + hexInteger + "|" + binInteger + ")";
  70. var exponent = "(?:[eE][+-]?\\d+)";
  71. var fraction = "(?:\\.\\d+)";
  72. var intPart = "(?:\\d+)";
  73. var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
  74. var exponentFloat = "(?:(?:" + pointFloat + "|" + intPart + ")" + exponent + ")";
  75. var floatNumber = "(?:" + exponentFloat + "|" + pointFloat + ")";
  76. var stringEscape = "\\\\(x[0-9A-Fa-f]{2}|[0-7]{3}|[\\\\abfnrtv'\"]|U[0-9A-Fa-f]{8}|u[0-9A-Fa-f]{4})";
  77. this.$rules = {
  78. "start" : [ {
  79. token : "comment",
  80. regex : "#.*$"
  81. }, {
  82. token : "string", // multi line """ string start
  83. regex : strPre + '"{3}',
  84. next : "qqstring3"
  85. }, {
  86. token : "string", // " string
  87. regex : strPre + '"(?=.)',
  88. next : "qqstring"
  89. }, {
  90. token : "string", // multi line ''' string start
  91. regex : strPre + "'{3}",
  92. next : "qstring3"
  93. }, {
  94. token : "string", // ' string
  95. regex : strPre + "'(?=.)",
  96. next : "qstring"
  97. }, {
  98. token : "constant.numeric", // imaginary
  99. regex : "(?:" + floatNumber + "|\\d+)[jJ]\\b"
  100. }, {
  101. token : "constant.numeric", // float
  102. regex : floatNumber
  103. }, {
  104. token : "constant.numeric", // long integer
  105. regex : integer + "[lL]\\b"
  106. }, {
  107. token : "constant.numeric", // integer
  108. regex : integer + "\\b"
  109. }, {
  110. token : keywordMapper,
  111. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  112. }, {
  113. token : "keyword.operator",
  114. regex : "\\+|\\-|\\*|\\*\\*|\\/|\\/\\/|%|<<|>>|&|\\||\\^|~|<|>|<=|=>|==|!=|<>|="
  115. }, {
  116. token : "paren.lparen",
  117. regex : "[\\[\\(\\{]"
  118. }, {
  119. token : "paren.rparen",
  120. regex : "[\\]\\)\\}]"
  121. }, {
  122. token : "text",
  123. regex : "\\s+"
  124. } ],
  125. "qqstring3" : [ {
  126. token : "constant.language.escape",
  127. regex : stringEscape
  128. }, {
  129. token : "string", // multi line """ string end
  130. regex : '"{3}',
  131. next : "start"
  132. }, {
  133. defaultToken : "string"
  134. } ],
  135. "qstring3" : [ {
  136. token : "constant.language.escape",
  137. regex : stringEscape
  138. }, {
  139. token : "string", // multi line ''' string end
  140. regex : "'{3}",
  141. next : "start"
  142. }, {
  143. defaultToken : "string"
  144. } ],
  145. "qqstring" : [{
  146. token : "constant.language.escape",
  147. regex : stringEscape
  148. }, {
  149. token : "string",
  150. regex : "\\\\$",
  151. next : "qqstring"
  152. }, {
  153. token : "string",
  154. regex : '"|$',
  155. next : "start"
  156. }, {
  157. defaultToken: "string"
  158. }],
  159. "qstring" : [{
  160. token : "constant.language.escape",
  161. regex : stringEscape
  162. }, {
  163. token : "string",
  164. regex : "\\\\$",
  165. next : "qstring"
  166. }, {
  167. token : "string",
  168. regex : "'|$",
  169. next : "start"
  170. }, {
  171. defaultToken: "string"
  172. }]
  173. };
  174. };
  175. oop.inherits(PythonHighlightRules, TextHighlightRules);
  176. exports.PythonHighlightRules = PythonHighlightRules;
  177. });