r_highlight_rules.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * This program is licensed to you under the terms of version 3 of the
  12. * GNU Affero General Public License. This program is distributed WITHOUT
  13. * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
  14. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
  15. * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
  16. *
  17. */
  18. define(function(require, exports, module)
  19. {
  20. var oop = require("../lib/oop");
  21. var lang = require("../lib/lang");
  22. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  23. var TexHighlightRules = require("./tex_highlight_rules").TexHighlightRules;
  24. var RHighlightRules = function()
  25. {
  26. var keywords = lang.arrayToMap(
  27. ("function|if|in|break|next|repeat|else|for|return|switch|while|try|tryCatch|stop|warning|require|library|attach|detach|source|setMethod|setGeneric|setGroupGeneric|setClass")
  28. .split("|")
  29. );
  30. var buildinConstants = lang.arrayToMap(
  31. ("NULL|NA|TRUE|FALSE|T|F|Inf|NaN|NA_integer_|NA_real_|NA_character_|" +
  32. "NA_complex_").split("|")
  33. );
  34. // regexp must not have capturing parentheses. Use (?:) instead.
  35. // regexps are ordered -> the first match is used
  36. this.$rules = {
  37. "start" : [
  38. {
  39. // Roxygen
  40. token : "comment.sectionhead",
  41. regex : "#+(?!').*(?:----|====|####)\\s*$"
  42. },
  43. {
  44. // Roxygen
  45. token : "comment",
  46. regex : "#+'",
  47. next : "rd-start"
  48. },
  49. {
  50. token : "comment",
  51. regex : "#.*$"
  52. },
  53. {
  54. token : "string", // multi line string start
  55. regex : '["]',
  56. next : "qqstring"
  57. },
  58. {
  59. token : "string", // multi line string start
  60. regex : "[']",
  61. next : "qstring"
  62. },
  63. {
  64. token : "constant.numeric", // hex
  65. regex : "0[xX][0-9a-fA-F]+[Li]?\\b"
  66. },
  67. {
  68. token : "constant.numeric", // explicit integer
  69. regex : "\\d+L\\b"
  70. },
  71. {
  72. token : "constant.numeric", // number
  73. regex : "\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d*)?i?\\b"
  74. },
  75. {
  76. token : "constant.numeric", // number with leading decimal
  77. regex : "\\.\\d+(?:[eE][+\\-]?\\d*)?i?\\b"
  78. },
  79. {
  80. token : "constant.language.boolean",
  81. regex : "(?:TRUE|FALSE|T|F)\\b"
  82. },
  83. {
  84. token : "identifier",
  85. regex : "`.*?`"
  86. },
  87. {
  88. onMatch : function(value) {
  89. if (keywords[value])
  90. return "keyword";
  91. else if (buildinConstants[value])
  92. return "constant.language";
  93. else if (value == '...' || value.match(/^\.\.\d+$/))
  94. return "variable.language";
  95. else
  96. return "identifier";
  97. },
  98. regex : "[a-zA-Z.][a-zA-Z0-9._]*\\b"
  99. },
  100. {
  101. token : "keyword.operator",
  102. regex : "%%|>=|<=|==|!=|\\->|<\\-|\\|\\||&&|=|\\+|\\-|\\*|/|\\^|>|<|!|&|\\||~|\\$|:"
  103. },
  104. {
  105. token : "keyword.operator", // infix operators
  106. regex : "%.*?%"
  107. },
  108. {
  109. // Obviously these are neither keywords nor operators, but
  110. // labelling them as such was the easiest way to get them
  111. // to be colored distinctly from regular text
  112. token : "paren.keyword.operator",
  113. regex : "[[({]"
  114. },
  115. {
  116. // Obviously these are neither keywords nor operators, but
  117. // labelling them as such was the easiest way to get them
  118. // to be colored distinctly from regular text
  119. token : "paren.keyword.operator",
  120. regex : "[\\])}]"
  121. },
  122. {
  123. token : "text",
  124. regex : "\\s+"
  125. }
  126. ],
  127. "qqstring" : [
  128. {
  129. token : "string",
  130. regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
  131. next : "start"
  132. },
  133. {
  134. token : "string",
  135. regex : '.+'
  136. }
  137. ],
  138. "qstring" : [
  139. {
  140. token : "string",
  141. regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
  142. next : "start"
  143. },
  144. {
  145. token : "string",
  146. regex : '.+'
  147. }
  148. ]
  149. };
  150. var rdRules = new TexHighlightRules("comment").getRules();
  151. // Make all embedded TeX virtual-comment so they don't interfere with
  152. // auto-indent.
  153. for (var i = 0; i < rdRules["start"].length; i++) {
  154. rdRules["start"][i].token += ".virtual-comment";
  155. }
  156. this.addRules(rdRules, "rd-");
  157. this.$rules["rd-start"].unshift({
  158. token: "text",
  159. regex: "^",
  160. next: "start"
  161. });
  162. this.$rules["rd-start"].unshift({
  163. token : "keyword",
  164. regex : "@(?!@)[^ ]*"
  165. });
  166. this.$rules["rd-start"].unshift({
  167. token : "comment",
  168. regex : "@@"
  169. });
  170. this.$rules["rd-start"].push({
  171. token : "comment",
  172. regex : "[^%\\\\[({\\])}]+"
  173. });
  174. };
  175. oop.inherits(RHighlightRules, TextHighlightRules);
  176. exports.RHighlightRules = RHighlightRules;
  177. });