1 /*! `ruby` grammar compiled for Highlight.js 11.11.1 */
3 var hljsGrammar
= (function () {
8 Description: Ruby is a dynamic, open source programming language with a focus on simplicity and productivity.
9 Website: https://www.ruby-lang.org/
10 Author: Anton Kovalyov <anton@kovalyov.net>
11 Contributors: Peter Leonov <gojpeg@yandex.ru>, Vasily Polovnyov <vast@whiteants.net>, Loren Segal <lsegal@soen.ca>, Pascal Hurni <phi@ruby-reactive.org>, Cedric Sohrauer <sohrauer@googlemail.com>
12 Category: common, scripting
16 const regex
= hljs
.regex
;
17 const RUBY_METHOD_RE
= '([a-zA-Z_]\\w*[!?=]?|[-+~]@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?)';
18 // TODO: move concepts like CAMEL_CASE into `modes.js`
19 const CLASS_NAME_RE
= regex
.either(
20 /\b([A-Z]+[a-z0-9]+)+/,
22 /\b([A-Z]+[a-z0-9]+)+[A-Z]+/,
25 const CLASS_NAME_WITH_NAMESPACE_RE
= regex
.concat(CLASS_NAME_RE
, /(::\w+)*/);
26 // very popular ruby built-ins that one might even assume
27 // are actual keywords (despite that not being the case)
38 const RUBY_KEYWORDS
= {
39 "variable.constant": [
44 "variable.language": [
108 const COMMENT_MODES
= [
112 { contains: [ YARDOCTAG
] }
118 contains: [ YARDOCTAG
],
122 hljs
.COMMENT('^__END__', hljs
.MATCH_NOTHING_RE
)
128 keywords: RUBY_KEYWORDS
133 hljs
.BACKSLASH_ESCAPE
,
150 begin: /%[qQwWx]?\(/,
154 begin: /%[qQwWx]?\[/,
158 begin: /%[qQwWx]?\{/,
166 begin: /%[qQwWx]?\//,
178 begin: /%[qQwWx]?\|/,
181 // in the following expressions, \B in the beginning suppresses recognition of ?-sequences
182 // where ? is the last character of a preceding identifier, as in: `func?4`
183 { begin: /\B\?(\\\d{1,3})/ },
184 { begin: /\B\?(\\x[A-Fa-f0-9]{1,2})/ },
185 { begin: /\B\?(\\u\{?[A-Fa-f0-9]{1,6}\}?)/ },
186 { begin: /\B\?(\\M-\\C-|\\M-\\c|\\c\\M-|\\M-|\\C-\\M-)[\x20-\x7e]/ },
187 { begin: /\B\?\\(c|C-)[\x20-\x7e]/ },
188 { begin: /\B\?\\?\S/ },
191 // this guard makes sure that we have an entire heredoc and not a false
192 // positive (auto-detect, etc.)
195 regex
.lookahead(/(\w+)(?=\W)[^\n]*\n(?:[^\n]*\n)*?\s*\1\b/)
198 hljs
.END_SAME_AS_BEGIN({
202 hljs
.BACKSLASH_ESCAPE
,
211 // Ruby syntax is underdocumented, but this grammar seems to be accurate
212 // as of version 2.7.2 (confirmed with (irb and `Ripper.sexp(...)`)
213 // https://docs.ruby-lang.org/en/2.7.0/doc/syntax/literals_rdoc.html#label-Numbers
214 const decimal = '[1-9](_?[0-9])*|0';
215 const digits
= '[0-9](_?[0-9])*';
220 // decimal integer/float, optionally exponential or rational, optionally imaginary
221 { begin: `\\b(${decimal})(\\.(${digits}))?([eE][+-]?(${digits})|r)?i?\\b` },
223 // explicit decimal/binary/octal/hexadecimal integer,
224 // optionally rational and/or imaginary
225 { begin: "\\b0[dD][0-9](_?[0-9])*r?i?\\b" },
226 { begin: "\\b0[bB][0-1](_?[0-1])*r?i?\\b" },
227 { begin: "\\b0[oO][0-7](_?[0-7])*r?i?\\b" },
228 { begin: "\\b0[xX][0-9a-fA-F](_?[0-9a-fA-F])*r?i?\\b" },
230 // 0-prefixed implicit octal integer, optionally rational and/or imaginary
231 { begin: "\\b0(_?[0-7])+r?i?\\b" }
246 keywords: RUBY_KEYWORDS
,
251 const INCLUDE_EXTEND
= {
253 /(include|extend)\s+/,
254 CLASS_NAME_WITH_NAMESPACE_RE
259 keywords: RUBY_KEYWORDS
262 const CLASS_DEFINITION
= {
267 CLASS_NAME_WITH_NAMESPACE_RE
,
269 CLASS_NAME_WITH_NAMESPACE_RE
274 /\b(class|module)\s+/,
275 CLASS_NAME_WITH_NAMESPACE_RE
281 4: "title.class.inherited"
283 keywords: RUBY_KEYWORDS
286 const UPPER_CASE_CONSTANT
= {
288 match: /\b[A-Z][A-Z_0-9]+\b/,
289 className: "variable.constant"
292 const METHOD_DEFINITION
= {
306 const OBJECT_CREATION
= {
309 CLASS_NAME_WITH_NAMESPACE_RE
,
318 const CLASS_REFERENCE
= {
320 match: CLASS_NAME_RE
,
324 const RUBY_DEFAULT_CONTAINS
= [
333 // swallow namespace qualifiers before symbols
334 begin: hljs
.IDENT_RE
+ '::' },
337 begin: hljs
.UNDERSCORE_IDENT_RE
+ '(!|\\?)?:',
345 { begin: RUBY_METHOD_RE
}
351 // negative-look forward attempts to prevent false matches like:
352 // @ident@ or $ident$ that might indicate this is not ruby at all
353 className: "variable",
354 begin: '(\\$\\W)|((\\$|@@?)(\\w+))(?=[^@$?])' + `(?![A-Za-z])(?![@$?'])`
362 relevance: 0, // this could be a lot of things (in other languages) other than params
363 keywords: RUBY_KEYWORDS
365 { // regexp container
366 begin: '(' + hljs
.RE_STARTERS_RE
+ '|unless)\\s*',
372 hljs
.BACKSLASH_ESCAPE
,
399 ].concat(IRB_OBJECT
, COMMENT_MODES
),
402 ].concat(IRB_OBJECT
, COMMENT_MODES
);
404 SUBST
.contains
= RUBY_DEFAULT_CONTAINS
;
405 PARAMS
.contains
= RUBY_DEFAULT_CONTAINS
;
409 const SIMPLE_PROMPT
= "[>?]>";
411 const DEFAULT_PROMPT
= "[\\w#]+\\(\\w+\\):\\d+:\\d+[>*]";
412 const RVM_PROMPT
= "(\\w+-)?\\d+\\.\\d+\\.\\d+(p\\d+)?[^\\d][^>]+>";
414 const IRB_DEFAULT
= [
419 contains: RUBY_DEFAULT_CONTAINS
423 className: 'meta.prompt',
424 begin: '^(' + SIMPLE_PROMPT
+ "|" + DEFAULT_PROMPT
+ '|' + RVM_PROMPT
+ ')(?=[ ])',
427 keywords: RUBY_KEYWORDS
,
428 contains: RUBY_DEFAULT_CONTAINS
433 COMMENT_MODES
.unshift(IRB_OBJECT
);
444 keywords: RUBY_KEYWORDS
,
446 contains: [ hljs
.SHEBANG({ binary: "ruby" }) ]
448 .concat(COMMENT_MODES
)
449 .concat(RUBY_DEFAULT_CONTAINS
)
457 hljs
.registerLanguage('ruby', hljsGrammar
);