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