ruby.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Ruby patterns
  3. *
  4. * @author Matthew King
  5. * @author Jesse Farmer <jesse@20bits.com>
  6. * @author actsasflinn
  7. */
  8. Rainbow.extend('ruby', [
  9. /**
  10. * __END__ DATA
  11. */
  12. {
  13. matches: {
  14. 1: 'variable.language',
  15. 2: {
  16. language: null
  17. }
  18. },
  19. //find __END__ and consume remaining text
  20. pattern: /^(__END__)\n((?:.*\n)*)/gm
  21. },
  22. /**
  23. * Strings
  24. * 1. No support for multi-line strings
  25. */
  26. {
  27. name: 'string',
  28. matches: {
  29. 1: 'string.open',
  30. 2: [{
  31. name: 'string.interpolation',
  32. matches: {
  33. 1: 'string.open',
  34. 2: {
  35. language: 'ruby'
  36. },
  37. 3: 'string.close'
  38. },
  39. pattern: /(\#\{)(.*?)(\})/g
  40. }],
  41. 3: 'string.close'
  42. },
  43. pattern: /("|`)(.*?[^\\\1])?(\1)/g
  44. },
  45. {
  46. name: 'string',
  47. pattern: /('|"|`)([^\\\1\n]|\\.)*?\1/g
  48. },
  49. {
  50. name: 'string',
  51. pattern: /%[qQ](?=(\(|\[|\{|&lt;|.)(.*?)(?:'|\)|\]|\}|&gt;|\1))(?:\(\2\)|\[\2\]|\{\2\}|\&lt;\2&gt;|\1\2\1)/g
  52. },
  53. /**
  54. * Heredocs
  55. * Heredocs of the form `<<'HTML' ... HTML` are unsupported.
  56. */
  57. {
  58. matches: {
  59. 1: 'string',
  60. 2: 'string',
  61. 3: 'string'
  62. },
  63. pattern: /(&lt;&lt;)(\w+).*?$([\s\S]*?^\2)/gm
  64. },
  65. {
  66. matches: {
  67. 1: 'string',
  68. 2: 'string',
  69. 3: 'string'
  70. },
  71. pattern: /(&lt;&lt;\-)(\w+).*?$([\s\S]*?\2)/gm
  72. },
  73. /**
  74. * Regular expressions
  75. * Escaped delimiter (`/\//`) is unsupported.
  76. */
  77. {
  78. name: 'string.regexp',
  79. matches: {
  80. 1: 'string.regexp',
  81. 2: {
  82. name: 'string.regexp',
  83. pattern: /\\(.){1}/g
  84. },
  85. 3: 'string.regexp',
  86. 4: 'string.regexp'
  87. },
  88. pattern: /(\/)(.*?)(\/)([a-z]*)/g
  89. },
  90. {
  91. name: 'string.regexp',
  92. matches: {
  93. 1: 'string.regexp',
  94. 2: {
  95. name: 'string.regexp',
  96. pattern: /\\(.){1}/g
  97. },
  98. 3: 'string.regexp',
  99. 4: 'string.regexp'
  100. },
  101. pattern: /%r(?=(\(|\[|\{|&lt;|.)(.*?)('|\)|\]|\}|&gt;|\1))(?:\(\2\)|\[\2\]|\{\2\}|\&lt;\2&gt;|\1\2\1)([a-z]*)/g
  102. },
  103. /**
  104. * Comments
  105. */
  106. {
  107. name: 'comment',
  108. pattern: /#.*$/gm
  109. },
  110. {
  111. name: 'comment',
  112. pattern: /^\=begin[\s\S]*?\=end$/gm
  113. },
  114. /**
  115. * Symbols
  116. */
  117. {
  118. matches: {
  119. 1: 'constant'
  120. },
  121. pattern: /(\w+:)[^:]/g
  122. },
  123. {
  124. matches: {
  125. 1: 'constant.symbol'
  126. },
  127. pattern: /[^:](:(?:\w+|(?=['"](.*?)['"])(?:"\2"|'\2')))/g
  128. },
  129. {
  130. name: 'constant.numeric',
  131. pattern: /\b(0x[\da-f]+|[\d_]+)\b/g
  132. },
  133. {
  134. name: 'support.class',
  135. pattern: /\b[A-Z]\w*(?=((\.|::)[A-Za-z]|\[))/g
  136. },
  137. {
  138. name: 'constant',
  139. pattern: /\b[A-Z]\w*\b/g
  140. },
  141. /**
  142. * Keywords, variables, constants, and operators
  143. * In Ruby some keywords are valid method names, e.g., MyClass#yield
  144. * Don't mark those instances as "keywords"
  145. */
  146. {
  147. matches: {
  148. 1: 'storage.class',
  149. 2: 'entity.name.class',
  150. 3: 'entity.other.inherited-class'
  151. },
  152. pattern: /\s*(class)\s+((?:(?:::)?[A-Z]\w*)+)(?:\s+&lt;\s+((?:(?:::)?[A-Z]\w*)+))?/g
  153. },
  154. {
  155. matches: {
  156. 1: 'storage.module',
  157. 2: 'entity.name.class'
  158. },
  159. pattern: /\s*(module)\s+((?:(?:::)?[A-Z]\w*)+)/g
  160. },
  161. {
  162. name: 'variable.global',
  163. pattern: /\$([a-zA-Z_]\w*)\b/g
  164. },
  165. {
  166. name: 'variable.class',
  167. pattern: /@@([a-zA-Z_]\w*)\b/g
  168. },
  169. {
  170. name: 'variable.instance',
  171. pattern: /@([a-zA-Z_]\w*)\b/g
  172. },
  173. {
  174. matches: {
  175. 1: 'keyword.control'
  176. },
  177. pattern: /[^\.]\b(BEGIN|begin|case|class|do|else|elsif|END|end|ensure|for|if|in|module|rescue|then|unless|until|when|while)\b(?![?!])/g
  178. },
  179. {
  180. matches: {
  181. 1: 'keyword.control.pseudo-method'
  182. },
  183. pattern: /[^\.]\b(alias|alias_method|break|next|redo|retry|return|super|undef|yield)\b(?![?!])|\bdefined\?|\bblock_given\?/g
  184. },
  185. {
  186. matches: {
  187. 1: 'constant.language'
  188. },
  189. pattern: /\b(nil|true|false)\b(?![?!])/g
  190. },
  191. {
  192. matches: {
  193. 1: 'variable.language'
  194. },
  195. pattern: /\b(__(FILE|LINE)__|self)\b(?![?!])/g
  196. },
  197. {
  198. matches: {
  199. 1: 'keyword.special-method'
  200. },
  201. pattern: /\b(require|gem|initialize|new|loop|include|extend|raise|attr_reader|attr_writer|attr_accessor|attr|catch|throw|private|module_function|public|protected)\b(?![?!])/g
  202. },
  203. {
  204. name: 'keyword.operator',
  205. pattern: /\s\?\s|=|&lt;&lt;|&lt;&lt;=|%=|&=|\*=|\*\*=|\+=|\-=|\^=|\|{1,2}=|&lt;&lt;|&lt;=&gt;|&lt;(?!&lt;|=)|&gt;(?!&lt;|=|&gt;)|&lt;=|&gt;=|===|==|=~|!=|!~|%|&amp;|\*\*|\*|\+|\-|\/|\||~|&gt;&gt;/g
  206. },
  207. {
  208. matches: {
  209. 1: 'keyword.operator.logical'
  210. },
  211. pattern: /[^\.]\b(and|not|or)\b/g
  212. },
  213. /**
  214. * Functions
  215. * 1. No support for marking function parameters
  216. */
  217. {
  218. matches: {
  219. 1: 'storage.function',
  220. 2: 'entity.name.function'
  221. },
  222. pattern: /(def)\s(.*?)(?=(\s|\())/g
  223. }
  224. ]);