stan.js 9.8 KB

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