source: node_modules/refractor/lang/ruby.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.9 KB
RevLine 
[d24f17c]1'use strict'
2
3module.exports = ruby
4ruby.displayName = 'ruby'
5ruby.aliases = ['rb']
6function ruby(Prism) {
7 /**
8 * Original by Samuel Flores
9 *
10 * Adds the following new token classes:
11 * constant, builtin, variable, symbol, regex
12 */
13 ;(function (Prism) {
14 Prism.languages.ruby = Prism.languages.extend('clike', {
15 comment: {
16 pattern: /#.*|^=begin\s[\s\S]*?^=end/m,
17 greedy: true
18 },
19 'class-name': {
20 pattern:
21 /(\b(?:class|module)\s+|\bcatch\s+\()[\w.\\]+|\b[A-Z_]\w*(?=\s*\.\s*new\b)/,
22 lookbehind: true,
23 inside: {
24 punctuation: /[.\\]/
25 }
26 },
27 keyword:
28 /\b(?:BEGIN|END|alias|and|begin|break|case|class|def|define_method|defined|do|each|else|elsif|end|ensure|extend|for|if|in|include|module|new|next|nil|not|or|prepend|private|protected|public|raise|redo|require|rescue|retry|return|self|super|then|throw|undef|unless|until|when|while|yield)\b/,
29 operator:
30 /\.{2,3}|&\.|===|<?=>|[!=]?~|(?:&&|\|\||<<|>>|\*\*|[+\-*/%<>!^&|=])=?|[?:]/,
31 punctuation: /[(){}[\].,;]/
32 })
33 Prism.languages.insertBefore('ruby', 'operator', {
34 'double-colon': {
35 pattern: /::/,
36 alias: 'punctuation'
37 }
38 })
39 var interpolation = {
40 pattern: /((?:^|[^\\])(?:\\{2})*)#\{(?:[^{}]|\{[^{}]*\})*\}/,
41 lookbehind: true,
42 inside: {
43 content: {
44 pattern: /^(#\{)[\s\S]+(?=\}$)/,
45 lookbehind: true,
46 inside: Prism.languages.ruby
47 },
48 delimiter: {
49 pattern: /^#\{|\}$/,
50 alias: 'punctuation'
51 }
52 }
53 }
54 delete Prism.languages.ruby.function
55 var percentExpression =
56 '(?:' +
57 [
58 /([^a-zA-Z0-9\s{(\[<=])(?:(?!\1)[^\\]|\\[\s\S])*\1/.source,
59 /\((?:[^()\\]|\\[\s\S]|\((?:[^()\\]|\\[\s\S])*\))*\)/.source,
60 /\{(?:[^{}\\]|\\[\s\S]|\{(?:[^{}\\]|\\[\s\S])*\})*\}/.source,
61 /\[(?:[^\[\]\\]|\\[\s\S]|\[(?:[^\[\]\\]|\\[\s\S])*\])*\]/.source,
62 /<(?:[^<>\\]|\\[\s\S]|<(?:[^<>\\]|\\[\s\S])*>)*>/.source
63 ].join('|') +
64 ')'
65 var symbolName =
66 /(?:"(?:\\.|[^"\\\r\n])*"|(?:\b[a-zA-Z_]\w*|[^\s\0-\x7F]+)[?!]?|\$.)/
67 .source
68 Prism.languages.insertBefore('ruby', 'keyword', {
69 'regex-literal': [
70 {
71 pattern: RegExp(
72 /%r/.source + percentExpression + /[egimnosux]{0,6}/.source
73 ),
74 greedy: true,
75 inside: {
76 interpolation: interpolation,
77 regex: /[\s\S]+/
78 }
79 },
80 {
81 pattern:
82 /(^|[^/])\/(?!\/)(?:\[[^\r\n\]]+\]|\\.|[^[/\\\r\n])+\/[egimnosux]{0,6}(?=\s*(?:$|[\r\n,.;})#]))/,
83 lookbehind: true,
84 greedy: true,
85 inside: {
86 interpolation: interpolation,
87 regex: /[\s\S]+/
88 }
89 }
90 ],
91 variable: /[@$]+[a-zA-Z_]\w*(?:[?!]|\b)/,
92 symbol: [
93 {
94 pattern: RegExp(/(^|[^:]):/.source + symbolName),
95 lookbehind: true,
96 greedy: true
97 },
98 {
99 pattern: RegExp(
100 /([\r\n{(,][ \t]*)/.source + symbolName + /(?=:(?!:))/.source
101 ),
102 lookbehind: true,
103 greedy: true
104 }
105 ],
106 'method-definition': {
107 pattern: /(\bdef\s+)\w+(?:\s*\.\s*\w+)?/,
108 lookbehind: true,
109 inside: {
110 function: /\b\w+$/,
111 keyword: /^self\b/,
112 'class-name': /^\w+/,
113 punctuation: /\./
114 }
115 }
116 })
117 Prism.languages.insertBefore('ruby', 'string', {
118 'string-literal': [
119 {
120 pattern: RegExp(/%[qQiIwWs]?/.source + percentExpression),
121 greedy: true,
122 inside: {
123 interpolation: interpolation,
124 string: /[\s\S]+/
125 }
126 },
127 {
128 pattern:
129 /("|')(?:#\{[^}]+\}|#(?!\{)|\\(?:\r\n|[\s\S])|(?!\1)[^\\#\r\n])*\1/,
130 greedy: true,
131 inside: {
132 interpolation: interpolation,
133 string: /[\s\S]+/
134 }
135 },
136 {
137 pattern: /<<[-~]?([a-z_]\w*)[\r\n](?:.*[\r\n])*?[\t ]*\1/i,
138 alias: 'heredoc-string',
139 greedy: true,
140 inside: {
141 delimiter: {
142 pattern: /^<<[-~]?[a-z_]\w*|\b[a-z_]\w*$/i,
143 inside: {
144 symbol: /\b\w+/,
145 punctuation: /^<<[-~]?/
146 }
147 },
148 interpolation: interpolation,
149 string: /[\s\S]+/
150 }
151 },
152 {
153 pattern: /<<[-~]?'([a-z_]\w*)'[\r\n](?:.*[\r\n])*?[\t ]*\1/i,
154 alias: 'heredoc-string',
155 greedy: true,
156 inside: {
157 delimiter: {
158 pattern: /^<<[-~]?'[a-z_]\w*'|\b[a-z_]\w*$/i,
159 inside: {
160 symbol: /\b\w+/,
161 punctuation: /^<<[-~]?'|'$/
162 }
163 },
164 string: /[\s\S]+/
165 }
166 }
167 ],
168 'command-literal': [
169 {
170 pattern: RegExp(/%x/.source + percentExpression),
171 greedy: true,
172 inside: {
173 interpolation: interpolation,
174 command: {
175 pattern: /[\s\S]+/,
176 alias: 'string'
177 }
178 }
179 },
180 {
181 pattern: /`(?:#\{[^}]+\}|#(?!\{)|\\(?:\r\n|[\s\S])|[^\\`#\r\n])*`/,
182 greedy: true,
183 inside: {
184 interpolation: interpolation,
185 command: {
186 pattern: /[\s\S]+/,
187 alias: 'string'
188 }
189 }
190 }
191 ]
192 })
193 delete Prism.languages.ruby.string
194 Prism.languages.insertBefore('ruby', 'number', {
195 builtin:
196 /\b(?:Array|Bignum|Binding|Class|Continuation|Dir|Exception|FalseClass|File|Fixnum|Float|Hash|IO|Integer|MatchData|Method|Module|NilClass|Numeric|Object|Proc|Range|Regexp|Stat|String|Struct|Symbol|TMS|Thread|ThreadGroup|Time|TrueClass)\b/,
197 constant: /\b[A-Z][A-Z0-9_]*(?:[?!]|\b)/
198 })
199 Prism.languages.rb = Prism.languages.ruby
200 })(Prism)
201}
Note: See TracBrowser for help on using the repository browser.