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