r_highlight_rules.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * r_highlight_rules.js
  3. *
  4. * Copyright (C) 2009-11 by RStudio, Inc.
  5. *
  6. * The Initial Developer of the Original Code is
  7. * Ajax.org B.V.
  8. * Portions created by the Initial Developer are Copyright (C) 2010
  9. * the Initial Developer. All Rights Reserved.
  10. *
  11. * Distributed under the BSD license:
  12. *
  13. * Copyright (c) 2010, Ajax.org B.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are met:
  18. * * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * * Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * * Neither the name of Ajax.org B.V. nor the
  24. * names of its contributors may be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  28. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  34. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  36. *
  37. */
  38. define(function(require, exports, module)
  39. {
  40. var oop = require("../lib/oop");
  41. var lang = require("../lib/lang");
  42. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  43. var TexHighlightRules = require("./tex_highlight_rules").TexHighlightRules;
  44. var RHighlightRules = function()
  45. {
  46. var keywords = lang.arrayToMap(
  47. ("function|if|in|break|next|repeat|else|for|return|switch|while|try|tryCatch|stop|warning|require|library|attach|detach|source|setMethod|setGeneric|setGroupGeneric|setClass")
  48. .split("|")
  49. );
  50. var buildinConstants = lang.arrayToMap(
  51. ("NULL|NA|TRUE|FALSE|T|F|Inf|NaN|NA_integer_|NA_real_|NA_character_|" +
  52. "NA_complex_").split("|")
  53. );
  54. // regexp must not have capturing parentheses. Use (?:) instead.
  55. // regexps are ordered -> the first match is used
  56. this.$rules = {
  57. "start" : [
  58. {
  59. // Roxygen
  60. token : "comment.sectionhead",
  61. regex : "#+(?!').*(?:----|====|####)\\s*$"
  62. },
  63. {
  64. // Roxygen
  65. token : "comment",
  66. regex : "#+'",
  67. next : "rd-start"
  68. },
  69. {
  70. token : "comment",
  71. regex : "#.*$"
  72. },
  73. {
  74. token : "string", // multi line string start
  75. regex : '["]',
  76. next : "qqstring"
  77. },
  78. {
  79. token : "string", // multi line string start
  80. regex : "[']",
  81. next : "qstring"
  82. },
  83. {
  84. token : "constant.numeric", // hex
  85. regex : "0[xX][0-9a-fA-F]+[Li]?\\b"
  86. },
  87. {
  88. token : "constant.numeric", // explicit integer
  89. regex : "\\d+L\\b"
  90. },
  91. {
  92. token : "constant.numeric", // number
  93. regex : "\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d*)?i?\\b"
  94. },
  95. {
  96. token : "constant.numeric", // number with leading decimal
  97. regex : "\\.\\d+(?:[eE][+\\-]?\\d*)?i?\\b"
  98. },
  99. {
  100. token : "constant.language.boolean",
  101. regex : "(?:TRUE|FALSE|T|F)\\b"
  102. },
  103. {
  104. token : "identifier",
  105. regex : "`.*?`"
  106. },
  107. {
  108. onMatch : function(value) {
  109. if (keywords[value])
  110. return "keyword";
  111. else if (buildinConstants[value])
  112. return "constant.language";
  113. else if (value == '...' || value.match(/^\.\.\d+$/))
  114. return "variable.language";
  115. else
  116. return "identifier";
  117. },
  118. regex : "[a-zA-Z.][a-zA-Z0-9._]*\\b"
  119. },
  120. {
  121. token : "keyword.operator",
  122. regex : "%%|>=|<=|==|!=|\\->|<\\-|\\|\\||&&|=|\\+|\\-|\\*|/|\\^|>|<|!|&|\\||~|\\$|:"
  123. },
  124. {
  125. token : "keyword.operator", // infix operators
  126. regex : "%.*?%"
  127. },
  128. {
  129. // Obviously these are neither keywords nor operators, but
  130. // labelling them as such was the easiest way to get them
  131. // to be colored distinctly from regular text
  132. token : "paren.keyword.operator",
  133. regex : "[[({]"
  134. },
  135. {
  136. // Obviously these are neither keywords nor operators, but
  137. // labelling them as such was the easiest way to get them
  138. // to be colored distinctly from regular text
  139. token : "paren.keyword.operator",
  140. regex : "[\\])}]"
  141. },
  142. {
  143. token : "text",
  144. regex : "\\s+"
  145. }
  146. ],
  147. "qqstring" : [
  148. {
  149. token : "string",
  150. regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
  151. next : "start"
  152. },
  153. {
  154. token : "string",
  155. regex : '.+'
  156. }
  157. ],
  158. "qstring" : [
  159. {
  160. token : "string",
  161. regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
  162. next : "start"
  163. },
  164. {
  165. token : "string",
  166. regex : '.+'
  167. }
  168. ]
  169. };
  170. var rdRules = new TexHighlightRules("comment").getRules();
  171. // Make all embedded TeX virtual-comment so they don't interfere with
  172. // auto-indent.
  173. for (var i = 0; i < rdRules["start"].length; i++) {
  174. rdRules["start"][i].token += ".virtual-comment";
  175. }
  176. this.addRules(rdRules, "rd-");
  177. this.$rules["rd-start"].unshift({
  178. token: "text",
  179. regex: "^",
  180. next: "start"
  181. });
  182. this.$rules["rd-start"].unshift({
  183. token : "keyword",
  184. regex : "@(?!@)[^ ]*"
  185. });
  186. this.$rules["rd-start"].unshift({
  187. token : "comment",
  188. regex : "@@"
  189. });
  190. this.$rules["rd-start"].push({
  191. token : "comment",
  192. regex : "[^%\\\\[({\\])}]+"
  193. });
  194. };
  195. oop.inherits(RHighlightRules, TextHighlightRules);
  196. exports.RHighlightRules = RHighlightRules;
  197. });