1 /*! `ruby` grammar compiled for Highlight.js 11.11.1 */
2 var hljsGrammar
= (function () {
7 Description: Ruby is a dynamic, open source programming language with a focus on simplicity and productivity.
8 Website: https://www.ruby-lang.org/
9 Author: Anton Kovalyov <anton@kovalyov.net>
10 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>
11 Category: common, scripting
15 const regex
= hljs
.regex
;
16 const RUBY_METHOD_RE
= '([a-zA-Z_]\\w*[!?=]?|[-+~]@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?)';
17 // TODO: move concepts like CAMEL_CASE into `modes.js`
18 const CLASS_NAME_RE
= regex
.either(
19 /\b([A-Z]+[a-z0-9]+)+/,
21 /\b([A-Z]+[a-z0-9]+)+[A-Z]+/,
24 const CLASS_NAME_WITH_NAMESPACE_RE
= regex
.concat(CLASS_NAME_RE
, /(::\w+)*/);
25 // very popular ruby built-ins that one might even assume
26 // are actual keywords (despite that not being the case)
37 const RUBY_KEYWORDS
= {
38 "variable.constant": [
43 "variable.language": [
107 const COMMENT_MODES
= [
111 { contains: [ YARDOCTAG
] }
117 contains: [ YARDOCTAG
],
121 hljs
.COMMENT('^__END__', hljs
.MATCH_NOTHING_RE
)
127 keywords: RUBY_KEYWORDS
132 hljs
.BACKSLASH_ESCAPE
,
149 begin: /%[qQwWx]?\(/,
153 begin: /%[qQwWx]?\[/,
157 begin: /%[qQwWx]?\{/,
165 begin: /%[qQwWx]?\//,
177 begin: /%[qQwWx]?\|/,
180 // in the following expressions, \B in the beginning suppresses recognition of ?-sequences
181 // where ? is the last character of a preceding identifier, as in: `func?4`
182 { begin: /\B\?(\\\d{1,3})/ },
183 { begin: /\B\?(\\x[A-Fa-f0-9]{1,2})/ },
184 { begin: /\B\?(\\u\{?[A-Fa-f0-9]{1,6}\}?)/ },
185 { begin: /\B\?(\\M-\\C-|\\M-\\c|\\c\\M-|\\M-|\\C-\\M-)[\x20-\x7e]/ },
186 { begin: /\B\?\\(c|C-)[\x20-\x7e]/ },
187 { begin: /\B\?\\?\S/ },
190 // this guard makes sure that we have an entire heredoc and not a false
191 // positive (auto-detect, etc.)
194 regex
.lookahead(/(\w+)(?=\W)[^\n]*\n(?:[^\n]*\n)*?\s*\1\b/)
197 hljs
.END_SAME_AS_BEGIN({
201 hljs
.BACKSLASH_ESCAPE
,
210 // Ruby syntax is underdocumented, but this grammar seems to be accurate
211 // as of version 2.7.2 (confirmed with (irb and `Ripper.sexp(...)`)
212 // https://docs.ruby-lang.org/en/2.7.0/doc/syntax/literals_rdoc.html#label-Numbers
213 const decimal = '[1-9](_?[0-9])*|0';
214 const digits
= '[0-9](_?[0-9])*';
219 // decimal integer/float, optionally exponential or rational, optionally imaginary
220 { begin: `\\b(${decimal})(\\.(${digits}))?([eE][+-]?(${digits})|r)?i?\\b` },
222 // explicit decimal/binary/octal/hexadecimal integer,
223 // optionally rational and/or imaginary
224 { begin: "\\b0[dD][0-9](_?[0-9])*r?i?\\b" },
225 { begin: "\\b0[bB][0-1](_?[0-1])*r?i?\\b" },
226 { begin: "\\b0[oO][0-7](_?[0-7])*r?i?\\b" },
227 { begin: "\\b0[xX][0-9a-fA-F](_?[0-9a-fA-F])*r?i?\\b" },
229 // 0-prefixed implicit octal integer, optionally rational and/or imaginary
230 { begin: "\\b0(_?[0-7])+r?i?\\b" }
245 keywords: RUBY_KEYWORDS
,
250 const INCLUDE_EXTEND
= {
252 /(include|extend)\s+/,
253 CLASS_NAME_WITH_NAMESPACE_RE
258 keywords: RUBY_KEYWORDS
261 const CLASS_DEFINITION
= {
266 CLASS_NAME_WITH_NAMESPACE_RE
,
268 CLASS_NAME_WITH_NAMESPACE_RE
273 /\b(class|module)\s+/,
274 CLASS_NAME_WITH_NAMESPACE_RE
280 4: "title.class.inherited"
282 keywords: RUBY_KEYWORDS
285 const UPPER_CASE_CONSTANT
= {
287 match: /\b[A-Z][A-Z_0-9]+\b/,
288 className: "variable.constant"
291 const METHOD_DEFINITION
= {
305 const OBJECT_CREATION
= {
308 CLASS_NAME_WITH_NAMESPACE_RE
,
317 const CLASS_REFERENCE
= {
319 match: CLASS_NAME_RE
,
323 const RUBY_DEFAULT_CONTAINS
= [
332 // swallow namespace qualifiers before symbols
333 begin: hljs
.IDENT_RE
+ '::' },
336 begin: hljs
.UNDERSCORE_IDENT_RE
+ '(!|\\?)?:',
344 { begin: RUBY_METHOD_RE
}
350 // negative-look forward attempts to prevent false matches like:
351 // @ident@ or $ident$ that might indicate this is not ruby at all
352 className: "variable",
353 begin: '(\\$\\W)|((\\$|@@?)(\\w+))(?=[^@$?])' + `(?![A-Za-z])(?![@$?'])`
361 relevance: 0, // this could be a lot of things (in other languages) other than params
362 keywords: RUBY_KEYWORDS
364 { // regexp container
365 begin: '(' + hljs
.RE_STARTERS_RE
+ '|unless)\\s*',
371 hljs
.BACKSLASH_ESCAPE
,
398 ].concat(IRB_OBJECT
, COMMENT_MODES
),
401 ].concat(IRB_OBJECT
, COMMENT_MODES
);
403 SUBST
.contains
= RUBY_DEFAULT_CONTAINS
;
404 PARAMS
.contains
= RUBY_DEFAULT_CONTAINS
;
408 const SIMPLE_PROMPT
= "[>?]>";
410 const DEFAULT_PROMPT
= "[\\w#]+\\(\\w+\\):\\d+:\\d+[>*]";
411 const RVM_PROMPT
= "(\\w+-)?\\d+\\.\\d+\\.\\d+(p\\d+)?[^\\d][^>]+>";
413 const IRB_DEFAULT
= [
418 contains: RUBY_DEFAULT_CONTAINS
422 className: 'meta.prompt',
423 begin: '^(' + SIMPLE_PROMPT
+ "|" + DEFAULT_PROMPT
+ '|' + RVM_PROMPT
+ ')(?=[ ])',
426 keywords: RUBY_KEYWORDS
,
427 contains: RUBY_DEFAULT_CONTAINS
432 COMMENT_MODES
.unshift(IRB_OBJECT
);
443 keywords: RUBY_KEYWORDS
,
445 contains: [ hljs
.SHEBANG({ binary: "ruby" }) ]
447 .concat(COMMENT_MODES
)
448 .concat(RUBY_DEFAULT_CONTAINS
)
456 export default hljsGrammar
;