stan.js 10 KB

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