]> luflow.net public git repositories - flow-web.git/blob - static/highlight/es/languages/stan.js
Initial commit.
[flow-web.git] / static / highlight / es / languages / stan.js
1 /*! `stan` grammar compiled for Highlight.js 11.11.1 */
2 var hljsGrammar = (function () {
3 'use strict';
4
5 /*
6 Language: Stan
7 Description: The Stan probabilistic programming language
8 Author: Sean Pinkney <sean.pinkney@gmail.com>
9 Website: http://mc-stan.org/
10 Category: scientific
11 */
12
13 function stan(hljs) {
14 const regex = hljs.regex;
15 // variable names cannot conflict with block identifiers
16 const BLOCKS = [
17 'functions',
18 'model',
19 'data',
20 'parameters',
21 'quantities',
22 'transformed',
23 'generated'
24 ];
25
26 const STATEMENTS = [
27 'for',
28 'in',
29 'if',
30 'else',
31 'while',
32 'break',
33 'continue',
34 'return'
35 ];
36
37 const TYPES = [
38 'array',
39 'tuple',
40 'complex',
41 'int',
42 'real',
43 'vector',
44 'complex_vector',
45 'ordered',
46 'positive_ordered',
47 'simplex',
48 'unit_vector',
49 'row_vector',
50 'complex_row_vector',
51 'matrix',
52 'complex_matrix',
53 'cholesky_factor_corr|10',
54 'cholesky_factor_cov|10',
55 'corr_matrix|10',
56 'cov_matrix|10',
57 'void'
58 ];
59
60 // to get the functions list
61 // clone the [stan-docs repo](https://github.com/stan-dev/docs)
62 // then cd into it and run this bash script https://gist.github.com/joshgoebel/dcd33f82d4059a907c986049893843cf
63 //
64 // the output files are
65 // distributions_quoted.txt
66 // functions_quoted.txt
67
68 const FUNCTIONS = [
69 'abs',
70 'acos',
71 'acosh',
72 'add_diag',
73 'algebra_solver',
74 'algebra_solver_newton',
75 'append_array',
76 'append_col',
77 'append_row',
78 'asin',
79 'asinh',
80 'atan',
81 'atan2',
82 'atanh',
83 'bessel_first_kind',
84 'bessel_second_kind',
85 'binary_log_loss',
86 'block',
87 'cbrt',
88 'ceil',
89 'chol2inv',
90 'cholesky_decompose',
91 'choose',
92 'col',
93 'cols',
94 'columns_dot_product',
95 'columns_dot_self',
96 'complex_schur_decompose',
97 'complex_schur_decompose_t',
98 'complex_schur_decompose_u',
99 'conj',
100 'cos',
101 'cosh',
102 'cov_exp_quad',
103 'crossprod',
104 'csr_extract',
105 'csr_extract_u',
106 'csr_extract_v',
107 'csr_extract_w',
108 'csr_matrix_times_vector',
109 'csr_to_dense_matrix',
110 'cumulative_sum',
111 'dae',
112 'dae_tol',
113 'determinant',
114 'diag_matrix',
115 'diagonal',
116 'diag_post_multiply',
117 'diag_pre_multiply',
118 'digamma',
119 'dims',
120 'distance',
121 'dot_product',
122 'dot_self',
123 'eigendecompose',
124 'eigendecompose_sym',
125 'eigenvalues',
126 'eigenvalues_sym',
127 'eigenvectors',
128 'eigenvectors_sym',
129 'erf',
130 'erfc',
131 'exp',
132 'exp2',
133 'expm1',
134 'falling_factorial',
135 'fdim',
136 'fft',
137 'fft2',
138 'floor',
139 'fma',
140 'fmax',
141 'fmin',
142 'fmod',
143 'gamma_p',
144 'gamma_q',
145 'generalized_inverse',
146 'get_imag',
147 'get_real',
148 'head',
149 'hmm_hidden_state_prob',
150 'hmm_marginal',
151 'hypot',
152 'identity_matrix',
153 'inc_beta',
154 'integrate_1d',
155 'integrate_ode',
156 'integrate_ode_adams',
157 'integrate_ode_bdf',
158 'integrate_ode_rk45',
159 'int_step',
160 'inv',
161 'inv_cloglog',
162 'inv_erfc',
163 'inverse',
164 'inverse_spd',
165 'inv_fft',
166 'inv_fft2',
167 'inv_inc_beta',
168 'inv_logit',
169 'inv_Phi',
170 'inv_sqrt',
171 'inv_square',
172 'is_inf',
173 'is_nan',
174 'lambert_w0',
175 'lambert_wm1',
176 'lbeta',
177 'lchoose',
178 'ldexp',
179 'lgamma',
180 'linspaced_array',
181 'linspaced_int_array',
182 'linspaced_row_vector',
183 'linspaced_vector',
184 'lmgamma',
185 'lmultiply',
186 'log',
187 'log1m',
188 'log1m_exp',
189 'log1m_inv_logit',
190 'log1p',
191 'log1p_exp',
192 'log_determinant',
193 'log_diff_exp',
194 'log_falling_factorial',
195 'log_inv_logit',
196 'log_inv_logit_diff',
197 'logit',
198 'log_mix',
199 'log_modified_bessel_first_kind',
200 'log_rising_factorial',
201 'log_softmax',
202 'log_sum_exp',
203 'machine_precision',
204 'map_rect',
205 'matrix_exp',
206 'matrix_exp_multiply',
207 'matrix_power',
208 'max',
209 'mdivide_left_spd',
210 'mdivide_left_tri_low',
211 'mdivide_right_spd',
212 'mdivide_right_tri_low',
213 'mean',
214 'min',
215 'modified_bessel_first_kind',
216 'modified_bessel_second_kind',
217 'multiply_lower_tri_self_transpose',
218 'negative_infinity',
219 'norm',
220 'norm1',
221 'norm2',
222 'not_a_number',
223 'num_elements',
224 'ode_adams',
225 'ode_adams_tol',
226 'ode_adjoint_tol_ctl',
227 'ode_bdf',
228 'ode_bdf_tol',
229 'ode_ckrk',
230 'ode_ckrk_tol',
231 'ode_rk45',
232 'ode_rk45_tol',
233 'one_hot_array',
234 'one_hot_int_array',
235 'one_hot_row_vector',
236 'one_hot_vector',
237 'ones_array',
238 'ones_int_array',
239 'ones_row_vector',
240 'ones_vector',
241 'owens_t',
242 'Phi',
243 'Phi_approx',
244 'polar',
245 'positive_infinity',
246 'pow',
247 'print',
248 'prod',
249 'proj',
250 'qr',
251 'qr_Q',
252 'qr_R',
253 'qr_thin',
254 'qr_thin_Q',
255 'qr_thin_R',
256 'quad_form',
257 'quad_form_diag',
258 'quad_form_sym',
259 'quantile',
260 'rank',
261 'reduce_sum',
262 'reject',
263 'rep_array',
264 'rep_matrix',
265 'rep_row_vector',
266 'rep_vector',
267 'reverse',
268 'rising_factorial',
269 'round',
270 'row',
271 'rows',
272 'rows_dot_product',
273 'rows_dot_self',
274 'scale_matrix_exp_multiply',
275 'sd',
276 'segment',
277 'sin',
278 'singular_values',
279 'sinh',
280 'size',
281 'softmax',
282 'sort_asc',
283 'sort_desc',
284 'sort_indices_asc',
285 'sort_indices_desc',
286 'sqrt',
287 'square',
288 'squared_distance',
289 'step',
290 'sub_col',
291 'sub_row',
292 'sum',
293 'svd',
294 'svd_U',
295 'svd_V',
296 'symmetrize_from_lower_tri',
297 'tail',
298 'tan',
299 'tanh',
300 'target',
301 'tcrossprod',
302 'tgamma',
303 'to_array_1d',
304 'to_array_2d',
305 'to_complex',
306 'to_int',
307 'to_matrix',
308 'to_row_vector',
309 'to_vector',
310 'trace',
311 'trace_gen_quad_form',
312 'trace_quad_form',
313 'trigamma',
314 'trunc',
315 'uniform_simplex',
316 'variance',
317 'zeros_array',
318 'zeros_int_array',
319 'zeros_row_vector'
320 ];
321
322 const DISTRIBUTIONS = [
323 'bernoulli',
324 'bernoulli_logit',
325 'bernoulli_logit_glm',
326 'beta',
327 'beta_binomial',
328 'beta_proportion',
329 'binomial',
330 'binomial_logit',
331 'categorical',
332 'categorical_logit',
333 'categorical_logit_glm',
334 'cauchy',
335 'chi_square',
336 'dirichlet',
337 'discrete_range',
338 'double_exponential',
339 'exp_mod_normal',
340 'exponential',
341 'frechet',
342 'gamma',
343 'gaussian_dlm_obs',
344 'gumbel',
345 'hmm_latent',
346 'hypergeometric',
347 'inv_chi_square',
348 'inv_gamma',
349 'inv_wishart',
350 'inv_wishart_cholesky',
351 'lkj_corr',
352 'lkj_corr_cholesky',
353 'logistic',
354 'loglogistic',
355 'lognormal',
356 'multi_gp',
357 'multi_gp_cholesky',
358 'multinomial',
359 'multinomial_logit',
360 'multi_normal',
361 'multi_normal_cholesky',
362 'multi_normal_prec',
363 'multi_student_cholesky_t',
364 'multi_student_t',
365 'multi_student_t_cholesky',
366 'neg_binomial',
367 'neg_binomial_2',
368 'neg_binomial_2_log',
369 'neg_binomial_2_log_glm',
370 'normal',
371 'normal_id_glm',
372 'ordered_logistic',
373 'ordered_logistic_glm',
374 'ordered_probit',
375 'pareto',
376 'pareto_type_2',
377 'poisson',
378 'poisson_log',
379 'poisson_log_glm',
380 'rayleigh',
381 'scaled_inv_chi_square',
382 'skew_double_exponential',
383 'skew_normal',
384 'std_normal',
385 'std_normal_log',
386 'student_t',
387 'uniform',
388 'von_mises',
389 'weibull',
390 'wiener',
391 'wishart',
392 'wishart_cholesky'
393 ];
394
395 const BLOCK_COMMENT = hljs.COMMENT(
396 /\/\*/,
397 /\*\//,
398 {
399 relevance: 0,
400 contains: [
401 {
402 scope: 'doctag',
403 match: /@(return|param)/
404 }
405 ]
406 }
407 );
408
409 const INCLUDE = {
410 scope: 'meta',
411 begin: /#include\b/,
412 end: /$/,
413 contains: [
414 {
415 match: /[a-z][a-z-._]+/,
416 scope: 'string'
417 },
418 hljs.C_LINE_COMMENT_MODE
419 ]
420 };
421
422 const RANGE_CONSTRAINTS = [
423 "lower",
424 "upper",
425 "offset",
426 "multiplier"
427 ];
428
429 return {
430 name: 'Stan',
431 aliases: [ 'stanfuncs' ],
432 keywords: {
433 $pattern: hljs.IDENT_RE,
434 title: BLOCKS,
435 type: TYPES,
436 keyword: STATEMENTS,
437 built_in: FUNCTIONS
438 },
439 contains: [
440 hljs.C_LINE_COMMENT_MODE,
441 INCLUDE,
442 hljs.HASH_COMMENT_MODE,
443 BLOCK_COMMENT,
444 {
445 scope: 'built_in',
446 match: /\s(pi|e|sqrt2|log2|log10)(?=\()/,
447 relevance: 0
448 },
449 {
450 match: regex.concat(/[<,]\s*/, regex.either(...RANGE_CONSTRAINTS), /\s*=/),
451 keywords: RANGE_CONSTRAINTS
452 },
453 {
454 scope: 'keyword',
455 match: /\btarget(?=\s*\+=)/,
456 },
457 {
458 // highlights the 'T' in T[,] for only Stan language distributrions
459 match: [
460 /~\s*/,
461 regex.either(...DISTRIBUTIONS),
462 /(?:\(\))/,
463 /\s*T(?=\s*\[)/
464 ],
465 scope: {
466 2: "built_in",
467 4: "keyword"
468 }
469 },
470 {
471 // highlights distributions that end with special endings
472 scope: 'built_in',
473 keywords: DISTRIBUTIONS,
474 begin: regex.concat(/\w*/, regex.either(...DISTRIBUTIONS), /(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/)
475 },
476 {
477 // highlights distributions after ~
478 begin: [
479 /~/,
480 /\s*/,
481 regex.concat(regex.either(...DISTRIBUTIONS), /(?=\s*[\(.*\)])/)
482 ],
483 scope: { 3: "built_in" }
484 },
485 {
486 // highlights user defined distributions after ~
487 begin: [
488 /~/,
489 /\s*\w+(?=\s*[\(.*\)])/,
490 '(?!.*/\b(' + regex.either(...DISTRIBUTIONS) + ')\b)'
491 ],
492 scope: { 2: "title.function" }
493 },
494 {
495 // highlights user defined distributions with special endings
496 scope: 'title.function',
497 begin: /\w*(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/
498 },
499 {
500 scope: 'number',
501 match: regex.concat(
502 // Comes from @RunDevelopment accessed 11/29/2021 at
503 // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56
504
505 // start of big noncapture group which
506 // 1. gets numbers that are by themselves
507 // 2. numbers that are separated by _
508 // 3. numbers that are separted by .
509 /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)/,
510 // grabs scientific notation
511 // grabs complex numbers with i
512 /(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/
513 ),
514 relevance: 0
515 },
516 {
517 scope: 'string',
518 begin: /"/,
519 end: /"/
520 }
521 ]
522 };
523 }
524
525 return stan;
526
527 })();
528 ;
529 export default hljsGrammar;