stan.js (9773B)
1 /* 2 Language: Stan 3 Description: The Stan probabilistic programming language 4 Author: Jeffrey B. Arnold <jeffrey.arnold@gmail.com> 5 Website: http://mc-stan.org/ 6 Category: scientific 7 */ 8 9 function stan(hljs) { 10 // variable names cannot conflict with block identifiers 11 var BLOCKS = [ 12 'functions', 13 'model', 14 'data', 15 'parameters', 16 'quantities', 17 'transformed', 18 'generated' 19 ]; 20 var STATEMENTS = [ 21 'for', 22 'in', 23 'if', 24 'else', 25 'while', 26 'break', 27 'continue', 28 'return' 29 ]; 30 var SPECIAL_FUNCTIONS = [ 31 'print', 32 'reject', 33 'increment_log_prob|10', 34 'integrate_ode|10', 35 'integrate_ode_rk45|10', 36 'integrate_ode_bdf|10', 37 'algebra_solver' 38 ]; 39 var VAR_TYPES = [ 40 'int', 41 'real', 42 'vector', 43 'ordered', 44 'positive_ordered', 45 'simplex', 46 'unit_vector', 47 'row_vector', 48 'matrix', 49 'cholesky_factor_corr|10', 50 'cholesky_factor_cov|10', 51 'corr_matrix|10', 52 'cov_matrix|10', 53 'void' 54 ]; 55 var FUNCTIONS = [ 56 'Phi', 'Phi_approx', 'abs', 'acos', 'acosh', 'algebra_solver', 'append_array', 57 'append_col', 'append_row', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 58 'bernoulli_cdf', 'bernoulli_lccdf', 'bernoulli_lcdf', 'bernoulli_logit_lpmf', 59 'bernoulli_logit_rng', 'bernoulli_lpmf', 'bernoulli_rng', 'bessel_first_kind', 60 'bessel_second_kind', 'beta_binomial_cdf', 'beta_binomial_lccdf', 61 'beta_binomial_lcdf', 'beta_binomial_lpmf', 'beta_binomial_rng', 'beta_cdf', 62 'beta_lccdf', 'beta_lcdf', 'beta_lpdf', 'beta_rng', 'binary_log_loss', 63 'binomial_cdf', 'binomial_coefficient_log', 'binomial_lccdf', 'binomial_lcdf', 64 'binomial_logit_lpmf', 'binomial_lpmf', 'binomial_rng', 'block', 65 'categorical_logit_lpmf', 'categorical_logit_rng', 'categorical_lpmf', 66 'categorical_rng', 'cauchy_cdf', 'cauchy_lccdf', 'cauchy_lcdf', 'cauchy_lpdf', 67 'cauchy_rng', 'cbrt', 'ceil', 'chi_square_cdf', 'chi_square_lccdf', 68 'chi_square_lcdf', 'chi_square_lpdf', 'chi_square_rng', 'cholesky_decompose', 69 'choose', 'col', 'cols', 'columns_dot_product', 'columns_dot_self', 'cos', 70 'cosh', 'cov_exp_quad', 'crossprod', 'csr_extract_u', 'csr_extract_v', 71 'csr_extract_w', 'csr_matrix_times_vector', 'csr_to_dense_matrix', 72 'cumulative_sum', 'determinant', 'diag_matrix', 'diag_post_multiply', 73 'diag_pre_multiply', 'diagonal', 'digamma', 'dims', 'dirichlet_lpdf', 74 'dirichlet_rng', 'distance', 'dot_product', 'dot_self', 75 'double_exponential_cdf', 'double_exponential_lccdf', 'double_exponential_lcdf', 76 'double_exponential_lpdf', 'double_exponential_rng', 'e', 'eigenvalues_sym', 77 'eigenvectors_sym', 'erf', 'erfc', 'exp', 'exp2', 'exp_mod_normal_cdf', 78 'exp_mod_normal_lccdf', 'exp_mod_normal_lcdf', 'exp_mod_normal_lpdf', 79 'exp_mod_normal_rng', 'expm1', 'exponential_cdf', 'exponential_lccdf', 80 'exponential_lcdf', 'exponential_lpdf', 'exponential_rng', 'fabs', 81 'falling_factorial', 'fdim', 'floor', 'fma', 'fmax', 'fmin', 'fmod', 82 'frechet_cdf', 'frechet_lccdf', 'frechet_lcdf', 'frechet_lpdf', 'frechet_rng', 83 'gamma_cdf', 'gamma_lccdf', 'gamma_lcdf', 'gamma_lpdf', 'gamma_p', 'gamma_q', 84 'gamma_rng', 'gaussian_dlm_obs_lpdf', 'get_lp', 'gumbel_cdf', 'gumbel_lccdf', 85 'gumbel_lcdf', 'gumbel_lpdf', 'gumbel_rng', 'head', 'hypergeometric_lpmf', 86 'hypergeometric_rng', 'hypot', 'inc_beta', 'int_step', 'integrate_ode', 87 'integrate_ode_bdf', 'integrate_ode_rk45', 'inv', 'inv_Phi', 88 'inv_chi_square_cdf', 'inv_chi_square_lccdf', 'inv_chi_square_lcdf', 89 'inv_chi_square_lpdf', 'inv_chi_square_rng', 'inv_cloglog', 'inv_gamma_cdf', 90 'inv_gamma_lccdf', 'inv_gamma_lcdf', 'inv_gamma_lpdf', 'inv_gamma_rng', 91 'inv_logit', 'inv_sqrt', 'inv_square', 'inv_wishart_lpdf', 'inv_wishart_rng', 92 'inverse', 'inverse_spd', 'is_inf', 'is_nan', 'lbeta', 'lchoose', 'lgamma', 93 'lkj_corr_cholesky_lpdf', 'lkj_corr_cholesky_rng', 'lkj_corr_lpdf', 94 'lkj_corr_rng', 'lmgamma', 'lmultiply', 'log', 'log10', 'log1m', 'log1m_exp', 95 'log1m_inv_logit', 'log1p', 'log1p_exp', 'log2', 'log_determinant', 96 'log_diff_exp', 'log_falling_factorial', 'log_inv_logit', 'log_mix', 97 'log_rising_factorial', 'log_softmax', 'log_sum_exp', 'logistic_cdf', 98 'logistic_lccdf', 'logistic_lcdf', 'logistic_lpdf', 'logistic_rng', 'logit', 99 'lognormal_cdf', 'lognormal_lccdf', 'lognormal_lcdf', 'lognormal_lpdf', 100 'lognormal_rng', 'machine_precision', 'matrix_exp', 'max', 'mdivide_left_spd', 101 'mdivide_left_tri_low', 'mdivide_right_spd', 'mdivide_right_tri_low', 'mean', 102 'min', 'modified_bessel_first_kind', 'modified_bessel_second_kind', 103 'multi_gp_cholesky_lpdf', 'multi_gp_lpdf', 'multi_normal_cholesky_lpdf', 104 'multi_normal_cholesky_rng', 'multi_normal_lpdf', 'multi_normal_prec_lpdf', 105 'multi_normal_rng', 'multi_student_t_lpdf', 'multi_student_t_rng', 106 'multinomial_lpmf', 'multinomial_rng', 'multiply_log', 107 'multiply_lower_tri_self_transpose', 'neg_binomial_2_cdf', 108 'neg_binomial_2_lccdf', 'neg_binomial_2_lcdf', 'neg_binomial_2_log_lpmf', 109 'neg_binomial_2_log_rng', 'neg_binomial_2_lpmf', 'neg_binomial_2_rng', 110 'neg_binomial_cdf', 'neg_binomial_lccdf', 'neg_binomial_lcdf', 111 'neg_binomial_lpmf', 'neg_binomial_rng', 'negative_infinity', 'normal_cdf', 112 'normal_lccdf', 'normal_lcdf', 'normal_lpdf', 'normal_rng', 'not_a_number', 113 'num_elements', 'ordered_logistic_lpmf', 'ordered_logistic_rng', 'owens_t', 114 'pareto_cdf', 'pareto_lccdf', 'pareto_lcdf', 'pareto_lpdf', 'pareto_rng', 115 'pareto_type_2_cdf', 'pareto_type_2_lccdf', 'pareto_type_2_lcdf', 116 'pareto_type_2_lpdf', 'pareto_type_2_rng', 'pi', 'poisson_cdf', 'poisson_lccdf', 117 'poisson_lcdf', 'poisson_log_lpmf', 'poisson_log_rng', 'poisson_lpmf', 118 'poisson_rng', 'positive_infinity', 'pow', 'print', 'prod', 'qr_Q', 'qr_R', 119 'quad_form', 'quad_form_diag', 'quad_form_sym', 'rank', 'rayleigh_cdf', 120 'rayleigh_lccdf', 'rayleigh_lcdf', 'rayleigh_lpdf', 'rayleigh_rng', 'reject', 121 'rep_array', 'rep_matrix', 'rep_row_vector', 'rep_vector', 'rising_factorial', 122 'round', 'row', 'rows', 'rows_dot_product', 'rows_dot_self', 123 'scaled_inv_chi_square_cdf', 'scaled_inv_chi_square_lccdf', 124 'scaled_inv_chi_square_lcdf', 'scaled_inv_chi_square_lpdf', 125 'scaled_inv_chi_square_rng', 'sd', 'segment', 'sin', 'singular_values', 'sinh', 126 'size', 'skew_normal_cdf', 'skew_normal_lccdf', 'skew_normal_lcdf', 127 'skew_normal_lpdf', 'skew_normal_rng', 'softmax', 'sort_asc', 'sort_desc', 128 'sort_indices_asc', 'sort_indices_desc', 'sqrt', 'sqrt2', 'square', 129 'squared_distance', 'step', 'student_t_cdf', 'student_t_lccdf', 130 'student_t_lcdf', 'student_t_lpdf', 'student_t_rng', 'sub_col', 'sub_row', 131 'sum', 'tail', 'tan', 'tanh', 'target', 'tcrossprod', 'tgamma', 'to_array_1d', 132 'to_array_2d', 'to_matrix', 'to_row_vector', 'to_vector', 'trace', 133 'trace_gen_quad_form', 'trace_quad_form', 'trigamma', 'trunc', 'uniform_cdf', 134 'uniform_lccdf', 'uniform_lcdf', 'uniform_lpdf', 'uniform_rng', 'variance', 135 'von_mises_lpdf', 'von_mises_rng', 'weibull_cdf', 'weibull_lccdf', 136 'weibull_lcdf', 'weibull_lpdf', 'weibull_rng', 'wiener_lpdf', 'wishart_lpdf', 137 'wishart_rng' 138 ]; 139 var DISTRIBUTIONS = [ 140 'bernoulli', 'bernoulli_logit', 'beta', 'beta_binomial', 'binomial', 141 'binomial_logit', 'categorical', 'categorical_logit', 'cauchy', 'chi_square', 142 'dirichlet', 'double_exponential', 'exp_mod_normal', 'exponential', 'frechet', 143 'gamma', 'gaussian_dlm_obs', 'gumbel', 'hypergeometric', 'inv_chi_square', 144 'inv_gamma', 'inv_wishart', 'lkj_corr', 'lkj_corr_cholesky', 'logistic', 145 'lognormal', 'multi_gp', 'multi_gp_cholesky', 'multi_normal', 146 'multi_normal_cholesky', 'multi_normal_prec', 'multi_student_t', 'multinomial', 147 'neg_binomial', 'neg_binomial_2', 'neg_binomial_2_log', 'normal', 148 'ordered_logistic', 'pareto', 'pareto_type_2', 'poisson', 'poisson_log', 149 'rayleigh', 'scaled_inv_chi_square', 'skew_normal', 'student_t', 'uniform', 150 'von_mises', 'weibull', 'wiener', 'wishart' 151 ]; 152 153 return { 154 name: 'Stan', 155 aliases: ['stanfuncs'], 156 keywords: { 157 $pattern: hljs.IDENT_RE, 158 title: BLOCKS.join(' '), 159 keyword: STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS).join(' '), 160 built_in: FUNCTIONS.join(' ') 161 }, 162 contains: [ 163 hljs.C_LINE_COMMENT_MODE, 164 hljs.COMMENT( 165 /#/, 166 /$/, 167 { 168 relevance: 0, 169 keywords: { 170 'meta-keyword': 'include' 171 } 172 } 173 ), 174 hljs.COMMENT( 175 /\/\*/, 176 /\*\//, 177 { 178 relevance: 0, 179 // highlight doc strings mentioned in Stan reference 180 contains: [ 181 { 182 className: 'doctag', 183 begin: /@(return|param)/ 184 } 185 ] 186 } 187 ), 188 { 189 // hack: in range constraints, lower must follow "<" 190 begin: /<\s*lower\s*=/, 191 keywords: 'lower' 192 }, 193 { 194 // hack: in range constraints, upper must follow either , or < 195 // <lower = ..., upper = ...> or <upper = ...> 196 begin: /[<,]\s*upper\s*=/, 197 keywords: 'upper' 198 }, 199 { 200 className: 'keyword', 201 begin: /\btarget\s*\+=/, 202 relevance: 10 203 }, 204 { 205 begin: '~\\s*(' + hljs.IDENT_RE + ')\\s*\\(', 206 keywords: DISTRIBUTIONS.join(' ') 207 }, 208 { 209 className: 'number', 210 variants: [ 211 { 212 begin: /\b\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/ 213 }, 214 { 215 begin: /\.\d+(?:[eE][+-]?\d+)?\b/ 216 } 217 ], 218 relevance: 0 219 }, 220 { 221 className: 'string', 222 begin: '"', 223 end: '"', 224 relevance: 0 225 } 226 ] 227 } 228 } 229 230 module.exports = stan;