]> luflow.net public git repositories - flow-web.git/blob - static/highlight/es/languages/r.js
Initial commit.
[flow-web.git] / static / highlight / es / languages / r.js
1 /*! `r` grammar compiled for Highlight.js 11.11.1 */
2 var hljsGrammar = (function () {
3 'use strict';
4
5 /*
6 Language: R
7 Description: R is a free software environment for statistical computing and graphics.
8 Author: Joe Cheng <joe@rstudio.org>
9 Contributors: Konrad Rudolph <konrad.rudolph@gmail.com>
10 Website: https://www.r-project.org
11 Category: common,scientific
12 */
13
14 /** @type LanguageFn */
15 function r(hljs) {
16 const regex = hljs.regex;
17 // Identifiers in R cannot start with `_`, but they can start with `.` if it
18 // is not immediately followed by a digit.
19 // R also supports quoted identifiers, which are near-arbitrary sequences
20 // delimited by backticks (`…`), which may contain escape sequences. These are
21 // handled in a separate mode. See `test/markup/r/names.txt` for examples.
22 // FIXME: Support Unicode identifiers.
23 const IDENT_RE = /(?:(?:[a-zA-Z]|\.[._a-zA-Z])[._a-zA-Z0-9]*)|\.(?!\d)/;
24 const NUMBER_TYPES_RE = regex.either(
25 // Special case: only hexadecimal binary powers can contain fractions
26 /0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*[pP][+-]?\d+i?/,
27 // Hexadecimal numbers without fraction and optional binary power
28 /0[xX][0-9a-fA-F]+(?:[pP][+-]?\d+)?[Li]?/,
29 // Decimal numbers
30 /(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?[Li]?/
31 );
32 const OPERATORS_RE = /[=!<>:]=|\|\||&&|:::?|<-|<<-|->>|->|\|>|[-+*\/?!$&|:<=>@^~]|\*\*/;
33 const PUNCTUATION_RE = regex.either(
34 /[()]/,
35 /[{}]/,
36 /\[\[/,
37 /[[\]]/,
38 /\\/,
39 /,/
40 );
41
42 return {
43 name: 'R',
44
45 keywords: {
46 $pattern: IDENT_RE,
47 keyword:
48 'function if in break next repeat else for while',
49 literal:
50 'NULL NA TRUE FALSE Inf NaN NA_integer_|10 NA_real_|10 '
51 + 'NA_character_|10 NA_complex_|10',
52 built_in:
53 // Builtin constants
54 'LETTERS letters month.abb month.name pi T F '
55 // Primitive functions
56 // These are all the functions in `base` that are implemented as a
57 // `.Primitive`, minus those functions that are also keywords.
58 + 'abs acos acosh all any anyNA Arg as.call as.character '
59 + 'as.complex as.double as.environment as.integer as.logical '
60 + 'as.null.default as.numeric as.raw asin asinh atan atanh attr '
61 + 'attributes baseenv browser c call ceiling class Conj cos cosh '
62 + 'cospi cummax cummin cumprod cumsum digamma dim dimnames '
63 + 'emptyenv exp expression floor forceAndCall gamma gc.time '
64 + 'globalenv Im interactive invisible is.array is.atomic is.call '
65 + 'is.character is.complex is.double is.environment is.expression '
66 + 'is.finite is.function is.infinite is.integer is.language '
67 + 'is.list is.logical is.matrix is.na is.name is.nan is.null '
68 + 'is.numeric is.object is.pairlist is.raw is.recursive is.single '
69 + 'is.symbol lazyLoadDBfetch length lgamma list log max min '
70 + 'missing Mod names nargs nzchar oldClass on.exit pos.to.env '
71 + 'proc.time prod quote range Re rep retracemem return round '
72 + 'seq_along seq_len seq.int sign signif sin sinh sinpi sqrt '
73 + 'standardGeneric substitute sum switch tan tanh tanpi tracemem '
74 + 'trigamma trunc unclass untracemem UseMethod xtfrm',
75 },
76
77 contains: [
78 // Roxygen comments
79 hljs.COMMENT(
80 /#'/,
81 /$/,
82 { contains: [
83 {
84 // Handle `@examples` separately to cause all subsequent code
85 // until the next `@`-tag on its own line to be kept as-is,
86 // preventing highlighting. This code is example R code, so nested
87 // doctags shouldn’t be treated as such. See
88 // `test/markup/r/roxygen.txt` for an example.
89 scope: 'doctag',
90 match: /@examples/,
91 starts: {
92 end: regex.lookahead(regex.either(
93 // end if another doc comment
94 /\n^#'\s*(?=@[a-zA-Z]+)/,
95 // or a line with no comment
96 /\n^(?!#')/
97 )),
98 endsParent: true
99 }
100 },
101 {
102 // Handle `@param` to highlight the parameter name following
103 // after.
104 scope: 'doctag',
105 begin: '@param',
106 end: /$/,
107 contains: [
108 {
109 scope: 'variable',
110 variants: [
111 { match: IDENT_RE },
112 { match: /`(?:\\.|[^`\\])+`/ }
113 ],
114 endsParent: true
115 }
116 ]
117 },
118 {
119 scope: 'doctag',
120 match: /@[a-zA-Z]+/
121 },
122 {
123 scope: 'keyword',
124 match: /\\[a-zA-Z]+/
125 }
126 ] }
127 ),
128
129 hljs.HASH_COMMENT_MODE,
130
131 {
132 scope: 'string',
133 contains: [ hljs.BACKSLASH_ESCAPE ],
134 variants: [
135 hljs.END_SAME_AS_BEGIN({
136 begin: /[rR]"(-*)\(/,
137 end: /\)(-*)"/
138 }),
139 hljs.END_SAME_AS_BEGIN({
140 begin: /[rR]"(-*)\{/,
141 end: /\}(-*)"/
142 }),
143 hljs.END_SAME_AS_BEGIN({
144 begin: /[rR]"(-*)\[/,
145 end: /\](-*)"/
146 }),
147 hljs.END_SAME_AS_BEGIN({
148 begin: /[rR]'(-*)\(/,
149 end: /\)(-*)'/
150 }),
151 hljs.END_SAME_AS_BEGIN({
152 begin: /[rR]'(-*)\{/,
153 end: /\}(-*)'/
154 }),
155 hljs.END_SAME_AS_BEGIN({
156 begin: /[rR]'(-*)\[/,
157 end: /\](-*)'/
158 }),
159 {
160 begin: '"',
161 end: '"',
162 relevance: 0
163 },
164 {
165 begin: "'",
166 end: "'",
167 relevance: 0
168 }
169 ],
170 },
171
172 // Matching numbers immediately following punctuation and operators is
173 // tricky since we need to look at the character ahead of a number to
174 // ensure the number is not part of an identifier, and we cannot use
175 // negative look-behind assertions. So instead we explicitly handle all
176 // possible combinations of (operator|punctuation), number.
177 // TODO: replace with negative look-behind when available
178 // { begin: /(?<![a-zA-Z0-9._])0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*[pP][+-]?\d+i?/ },
179 // { begin: /(?<![a-zA-Z0-9._])0[xX][0-9a-fA-F]+([pP][+-]?\d+)?[Li]?/ },
180 // { begin: /(?<![a-zA-Z0-9._])(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?[Li]?/ }
181 {
182 relevance: 0,
183 variants: [
184 {
185 scope: {
186 1: 'operator',
187 2: 'number'
188 },
189 match: [
190 OPERATORS_RE,
191 NUMBER_TYPES_RE
192 ]
193 },
194 {
195 scope: {
196 1: 'operator',
197 2: 'number'
198 },
199 match: [
200 /%[^%]*%/,
201 NUMBER_TYPES_RE
202 ]
203 },
204 {
205 scope: {
206 1: 'punctuation',
207 2: 'number'
208 },
209 match: [
210 PUNCTUATION_RE,
211 NUMBER_TYPES_RE
212 ]
213 },
214 {
215 scope: { 2: 'number' },
216 match: [
217 /[^a-zA-Z0-9._]|^/, // not part of an identifier, or start of document
218 NUMBER_TYPES_RE
219 ]
220 }
221 ]
222 },
223
224 // Operators/punctuation when they're not directly followed by numbers
225 {
226 // Relevance boost for the most common assignment form.
227 scope: { 3: 'operator' },
228 match: [
229 IDENT_RE,
230 /\s+/,
231 /<-/,
232 /\s+/
233 ]
234 },
235
236 {
237 scope: 'operator',
238 relevance: 0,
239 variants: [
240 { match: OPERATORS_RE },
241 { match: /%[^%]*%/ }
242 ]
243 },
244
245 {
246 scope: 'punctuation',
247 relevance: 0,
248 match: PUNCTUATION_RE
249 },
250
251 {
252 // Escaped identifier
253 begin: '`',
254 end: '`',
255 contains: [ { begin: /\\./ } ]
256 }
257 ]
258 };
259 }
260
261 return r;
262
263 })();
264 ;
265 export default hljsGrammar;