stan.js 9.3 KB

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