🌐 AI搜索 & 代理 主页
Skip to content

Commit 85db1cf

Browse files
jrnoldmarcoscaceresjoshgoebel
committed
enh(stan) updated with improved coverage of language keywords and patterns. (#1829)
- Almost complete rewrite. Corrected most patterns and updated function definitions. - Add "stanfuncs" as an alias Went ahead and gave @jrnold author credit vs contributor credit for this language since this is really a ground-up rewrite. Co-authored-by: Marcos Cáceres <marcos@marcosc.com> Co-authored-by: Josh Goebel <me@joshgoebel.com>
1 parent bd54565 commit 85db1cf

File tree

4 files changed

+203
-63
lines changed

4 files changed

+203
-63
lines changed

AUTHORS.en.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ Contributors:
271271
- Harmon <Harmon.Public@gmail.com>
272272
- Eric Bailey <eric.w.bailey@gmail.com>
273273
- Gustavo Costa <gusbemacbe@gmail.com>
274+
- Jeffrey Arnold <jeffrey.arnold@gmail.com>
274275
- Antoine Boisier-Michaud <aboisiermichaud@gmail.com>
275276
- Alejandro Isaza <al@isaza.ca>
276277
- Laurent Voullemier <laurent.voullemier@gmail.com>

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Core Changes:
1515
Language Improvements:
1616

1717
- (vbnet) add nameof operator to the keywords (#2329) [Youssef Victor][]
18+
- (stan) updated with improved coverage of language keywords and patterns. (#1829) [Jeffrey Arnold][]
1819

20+
[Jeffrey Arnold]: https://github.com/jrnold
1921
[Josh Goebel]: https://github.com/yyyc514
2022
[Youssef Victor]: https://github.com/Youssef1313
2123

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ The table below shows the full list of supported languages (and corresponding cl
208208
| Smali | smali | |
209209
| Smalltalk | smalltalk, st | |
210210
| Solidity | solidity, sol | [highlightjs-solidity](https://github.com/highlightjs/highlightjs-solidity) |
211-
| Stan | stan | |
211+
| Stan | stan, stanfuncs | |
212212
| Stata | stata | |
213213
| Structured Text | iecst, scl, stl, structured-text | [highlightjs-structured-text](https://github.com/highlightjs/highlightjs-structured-text) |
214214
| Stylus | stylus, styl | |

src/languages/stan.js

Lines changed: 199 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,227 @@
11
/*
22
Language: Stan
33
Description: The Stan probabilistic programming language
4-
Author: Brendan Rocks <rocks.brendan@gmail.com>
4+
Author: Jeffrey B. Arnold <jeffrey.arnold@gmail.com>
55
Website: http://mc-stan.org/
66
Category: scientific
77
*/
88

99
function(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+
10153
return {
154+
aliases: ['stanfuncs'],
155+
keywords: {
156+
'title': BLOCKS.join(' '),
157+
'keyword': STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS).join(' '),
158+
'built_in': FUNCTIONS.join(' ')
159+
},
160+
lexemes: hljs.IDENT_RE,
11161
contains: [
12-
hljs.HASH_COMMENT_MODE,
13162
hljs.C_LINE_COMMENT_MODE,
14-
hljs.C_BLOCK_COMMENT_MODE,
163+
hljs.COMMENT(
164+
/#/,
165+
/$/,
166+
{
167+
relevance: 0,
168+
keywords: {
169+
'meta-keyword': 'include'
170+
}
171+
}
172+
),
173+
hljs.COMMENT(
174+
/\/\*/,
175+
/\*\//,
176+
{
177+
relevance: 0,
178+
// highlight doc strings mentioned in Stan reference
179+
contains: [
180+
{
181+
className: 'doctag',
182+
begin: /@(return|param)/
183+
}
184+
]
185+
}
186+
),
15187
{
16-
begin: hljs.UNDERSCORE_IDENT_RE,
17-
lexemes: hljs.UNDERSCORE_IDENT_RE,
18-
keywords: {
19-
// Stan's keywords
20-
name:
21-
'for in while repeat until if then else',
22-
// Stan's probablity distributions (less beta and gamma, as commonly
23-
// used for parameter names). So far, _log and _rng variants are not
24-
// included
25-
symbol:
26-
'bernoulli bernoulli_logit binomial binomial_logit ' +
27-
'beta_binomial hypergeometric categorical categorical_logit ' +
28-
'ordered_logistic neg_binomial neg_binomial_2 ' +
29-
'neg_binomial_2_log poisson poisson_log multinomial normal ' +
30-
'exp_mod_normal skew_normal student_t cauchy double_exponential ' +
31-
'logistic gumbel lognormal chi_square inv_chi_square ' +
32-
'scaled_inv_chi_square exponential inv_gamma weibull frechet ' +
33-
'rayleigh wiener pareto pareto_type_2 von_mises uniform ' +
34-
'multi_normal multi_normal_prec multi_normal_cholesky multi_gp ' +
35-
'multi_gp_cholesky multi_student_t gaussian_dlm_obs dirichlet ' +
36-
'lkj_corr lkj_corr_cholesky wishart inv_wishart',
37-
// Stan's data types
38-
'selector-tag':
39-
'int real vector simplex unit_vector ordered positive_ordered ' +
40-
'row_vector matrix cholesky_factor_corr cholesky_factor_cov ' +
41-
'corr_matrix cov_matrix',
42-
// Stan's model blocks
43-
title:
44-
'functions model data parameters quantities transformed ' +
45-
'generated',
46-
literal:
47-
'true false'
48-
},
49-
relevance: 0
188+
// hack: in range constraints, lower must follow "<"
189+
begin: /<\s*lower\s*=/,
190+
keywords: 'lower'
50191
},
51-
// The below is all taken from the R language definition
52192
{
53-
// hex value
54-
className: 'number',
55-
begin: "0[xX][0-9a-fA-F]+[Li]?\\b",
56-
relevance: 0
193+
// hack: in range constraints, upper must follow either , or <
194+
// <lower = ..., upper = ...> or <upper = ...>
195+
begin: /[<,]*upper\s*=/,
196+
keywords: 'upper'
57197
},
58198
{
59-
// hex value
60-
className: 'number',
61-
begin: "0[xX][0-9a-fA-F]+[Li]?\\b",
62-
relevance: 0
199+
className: 'keyword',
200+
begin: /\btarget\s*\+=/,
201+
relevance: 10
63202
},
64203
{
65-
// explicit integer
66-
className: 'number',
67-
begin: "\\d+(?:[eE][+\\-]?\\d*)?L\\b",
68-
relevance: 0
69-
},
70-
{
71-
// number with trailing decimal
72-
className: 'number',
73-
begin: "\\d+\\.(?!\\d)(?:i\\b)?",
74-
relevance: 0
204+
begin: '~\\s*(' + hljs.IDENT_RE + ')\\s*\\(',
205+
keywords: DISTRIBUTIONS.join(' ')
75206
},
76207
{
77-
// number
78208
className: 'number',
79-
begin: "\\d+(?:\\.\\d*)?(?:[eE][+\\-]?\\d*)?i?\\b",
209+
variants: [
210+
{
211+
begin: /\b\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/
212+
},
213+
{
214+
begin: /\.\d+(?:[eE][+-]?\d+)?\b/
215+
}
216+
],
80217
relevance: 0
81218
},
82219
{
83-
// number with leading decimal
84-
className: 'number',
85-
begin: "\\.\\d+(?:[eE][+\\-]?\\d*)?i?\\b",
220+
className: 'string',
221+
begin: '"',
222+
end: '"',
86223
relevance: 0
87224
}
88225
]
89-
};
226+
}
90227
}

0 commit comments

Comments
 (0)