]>
luflow.net public git repositories - flow-web.git/blob - static/highlight/languages/r.js
1 /*! `r` grammar compiled for Highlight.js 11.11.1 */
3 var hljsGrammar
= (function () {
8 Description: R is a free software environment for statistical computing and graphics.
9 Author: Joe Cheng <joe@rstudio.org>
10 Contributors: Konrad Rudolph <konrad.rudolph@gmail.com>
11 Website: https://www.r-project.org
12 Category: common,scientific
15 /** @type LanguageFn */
17 const regex
= hljs
.regex
;
18 // Identifiers in R cannot start with `_`, but they can start with `.` if it
19 // is not immediately followed by a digit.
20 // R also supports quoted identifiers, which are near-arbitrary sequences
21 // delimited by backticks (`…`), which may contain escape sequences. These are
22 // handled in a separate mode. See `test/markup/r/names.txt` for examples.
23 // FIXME: Support Unicode identifiers.
24 const IDENT_RE
= /(?:(?:[a-zA-Z]|\.[._a-zA-Z])[._a-zA-Z0-9]*)|\.(?!\d)/;
25 const NUMBER_TYPES_RE
= regex
.either(
26 // Special case: only hexadecimal binary powers can contain fractions
27 /0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*[pP][+-]?\d+i?/,
28 // Hexadecimal numbers without fraction and optional binary power
29 /0[xX][0-9a-fA-F]+(?:[pP][+-]?\d+)?[Li]?/,
31 /(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?[Li]?/
33 const OPERATORS_RE
= /[=!<>:]=|\|\||&&|:::?|<-|<<-|->>|->|\|>|[-+*\/?!$&|:<=>@^~]|\*\*/;
34 const PUNCTUATION_RE
= regex
.either(
49 'function if in break next repeat else for while',
51 'NULL NA TRUE FALSE Inf NaN NA_integer_|10 NA_real_|10 '
52 + 'NA_character_|10 NA_complex_|10',
55 'LETTERS letters month.abb month.name pi T F '
56 // Primitive functions
57 // These are all the functions in `base` that are implemented as a
58 // `.Primitive`, minus those functions that are also keywords.
59 + 'abs acos acosh all any anyNA Arg as.call as.character '
60 + 'as.complex as.double as.environment as.integer as.logical '
61 + 'as.null.default as.numeric as.raw asin asinh atan atanh attr '
62 + 'attributes baseenv browser c call ceiling class Conj cos cosh '
63 + 'cospi cummax cummin cumprod cumsum digamma dim dimnames '
64 + 'emptyenv exp expression floor forceAndCall gamma gc.time '
65 + 'globalenv Im interactive invisible is.array is.atomic is.call '
66 + 'is.character is.complex is.double is.environment is.expression '
67 + 'is.finite is.function is.infinite is.integer is.language '
68 + 'is.list is.logical is.matrix is.na is.name is.nan is.null '
69 + 'is.numeric is.object is.pairlist is.raw is.recursive is.single '
70 + 'is.symbol lazyLoadDBfetch length lgamma list log max min '
71 + 'missing Mod names nargs nzchar oldClass on.exit pos.to.env '
72 + 'proc.time prod quote range Re rep retracemem return round '
73 + 'seq_along seq_len seq.int sign signif sin sinh sinpi sqrt '
74 + 'standardGeneric substitute sum switch tan tanh tanpi tracemem '
75 + 'trigamma trunc unclass untracemem UseMethod xtfrm',
85 // Handle `@examples` separately to cause all subsequent code
86 // until the next `@`-tag on its own line to be kept as-is,
87 // preventing highlighting. This code is example R code, so nested
88 // doctags shouldn’t be treated as such. See
89 // `test/markup/r/roxygen.txt` for an example.
93 end: regex
.lookahead(regex
.either(
94 // end if another doc comment
95 /\n^#'\s*(?=@[a-zA-Z]+)/,
96 // or a line with no comment
103 // Handle `@param` to highlight the parameter name following
113 { match: /`(?:\\.|[^`\\])+`/ }
130 hljs
.HASH_COMMENT_MODE
,
134 contains: [ hljs
.BACKSLASH_ESCAPE
],
136 hljs
.END_SAME_AS_BEGIN({
137 begin: /[rR]"(-*)\(/,
140 hljs
.END_SAME_AS_BEGIN({
141 begin: /[rR]"(-*)\{/,
144 hljs
.END_SAME_AS_BEGIN({
145 begin: /[rR]"(-*)\[/,
148 hljs
.END_SAME_AS_BEGIN({
149 begin: /[rR]'(-*)\(/,
152 hljs
.END_SAME_AS_BEGIN({
153 begin: /[rR]'(-*)\{/,
156 hljs
.END_SAME_AS_BEGIN({
157 begin: /[rR]'(-*)\[/,
173 // Matching numbers immediately following punctuation and operators is
174 // tricky since we need to look at the character ahead of a number to
175 // ensure the number is not part of an identifier, and we cannot use
176 // negative look-behind assertions. So instead we explicitly handle all
177 // possible combinations of (operator|punctuation), number.
178 // TODO: replace with negative look-behind when available
179 // { begin: /(?<![a-zA-Z0-9._])0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*[pP][+-]?\d+i?/ },
180 // { begin: /(?<![a-zA-Z0-9._])0[xX][0-9a-fA-F]+([pP][+-]?\d+)?[Li]?/ },
181 // { begin: /(?<![a-zA-Z0-9._])(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?[Li]?/ }
216 scope: { 2: 'number' },
218 /[^a-zA-Z0-9._]|^/, // not part of an identifier, or start of document
225 // Operators/punctuation when they're not directly followed by numbers
227 // Relevance boost for the most common assignment form.
228 scope: { 3: 'operator' },
241 { match: OPERATORS_RE
},
247 scope: 'punctuation',
249 match: PUNCTUATION_RE
253 // Escaped identifier
256 contains: [ { begin: /\\./ } ]
266 hljs
.registerLanguage('r', hljsGrammar
);