coffee_highlight_rules.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../lib/oop");
  33. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  34. oop.inherits(CoffeeHighlightRules, TextHighlightRules);
  35. function CoffeeHighlightRules() {
  36. var identifier = "[$A-Za-z_\\x7f-\\uffff][$\\w\\x7f-\\uffff]*";
  37. var keywords = (
  38. "this|throw|then|try|typeof|super|switch|return|break|by|continue|" +
  39. "catch|class|in|instanceof|is|isnt|if|else|extends|for|own|" +
  40. "finally|function|while|when|new|no|not|delete|debugger|do|loop|of|off|" +
  41. "or|on|unless|until|and|yes"
  42. );
  43. var langConstant = (
  44. "true|false|null|undefined|NaN|Infinity"
  45. );
  46. var illegal = (
  47. "case|const|default|function|var|void|with|enum|export|implements|" +
  48. "interface|let|package|private|protected|public|static|yield|" +
  49. "__hasProp|slice|bind|indexOf"
  50. );
  51. var supportClass = (
  52. "Array|Boolean|Date|Function|Number|Object|RegExp|ReferenceError|String|" +
  53. "Error|EvalError|InternalError|RangeError|ReferenceError|StopIteration|" +
  54. "SyntaxError|TypeError|URIError|" +
  55. "ArrayBuffer|Float32Array|Float64Array|Int16Array|Int32Array|Int8Array|" +
  56. "Uint16Array|Uint32Array|Uint8Array|Uint8ClampedArray"
  57. );
  58. var supportFunction = (
  59. "Math|JSON|isNaN|isFinite|parseInt|parseFloat|encodeURI|" +
  60. "encodeURIComponent|decodeURI|decodeURIComponent|String|"
  61. );
  62. var variableLanguage = (
  63. "window|arguments|prototype|document"
  64. );
  65. var keywordMapper = this.createKeywordMapper({
  66. "keyword": keywords,
  67. "constant.language": langConstant,
  68. "invalid.illegal": illegal,
  69. "language.support.class": supportClass,
  70. "language.support.function": supportFunction,
  71. "variable.language": variableLanguage
  72. }, "identifier");
  73. var functionRule = {
  74. token: ["paren.lparen", "variable.parameter", "paren.rparen", "text", "storage.type"],
  75. regex: /(?:(\()((?:"[^")]*?"|'[^')]*?'|\/[^\/)]*?\/|[^()\"'\/])*?)(\))(\s*))?([\-=]>)/.source
  76. };
  77. var stringEscape = /\\(?:x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|[0-2][0-7]{0,2}|3[0-6][0-7]?|37[0-7]?|[4-7][0-7]?|.)/;
  78. this.$rules = {
  79. start : [
  80. {
  81. token : "constant.numeric",
  82. regex : "(?:0x[\\da-fA-F]+|(?:\\d+(?:\\.\\d+)?|\\.\\d+)(?:[eE][+-]?\\d+)?)"
  83. }, {
  84. stateName: "qdoc",
  85. token : "string", regex : "'''", next : [
  86. {token : "string", regex : "'''", next : "start"},
  87. {token : "constant.language.escape", regex : stringEscape},
  88. {defaultToken: "string"}
  89. ]
  90. }, {
  91. stateName: "qqdoc",
  92. token : "string",
  93. regex : '"""',
  94. next : [
  95. {token : "string", regex : '"""', next : "start"},
  96. {token : "paren.string", regex : '#{', push : "start"},
  97. {token : "constant.language.escape", regex : stringEscape},
  98. {defaultToken: "string"}
  99. ]
  100. }, {
  101. stateName: "qstring",
  102. token : "string", regex : "'", next : [
  103. {token : "string", regex : "'", next : "start"},
  104. {token : "constant.language.escape", regex : stringEscape},
  105. {defaultToken: "string"}
  106. ]
  107. }, {
  108. stateName: "qqstring",
  109. token : "string.start", regex : '"', next : [
  110. {token : "string.end", regex : '"', next : "start"},
  111. {token : "paren.string", regex : '#{', push : "start"},
  112. {token : "constant.language.escape", regex : stringEscape},
  113. {defaultToken: "string"}
  114. ]
  115. }, {
  116. stateName: "js",
  117. token : "string", regex : "`", next : [
  118. {token : "string", regex : "`", next : "start"},
  119. {token : "constant.language.escape", regex : stringEscape},
  120. {defaultToken: "string"}
  121. ]
  122. }, {
  123. regex: "[{}]", onMatch: function(val, state, stack) {
  124. this.next = "";
  125. if (val == "{" && stack.length) {
  126. stack.unshift("start", state);
  127. return "paren";
  128. }
  129. if (val == "}" && stack.length) {
  130. stack.shift();
  131. this.next = stack.shift() || "";
  132. if (this.next.indexOf("string") != -1)
  133. return "paren.string";
  134. }
  135. return "paren";
  136. }
  137. }, {
  138. token : "string.regex",
  139. regex : "///",
  140. next : "heregex"
  141. }, {
  142. token : "string.regex",
  143. regex : /(?:\/(?![\s=])[^[\/\n\\]*(?:(?:\\[\s\S]|\[[^\]\n\\]*(?:\\[\s\S][^\]\n\\]*)*])[^[\/\n\\]*)*\/)(?:[imgy]{0,4})(?!\w)/
  144. }, {
  145. token : "comment",
  146. regex : "###(?!#)",
  147. next : "comment"
  148. }, {
  149. token : "comment",
  150. regex : "#.*"
  151. }, {
  152. token : ["punctuation.operator", "text", "identifier"],
  153. regex : "(\\.)(\\s*)(" + illegal + ")"
  154. }, {
  155. token : "punctuation.operator",
  156. regex : "\\.{1,3}"
  157. }, {
  158. //class A extends B
  159. token : ["keyword", "text", "language.support.class",
  160. "text", "keyword", "text", "language.support.class"],
  161. regex : "(class)(\\s+)(" + identifier + ")(?:(\\s+)(extends)(\\s+)(" + identifier + "))?"
  162. }, {
  163. //play = (...) ->
  164. token : ["entity.name.function", "text", "keyword.operator", "text"].concat(functionRule.token),
  165. regex : "(" + identifier + ")(\\s*)([=:])(\\s*)" + functionRule.regex
  166. },
  167. functionRule,
  168. {
  169. token : "variable",
  170. regex : "@(?:" + identifier + ")?"
  171. }, {
  172. token: keywordMapper,
  173. regex : identifier
  174. }, {
  175. token : "punctuation.operator",
  176. regex : "\\,|\\."
  177. }, {
  178. token : "storage.type",
  179. regex : "[\\-=]>"
  180. }, {
  181. token : "keyword.operator",
  182. regex : "(?:[-+*/%<>&|^!?=]=|>>>=?|\\-\\-|\\+\\+|::|&&=|\\|\\|=|<<=|>>=|\\?\\.|\\.{2,3}|[!*+-=><])"
  183. }, {
  184. token : "paren.lparen",
  185. regex : "[({[]"
  186. }, {
  187. token : "paren.rparen",
  188. regex : "[\\]})]"
  189. }, {
  190. token : "text",
  191. regex : "\\s+"
  192. }],
  193. heregex : [{
  194. token : "string.regex",
  195. regex : '.*?///[imgy]{0,4}',
  196. next : "start"
  197. }, {
  198. token : "comment.regex",
  199. regex : "\\s+(?:#.*)?"
  200. }, {
  201. token : "string.regex",
  202. regex : "\\S+"
  203. }],
  204. comment : [{
  205. token : "comment",
  206. regex : '###',
  207. next : "start"
  208. }, {
  209. defaultToken : "comment"
  210. }]
  211. };
  212. this.normalizeRules();
  213. }
  214. exports.CoffeeHighlightRules = CoffeeHighlightRules;
  215. });