ruby_highlight_rules.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. // exports is for Haml
  35. var constantOtherSymbol = exports.constantOtherSymbol = {
  36. token : "constant.other.symbol.ruby", // symbol
  37. regex : "[:](?:[A-Za-z_]|[@$](?=[a-zA-Z0-9_]))[a-zA-Z0-9_]*[!=?]?"
  38. };
  39. var qString = exports.qString = {
  40. token : "string", // single line
  41. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  42. };
  43. var qqString = exports.qqString = {
  44. token : "string", // single line
  45. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  46. };
  47. var tString = exports.tString = {
  48. token : "string", // backtick string
  49. regex : "[`](?:(?:\\\\.)|(?:[^'\\\\]))*?[`]"
  50. };
  51. var constantNumericHex = exports.constantNumericHex = {
  52. token : "constant.numeric", // hex
  53. regex : "0[xX][0-9a-fA-F](?:[0-9a-fA-F]|_(?=[0-9a-fA-F]))*\\b"
  54. };
  55. var constantNumericFloat = exports.constantNumericFloat = {
  56. token : "constant.numeric", // float
  57. regex : "[+-]?\\d(?:\\d|_(?=\\d))*(?:(?:\\.\\d(?:\\d|_(?=\\d))*)?(?:[eE][+-]?\\d+)?)?\\b"
  58. };
  59. var RubyHighlightRules = function() {
  60. var builtinFunctions = (
  61. "abort|Array|assert|assert_equal|assert_not_equal|assert_same|assert_not_same|" +
  62. "assert_nil|assert_not_nil|assert_match|assert_no_match|assert_in_delta|assert_throws|" +
  63. "assert_raise|assert_nothing_raised|assert_instance_of|assert_kind_of|assert_respond_to|" +
  64. "assert_operator|assert_send|assert_difference|assert_no_difference|assert_recognizes|" +
  65. "assert_generates|assert_response|assert_redirected_to|assert_template|assert_select|" +
  66. "assert_select_email|assert_select_rjs|assert_select_encoded|css_select|at_exit|" +
  67. "attr|attr_writer|attr_reader|attr_accessor|attr_accessible|autoload|binding|block_given?|callcc|" +
  68. "caller|catch|chomp|chomp!|chop|chop!|defined?|delete_via_redirect|eval|exec|exit|" +
  69. "exit!|fail|Float|flunk|follow_redirect!|fork|form_for|form_tag|format|gets|global_variables|gsub|" +
  70. "gsub!|get_via_redirect|host!|https?|https!|include|Integer|lambda|link_to|" +
  71. "link_to_unless_current|link_to_function|link_to_remote|load|local_variables|loop|open|open_session|" +
  72. "p|print|printf|proc|putc|puts|post_via_redirect|put_via_redirect|raise|rand|" +
  73. "raw|readline|readlines|redirect?|request_via_redirect|require|scan|select|" +
  74. "set_trace_func|sleep|split|sprintf|srand|String|stylesheet_link_tag|syscall|system|sub|sub!|test|" +
  75. "throw|trace_var|trap|untrace_var|atan2|cos|exp|frexp|ldexp|log|log10|sin|sqrt|tan|" +
  76. "render|javascript_include_tag|csrf_meta_tag|label_tag|text_field_tag|submit_tag|check_box_tag|" +
  77. "content_tag|radio_button_tag|text_area_tag|password_field_tag|hidden_field_tag|" +
  78. "fields_for|select_tag|options_for_select|options_from_collection_for_select|collection_select|" +
  79. "time_zone_select|select_date|select_time|select_datetime|date_select|time_select|datetime_select|" +
  80. "select_year|select_month|select_day|select_hour|select_minute|select_second|file_field_tag|" +
  81. "file_field|respond_to|skip_before_filter|around_filter|after_filter|verify|" +
  82. "protect_from_forgery|rescue_from|helper_method|redirect_to|before_filter|" +
  83. "send_data|send_file|validates_presence_of|validates_uniqueness_of|validates_length_of|" +
  84. "validates_format_of|validates_acceptance_of|validates_associated|validates_exclusion_of|" +
  85. "validates_inclusion_of|validates_numericality_of|validates_with|validates_each|" +
  86. "authenticate_or_request_with_http_basic|authenticate_or_request_with_http_digest|" +
  87. "filter_parameter_logging|match|get|post|resources|redirect|scope|assert_routing|" +
  88. "translate|localize|extract_locale_from_tld|caches_page|expire_page|caches_action|expire_action|" +
  89. "cache|expire_fragment|expire_cache_for|observe|cache_sweeper|" +
  90. "has_many|has_one|belongs_to|has_and_belongs_to_many"
  91. );
  92. var keywords = (
  93. "alias|and|BEGIN|begin|break|case|class|def|defined|do|else|elsif|END|end|ensure|" +
  94. "__FILE__|finally|for|gem|if|in|__LINE__|module|next|not|or|private|protected|public|" +
  95. "redo|rescue|retry|return|super|then|undef|unless|until|when|while|yield"
  96. );
  97. var buildinConstants = (
  98. "true|TRUE|false|FALSE|nil|NIL|ARGF|ARGV|DATA|ENV|RUBY_PLATFORM|RUBY_RELEASE_DATE|" +
  99. "RUBY_VERSION|STDERR|STDIN|STDOUT|TOPLEVEL_BINDING"
  100. );
  101. var builtinVariables = (
  102. "\$DEBUG|\$defout|\$FILENAME|\$LOAD_PATH|\$SAFE|\$stdin|\$stdout|\$stderr|\$VERBOSE|" +
  103. "$!|root_url|flash|session|cookies|params|request|response|logger|self"
  104. );
  105. var keywordMapper = this.$keywords = this.createKeywordMapper({
  106. "keyword": keywords,
  107. "constant.language": buildinConstants,
  108. "variable.language": builtinVariables,
  109. "support.function": builtinFunctions,
  110. "invalid.deprecated": "debugger" // TODO is this a remnant from js mode?
  111. }, "identifier");
  112. // regexp must not have capturing parentheses. Use (?:) instead.
  113. // regexps are ordered -> the first match is used
  114. this.$rules = {
  115. "start" : [
  116. {
  117. token : "comment",
  118. regex : "#.*$"
  119. }, {
  120. token : "comment", // multi line comment
  121. regex : "^=begin(?:$|\\s.*$)",
  122. next : "comment"
  123. }, {
  124. token : "string.regexp",
  125. regex : "[/](?:(?:\\[(?:\\\\]|[^\\]])+\\])|(?:\\\\/|[^\\]/]))*[/]\\w*\\s*(?=[).,;]|$)"
  126. },
  127. [{
  128. regex: "[{}]", onMatch: function(val, state, stack) {
  129. this.next = val == "{" ? this.nextState : "";
  130. if (val == "{" && stack.length) {
  131. stack.unshift("start", state);
  132. return "paren.lparen";
  133. }
  134. if (val == "}" && stack.length) {
  135. stack.shift();
  136. this.next = stack.shift();
  137. if (this.next.indexOf("string") != -1)
  138. return "paren.end";
  139. }
  140. return val == "{" ? "paren.lparen" : "paren.rparen";
  141. },
  142. nextState: "start"
  143. }, {
  144. token : "string.start",
  145. regex : /"/,
  146. push : [{
  147. token : "constant.language.escape",
  148. regex : /\\(?:[nsrtvfbae'"\\]|c.|C-.|M-.(?:\\C-.)?|[0-7]{3}|x[\da-fA-F]{2}|u[\da-fA-F]{4})/
  149. }, {
  150. token : "paren.start",
  151. regex : /\#{/,
  152. push : "start"
  153. }, {
  154. token : "string.end",
  155. regex : /"/,
  156. next : "pop"
  157. }, {
  158. defaultToken: "string"
  159. }]
  160. }, {
  161. token : "string.start",
  162. regex : /`/,
  163. push : [{
  164. token : "constant.language.escape",
  165. regex : /\\(?:[nsrtvfbae'"\\]|c.|C-.|M-.(?:\\C-.)?|[0-7]{3}|x[\da-fA-F]{2}|u[\da-fA-F]{4})/
  166. }, {
  167. token : "paren.start",
  168. regex : /\#{/,
  169. push : "start"
  170. }, {
  171. token : "string.end",
  172. regex : /`/,
  173. next : "pop"
  174. }, {
  175. defaultToken: "string"
  176. }]
  177. }, {
  178. token : "string.start",
  179. regex : /'/,
  180. push : [{
  181. token : "constant.language.escape",
  182. regex : /\\['\\]/
  183. }, {
  184. token : "string.end",
  185. regex : /'/,
  186. next : "pop"
  187. }, {
  188. defaultToken: "string"
  189. }]
  190. }],
  191. // TODO: add support for %[QqxWwrs][{[(]
  192. {
  193. token : "text", // namespaces aren't symbols
  194. regex : "::"
  195. }, {
  196. token : "variable.instance", // instance variable
  197. regex : "@{1,2}[a-zA-Z_\\d]+"
  198. }, {
  199. token : "support.class", // class name
  200. regex : "[A-Z][a-zA-Z_\\d]+"
  201. },
  202. constantOtherSymbol,
  203. constantNumericHex,
  204. constantNumericFloat,
  205. {
  206. token : "constant.language.boolean",
  207. regex : "(?:true|false)\\b"
  208. }, {
  209. token : keywordMapper,
  210. // TODO: Unicode escape sequences
  211. // TODO: Unicode identifiers
  212. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  213. }, {
  214. token : "punctuation.separator.key-value",
  215. regex : "=>"
  216. }, {
  217. stateName: "heredoc",
  218. onMatch : function(value, currentState, stack) {
  219. var next = value[2] == '-' ? "indentedHeredoc" : "heredoc";
  220. var tokens = value.split(this.splitRegex);
  221. stack.push(next, tokens[3]);
  222. return [
  223. {type:"constant", value: tokens[1]},
  224. {type:"string", value: tokens[2]},
  225. {type:"support.class", value: tokens[3]},
  226. {type:"string", value: tokens[4]}
  227. ];
  228. },
  229. regex : "(<<-?)(['\"`]?)([\\w]+)(['\"`]?)",
  230. rules: {
  231. heredoc: [{
  232. onMatch: function(value, currentState, stack) {
  233. if (value === stack[1]) {
  234. stack.shift();
  235. stack.shift();
  236. this.next = stack[0] || "start";
  237. return "support.class";
  238. }
  239. this.next = "";
  240. return "string";
  241. },
  242. regex: ".*$",
  243. next: "start"
  244. }],
  245. indentedHeredoc: [{
  246. token: "string",
  247. regex: "^ +"
  248. }, {
  249. onMatch: function(value, currentState, stack) {
  250. if (value === stack[1]) {
  251. stack.shift();
  252. stack.shift();
  253. this.next = stack[0] || "start";
  254. return "support.class";
  255. }
  256. this.next = "";
  257. return "string";
  258. },
  259. regex: ".*$",
  260. next: "start"
  261. }]
  262. }
  263. }, {
  264. regex : "$",
  265. token : "empty",
  266. next : function(currentState, stack) {
  267. if (stack[0] === "heredoc" || stack[0] === "indentedHeredoc")
  268. return stack[0];
  269. return currentState;
  270. }
  271. }, {
  272. token : "string.character",
  273. regex : "\\B\\?."
  274. }, {
  275. token : "keyword.operator",
  276. regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|instanceof|new|delete|typeof|void)"
  277. }, {
  278. token : "paren.lparen",
  279. regex : "[[({]"
  280. }, {
  281. token : "paren.rparen",
  282. regex : "[\\])}]"
  283. }, {
  284. token : "text",
  285. regex : "\\s+"
  286. }
  287. ],
  288. "comment" : [
  289. {
  290. token : "comment", // closing comment
  291. regex : "^=end(?:$|\\s.*$)",
  292. next : "start"
  293. }, {
  294. token : "comment", // comment spanning whole line
  295. regex : ".+"
  296. }
  297. ]
  298. };
  299. this.normalizeRules();
  300. };
  301. oop.inherits(RubyHighlightRules, TextHighlightRules);
  302. exports.RubyHighlightRules = RubyHighlightRules;
  303. });