From fadede5bbb0416e8a96b0d79c104dd0b90734706 Mon Sep 17 00:00:00 2001 From: spinkney Date: Mon, 29 Nov 2021 17:46:24 -0500 Subject: [PATCH 01/31] update stan --- src/languages/stan.js | 663 +++++++++++++---------------------- test/detect/stan/default.txt | 63 ++-- 2 files changed, 279 insertions(+), 447 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index 3bd657282b..6105735229 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -30,13 +30,31 @@ export default function(hljs) { const SPECIAL_FUNCTIONS = [ 'print', 'reject', - 'increment_log_prob|10', - 'integrate_ode|10', - 'integrate_ode_rk45|10', - 'integrate_ode_bdf|10', - 'algebra_solver' + 'hmm_marginal', + 'hmm_latent_rng', + 'hmm_hidden_state_prob', + 'algebra_solver', + 'algebra_solver_newton', + 'map_rect', + 'reduce_sum', + 'reduce_sum_static', + 'ode_rk45', + 'ode_rk45_tol', + 'ode_ckrk', + 'ode_ckrk_tol', + 'ode_adams', + 'ode_adams_tol', + 'ode_bdf', + 'ode_bdf_tol', + 'ode_adjoint_tol_ctl', + 'integrate_1d', + 'integrate_ode_rk45', + 'integrate_ode', + 'integrate_ode_adams', + 'integrate_ode_bdf' ]; const VAR_TYPES = [ + 'complex', 'int', 'real', 'vector', @@ -53,419 +71,219 @@ export default function(hljs) { 'void' ]; const FUNCTIONS = [ - 'Phi', - 'Phi_approx', - 'abs', - 'acos', - 'acosh', - 'algebra_solver', - 'append_array', - 'append_col', - 'append_row', - 'asin', - 'asinh', - 'atan', - 'atan2', - 'atanh', - 'bernoulli_cdf', - 'bernoulli_lccdf', - 'bernoulli_lcdf', - 'bernoulli_logit_lpmf', - 'bernoulli_logit_rng', - 'bernoulli_lpmf', - 'bernoulli_rng', - 'bessel_first_kind', - 'bessel_second_kind', - 'beta_binomial_cdf', - 'beta_binomial_lccdf', - 'beta_binomial_lcdf', - 'beta_binomial_lpmf', - 'beta_binomial_rng', - 'beta_cdf', - 'beta_lccdf', - 'beta_lcdf', - 'beta_lpdf', - 'beta_rng', - 'binary_log_loss', - 'binomial_cdf', - 'binomial_coefficient_log', - 'binomial_lccdf', - 'binomial_lcdf', - 'binomial_logit_lpmf', - 'binomial_lpmf', - 'binomial_rng', - 'block', - 'categorical_logit_lpmf', - 'categorical_logit_rng', - 'categorical_lpmf', - 'categorical_rng', - 'cauchy_cdf', - 'cauchy_lccdf', - 'cauchy_lcdf', - 'cauchy_lpdf', - 'cauchy_rng', - 'cbrt', - 'ceil', - 'chi_square_cdf', - 'chi_square_lccdf', - 'chi_square_lcdf', - 'chi_square_lpdf', - 'chi_square_rng', - 'cholesky_decompose', - 'choose', - 'col', - 'cols', - 'columns_dot_product', - 'columns_dot_self', - 'cos', - 'cosh', - 'cov_exp_quad', - 'crossprod', - 'csr_extract_u', - 'csr_extract_v', - 'csr_extract_w', - 'csr_matrix_times_vector', - 'csr_to_dense_matrix', - 'cumulative_sum', - 'determinant', - 'diag_matrix', - 'diag_post_multiply', - 'diag_pre_multiply', - 'diagonal', - 'digamma', - 'dims', - 'dirichlet_lpdf', - 'dirichlet_rng', - 'distance', - 'dot_product', - 'dot_self', - 'double_exponential_cdf', - 'double_exponential_lccdf', - 'double_exponential_lcdf', - 'double_exponential_lpdf', - 'double_exponential_rng', - 'e', - 'eigenvalues_sym', - 'eigenvectors_sym', - 'erf', - 'erfc', - 'exp', - 'exp2', - 'exp_mod_normal_cdf', - 'exp_mod_normal_lccdf', - 'exp_mod_normal_lcdf', - 'exp_mod_normal_lpdf', - 'exp_mod_normal_rng', - 'expm1', - 'exponential_cdf', - 'exponential_lccdf', - 'exponential_lcdf', - 'exponential_lpdf', - 'exponential_rng', - 'fabs', - 'falling_factorial', - 'fdim', - 'floor', - 'fma', - 'fmax', - 'fmin', - 'fmod', - 'frechet_cdf', - 'frechet_lccdf', - 'frechet_lcdf', - 'frechet_lpdf', - 'frechet_rng', - 'gamma_cdf', - 'gamma_lccdf', - 'gamma_lcdf', - 'gamma_lpdf', - 'gamma_p', - 'gamma_q', - 'gamma_rng', - 'gaussian_dlm_obs_lpdf', - 'get_lp', - 'gumbel_cdf', - 'gumbel_lccdf', - 'gumbel_lcdf', - 'gumbel_lpdf', - 'gumbel_rng', - 'head', - 'hypergeometric_lpmf', - 'hypergeometric_rng', - 'hypot', - 'inc_beta', - 'int_step', - 'integrate_ode', - 'integrate_ode_bdf', - 'integrate_ode_rk45', - 'inv', - 'inv_Phi', - 'inv_chi_square_cdf', - 'inv_chi_square_lccdf', - 'inv_chi_square_lcdf', - 'inv_chi_square_lpdf', - 'inv_chi_square_rng', - 'inv_cloglog', - 'inv_gamma_cdf', - 'inv_gamma_lccdf', - 'inv_gamma_lcdf', - 'inv_gamma_lpdf', - 'inv_gamma_rng', - 'inv_logit', - 'inv_sqrt', - 'inv_square', - 'inv_wishart_lpdf', - 'inv_wishart_rng', - 'inverse', - 'inverse_spd', - 'is_inf', - 'is_nan', - 'lbeta', - 'lchoose', - 'lgamma', - 'lkj_corr_cholesky_lpdf', - 'lkj_corr_cholesky_rng', - 'lkj_corr_lpdf', - 'lkj_corr_rng', - 'lmgamma', - 'lmultiply', - 'log', - 'log10', - 'log1m', - 'log1m_exp', - 'log1m_inv_logit', - 'log1p', - 'log1p_exp', - 'log2', - 'log_determinant', - 'log_diff_exp', - 'log_falling_factorial', - 'log_inv_logit', - 'log_mix', - 'log_rising_factorial', - 'log_softmax', - 'log_sum_exp', - 'logistic_cdf', - 'logistic_lccdf', - 'logistic_lcdf', - 'logistic_lpdf', - 'logistic_rng', - 'logit', - 'lognormal_cdf', - 'lognormal_lccdf', - 'lognormal_lcdf', - 'lognormal_lpdf', - 'lognormal_rng', - 'machine_precision', - 'matrix_exp', - 'max', - 'mdivide_left_spd', - 'mdivide_left_tri_low', - 'mdivide_right_spd', - 'mdivide_right_tri_low', - 'mean', - 'min', - 'modified_bessel_first_kind', - 'modified_bessel_second_kind', - 'multi_gp_cholesky_lpdf', - 'multi_gp_lpdf', - 'multi_normal_cholesky_lpdf', - 'multi_normal_cholesky_rng', - 'multi_normal_lpdf', - 'multi_normal_prec_lpdf', - 'multi_normal_rng', - 'multi_student_t_lpdf', - 'multi_student_t_rng', - 'multinomial_lpmf', - 'multinomial_rng', - 'multiply_log', - 'multiply_lower_tri_self_transpose', - 'neg_binomial_2_cdf', - 'neg_binomial_2_lccdf', - 'neg_binomial_2_lcdf', - 'neg_binomial_2_log_lpmf', - 'neg_binomial_2_log_rng', - 'neg_binomial_2_lpmf', - 'neg_binomial_2_rng', - 'neg_binomial_cdf', - 'neg_binomial_lccdf', - 'neg_binomial_lcdf', - 'neg_binomial_lpmf', - 'neg_binomial_rng', - 'negative_infinity', - 'normal_cdf', - 'normal_lccdf', - 'normal_lcdf', - 'normal_lpdf', - 'normal_rng', - 'not_a_number', - 'num_elements', - 'ordered_logistic_lpmf', - 'ordered_logistic_rng', + // Integer-Valued Basic Functions + + // Absolute functions + 'abs', 'int_step', + + // Bound functions + 'min', 'max', + + // Size functions + 'size', + + // Real-Valued Basic Functions + + // Log probability function + 'target', 'get_lp', + + // Logical functions + 'step', 'is_inf', 'is_nan', + + // Step-like functions + 'fabs', 'fdim', 'fmin', 'fmax', 'fmod', 'floor', 'ceil', 'round', + 'trunc', + + // Power and logarithm functions + 'sqrt', 'cbrt', 'square', 'exp', 'exp2', 'log', 'log2', 'log10', + 'pow', 'inv', 'inv_sqrt', 'inv_square', + + // Trigonometric functions + 'hypot', 'cos', 'sin', 'tan', 'acos', 'asin', 'atan', 'atan2', + + // Hyperbolic trigonometric functions + 'cosh', 'sinh', 'tanh', 'acosh', 'asinh', 'atanh', + + // Link functions + 'logit', 'inv_logit', 'inv_cloglog', + + // Probability-related functions + 'erf', 'erfc', 'Phi', 'inv_Phi', 'Phi_approx', 'binary_log_loss', 'owens_t', - 'pareto_cdf', - 'pareto_lccdf', - 'pareto_lcdf', - 'pareto_lpdf', - 'pareto_rng', - 'pareto_type_2_cdf', - 'pareto_type_2_lccdf', - 'pareto_type_2_lcdf', - 'pareto_type_2_lpdf', - 'pareto_type_2_rng', - 'pi', - 'poisson_cdf', - 'poisson_lccdf', - 'poisson_lcdf', - 'poisson_log_lpmf', - 'poisson_log_rng', - 'poisson_lpmf', - 'poisson_rng', - 'positive_infinity', - 'pow', - 'print', - 'prod', - 'qr_Q', - 'qr_R', - 'quad_form', - 'quad_form_diag', - 'quad_form_sym', - 'rank', - 'rayleigh_cdf', - 'rayleigh_lccdf', - 'rayleigh_lcdf', - 'rayleigh_lpdf', - 'rayleigh_rng', - 'reject', + + // Combinatorial functions + 'beta', 'inc_beta', 'lbeta', 'tgamma', 'lgamma', 'digamma', + 'trigamma', 'lmgamma', 'gamma_p', 'gamma_q', + 'binomial_coefficient_log', 'choose', 'bessel_first_kind', + 'bessel_second_kind', 'modified_bessel_first_kind', + 'log_modified_bessel_first_kind', 'modified_bessel_second_kind', + 'falling_factorial', 'lchoose', 'log_falling_factorial', + 'rising_factorial', 'log_rising_factorial', + + // Composed functions + 'expm1', 'fma', 'multiply_log', 'ldexp', 'lmultiply', 'log1p', + 'log1m', 'log1p_exp', 'log1m_exp', 'log_diff_exp', 'log_mix', + 'log_sum_exp', 'log_inv_logit', 'log_inv_logit_diff', + 'log1m_inv_logit', + + // Special functions + 'lambert_w0', 'lambert_wm1', + + // Complex Conversion Functions + 'get_real', 'get_imag', + + // Complex-Valued Basic Functions + + // Complex Construction Functions + 'to_complex', + + // Array Operations + + // Reductions + 'sum', 'prod', 'log_sum_exp', 'mean', 'variance', 'sd', 'distance', + 'squared_distance', 'quantile', + + // Array size and dimension function + 'dims', 'num_elements', + + // Array broadcasting 'rep_array', - 'rep_matrix', - 'rep_row_vector', - 'rep_vector', - 'rising_factorial', - 'round', - 'row', - 'rows', - 'rows_dot_product', - 'rows_dot_self', - 'scaled_inv_chi_square_cdf', - 'scaled_inv_chi_square_lccdf', - 'scaled_inv_chi_square_lcdf', - 'scaled_inv_chi_square_lpdf', - 'scaled_inv_chi_square_rng', - 'sd', + + // Array concatenation + 'append_array', + + // Sorting functions + 'sort_asc', 'sort_desc', 'sort_indices_asc', 'sort_indices_desc', + 'rank', + + // Reversing functions + 'reverse', + + // Matrix Operations + + // Integer-valued matrix size functions + 'num_elements', 'rows', 'cols', + + // Dot products and specialized products + 'dot_product', 'columns_dot_product', 'rows_dot_product', 'dot_self', + 'columns_dot_self', 'rows_dot_self', 'tcrossprod', 'crossprod', + 'quad_form', 'quad_form_diag', 'quad_form_sym', 'trace_quad_form', + 'trace_gen_quad_form', 'multply_lower_tri_self_transpose', + 'diag_pre_multiply', 'diag_post_multiply', + + // Broadcast functions + 'rep_vector', 'rep_row_vector', 'rep_matrix', + 'symmetrize_from_lower_tri', + + // Diagonal matrix functions + 'add_diag', 'diagonal', 'diag_matrix', 'identity_matrix', + + // Container construction functions + 'linspaced_array', 'linspaced_int_array', 'linspaced_vector', + 'linspaced_row_vector', 'one_hot_int_array', 'one_hot_array', + 'one_hot_vector', 'one_hot_row_vector', 'ones_int_array', + 'ones_array', 'ones_vector', 'ones_row_vector', 'zeros_int_array', + 'zeros_array', 'zeros_vector', 'zeros_row_vector', 'uniform_simplex', + + // Slicing and blocking functions + 'col', 'row', 'block', 'sub_col', 'sub_row', 'head', 'tail', 'segment', - 'sin', - 'singular_values', - 'sinh', - 'size', - 'skew_normal_cdf', - 'skew_normal_lccdf', - 'skew_normal_lcdf', - 'skew_normal_lpdf', - 'skew_normal_rng', - 'softmax', - 'sort_asc', - 'sort_desc', - 'sort_indices_asc', - 'sort_indices_desc', - 'sqrt', - 'sqrt2', - 'square', - 'squared_distance', - 'step', - 'student_t_cdf', - 'student_t_lccdf', - 'student_t_lcdf', - 'student_t_lpdf', - 'student_t_rng', - 'sub_col', - 'sub_row', - 'sum', - 'tail', - 'tan', - 'tanh', - 'target', - 'tcrossprod', - 'tgamma', + + // Matrix concatenation + 'append_col', 'append_row', + + // Special matrix functions + 'softmax', 'log_softmax', 'cumulative_sum', + + // Covariance functions + 'cov_exp_quad', + + // Linear algebra functions and solvers + 'mdivide_left_tri_low', 'mdivide_right_tri_low', 'mdivide_left_spd', + 'mdivide_right_spd', 'matrix_exp', 'matrix_exp_multiply', + 'scale_matrix_exp_multiply', 'matrix_power', 'trace', 'determinant', + 'log_determinant', 'inverse', 'inverse_spd', 'chol2inv', + 'generalized_inverse', 'eigenvalues_sym', 'eigenvectors_sym', + 'qr_thin_Q', 'qr_thin_R', 'qr_Q', 'qr_R', 'cholseky_decompose', + 'singular_values', 'svd_U', 'svd_V', + + // Sparse Matrix Operations + + // Conversion functions + 'csr_extract_w', 'csr_extract_v', 'csr_extract_u', + 'csr_to_dense_matrix', + + // Sparse matrix arithmetic + 'csr_matrix_times_vector', + + // Mixed Operations + 'to_matrix', 'to_vector', 'to_row_vector', 'to_array_2d', 'to_array_1d', - 'to_array_2d', - 'to_matrix', - 'to_row_vector', - 'to_vector', - 'trace', - 'trace_gen_quad_form', - 'trace_quad_form', - 'trigamma', - 'trunc', - 'uniform_cdf', - 'uniform_lccdf', - 'uniform_lcdf', - 'uniform_lpdf', - 'uniform_rng', - 'variance', - 'von_mises_lpdf', - 'von_mises_rng', - 'weibull_cdf', - 'weibull_lccdf', - 'weibull_lcdf', - 'weibull_lpdf', - 'weibull_rng', - 'wiener_lpdf', - 'wishart_lpdf', - 'wishart_rng' + + // Mathematical constants + 'pi', 'e', 'sqrt2', 'log2', 'log10', + + // Special values + 'not_a_number', 'positive_infinity', 'negative_infinity', + 'machine_precision' + ]; const DISTRIBUTIONS = [ - 'bernoulli', - 'bernoulli_logit', - 'beta', - 'beta_binomial', - 'binomial', - 'binomial_logit', - 'categorical', - 'categorical_logit', - 'cauchy', - 'chi_square', - 'dirichlet', - 'double_exponential', - 'exp_mod_normal', - 'exponential', - 'frechet', - 'gamma', - 'gaussian_dlm_obs', - 'gumbel', - 'hypergeometric', - 'inv_chi_square', - 'inv_gamma', - 'inv_wishart', - 'lkj_corr', - 'lkj_corr_cholesky', - 'logistic', - 'lognormal', - 'multi_gp', - 'multi_gp_cholesky', - 'multi_normal', - 'multi_normal_cholesky', - 'multi_normal_prec', - 'multi_student_t', - 'multinomial', - 'neg_binomial', - 'neg_binomial_2', - 'neg_binomial_2_log', - 'normal', - 'ordered_logistic', - 'pareto', - 'pareto_type_2', - 'poisson', - 'poisson_log', - 'rayleigh', - 'scaled_inv_chi_square', - 'skew_normal', - 'student_t', - 'uniform', + // Discrete Distributions + + // Binary Distributions + 'bernoulli', 'bernoulli_logit', 'bernoulli_logit_glm', + + // Bounded Discrete Distributions + 'binomial', 'binomial_logit', 'beta_binomial', 'hypergeometric', + 'categorical', 'categorical_logit_glm', 'discrete_range', + 'ordered_logistic', 'ordered_logistic_glm', 'ordered_probit', + + // Unbounded Discrete Distributions + 'neg_binomial', 'neg_binomial_2', 'neg_binomial_2_log', + 'neg_binomial_2_log_glm', 'poisson', 'poisson_log', + 'poisson_log_glm', + + // Multivariate Discrete Distributions + 'multinomial', 'multinomial_logit', + + // Continuous Distributions + + // Unbounded Continuous Distributions + 'normal', 'std_normal', 'normal_id_glm', 'exp_mod_normal', + 'skew_normal', 'student_t', 'cauchy', 'double_exponential', + 'logistic', 'gumbel', 'skew_double_exponential', + + // Positive Continuous Distributions + 'lognormal', 'chi_square', 'inv_chi_square', + 'scaled_inv_chi_square', 'exponential', 'gamma', 'inv_gamma', + 'weibull', 'frechet', 'rayleigh', + + // Positive Lower-Bounded Distributions + 'pareto', 'pareto_type_2', 'wiener', + + // Continuous Distributions on [0, 1] + 'beta', 'beta_proportion', + + // Circular Distributions 'von_mises', - 'weibull', - 'wiener', - 'wishart' + + // Bounded Continuous Distributions + 'uniform', + + // Distributions over Unbounded Vectors + 'multi_normal', 'multi_normal_prec', 'multi_normal_cholesky', + 'multi_gp', 'multi_gp_cholesky', 'multi_student_t', + 'gaussian_dlm_obs', + + // Simplex Distributions + 'dirichlet', + + // Correlation Matrix Distributions + 'lkj_corr', 'lkj_corr_cholesky', + + // Covariance Matrix Distributions + 'wishart', 'inv_wishart' ]; const BLOCK_COMMENT = hljs.COMMENT( @@ -487,11 +305,11 @@ export default function(hljs) { begin: /^#include\b/, end: /$/, relevance: 0, // relevance comes from keywords - keywords: "include", + keywords: 'include', contains: [ { match: /[a-z][a-z-.]+/, - className: "string" + className: 'string' }, hljs.C_LINE_COMMENT_MODE ] @@ -512,8 +330,9 @@ export default function(hljs) { hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, { - // hack: in range constraints, lower must follow "<" - begin: /<\s*lower\s*=/, + // hack: in range constraints, lower must follow either , or < + // or + begin: /[<,]\s*lower\s*=/, keywords: 'lower' }, { @@ -522,6 +341,18 @@ export default function(hljs) { begin: /[<,]\s*upper\s*=/, keywords: 'upper' }, + { + // hack: in range constraints, upper must follow either , or < + // or + begin: /[<,]\s*multiplier\s*=/, + keywords: 'offset' + }, + { + // hack: in range constraints, upper must follow either , or < + // or + begin: /[<,]\s*offset\s*=/, + keywords: 'multiplier' + }, { className: 'keyword', begin: /\btarget\s*\+=/ @@ -534,7 +365,7 @@ export default function(hljs) { className: 'number', variants: [ { - begin: /\b\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/ + begin: /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:E[+-]?\d+(?:_\d+)*)?i?(?!\w)/i }, { begin: /\.\d+(?:[eE][+-]?\d+)?\b/ diff --git a/test/detect/stan/default.txt b/test/detect/stan/default.txt index 99e56c45ec..c2fd30d03c 100644 --- a/test/detect/stan/default.txt +++ b/test/detect/stan/default.txt @@ -1,39 +1,40 @@ -// Multivariate Regression Example -// Taken from stan-reference-2.8.0.pdf p.66 - +functions { + // #include normal_copula.stanfunctions +} data { - int N; // num individuals - int K; // num ind predictors - int J; // num groups - int L; // num group predictors - int jj[N]; // group for individual - matrix[N,K] x; // individual predictors - row_vector[L] u[J]; // group predictors - vector[N] y; // outcomes + int N; + int K; + matrix[N, K] x; } +transformed data { + complex zi = 1+3.14i; + zi = zi * 0i; + complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); + real x = get_real(3i - 40e-3i + 1e10i); +}› parameters { - corr_matrix[K] Omega; // prior correlation - vector[K] tau; // prior scale - matrix[L,K] gamma; // group coeffs - vector[K] beta[J]; // indiv coeffs by group - real sigma; // prediction error scale + array[K - 1] real mu; + array[K + 1] real sigma; + cholesky_factor_corr[K] L; + real delta; } model { - tau ~ cauchy(0,2.5); - Omega ~ lkj_corr(2); - to_vector(gamma) ~ normal(0, 5); - { - row_vector[K] u_gamma[J]; - for (j in 1:J) - u_gamma[j] <- u[j] * gamma; - beta ~ multi_normal(u_gamma, quad_form_diag(Omega, tau)); - } + target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]); + target += gumbel_lpdf(x[ : , 2] | mu[2], sigma[2]); + target += lognormal_lpdf(x[ : , 3] | mu[3], sigma[3]); + target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]); + { - vector[N] x_beta_jj; - for (n in 1:N) - x_beta_jj[n] <- x[n] * beta[jj[n]]; - y ~ normal(x_beta_jj, sigma); + matrix[K, N] y; + for (n in 1 : N) { + y[1, n] = inv_Phi(normal_cdf(x[n, 1] | mu[1], sigma[1])); + y[2, n] = inv_Phi(gumbel_cdf(x[n, 2] | mu[2], sigma[2])); + y[3, n] = inv_Phi(lognormal_cdf(x[n, 3] | mu[3], sigma[3])); + y[4, n] = inv_Phi(weibull_cdf(x[n, 4] | sigma[4], sigma[5])); + } + // y ~ multi_normal_copula(L); } } - -# Note: Octothorpes indicate comments, too! +generated quantities { + matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); +} From 83289eb8b154051a6b721037e90949ed3a2040a5 Mon Sep 17 00:00:00 2001 From: spinkney Date: Mon, 29 Nov 2021 17:48:43 -0500 Subject: [PATCH 02/31] add array --- src/languages/stan.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/languages/stan.js b/src/languages/stan.js index 6105735229..d4703c51ba 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -54,6 +54,7 @@ export default function(hljs) { 'integrate_ode_bdf' ]; const VAR_TYPES = [ + `array`, 'complex', 'int', 'real', From 5c66f61bdaa5b04e3ff17c582423f7aab2b3926d Mon Sep 17 00:00:00 2001 From: spinkney Date: Mon, 29 Nov 2021 23:15:29 -0500 Subject: [PATCH 03/31] update with keyword function for distributions --- src/languages/stan.js | 72 ++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index d4703c51ba..03f8f49702 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -17,16 +17,16 @@ export default function(hljs) { 'transformed', 'generated' ]; - const STATEMENTS = [ - 'for', - 'in', - 'if', - 'else', - 'while', - 'break', - 'continue', - 'return' - ]; + // const STATEMENTS = [ + // 'for', + // 'in', + // 'if', + // 'else', + // 'while', + // 'break', + // 'continue', + // 'return' + // ]; const SPECIAL_FUNCTIONS = [ 'print', 'reject', @@ -169,7 +169,7 @@ export default function(hljs) { 'dot_product', 'columns_dot_product', 'rows_dot_product', 'dot_self', 'columns_dot_self', 'rows_dot_self', 'tcrossprod', 'crossprod', 'quad_form', 'quad_form_diag', 'quad_form_sym', 'trace_quad_form', - 'trace_gen_quad_form', 'multply_lower_tri_self_transpose', + 'trace_gen_quad_form', 'multiply_lower_tri_self_transpose', 'diag_pre_multiply', 'diag_post_multiply', // Broadcast functions @@ -202,7 +202,7 @@ export default function(hljs) { // Linear algebra functions and solvers 'mdivide_left_tri_low', 'mdivide_right_tri_low', 'mdivide_left_spd', 'mdivide_right_spd', 'matrix_exp', 'matrix_exp_multiply', - 'scale_matrix_exp_multiply', 'matrix_power', 'trace', 'determinant', + 'scale_matrix_exp_multiply', 'matrix_power','trace', 'determinant', 'log_determinant', 'inverse', 'inverse_spd', 'chol2inv', 'generalized_inverse', 'eigenvalues_sym', 'eigenvectors_sym', 'qr_thin_Q', 'qr_thin_R', 'qr_Q', 'qr_R', 'cholseky_decompose', @@ -221,9 +221,6 @@ export default function(hljs) { 'to_matrix', 'to_vector', 'to_row_vector', 'to_array_2d', 'to_array_1d', - // Mathematical constants - 'pi', 'e', 'sqrt2', 'log2', 'log10', - // Special values 'not_a_number', 'positive_infinity', 'negative_infinity', 'machine_precision' @@ -287,6 +284,27 @@ export default function(hljs) { 'wishart', 'inv_wishart' ]; + const expanded = DISTRIBUTIONS.flatMap(name => [ + `${name}_lpdf`, + `${name}_lpmf`, + `${name}_lccdf`, + `${name}_lupdf`, + `${name}_lcdf`, + `${name}_cdf`, + `${name}_lupdf`, + `${name}_qf` + ] + ) + + // Mathematical constants + const LITERALS = [ + 'pi', + 'e', + 'sqrt2', + 'log2', + 'log10' + ]; + const BLOCK_COMMENT = hljs.COMMENT( /\/\*/, /\*\//, @@ -303,10 +321,10 @@ export default function(hljs) { const INCLUDE = { className: 'meta', - begin: /^#include\b/, + begin: /#\s*[a-z]+\b/, end: /$/, relevance: 0, // relevance comes from keywords - keywords: 'include', + keywords: '#include', contains: [ { match: /[a-z][a-z-.]+/, @@ -322,14 +340,22 @@ export default function(hljs) { keywords: { $pattern: hljs.IDENT_RE, title: BLOCKS, - keyword: STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS), - built_in: FUNCTIONS + keyword: VAR_TYPES.concat(SPECIAL_FUNCTIONS), + built_in: FUNCTIONS.concat(DISTRIBUTIONS).concat(expanded).concat(LITERALS) }, contains: [ hljs.C_LINE_COMMENT_MODE, INCLUDE, hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, + { + // Distinct highlighting for: [for, in, if, else, while, + // break, continue, return] + className: 'flow', + begin: /\s(for|in|if|else|while|break|continue|return)\s/, + //keywords: 'for in if else while break continue return', + relevance: 0 + }, { // hack: in range constraints, lower must follow either , or < // or @@ -356,17 +382,13 @@ export default function(hljs) { }, { className: 'keyword', - begin: /\btarget\s*\+=/ - }, - { - begin: '~\\s*(' + hljs.IDENT_RE + ')\\s*\\(', - keywords: DISTRIBUTIONS + begin: /\btarget\s*/, }, { className: 'number', variants: [ { - begin: /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:E[+-]?\d+(?:_\d+)*)?i?(?!\w)/i + begin: /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/i }, { begin: /\.\d+(?:[eE][+-]?\d+)?\b/ From ded95c3f29d06ce3fb9d6c52d16e3d0f8016febc Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 30 Nov 2021 11:39:07 -0500 Subject: [PATCH 04/31] update stan --- src/languages/stan.js | 77 +++++++++++++++++++++++++------------------ tsconfig.json | 2 +- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index 03f8f49702..5ded629de0 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -17,16 +17,18 @@ export default function(hljs) { 'transformed', 'generated' ]; - // const STATEMENTS = [ - // 'for', - // 'in', - // 'if', - // 'else', - // 'while', - // 'break', - // 'continue', - // 'return' - // ]; + + const STATEMENTS = [ + 'for', + 'in', + 'if', + 'else', + 'while', + 'break', + 'continue', + 'return' + ]; + const SPECIAL_FUNCTIONS = [ 'print', 'reject', @@ -60,7 +62,7 @@ export default function(hljs) { 'real', 'vector', 'ordered', - 'positive_ordered', + 'positive_ordered|10', 'simplex', 'unit_vector', 'row_vector', @@ -295,15 +297,17 @@ export default function(hljs) { `${name}_qf` ] ) - - // Mathematical constants - const LITERALS = [ - 'pi', - 'e', - 'sqrt2', - 'log2', - 'log10' - ]; + const DELIMIT = [ + '{', + '}', + '(', + ')', + '[', + ']', + ',', + ':', + '|' + ] const BLOCK_COMMENT = hljs.COMMENT( /\/\*/, @@ -340,22 +344,31 @@ export default function(hljs) { keywords: { $pattern: hljs.IDENT_RE, title: BLOCKS, - keyword: VAR_TYPES.concat(SPECIAL_FUNCTIONS), - built_in: FUNCTIONS.concat(DISTRIBUTIONS).concat(expanded).concat(LITERALS) + keyword: STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS), + built_in: FUNCTIONS.concat(DISTRIBUTIONS).concat(expanded), + punctuation: DELIMIT }, contains: [ hljs.C_LINE_COMMENT_MODE, INCLUDE, hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, - { - // Distinct highlighting for: [for, in, if, else, while, - // break, continue, return] - className: 'flow', - begin: /\s(for|in|if|else|while|break|continue|return)\s/, - //keywords: 'for in if else while break continue return', - relevance: 0 - }, + { + className: 'built_in', + variants: [ + { + begin: /(?<=~)\s+(\w+)/ + }, + { + begin: /(?<=\+=)\s+(\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf))/ + } + ] + }, + { + className: 'symbol', + begin: /\s+(pi|e|sqrt2|log2|log10)(?=\()/, + relevance: 0 + }, { // hack: in range constraints, lower must follow either , or < // or @@ -371,13 +384,13 @@ export default function(hljs) { { // hack: in range constraints, upper must follow either , or < // or - begin: /[<,]\s*multiplier\s*=/, + begin: /[<,]\s*offset\s*=/, keywords: 'offset' }, { // hack: in range constraints, upper must follow either , or < // or - begin: /[<,]\s*offset\s*=/, + begin: /[<,]\s*multiplier\s*=/, keywords: 'multiplier' }, { diff --git a/tsconfig.json b/tsconfig.json index 0e20167ca0..8e2f9baeff 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { /* Basic Options */ - "target": "es2016", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ + "target": "es2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ "module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ "allowJs": true, /* Allow javascript files to be compiled. */ From 344bb1d7a80ca429557c399bc9b980d2d48cf23d Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 30 Nov 2021 11:40:55 -0500 Subject: [PATCH 05/31] update author --- src/languages/stan.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index 3bd657282b..ad3f3dced0 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -1,7 +1,7 @@ /* Language: Stan Description: The Stan probabilistic programming language -Author: Jeffrey B. Arnold +Author: Sean Pinkney Website: http://mc-stan.org/ Category: scientific */ From 4affdc7d3544d38370f80ff627edcd0f82b4a218 Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 30 Nov 2021 11:52:44 -0500 Subject: [PATCH 06/31] add tests and update CHANGES.md --- CHANGES.md | 1 + test/detect/stan/default.txt | 8 ++-- test/markup/stan/default.expect.txt | 63 +++++++++++++++-------------- test/markup/stan/default.txt | 63 +++++++++++++++-------------- 4 files changed, 69 insertions(+), 66 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6a8581edfd..db7b0a848a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ Grammars: - enh(clojure) Add `regex` mode to regex literal - fix(clojure) Remove inconsistent/broken highlighting for metadata - enh(clojure) Add `punctuation` mode for commas. + - fix(stan) Updated for Stan 2.28 (#3410) Developer Tools: diff --git a/test/detect/stan/default.txt b/test/detect/stan/default.txt index c2fd30d03c..a4b42f332e 100644 --- a/test/detect/stan/default.txt +++ b/test/detect/stan/default.txt @@ -1,5 +1,5 @@ functions { - // #include normal_copula.stanfunctions + #include normal_copula.stanfunctions } data { int N; @@ -16,7 +16,6 @@ parameters { array[K - 1] real mu; array[K + 1] real sigma; cholesky_factor_corr[K] L; - real delta; } model { target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]); @@ -32,9 +31,10 @@ model { y[3, n] = inv_Phi(lognormal_cdf(x[n, 3] | mu[3], sigma[3])); y[4, n] = inv_Phi(weibull_cdf(x[n, 4] | sigma[4], sigma[5])); } - // y ~ multi_normal_copula(L); + y ~ multi_normal(L); + } } generated quantities { matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); -} +} \ No newline at end of file diff --git a/test/markup/stan/default.expect.txt b/test/markup/stan/default.expect.txt index 6a32c43433..42e5382e37 100644 --- a/test/markup/stan/default.expect.txt +++ b/test/markup/stan/default.expect.txt @@ -1,39 +1,40 @@ -// Multivariate Regression Example -// Taken from stan-reference-2.8.0.pdf p.66 - +functions { + #include normal_copula.stanfunctions +} data { - int<lower=0> N; // num individuals - int<lower=1> K; // num ind predictors - int<lower=1> J; // num groups - int<lower=1> L; // num group predictors - int<lower=1,upper=J> jj[N]; // group for individual - matrix[N,K] x; // individual predictors - row_vector[L] u[J]; // group predictors - vector[N] y; // outcomes + int<lower=0> N; + int K; + matrix[N, K] x; } +transformed data { + complex zi = 1+3.14i; + zi = zi * 0i; + complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); + real x = get_real(3i - 40e-3i + 1e10i); +}› parameters { - corr_matrix[K] Omega; // prior correlation - vector<lower=0>[K] tau; // prior scale - matrix[L,K] gamma; // group coeffs - vector[K] beta[J]; // indiv coeffs by group - real<lower=0> sigma; // prediction error scale + array[K - 1] real mu; + array[K + 1] real<lower=0> sigma; + cholesky_factor_corr[K] L; } model { - tau ~ cauchy(0,2.5); - Omega ~ lkj_corr(2); - to_vector(gamma) ~ normal(0, 5); - { - row_vector[K] u_gamma[J]; - for (j in 1:J) - u_gamma[j] <- u[j] * gamma; - beta ~ multi_normal(u_gamma, quad_form_diag(Omega, tau)); - } + target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]); + target += gumbel_lpdf(x[ : , 2] | mu[2], sigma[2]); + target += lognormal_lpdf(x[ : , 3] | mu[3], sigma[3]); + target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]); + { - vector[N] x_beta_jj; - for (n in 1:N) - x_beta_jj[n] <- x[n] * beta[jj[n]]; - y ~ normal(x_beta_jj, sigma); + matrix[K, N] y; + for (n in 1 : N) { + y[1, n] = inv_Phi(normal_cdf(x[n, 1] | mu[1], sigma[1])); + y[2, n] = inv_Phi(gumbel_cdf(x[n, 2] | mu[2], sigma[2])); + y[3, n] = inv_Phi(lognormal_cdf(x[n, 3] | mu[3], sigma[3])); + y[4, n] = inv_Phi(weibull_cdf(x[n, 4] | sigma[4], sigma[5])); + } + y ~ multi_normal(L); + } } - -# Note: Octothorpes indicate comments, too! +generated quantities { + matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); +} \ No newline at end of file diff --git a/test/markup/stan/default.txt b/test/markup/stan/default.txt index 99e56c45ec..a4b42f332e 100644 --- a/test/markup/stan/default.txt +++ b/test/markup/stan/default.txt @@ -1,39 +1,40 @@ -// Multivariate Regression Example -// Taken from stan-reference-2.8.0.pdf p.66 - +functions { + #include normal_copula.stanfunctions +} data { - int N; // num individuals - int K; // num ind predictors - int J; // num groups - int L; // num group predictors - int jj[N]; // group for individual - matrix[N,K] x; // individual predictors - row_vector[L] u[J]; // group predictors - vector[N] y; // outcomes + int N; + int K; + matrix[N, K] x; } +transformed data { + complex zi = 1+3.14i; + zi = zi * 0i; + complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); + real x = get_real(3i - 40e-3i + 1e10i); +}› parameters { - corr_matrix[K] Omega; // prior correlation - vector[K] tau; // prior scale - matrix[L,K] gamma; // group coeffs - vector[K] beta[J]; // indiv coeffs by group - real sigma; // prediction error scale + array[K - 1] real mu; + array[K + 1] real sigma; + cholesky_factor_corr[K] L; } model { - tau ~ cauchy(0,2.5); - Omega ~ lkj_corr(2); - to_vector(gamma) ~ normal(0, 5); - { - row_vector[K] u_gamma[J]; - for (j in 1:J) - u_gamma[j] <- u[j] * gamma; - beta ~ multi_normal(u_gamma, quad_form_diag(Omega, tau)); - } + target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]); + target += gumbel_lpdf(x[ : , 2] | mu[2], sigma[2]); + target += lognormal_lpdf(x[ : , 3] | mu[3], sigma[3]); + target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]); + { - vector[N] x_beta_jj; - for (n in 1:N) - x_beta_jj[n] <- x[n] * beta[jj[n]]; - y ~ normal(x_beta_jj, sigma); + matrix[K, N] y; + for (n in 1 : N) { + y[1, n] = inv_Phi(normal_cdf(x[n, 1] | mu[1], sigma[1])); + y[2, n] = inv_Phi(gumbel_cdf(x[n, 2] | mu[2], sigma[2])); + y[3, n] = inv_Phi(lognormal_cdf(x[n, 3] | mu[3], sigma[3])); + y[4, n] = inv_Phi(weibull_cdf(x[n, 4] | sigma[4], sigma[5])); + } + y ~ multi_normal(L); + } } - -# Note: Octothorpes indicate comments, too! +generated quantities { + matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); +} \ No newline at end of file From ba260720ba72eccdad3e9718cfd1384f20395824 Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 30 Nov 2021 12:26:20 -0500 Subject: [PATCH 07/31] add truncation highlighting --- CHANGES.md | 2 +- src/languages/stan.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index db7b0a848a..359752cdf8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,7 +16,7 @@ Grammars: - enh(clojure) Add `regex` mode to regex literal - fix(clojure) Remove inconsistent/broken highlighting for metadata - enh(clojure) Add `punctuation` mode for commas. - - fix(stan) Updated for Stan 2.28 (#3410) + - fix(stan) Updated for Stan 2.28 and other misc. improvements (#3410) Developer Tools: diff --git a/src/languages/stan.js b/src/languages/stan.js index 66eac48f8d..b1549e55b6 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -368,6 +368,10 @@ export default function(hljs) { className: 'symbol', begin: /\s+(pi|e|sqrt2|log2|log10)(?=\()/, relevance: 0 + }, + { + begin: /(?<=\))\s*T(?=\s*\[)/, + keywords: 'T' }, { // hack: in range constraints, lower must follow either , or < From 80f8840726b047b110a6ed5e08e3ab822e77443d Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 30 Nov 2021 14:00:02 -0500 Subject: [PATCH 08/31] reviewer comments --- src/languages/stan.js | 45 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index b1549e55b6..049518b867 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -286,7 +286,7 @@ export default function(hljs) { 'wishart', 'inv_wishart' ]; - const expanded = DISTRIBUTIONS.flatMap(name => [ + const DISTRIBUTIONS_EXPANDED = DISTRIBUTIONS.flatMap(name => [ `${name}_lpdf`, `${name}_lpmf`, `${name}_lccdf`, @@ -297,17 +297,17 @@ export default function(hljs) { `${name}_qf` ] ) - const DELIMIT = [ - '{', - '}', - '(', - ')', - '[', - ']', - ',', - ':', - '|' - ] + // const DELIMIT = [ + // '{', + // '}', + // '(', + // ')', + // '[', + // ']', + // ',', + // ':', + // '|' + // ] const BLOCK_COMMENT = hljs.COMMENT( /\/\*/, @@ -345,8 +345,8 @@ export default function(hljs) { $pattern: hljs.IDENT_RE, title: BLOCKS, keyword: STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS), - built_in: FUNCTIONS.concat(DISTRIBUTIONS).concat(expanded), - punctuation: DELIMIT + built_in: FUNCTIONS.concat(DISTRIBUTIONS).concat(DISTRIBUTIONS_EXPANDED), + // punctuation: DELIMIT }, contains: [ hljs.C_LINE_COMMENT_MODE, @@ -354,6 +354,7 @@ export default function(hljs) { hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, { + // handle user defined distributions className: 'built_in', variants: [ { @@ -366,13 +367,17 @@ export default function(hljs) { }, { className: 'symbol', - begin: /\s+(pi|e|sqrt2|log2|log10)(?=\()/, + begin: /(pi|e|sqrt2|log2|log10)(?=\()/, relevance: 0 }, - { - begin: /(?<=\))\s*T(?=\s*\[)/, - keywords: 'T' - }, + // cannot use regex look-behind yet because of Safari + // once that is enabled can uncomment this to get + // correct highlighting for truncation while + // allowing an array named T to not be highlighted + // { + // begin: /\bT(?=\s*\[)/, + // keywords: 'T' + // }, { // hack: in range constraints, lower must follow either , or < // or @@ -405,6 +410,8 @@ export default function(hljs) { className: 'number', variants: [ { + // Comes from @RunDevelopment accessed 11/29/2021 at + // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 begin: /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/i }, { From a68bf22963b7f04d175a0d60371c111cdb90b26b Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 30 Nov 2021 15:36:04 -0500 Subject: [PATCH 09/31] reviewer comments --- src/languages/stan.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index 049518b867..8e5c7b8332 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -50,10 +50,10 @@ export default function(hljs) { 'ode_bdf_tol', 'ode_adjoint_tol_ctl', 'integrate_1d', - 'integrate_ode_rk45', - 'integrate_ode', - 'integrate_ode_adams', - 'integrate_ode_bdf' + 'integrate_ode_rk45|10', + 'integrate_ode|10', + 'integrate_ode_adams|10', + 'integrate_ode_bdf|10' ]; const VAR_TYPES = [ `array`, @@ -328,7 +328,6 @@ export default function(hljs) { begin: /#\s*[a-z]+\b/, end: /$/, relevance: 0, // relevance comes from keywords - keywords: '#include', contains: [ { match: /[a-z][a-z-.]+/, @@ -345,7 +344,7 @@ export default function(hljs) { $pattern: hljs.IDENT_RE, title: BLOCKS, keyword: STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS), - built_in: FUNCTIONS.concat(DISTRIBUTIONS).concat(DISTRIBUTIONS_EXPANDED), + built_in: FUNCTIONS, // punctuation: DELIMIT }, contains: [ @@ -358,13 +357,17 @@ export default function(hljs) { className: 'built_in', variants: [ { - begin: /(?<=~)\s+(\w+)/ + begin: /(?<=~)\s+(\w+(?=\())/ }, { begin: /(?<=\+=)\s+(\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf))/ } ] }, + { + begin: '~\s*(' + hljs.IDENT_RE + ')\\s*\\(', + keywords: DISTRIBUTIONS.concat(DISTRIBUTIONS_EXPANDED) + }, { className: 'symbol', begin: /(pi|e|sqrt2|log2|log10)(?=\()/, From 0f68f336a964fc92635913b5cf746a3184bea257 Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 30 Nov 2021 15:40:21 -0500 Subject: [PATCH 10/31] remove 10 on positive ordered --- src/languages/stan.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index 8e5c7b8332..fb504cd346 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -62,7 +62,7 @@ export default function(hljs) { 'real', 'vector', 'ordered', - 'positive_ordered|10', + 'positive_ordered', 'simplex', 'unit_vector', 'row_vector', From c35fc6b581ea837daaee35dd71780d1dba87ef28 Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 30 Nov 2021 15:46:03 -0500 Subject: [PATCH 11/31] add #include --- src/languages/stan.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index fb504cd346..f7e8c3d282 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -325,7 +325,7 @@ export default function(hljs) { const INCLUDE = { className: 'meta', - begin: /#\s*[a-z]+\b/, + begin: /#include\b/, end: /$/, relevance: 0, // relevance comes from keywords contains: [ From 9c5e95de2fb5433b9e5915b5052f637078c90333 Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 30 Nov 2021 16:37:57 -0500 Subject: [PATCH 12/31] look ahead filter for udf with suffix --- src/languages/stan.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index f7e8c3d282..fa18ceaf68 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -360,7 +360,7 @@ export default function(hljs) { begin: /(?<=~)\s+(\w+(?=\())/ }, { - begin: /(?<=\+=)\s+(\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf))/ + begin: /(?<=\+=)\s+(\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf))(?=\()/ } ] }, From eee2427cda2e45f894dfe2592bb87b6d0fc1b0ec Mon Sep 17 00:00:00 2001 From: spinkney Date: Wed, 1 Dec 2021 12:33:01 -0500 Subject: [PATCH 13/31] add all functions in alphabetical order plus other --- src/languages/stan.js | 778 +++++++++++++++++++++++++++--------------- 1 file changed, 498 insertions(+), 280 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index fa18ceaf68..ecd648fa01 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -29,34 +29,8 @@ export default function(hljs) { 'return' ]; - const SPECIAL_FUNCTIONS = [ - 'print', - 'reject', - 'hmm_marginal', - 'hmm_latent_rng', - 'hmm_hidden_state_prob', - 'algebra_solver', - 'algebra_solver_newton', - 'map_rect', - 'reduce_sum', - 'reduce_sum_static', - 'ode_rk45', - 'ode_rk45_tol', - 'ode_ckrk', - 'ode_ckrk_tol', - 'ode_adams', - 'ode_adams_tol', - 'ode_bdf', - 'ode_bdf_tol', - 'ode_adjoint_tol_ctl', - 'integrate_1d', - 'integrate_ode_rk45|10', - 'integrate_ode|10', - 'integrate_ode_adams|10', - 'integrate_ode_bdf|10' - ]; const VAR_TYPES = [ - `array`, + 'array', 'complex', 'int', 'real', @@ -73,241 +47,467 @@ export default function(hljs) { 'cov_matrix|10', 'void' ]; - const FUNCTIONS = [ - // Integer-Valued Basic Functions - - // Absolute functions - 'abs', 'int_step', - - // Bound functions - 'min', 'max', - - // Size functions - 'size', - - // Real-Valued Basic Functions - - // Log probability function - 'target', 'get_lp', - - // Logical functions - 'step', 'is_inf', 'is_nan', - - // Step-like functions - 'fabs', 'fdim', 'fmin', 'fmax', 'fmod', 'floor', 'ceil', 'round', - 'trunc', - - // Power and logarithm functions - 'sqrt', 'cbrt', 'square', 'exp', 'exp2', 'log', 'log2', 'log10', - 'pow', 'inv', 'inv_sqrt', 'inv_square', - - // Trigonometric functions - 'hypot', 'cos', 'sin', 'tan', 'acos', 'asin', 'atan', 'atan2', - - // Hyperbolic trigonometric functions - 'cosh', 'sinh', 'tanh', 'acosh', 'asinh', 'atanh', - - // Link functions - 'logit', 'inv_logit', 'inv_cloglog', - - // Probability-related functions - 'erf', 'erfc', 'Phi', 'inv_Phi', 'Phi_approx', 'binary_log_loss', - 'owens_t', - - // Combinatorial functions - 'beta', 'inc_beta', 'lbeta', 'tgamma', 'lgamma', 'digamma', - 'trigamma', 'lmgamma', 'gamma_p', 'gamma_q', - 'binomial_coefficient_log', 'choose', 'bessel_first_kind', - 'bessel_second_kind', 'modified_bessel_first_kind', - 'log_modified_bessel_first_kind', 'modified_bessel_second_kind', - 'falling_factorial', 'lchoose', 'log_falling_factorial', - 'rising_factorial', 'log_rising_factorial', - // Composed functions - 'expm1', 'fma', 'multiply_log', 'ldexp', 'lmultiply', 'log1p', - 'log1m', 'log1p_exp', 'log1m_exp', 'log_diff_exp', 'log_mix', - 'log_sum_exp', 'log_inv_logit', 'log_inv_logit_diff', - 'log1m_inv_logit', - - // Special functions - 'lambert_w0', 'lambert_wm1', - - // Complex Conversion Functions - 'get_real', 'get_imag', - - // Complex-Valued Basic Functions - - // Complex Construction Functions - 'to_complex', - - // Array Operations - - // Reductions - 'sum', 'prod', 'log_sum_exp', 'mean', 'variance', 'sd', 'distance', - 'squared_distance', 'quantile', - - // Array size and dimension function - 'dims', 'num_elements', - - // Array broadcasting - 'rep_array', - - // Array concatenation + // to get the functions list + // clone the [stan-docs repo](https://github.com/stan-dev/docs) + // then cd into it run the python script with the major and minor version numbers: + // ```bash + // python3 extract_function_sigs.py 2 28 + // ``` + // then run a bash command to extract only the unique names + // ```bash + // cut -d ';' -f1 stan-functions-2_28.txt | uniq -u | sed "s/^/'/;s/$/',/" > stan_functions_quoted.txt + // ``` + const FUNCTIONS = [ + 'Phi', + 'Phi_approx', 'append_array', - - // Sorting functions - 'sort_asc', 'sort_desc', 'sort_indices_asc', 'sort_indices_desc', - 'rank', - - // Reversing functions - 'reverse', - - // Matrix Operations - - // Integer-valued matrix size functions - 'num_elements', 'rows', 'cols', - - // Dot products and specialized products - 'dot_product', 'columns_dot_product', 'rows_dot_product', 'dot_self', - 'columns_dot_self', 'rows_dot_self', 'tcrossprod', 'crossprod', - 'quad_form', 'quad_form_diag', 'quad_form_sym', 'trace_quad_form', - 'trace_gen_quad_form', 'multiply_lower_tri_self_transpose', - 'diag_pre_multiply', 'diag_post_multiply', - - // Broadcast functions - 'rep_vector', 'rep_row_vector', 'rep_matrix', - 'symmetrize_from_lower_tri', - - // Diagonal matrix functions - 'add_diag', 'diagonal', 'diag_matrix', 'identity_matrix', - - // Container construction functions - 'linspaced_array', 'linspaced_int_array', 'linspaced_vector', - 'linspaced_row_vector', 'one_hot_int_array', 'one_hot_array', - 'one_hot_vector', 'one_hot_row_vector', 'ones_int_array', - 'ones_array', 'ones_vector', 'ones_row_vector', 'zeros_int_array', - 'zeros_array', 'zeros_vector', 'zeros_row_vector', 'uniform_simplex', - - // Slicing and blocking functions - 'col', 'row', 'block', 'sub_col', 'sub_row', 'head', 'tail', - 'segment', - - // Matrix concatenation - 'append_col', 'append_row', - - // Special matrix functions - 'softmax', 'log_softmax', 'cumulative_sum', - - // Covariance functions - 'cov_exp_quad', - - // Linear algebra functions and solvers - 'mdivide_left_tri_low', 'mdivide_right_tri_low', 'mdivide_left_spd', - 'mdivide_right_spd', 'matrix_exp', 'matrix_exp_multiply', - 'scale_matrix_exp_multiply', 'matrix_power','trace', 'determinant', - 'log_determinant', 'inverse', 'inverse_spd', 'chol2inv', - 'generalized_inverse', 'eigenvalues_sym', 'eigenvectors_sym', - 'qr_thin_Q', 'qr_thin_R', 'qr_Q', 'qr_R', 'cholseky_decompose', - 'singular_values', 'svd_U', 'svd_V', - - // Sparse Matrix Operations - - // Conversion functions - 'csr_extract_w', 'csr_extract_v', 'csr_extract_u', - 'csr_to_dense_matrix', - - // Sparse matrix arithmetic + 'arg', + 'atan2', + 'bernoulli', + 'bernoulli_cdf', + 'bernoulli_lccdf', + 'bernoulli_lcdf', + 'bernoulli_logit', + 'bernoulli_logit_glm', + 'bernoulli_logit_lpmf', + 'bernoulli_logit_lupmf', + 'bernoulli_logit_rng', + 'bernoulli_lpmf', + 'bernoulli_lupmf', + 'bernoulli_rng', + 'beta_binomial', + 'beta_binomial_cdf', + 'beta_binomial_lccdf', + 'beta_binomial_lcdf', + 'beta_binomial_lpmf', + 'beta_binomial_lupmf', + 'beta_binomial_rng', + 'beta_cdf', + 'beta_lccdf', + 'beta_lcdf', + 'beta_lpdf', + 'beta_lupdf', + 'beta_proportion', + 'beta_proportion_lccdf', + 'beta_proportion_lcdf', + 'beta_proportion_lpdf', + 'beta_proportion_lupdf', + 'beta_proportion_rng', + 'beta_rng', + 'binomial', + 'binomial_cdf', + 'binomial_lccdf', + 'binomial_lcdf', + 'binomial_logit', + 'binomial_logit_lpmf', + 'binomial_logit_lupmf', + 'binomial_lpmf', + 'binomial_lupmf', + 'binomial_rng', + 'block', + 'categorical', + 'categorical_logit', + 'categorical_logit_glm', + 'categorical_logit_lpmf', + 'categorical_logit_lupmf', + 'categorical_logit_rng', + 'categorical_lpmf', + 'categorical_lupmf', + 'categorical_rng', + 'cauchy', + 'cauchy_cdf', + 'cauchy_lccdf', + 'cauchy_lcdf', + 'cauchy_lpdf', + 'cauchy_lupdf', + 'cauchy_rng', + 'cbrt', + 'ceil', + 'chi_square', + 'chi_square_cdf', + 'chi_square_lccdf', + 'chi_square_lcdf', + 'chi_square_lpdf', + 'chi_square_lupdf', + 'chi_square_rng', + 'chol2inv', + 'cholesky_decompose', + 'col', + 'conj', + 'crossprod', + 'csr_extract_u', + 'csr_extract_v', + 'csr_extract_w', 'csr_matrix_times_vector', - - // Mixed Operations - 'to_matrix', 'to_vector', 'to_row_vector', 'to_array_2d', - 'to_array_1d', - - // Special values - 'not_a_number', 'positive_infinity', 'negative_infinity', - 'machine_precision' - - ]; - const DISTRIBUTIONS = [ - // Discrete Distributions - - // Binary Distributions - 'bernoulli', 'bernoulli_logit', 'bernoulli_logit_glm', - - // Bounded Discrete Distributions - 'binomial', 'binomial_logit', 'beta_binomial', 'hypergeometric', - 'categorical', 'categorical_logit_glm', 'discrete_range', - 'ordered_logistic', 'ordered_logistic_glm', 'ordered_probit', - - // Unbounded Discrete Distributions - 'neg_binomial', 'neg_binomial_2', 'neg_binomial_2_log', - 'neg_binomial_2_log_glm', 'poisson', 'poisson_log', + 'csr_to_dense_matrix', + 'determinant', + 'diag_matrix', + 'diagonal', + 'digamma', + 'dims', + 'dirichlet', + 'dirichlet_lpdf', + 'dirichlet_lupdf', + 'dirichlet_rng', + 'discrete_range', + 'discrete_range_cdf', + 'discrete_range_lccdf', + 'discrete_range_lcdf', + 'discrete_range_lpmf', + 'discrete_range_lupmf', + 'discrete_range_rng', + 'double_exponential', + 'double_exponential_cdf', + 'double_exponential_lccdf', + 'double_exponential_lcdf', + 'double_exponential_lpdf', + 'double_exponential_lupdf', + 'double_exponential_rng', + 'e', + 'eigenvalues_sym', + 'eigenvectors_sym', + 'erf', + 'erfc', + 'exp2', + 'exp_mod_normal', + 'exp_mod_normal_cdf', + 'exp_mod_normal_lccdf', + 'exp_mod_normal_lcdf', + 'exp_mod_normal_lpdf', + 'exp_mod_normal_lupdf', + 'exp_mod_normal_rng', + 'expm1', + 'exponential', + 'exponential_cdf', + 'exponential_lccdf', + 'exponential_lcdf', + 'exponential_lpdf', + 'exponential_lupdf', + 'exponential_rng', + 'fabs', + 'floor', + 'fma', + 'frechet', + 'frechet_cdf', + 'frechet_lccdf', + 'frechet_lcdf', + 'frechet_lpdf', + 'frechet_lupdf', + 'frechet_rng', + 'gamma', + 'gamma_cdf', + 'gamma_lccdf', + 'gamma_lcdf', + 'gamma_lpdf', + 'gamma_lupdf', + 'gamma_rng', + 'gaussian_dlm_obs', + 'generalized_inverse', + 'get_imag', + 'get_lp', + 'get_real', + 'gumbel', + 'gumbel_cdf', + 'gumbel_lccdf', + 'gumbel_lcdf', + 'gumbel_lpdf', + 'gumbel_lupdf', + 'gumbel_rng', + 'hmm_hidden_state_prob', + 'hmm_latent_rng', + 'hmm_marginal', + 'hypergeometric', + 'hypergeometric_lpmf', + 'hypergeometric_lupmf', + 'hypergeometric_rng', + 'identity_matrix', + 'inc_beta', + 'integrate_ode', + 'inv', + 'inv_Phi', + 'inv_chi_square', + 'inv_chi_square_cdf', + 'inv_chi_square_lccdf', + 'inv_chi_square_lcdf', + 'inv_chi_square_lpdf', + 'inv_chi_square_lupdf', + 'inv_chi_square_rng', + 'inv_cloglog', + 'inv_gamma', + 'inv_gamma_cdf', + 'inv_gamma_lccdf', + 'inv_gamma_lcdf', + 'inv_gamma_lpdf', + 'inv_gamma_lupdf', + 'inv_gamma_rng', + 'inv_logit', + 'inv_sqrt', + 'inv_square', + 'inv_wishart', + 'inv_wishart_lpdf', + 'inv_wishart_lupdf', + 'inv_wishart_rng', + 'inverse', + 'inverse_spd', + 'is_inf', + 'is_nan', + 'lambert_w0', + 'lambert_wm1', + 'lgamma', + 'linspaced_array', + 'linspaced_int_array', + 'linspaced_row_vector', + 'linspaced_vector', + 'lkj_corr', + 'lkj_corr_cholesky', + 'lkj_corr_cholesky_lpdf', + 'lkj_corr_cholesky_lupdf', + 'lkj_corr_cholesky_rng', + 'lkj_corr_lpdf', + 'lkj_corr_lupdf', + 'lkj_corr_rng', + 'log1m', + 'log1m_exp', + 'log1m_inv_logit', + 'log1p', + 'log1p_exp', + 'log_determinant', + 'log_falling_factorial', + 'log_inv_logit', + 'log_inv_logit_diff', + 'log_mix', + 'log_softmax', + 'logistic', + 'logistic_cdf', + 'logistic_lccdf', + 'logistic_lcdf', + 'logistic_lpdf', + 'logistic_lupdf', + 'logistic_rng', + 'logit', + 'lognormal', + 'lognormal_cdf', + 'lognormal_lccdf', + 'lognormal_lcdf', + 'lognormal_lpdf', + 'lognormal_lupdf', + 'lognormal_rng', + 'machine_precision', + 'map_rect', + 'matrix_exp', + 'matrix_exp_multiply', + 'matrix_power', + 'multi_gp', + 'multi_gp_cholesky', + 'multi_gp_cholesky_lpdf', + 'multi_gp_cholesky_lupdf', + 'multi_gp_lpdf', + 'multi_gp_lupdf', + 'multi_normal', + 'multi_normal_cholesky', + 'multi_normal_prec', + 'multi_student_t', + 'multinomial', + 'multinomial_logit', + 'multinomial_logit_lpmf', + 'multinomial_logit_lupmf', + 'multinomial_logit_rng', + 'multinomial_lpmf', + 'multinomial_lupmf', + 'multinomial_rng', + 'multiply_lower_tri_self_transpose', + 'neg_binomial', + 'neg_binomial_2', + 'neg_binomial_2_cdf', + 'neg_binomial_2_lccdf', + 'neg_binomial_2_lcdf', + 'neg_binomial_2_log', + 'neg_binomial_2_log_glm', + 'neg_binomial_2_log_lpmf', + 'neg_binomial_2_log_lupmf', + 'neg_binomial_2_log_rng', + 'neg_binomial_2_lpmf', + 'neg_binomial_2_lupmf', + 'neg_binomial_2_rng', + 'neg_binomial_cdf', + 'neg_binomial_lccdf', + 'neg_binomial_lcdf', + 'neg_binomial_lpmf', + 'neg_binomial_lupmf', + 'neg_binomial_rng', + 'negative_infinity', + 'norm', + 'normal', + 'normal_cdf', + 'normal_id_glm', + 'normal_lccdf', + 'normal_lcdf', + 'normal_lpdf', + 'normal_lupdf', + 'normal_rng', + 'not_a_number', + 'ode_adams', + 'ode_adams_tol', + 'ode_adjoint_tol_ctl', + 'ode_bdf', + 'ode_bdf_tol', + 'ode_ckrk', + 'ode_ckrk_tol', + 'ode_rk45', + 'ode_rk45_tol', + 'one_hot_array', + 'one_hot_int_array', + 'one_hot_row_vector', + 'one_hot_vector', + 'ones_array', + 'ones_int_array', + 'ones_row_vector', + 'ones_vector', + 'operator%/%', + 'operator%', + 'operator=', + 'ordered_logistic', + 'ordered_logistic_lpmf', + 'ordered_logistic_lupmf', + 'ordered_logistic_rng', + 'ordered_probit', + 'ordered_probit_lpmf', + 'ordered_probit_lupmf', + 'ordered_probit_rng', + 'pareto', + 'pareto_cdf', + 'pareto_lccdf', + 'pareto_lcdf', + 'pareto_lpdf', + 'pareto_lupdf', + 'pareto_rng', + 'pareto_type_2', + 'pareto_type_2_cdf', + 'pareto_type_2_lccdf', + 'pareto_type_2_lcdf', + 'pareto_type_2_lpdf', + 'pareto_type_2_lupdf', + 'pareto_type_2_rng', + 'pi', + 'poisson', + 'poisson_cdf', + 'poisson_lccdf', + 'poisson_lcdf', + 'poisson_log', 'poisson_log_glm', - - // Multivariate Discrete Distributions - 'multinomial', 'multinomial_logit', - - // Continuous Distributions - - // Unbounded Continuous Distributions - 'normal', 'std_normal', 'normal_id_glm', 'exp_mod_normal', - 'skew_normal', 'student_t', 'cauchy', 'double_exponential', - 'logistic', 'gumbel', 'skew_double_exponential', - - // Positive Continuous Distributions - 'lognormal', 'chi_square', 'inv_chi_square', - 'scaled_inv_chi_square', 'exponential', 'gamma', 'inv_gamma', - 'weibull', 'frechet', 'rayleigh', - - // Positive Lower-Bounded Distributions - 'pareto', 'pareto_type_2', 'wiener', - - // Continuous Distributions on [0, 1] - 'beta', 'beta_proportion', - - // Circular Distributions - 'von_mises', - - // Bounded Continuous Distributions + 'poisson_log_lpmf', + 'poisson_log_lupmf', + 'poisson_log_rng', + 'poisson_lpmf', + 'poisson_lupmf', + 'poisson_rng', + 'polar', + 'positive_infinity', + 'print', + 'proj', + 'qr_Q', + 'qr_R', + 'qr_thin_Q', + 'qr_thin_R', + 'rayleigh', + 'rayleigh_cdf', + 'rayleigh_lccdf', + 'rayleigh_lcdf', + 'rayleigh_lpdf', + 'rayleigh_lupdf', + 'rayleigh_rng', + 'reduce_sum', + 'reject', + 'rep_row_vector', + 'rep_vector', + 'round', + 'row', + 'scale_matrix_exp_multiply', + 'scaled_inv_chi_square', + 'scaled_inv_chi_square_cdf', + 'scaled_inv_chi_square_lccdf', + 'scaled_inv_chi_square_lcdf', + 'scaled_inv_chi_square_lpdf', + 'scaled_inv_chi_square_lupdf', + 'scaled_inv_chi_square_rng', + 'singular_values', + 'skew_double_exponential', + 'skew_double_exponential_cdf', + 'skew_double_exponential_lccdf', + 'skew_double_exponential_lcdf', + 'skew_double_exponential_lpdf', + 'skew_double_exponential_lupdf', + 'skew_double_exponential_rng', + 'skew_normal', + 'skew_normal_cdf', + 'skew_normal_lccdf', + 'skew_normal_lcdf', + 'skew_normal_lpdf', + 'skew_normal_lupdf', + 'skew_normal_rng', + 'softmax', + 'sqrt2', + 'square', + 'std_normal', + 'std_normal_cdf', + 'std_normal_lccdf', + 'std_normal_lcdf', + 'std_normal_lpdf', + 'std_normal_lupdf', + 'std_normal_rng', + 'step', + 'student_t', + 'student_t_cdf', + 'student_t_lccdf', + 'student_t_lcdf', + 'student_t_lpdf', + 'student_t_lupdf', + 'student_t_rng', + 'sub_col', + 'sub_row', + 'svd_U', + 'svd_V', + 'symmetrize_from_lower_tri', + 'target', + 'tcrossprod', + 'tgamma', + 'to_array_2d', + 'trace', + 'trace_gen_quad_form', + 'trace_quad_form', + 'trigamma', + 'trunc', 'uniform', - - // Distributions over Unbounded Vectors - 'multi_normal', 'multi_normal_prec', 'multi_normal_cholesky', - 'multi_gp', 'multi_gp_cholesky', 'multi_student_t', - 'gaussian_dlm_obs', - - // Simplex Distributions - 'dirichlet', - - // Correlation Matrix Distributions - 'lkj_corr', 'lkj_corr_cholesky', - - // Covariance Matrix Distributions - 'wishart', 'inv_wishart' + 'uniform_cdf', + 'uniform_lccdf', + 'uniform_lcdf', + 'uniform_lpdf', + 'uniform_lupdf', + 'uniform_rng', + 'uniform_simplex', + 'von_mises', + 'von_mises_lpdf', + 'von_mises_lupdf', + 'von_mises_rng', + 'weibull', + 'weibull_cdf', + 'weibull_lccdf', + 'weibull_lcdf', + 'weibull_lpdf', + 'weibull_lupdf', + 'weibull_rng', + 'wiener', + 'wiener_lpdf', + 'wiener_lupdf', + 'wishart', + 'wishart_lpdf', + 'wishart_lupdf', + 'wishart_rng', + 'zeros_array', + 'zeros_int_array' ]; - - const DISTRIBUTIONS_EXPANDED = DISTRIBUTIONS.flatMap(name => [ - `${name}_lpdf`, - `${name}_lpmf`, - `${name}_lccdf`, - `${name}_lupdf`, - `${name}_lcdf`, - `${name}_cdf`, - `${name}_lupdf`, - `${name}_qf` - ] - ) - // const DELIMIT = [ - // '{', - // '}', - // '(', - // ')', - // '[', - // ']', - // ',', - // ':', - // '|' - // ] + const BLOCK_COMMENT = hljs.COMMENT( /\/\*/, @@ -337,48 +537,68 @@ export default function(hljs) { ] }; + var numberRegex = new RegExp([ + // Comes from @RunDevelopment accessed 11/29/2021 at + // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 + + // start of big noncapture group which + // 1. gets numbers that are by themselves + // 2. numbers that are separated by _ + // 3. numbers that are separted by . + /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)/ + // grabs scientific notation + // grabs complex numbers with i + ,/(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/ + ].map(function(r) {return r.source}).join(''), 'i'); + return { name: 'Stan', aliases: [ 'stanfuncs' ], keywords: { $pattern: hljs.IDENT_RE, title: BLOCKS, - keyword: STATEMENTS.concat(VAR_TYPES).concat(SPECIAL_FUNCTIONS), - built_in: FUNCTIONS, - // punctuation: DELIMIT + keyword: STATEMENTS.concat(VAR_TYPES), + built_in: FUNCTIONS }, contains: [ hljs.C_LINE_COMMENT_MODE, INCLUDE, hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, + // { + // Uses lookback! Uncomment once lookbacks are allowed + // handles distributions in a bit better way by looking + // back for either ~ or += instead of just relying on + // the user to have placed those + + // handle user defined distributions + // className: 'built_in', + // variants: [ + // { + // works when using a '~' to declare the target adjustment + // begin: /(?<=~)\s+(\w+)/ + // }, + // { + // works when using '+=' to declare the target adjustment + // begin: /(?<=\+=)\s+(\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf))/ + // } + // ], + // keywords: DISTRIBUTIONS.concat(DISTRIBUTIONS_EXPANDED) + // }, { - // handle user defined distributions - className: 'built_in', - variants: [ - { - begin: /(?<=~)\s+(\w+(?=\())/ - }, - { - begin: /(?<=\+=)\s+(\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf))(?=\()/ - } - ] - }, - { - begin: '~\s*(' + hljs.IDENT_RE + ')\\s*\\(', - keywords: DISTRIBUTIONS.concat(DISTRIBUTIONS_EXPANDED) + className: 'built_in', + begin: '\\s*(' + hljs.IDENT_RE + ')(?=\\()' }, { className: 'symbol', - begin: /(pi|e|sqrt2|log2|log10)(?=\()/, + begin: /\s(pi|e|sqrt2|log2|log10)(?=\()/, relevance: 0 }, - // cannot use regex look-behind yet because of Safari - // once that is enabled can uncomment this to get + // Uses lookback! Uncomment once lookbacks are allowed // correct highlighting for truncation while // allowing an array named T to not be highlighted // { - // begin: /\bT(?=\s*\[)/, + // begin: /(?<=\))\s*T(?=\s*\[)/ // keywords: 'T' // }, { @@ -413,9 +633,7 @@ export default function(hljs) { className: 'number', variants: [ { - // Comes from @RunDevelopment accessed 11/29/2021 at - // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 - begin: /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/i + begin: numberRegex }, { begin: /\.\d+(?:[eE][+-]?\d+)?\b/ From f8fb91527981a00a6877c758f5645f9fd9fc44e1 Mon Sep 17 00:00:00 2001 From: spinkney Date: Wed, 1 Dec 2021 12:56:28 -0500 Subject: [PATCH 14/31] handle the symbols separately --- src/languages/stan.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index ecd648fa01..45d4ddf84d 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -508,6 +508,13 @@ export default function(hljs) { 'zeros_int_array' ]; + // handle + const symbols = ['pi', 'e', 'sqrt2', 'log2', 'log10'] + var FUNCTIONS_NO_SYMBOLS = FUNCTIONS; + + FUNCTIONS_NO_SYMBOLS = FUNCTIONS_NO_SYMBOLS.filter(function(val){ + return (symbols.indexOf(val) == -1 ? true : false) + }) const BLOCK_COMMENT = hljs.COMMENT( /\/\*/, @@ -558,7 +565,7 @@ export default function(hljs) { $pattern: hljs.IDENT_RE, title: BLOCKS, keyword: STATEMENTS.concat(VAR_TYPES), - built_in: FUNCTIONS + built_in: FUNCTIONS_NO_SYMBOLS }, contains: [ hljs.C_LINE_COMMENT_MODE, From 2594deecc24f10bc51d9a9812710d378b3a7b466 Mon Sep 17 00:00:00 2001 From: spinkney Date: Wed, 1 Dec 2021 12:59:16 -0500 Subject: [PATCH 15/31] flesh out comment for symbols --- src/languages/stan.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index 45d4ddf84d..20f75dc60c 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -508,7 +508,9 @@ export default function(hljs) { 'zeros_int_array' ]; - // handle + // handle symbols separately or else + // e and other symbols will be highlighted + // when used as variables const symbols = ['pi', 'e', 'sqrt2', 'log2', 'log10'] var FUNCTIONS_NO_SYMBOLS = FUNCTIONS; From b65a358b98874a21f94c8a3d97faa01d96408551 Mon Sep 17 00:00:00 2001 From: spinkney Date: Sat, 4 Dec 2021 08:45:22 -0500 Subject: [PATCH 16/31] removed all lookbacks and updated with param lists --- src/languages/stan.js | 837 +++++++++++++++++------------------------- 1 file changed, 339 insertions(+), 498 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index 20f75dc60c..ba20ac1e51 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -50,475 +50,328 @@ export default function(hljs) { // to get the functions list // clone the [stan-docs repo](https://github.com/stan-dev/docs) - // then cd into it run the python script with the major and minor version numbers: - // ```bash - // python3 extract_function_sigs.py 2 28 - // ``` - // then run a bash command to extract only the unique names - // ```bash - // cut -d ';' -f1 stan-functions-2_28.txt | uniq -u | sed "s/^/'/;s/$/',/" > stan_functions_quoted.txt - // ``` - const FUNCTIONS = [ - 'Phi', - 'Phi_approx', - 'append_array', - 'arg', - 'atan2', - 'bernoulli', - 'bernoulli_cdf', - 'bernoulli_lccdf', - 'bernoulli_lcdf', - 'bernoulli_logit', - 'bernoulli_logit_glm', - 'bernoulli_logit_lpmf', - 'bernoulli_logit_lupmf', - 'bernoulli_logit_rng', - 'bernoulli_lpmf', - 'bernoulli_lupmf', - 'bernoulli_rng', - 'beta_binomial', - 'beta_binomial_cdf', - 'beta_binomial_lccdf', - 'beta_binomial_lcdf', - 'beta_binomial_lpmf', - 'beta_binomial_lupmf', - 'beta_binomial_rng', - 'beta_cdf', - 'beta_lccdf', - 'beta_lcdf', - 'beta_lpdf', - 'beta_lupdf', - 'beta_proportion', - 'beta_proportion_lccdf', - 'beta_proportion_lcdf', - 'beta_proportion_lpdf', - 'beta_proportion_lupdf', - 'beta_proportion_rng', - 'beta_rng', - 'binomial', - 'binomial_cdf', - 'binomial_lccdf', - 'binomial_lcdf', - 'binomial_logit', - 'binomial_logit_lpmf', - 'binomial_logit_lupmf', - 'binomial_lpmf', - 'binomial_lupmf', - 'binomial_rng', - 'block', - 'categorical', - 'categorical_logit', - 'categorical_logit_glm', - 'categorical_logit_lpmf', - 'categorical_logit_lupmf', - 'categorical_logit_rng', - 'categorical_lpmf', - 'categorical_lupmf', - 'categorical_rng', - 'cauchy', - 'cauchy_cdf', - 'cauchy_lccdf', - 'cauchy_lcdf', - 'cauchy_lpdf', - 'cauchy_lupdf', - 'cauchy_rng', - 'cbrt', - 'ceil', - 'chi_square', - 'chi_square_cdf', - 'chi_square_lccdf', - 'chi_square_lcdf', - 'chi_square_lpdf', - 'chi_square_lupdf', - 'chi_square_rng', - 'chol2inv', - 'cholesky_decompose', - 'col', - 'conj', - 'crossprod', - 'csr_extract_u', - 'csr_extract_v', - 'csr_extract_w', - 'csr_matrix_times_vector', - 'csr_to_dense_matrix', - 'determinant', - 'diag_matrix', - 'diagonal', - 'digamma', - 'dims', - 'dirichlet', - 'dirichlet_lpdf', - 'dirichlet_lupdf', - 'dirichlet_rng', - 'discrete_range', - 'discrete_range_cdf', - 'discrete_range_lccdf', - 'discrete_range_lcdf', - 'discrete_range_lpmf', - 'discrete_range_lupmf', - 'discrete_range_rng', - 'double_exponential', - 'double_exponential_cdf', - 'double_exponential_lccdf', - 'double_exponential_lcdf', - 'double_exponential_lpdf', - 'double_exponential_lupdf', - 'double_exponential_rng', - 'e', - 'eigenvalues_sym', - 'eigenvectors_sym', - 'erf', - 'erfc', - 'exp2', - 'exp_mod_normal', - 'exp_mod_normal_cdf', - 'exp_mod_normal_lccdf', - 'exp_mod_normal_lcdf', - 'exp_mod_normal_lpdf', - 'exp_mod_normal_lupdf', - 'exp_mod_normal_rng', - 'expm1', - 'exponential', - 'exponential_cdf', - 'exponential_lccdf', - 'exponential_lcdf', - 'exponential_lpdf', - 'exponential_lupdf', - 'exponential_rng', - 'fabs', - 'floor', - 'fma', - 'frechet', - 'frechet_cdf', - 'frechet_lccdf', - 'frechet_lcdf', - 'frechet_lpdf', - 'frechet_lupdf', - 'frechet_rng', - 'gamma', - 'gamma_cdf', - 'gamma_lccdf', - 'gamma_lcdf', - 'gamma_lpdf', - 'gamma_lupdf', - 'gamma_rng', - 'gaussian_dlm_obs', - 'generalized_inverse', - 'get_imag', - 'get_lp', - 'get_real', - 'gumbel', - 'gumbel_cdf', - 'gumbel_lccdf', - 'gumbel_lcdf', - 'gumbel_lpdf', - 'gumbel_lupdf', - 'gumbel_rng', - 'hmm_hidden_state_prob', - 'hmm_latent_rng', - 'hmm_marginal', - 'hypergeometric', - 'hypergeometric_lpmf', - 'hypergeometric_lupmf', - 'hypergeometric_rng', - 'identity_matrix', - 'inc_beta', - 'integrate_ode', - 'inv', - 'inv_Phi', - 'inv_chi_square', - 'inv_chi_square_cdf', - 'inv_chi_square_lccdf', - 'inv_chi_square_lcdf', - 'inv_chi_square_lpdf', - 'inv_chi_square_lupdf', - 'inv_chi_square_rng', - 'inv_cloglog', - 'inv_gamma', - 'inv_gamma_cdf', - 'inv_gamma_lccdf', - 'inv_gamma_lcdf', - 'inv_gamma_lpdf', - 'inv_gamma_lupdf', - 'inv_gamma_rng', - 'inv_logit', - 'inv_sqrt', - 'inv_square', - 'inv_wishart', - 'inv_wishart_lpdf', - 'inv_wishart_lupdf', - 'inv_wishart_rng', - 'inverse', - 'inverse_spd', - 'is_inf', - 'is_nan', - 'lambert_w0', - 'lambert_wm1', - 'lgamma', - 'linspaced_array', - 'linspaced_int_array', - 'linspaced_row_vector', - 'linspaced_vector', - 'lkj_corr', - 'lkj_corr_cholesky', - 'lkj_corr_cholesky_lpdf', - 'lkj_corr_cholesky_lupdf', - 'lkj_corr_cholesky_rng', - 'lkj_corr_lpdf', - 'lkj_corr_lupdf', - 'lkj_corr_rng', - 'log1m', - 'log1m_exp', - 'log1m_inv_logit', - 'log1p', - 'log1p_exp', - 'log_determinant', - 'log_falling_factorial', - 'log_inv_logit', - 'log_inv_logit_diff', - 'log_mix', - 'log_softmax', - 'logistic', - 'logistic_cdf', - 'logistic_lccdf', - 'logistic_lcdf', - 'logistic_lpdf', - 'logistic_lupdf', - 'logistic_rng', - 'logit', - 'lognormal', - 'lognormal_cdf', - 'lognormal_lccdf', - 'lognormal_lcdf', - 'lognormal_lpdf', - 'lognormal_lupdf', - 'lognormal_rng', - 'machine_precision', - 'map_rect', - 'matrix_exp', - 'matrix_exp_multiply', - 'matrix_power', - 'multi_gp', - 'multi_gp_cholesky', - 'multi_gp_cholesky_lpdf', - 'multi_gp_cholesky_lupdf', - 'multi_gp_lpdf', - 'multi_gp_lupdf', - 'multi_normal', - 'multi_normal_cholesky', - 'multi_normal_prec', - 'multi_student_t', - 'multinomial', - 'multinomial_logit', - 'multinomial_logit_lpmf', - 'multinomial_logit_lupmf', - 'multinomial_logit_rng', - 'multinomial_lpmf', - 'multinomial_lupmf', - 'multinomial_rng', - 'multiply_lower_tri_self_transpose', - 'neg_binomial', - 'neg_binomial_2', - 'neg_binomial_2_cdf', - 'neg_binomial_2_lccdf', - 'neg_binomial_2_lcdf', - 'neg_binomial_2_log', - 'neg_binomial_2_log_glm', - 'neg_binomial_2_log_lpmf', - 'neg_binomial_2_log_lupmf', - 'neg_binomial_2_log_rng', - 'neg_binomial_2_lpmf', - 'neg_binomial_2_lupmf', - 'neg_binomial_2_rng', - 'neg_binomial_cdf', - 'neg_binomial_lccdf', - 'neg_binomial_lcdf', - 'neg_binomial_lpmf', - 'neg_binomial_lupmf', - 'neg_binomial_rng', - 'negative_infinity', - 'norm', - 'normal', - 'normal_cdf', - 'normal_id_glm', - 'normal_lccdf', - 'normal_lcdf', - 'normal_lpdf', - 'normal_lupdf', - 'normal_rng', - 'not_a_number', - 'ode_adams', - 'ode_adams_tol', - 'ode_adjoint_tol_ctl', - 'ode_bdf', - 'ode_bdf_tol', - 'ode_ckrk', - 'ode_ckrk_tol', - 'ode_rk45', - 'ode_rk45_tol', - 'one_hot_array', - 'one_hot_int_array', - 'one_hot_row_vector', - 'one_hot_vector', - 'ones_array', - 'ones_int_array', - 'ones_row_vector', - 'ones_vector', - 'operator%/%', - 'operator%', - 'operator=', - 'ordered_logistic', - 'ordered_logistic_lpmf', - 'ordered_logistic_lupmf', - 'ordered_logistic_rng', - 'ordered_probit', - 'ordered_probit_lpmf', - 'ordered_probit_lupmf', - 'ordered_probit_rng', - 'pareto', - 'pareto_cdf', - 'pareto_lccdf', - 'pareto_lcdf', - 'pareto_lpdf', - 'pareto_lupdf', - 'pareto_rng', - 'pareto_type_2', - 'pareto_type_2_cdf', - 'pareto_type_2_lccdf', - 'pareto_type_2_lcdf', - 'pareto_type_2_lpdf', - 'pareto_type_2_lupdf', - 'pareto_type_2_rng', - 'pi', - 'poisson', - 'poisson_cdf', - 'poisson_lccdf', - 'poisson_lcdf', - 'poisson_log', - 'poisson_log_glm', - 'poisson_log_lpmf', - 'poisson_log_lupmf', - 'poisson_log_rng', - 'poisson_lpmf', - 'poisson_lupmf', - 'poisson_rng', - 'polar', - 'positive_infinity', - 'print', - 'proj', - 'qr_Q', - 'qr_R', - 'qr_thin_Q', - 'qr_thin_R', - 'rayleigh', - 'rayleigh_cdf', - 'rayleigh_lccdf', - 'rayleigh_lcdf', - 'rayleigh_lpdf', - 'rayleigh_lupdf', - 'rayleigh_rng', - 'reduce_sum', - 'reject', - 'rep_row_vector', - 'rep_vector', - 'round', - 'row', - 'scale_matrix_exp_multiply', - 'scaled_inv_chi_square', - 'scaled_inv_chi_square_cdf', - 'scaled_inv_chi_square_lccdf', - 'scaled_inv_chi_square_lcdf', - 'scaled_inv_chi_square_lpdf', - 'scaled_inv_chi_square_lupdf', - 'scaled_inv_chi_square_rng', - 'singular_values', - 'skew_double_exponential', - 'skew_double_exponential_cdf', - 'skew_double_exponential_lccdf', - 'skew_double_exponential_lcdf', - 'skew_double_exponential_lpdf', - 'skew_double_exponential_lupdf', - 'skew_double_exponential_rng', - 'skew_normal', - 'skew_normal_cdf', - 'skew_normal_lccdf', - 'skew_normal_lcdf', - 'skew_normal_lpdf', - 'skew_normal_lupdf', - 'skew_normal_rng', - 'softmax', - 'sqrt2', - 'square', - 'std_normal', - 'std_normal_cdf', - 'std_normal_lccdf', - 'std_normal_lcdf', - 'std_normal_lpdf', - 'std_normal_lupdf', - 'std_normal_rng', - 'step', - 'student_t', - 'student_t_cdf', - 'student_t_lccdf', - 'student_t_lcdf', - 'student_t_lpdf', - 'student_t_lupdf', - 'student_t_rng', - 'sub_col', - 'sub_row', - 'svd_U', - 'svd_V', - 'symmetrize_from_lower_tri', - 'target', - 'tcrossprod', - 'tgamma', - 'to_array_2d', - 'trace', - 'trace_gen_quad_form', - 'trace_quad_form', - 'trigamma', - 'trunc', - 'uniform', - 'uniform_cdf', - 'uniform_lccdf', - 'uniform_lcdf', - 'uniform_lpdf', - 'uniform_lupdf', - 'uniform_rng', - 'uniform_simplex', - 'von_mises', - 'von_mises_lpdf', - 'von_mises_lupdf', - 'von_mises_rng', - 'weibull', - 'weibull_cdf', - 'weibull_lccdf', - 'weibull_lcdf', - 'weibull_lpdf', - 'weibull_lupdf', - 'weibull_rng', - 'wiener', - 'wiener_lpdf', - 'wiener_lupdf', - 'wishart', - 'wishart_lpdf', - 'wishart_lupdf', - 'wishart_rng', - 'zeros_array', - 'zeros_int_array' + // then cd into it and run this bash script https://gist.github.com/spinkney/b876a13ca63370364749622120fa71d6 + // + // the output files are + // distributions_quoted.txt + // functions_quoted.txt + + const FUNCTIONS = [ + 'Phi', + 'Phi_approx', + 'abs', + 'acos', + 'acosh', + 'add_diag', + 'algebra_solver', + 'algebra_solver_newton', + 'append_array', + 'append_col', + 'append_row', + 'asin', + 'asinh', + 'atan', + 'atan2', + 'atanh', + 'bessel_first_kind', + 'bessel_second_kind', + 'binary_log_loss', + 'binomial_coefficient_log', + 'block', + 'cbrt', + 'ceil', + 'chol2inv', + 'cholesky_decompose', + 'choose', + 'col', + 'cols', + 'columns_dot_product', + 'columns_dot_self', + 'conj', + 'cos', + 'cosh', + 'cov_exp_quad', + 'crossprod', + 'csr_extract_u', + 'csr_extract_v', + 'csr_extract_w', + 'csr_matrix_times_vector', + 'csr_to_dense_matrix', + 'cumulative_sum', + 'determinant', + 'diag_matrix', + 'diag_post_multiply', + 'diag_pre_multiply', + 'diagonal', + 'digamma', + 'dims', + 'distance', + 'dot_product', + 'dot_self', + 'eigenvalues_sym', + 'eigenvectors_sym', + 'erf', + 'erfc', + 'exp', + 'exp2', + 'expm1', + 'fabs', + 'falling_factorial', + 'fdim', + 'floor', + 'fma', + 'fmax', + 'fmin', + 'fmod', + 'gamma_p', + 'gamma_q', + 'generalized_inverse', + 'get_imag', + 'get_lp', + 'get_real', + 'head', + 'hmm_hidden_state_prob', + 'hmm_marginal', + 'hypot', + 'identity_matrix', + 'inc_beta', + 'int_step', + 'integrate_1d', + 'integrate_ode', + 'integrate_ode_adams', + 'integrate_ode_bdf', + 'integrate_ode_rk45', + 'inv', + 'inv_Phi', + 'inv_cloglog', + 'inv_logit', + 'inv_sqrt', + 'inv_square', + 'inverse', + 'inverse_spd', + 'is_inf', + 'is_nan', + 'lambert_w0', + 'lambert_wm1', + 'lbeta', + 'lchoose', + 'ldexp', + 'lgamma', + 'linspaced_array', + 'linspaced_int_array', + 'linspaced_row_vector', + 'linspaced_vector', + 'lmgamma', + 'lmultiply', + 'log', + 'log1m', + 'log1m_exp', + 'log1m_inv_logit', + 'log1p', + 'log1p_exp', + 'log_determinant', + 'log_diff_exp', + 'log_falling_factorial', + 'log_inv_logit', + 'log_inv_logit_diff', + 'log_mix', + 'log_modified_bessel_first_kind', + 'log_rising_factorial', + 'log_softmax', + 'log_sum_exp', + 'logit', + 'machine_precision', + 'map_rect', + 'matrix_exp', + 'matrix_exp_multiply', + 'matrix_power', + 'max', + 'mdivide_left_spd', + 'mdivide_left_tri_low', + 'mdivide_right_spd', + 'mdivide_right_tri_low', + 'mean', + 'min', + 'modified_bessel_first_kind', + 'modified_bessel_second_kind', + 'multiply_log', + 'multiply_lower_tri_self_transpose', + 'negative_infinity', + 'norm', + 'not_a_number', + 'num_elements', + 'ode_adams', + 'ode_adams_tol', + 'ode_adjoint_tol_ctl', + 'ode_bdf', + 'ode_bdf_tol', + 'ode_ckrk', + 'ode_ckrk_tol', + 'ode_rk45', + 'ode_rk45_tol', + 'one_hot_array', + 'one_hot_int_array', + 'one_hot_row_vector', + 'one_hot_vector', + 'ones_array', + 'ones_int_array', + 'ones_row_vector', + 'ones_vector', + 'owens_t', + 'polar', + 'positive_infinity', + 'pow', + 'print', + 'prod', + 'proj', + 'qr_Q', + 'qr_R', + 'qr_thin_Q', + 'qr_thin_R', + 'quad_form', + 'quad_form_diag', + 'quad_form_sym', + 'quantile', + 'rank', + 'reduce_sum', + 'reject', + 'rep_array', + 'rep_matrix', + 'rep_row_vector', + 'rep_vector', + 'reverse', + 'rising_factorial', + 'round', + 'row', + 'rows', + 'rows_dot_product', + 'rows_dot_self', + 'scale_matrix_exp_multiply', + 'sd', + 'segment', + 'sin', + 'singular_values', + 'sinh', + 'size', + 'softmax', + 'sort_asc', + 'sort_desc', + 'sort_indices_asc', + 'sort_indices_desc', + 'sqrt', + 'square', + 'squared_distance', + 'step', + 'sub_col', + 'sub_row', + 'sum', + 'svd_U', + 'svd_V', + 'symmetrize_from_lower_tri', + 'tail', + 'tan', + 'tanh', + 'target', + 'tcrossprod', + 'tgamma', + 'to_array_1d', + 'to_array_2d', + 'to_complex', + 'to_matrix', + 'to_row_vector', + 'to_vector', + 'trace', + 'trace_gen_quad_form', + 'trace_quad_form', + 'trigamma', + 'trunc', + 'uniform_simplex', + 'variance', + 'zeros_array', + 'zeros_int_array', + 'zeros_row_vector' ]; - - // handle symbols separately or else - // e and other symbols will be highlighted - // when used as variables - const symbols = ['pi', 'e', 'sqrt2', 'log2', 'log10'] - var FUNCTIONS_NO_SYMBOLS = FUNCTIONS; - FUNCTIONS_NO_SYMBOLS = FUNCTIONS_NO_SYMBOLS.filter(function(val){ - return (symbols.indexOf(val) == -1 ? true : false) - }) + const DISTRIBUTIONS = [ + 'bernoulli', + 'bernoulli_logit', + 'bernoulli_logit_glm', + 'beta', + 'beta_binomial', + 'beta_proportion', + 'binomial', + 'binomial_logit', + 'categorical', + 'categorical_logit', + 'categorical_logit_glm', + 'cauchy', + 'chi_square', + 'dirichlet', + 'discrete_range', + 'double_exponential', + 'exp_mod_normal', + 'exponential', + 'frechet', + 'gamma', + 'gaussian_dlm_obs', + 'gumbel', + 'hmm_latent', + 'hypergeometric', + 'inv_chi_square', + 'inv_gamma', + 'inv_wishart', + 'lkj_corr', + 'lkj_corr_cholesky', + 'logistic', + 'lognormal', + 'multi_gp', + 'multi_gp_cholesky', + 'multi_normal', + 'multi_normal_cholesky', + 'multi_normal_prec', + 'multi_student_t', + 'multinomial', + 'multinomial_logit', + 'neg_binomial', + 'neg_binomial_2', + 'neg_binomial_2_log', + 'neg_binomial_2_log_glm', + 'normal', + 'normal_id_glm', + 'ordered_logistic', + 'ordered_logistic_glm', + 'ordered_probit', + 'pareto', + 'pareto_type_2', + 'poisson', + 'poisson_log', + 'poisson_log_glm', + 'rayleigh', + 'scaled_inv_chi_square', + 'skew_double_exponential', + 'skew_normal', + 'std_normal', + 'student_t', + 'uniform', + 'von_mises', + 'weibull', + 'wiener', + 'wishart' + ]; + + const DISTRIBUTIONS_EXPANDED = DISTRIBUTIONS.flatMap(name => [ + `${name}_lpdf`, + `${name}_lpmf`, + `${name}_lccdf`, + `${name}_lupdf`, + `${name}_lcdf`, + `${name}_cdf`, + `${name}_lupdf`, + `${name}_qf` +] +) - const BLOCK_COMMENT = hljs.COMMENT( + const BLOCK_COMMENT = hljs.COMMENT( /\/\*/, /\*\//, { @@ -567,49 +420,29 @@ export default function(hljs) { $pattern: hljs.IDENT_RE, title: BLOCKS, keyword: STATEMENTS.concat(VAR_TYPES), - built_in: FUNCTIONS_NO_SYMBOLS + built_in: FUNCTIONS }, contains: [ hljs.C_LINE_COMMENT_MODE, INCLUDE, hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, - // { - // Uses lookback! Uncomment once lookbacks are allowed - // handles distributions in a bit better way by looking - // back for either ~ or += instead of just relying on - // the user to have placed those - - // handle user defined distributions - // className: 'built_in', - // variants: [ - // { - // works when using a '~' to declare the target adjustment - // begin: /(?<=~)\s+(\w+)/ - // }, - // { - // works when using '+=' to declare the target adjustment - // begin: /(?<=\+=)\s+(\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf))/ - // } - // ], - // keywords: DISTRIBUTIONS.concat(DISTRIBUTIONS_EXPANDED) - // }, - { - className: 'built_in', - begin: '\\s*(' + hljs.IDENT_RE + ')(?=\\()' - }, { className: 'symbol', begin: /\s(pi|e|sqrt2|log2|log10)(?=\()/, relevance: 0 }, - // Uses lookback! Uncomment once lookbacks are allowed - // correct highlighting for truncation while - // allowing an array named T to not be highlighted - // { - // begin: /(?<=\))\s*T(?=\s*\[)/ - // keywords: 'T' - // }, + { + // highlights truncation T + // there must be a word with parenthesis ending prior to T[ + begin: [ + /\w+\(.*\)\s*/, + /T(?=\s*\[)/ + ], + beginScope: { + 2: "keyword" + }, + }, { // hack: in range constraints, lower must follow either , or < // or @@ -638,6 +471,14 @@ export default function(hljs) { className: 'keyword', begin: /\btarget\s*/, }, + { + begin: '~\\s*(' + hljs.IDENT_RE + ')\\s*\\(', + keywords: DISTRIBUTIONS + }, + { + begin: '\\s*(' + hljs.IDENT_RE + ')((_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf))\\s*\\(', + keywords: DISTRIBUTIONS_EXPANDED + }, { className: 'number', variants: [ From 616ad0d31ab43a73633d441a3ee0531127f19fb2 Mon Sep 17 00:00:00 2001 From: spinkney Date: Sat, 4 Dec 2021 14:04:06 -0500 Subject: [PATCH 17/31] everything works without lookbacks --- src/languages/stan.js | 60 +++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index ba20ac1e51..a21feb2c6d 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -358,19 +358,7 @@ export default function(hljs) { 'wiener', 'wishart' ]; - - const DISTRIBUTIONS_EXPANDED = DISTRIBUTIONS.flatMap(name => [ - `${name}_lpdf`, - `${name}_lpmf`, - `${name}_lccdf`, - `${name}_lupdf`, - `${name}_lcdf`, - `${name}_cdf`, - `${name}_lupdf`, - `${name}_qf` -] -) - + const BLOCK_COMMENT = hljs.COMMENT( /\/\*/, /\*\//, @@ -432,17 +420,6 @@ export default function(hljs) { begin: /\s(pi|e|sqrt2|log2|log10)(?=\()/, relevance: 0 }, - { - // highlights truncation T - // there must be a word with parenthesis ending prior to T[ - begin: [ - /\w+\(.*\)\s*/, - /T(?=\s*\[)/ - ], - beginScope: { - 2: "keyword" - }, - }, { // hack: in range constraints, lower must follow either , or < // or @@ -472,13 +449,36 @@ export default function(hljs) { begin: /\btarget\s*/, }, { - begin: '~\\s*(' + hljs.IDENT_RE + ')\\s*\\(', - keywords: DISTRIBUTIONS - }, + // highlights the 'T' in T[,] for only Stan language distributrions + begin: [ + /~\s*/, + hljs.regex.either(...DISTRIBUTIONS), + // /[a-z_A-Z0-9]*/, + /(?:\(\))/, + /\s*T(?=\s*\[)/ + ], + beginScope: { + 2: "built_in", + 4: "keyword" + }, + keywords: DISTRIBUTIONS + }, + { + // highlights distributrions that end with special endings + className: 'built_in', + begin: /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ + }, { - begin: '\\s*(' + hljs.IDENT_RE + ')((_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf))\\s*\\(', - keywords: DISTRIBUTIONS_EXPANDED - }, + // highlights user defined distributions + begin: [ + /~/, + /\s+/, + /\w+(?=\s*[\(.*\)])/ + ], + beginScope: { + 3: "built_in" + }, + }, { className: 'number', variants: [ From 893be6e77c50fbd38d86c02e2fb5738cfb03881e Mon Sep 17 00:00:00 2001 From: spinkney Date: Sun, 5 Dec 2021 05:06:41 -0500 Subject: [PATCH 18/31] do not highlight _ distributions if after ~ --- src/languages/stan.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index a21feb2c6d..f168ce0ba6 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -468,11 +468,21 @@ export default function(hljs) { className: 'built_in', begin: /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ }, + { + // cannot ever have these endings with the ~ + begin: [ + /~\s*/, + /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ + ], + beginScope: { + 2: "illegal" + } + }, { // highlights user defined distributions begin: [ /~/, - /\s+/, + /\s*/, /\w+(?=\s*[\(.*\)])/ ], beginScope: { From 00b5493c90d4b64af83e4d99895308d064778abd Mon Sep 17 00:00:00 2001 From: spinkney Date: Sun, 5 Dec 2021 05:39:29 -0500 Subject: [PATCH 19/31] minor cleanup --- src/languages/stan.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index f168ce0ba6..aa3e87ca8c 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -453,15 +453,13 @@ export default function(hljs) { begin: [ /~\s*/, hljs.regex.either(...DISTRIBUTIONS), - // /[a-z_A-Z0-9]*/, /(?:\(\))/, /\s*T(?=\s*\[)/ ], beginScope: { 2: "built_in", 4: "keyword" - }, - keywords: DISTRIBUTIONS + } }, { // highlights distributrions that end with special endings From 8d5b59fa5dc48a68f6eefb7dd314f4622a893381 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Mon, 6 Dec 2021 19:15:43 -0500 Subject: [PATCH 20/31] eslint --fix --- src/languages/stan.js | 698 +++++++++++++++++++++--------------------- 1 file changed, 345 insertions(+), 353 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index aa3e87ca8c..a95ab8f3eb 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -48,318 +48,318 @@ export default function(hljs) { 'void' ]; - // to get the functions list - // clone the [stan-docs repo](https://github.com/stan-dev/docs) - // then cd into it and run this bash script https://gist.github.com/spinkney/b876a13ca63370364749622120fa71d6 - // - // the output files are - // distributions_quoted.txt - // functions_quoted.txt + // to get the functions list + // clone the [stan-docs repo](https://github.com/stan-dev/docs) + // then cd into it and run this bash script https://gist.github.com/spinkney/b876a13ca63370364749622120fa71d6 + // + // the output files are + // distributions_quoted.txt + // functions_quoted.txt - const FUNCTIONS = [ - 'Phi', - 'Phi_approx', - 'abs', - 'acos', - 'acosh', - 'add_diag', - 'algebra_solver', - 'algebra_solver_newton', - 'append_array', - 'append_col', - 'append_row', - 'asin', - 'asinh', - 'atan', - 'atan2', - 'atanh', - 'bessel_first_kind', - 'bessel_second_kind', - 'binary_log_loss', - 'binomial_coefficient_log', - 'block', - 'cbrt', - 'ceil', - 'chol2inv', - 'cholesky_decompose', - 'choose', - 'col', - 'cols', - 'columns_dot_product', - 'columns_dot_self', - 'conj', - 'cos', - 'cosh', - 'cov_exp_quad', - 'crossprod', - 'csr_extract_u', - 'csr_extract_v', - 'csr_extract_w', - 'csr_matrix_times_vector', - 'csr_to_dense_matrix', - 'cumulative_sum', - 'determinant', - 'diag_matrix', - 'diag_post_multiply', - 'diag_pre_multiply', - 'diagonal', - 'digamma', - 'dims', - 'distance', - 'dot_product', - 'dot_self', - 'eigenvalues_sym', - 'eigenvectors_sym', - 'erf', - 'erfc', - 'exp', - 'exp2', - 'expm1', - 'fabs', - 'falling_factorial', - 'fdim', - 'floor', - 'fma', - 'fmax', - 'fmin', - 'fmod', - 'gamma_p', - 'gamma_q', - 'generalized_inverse', - 'get_imag', - 'get_lp', - 'get_real', - 'head', - 'hmm_hidden_state_prob', - 'hmm_marginal', - 'hypot', - 'identity_matrix', - 'inc_beta', - 'int_step', - 'integrate_1d', - 'integrate_ode', - 'integrate_ode_adams', - 'integrate_ode_bdf', - 'integrate_ode_rk45', - 'inv', - 'inv_Phi', - 'inv_cloglog', - 'inv_logit', - 'inv_sqrt', - 'inv_square', - 'inverse', - 'inverse_spd', - 'is_inf', - 'is_nan', - 'lambert_w0', - 'lambert_wm1', - 'lbeta', - 'lchoose', - 'ldexp', - 'lgamma', - 'linspaced_array', - 'linspaced_int_array', - 'linspaced_row_vector', - 'linspaced_vector', - 'lmgamma', - 'lmultiply', - 'log', - 'log1m', - 'log1m_exp', - 'log1m_inv_logit', - 'log1p', - 'log1p_exp', - 'log_determinant', - 'log_diff_exp', - 'log_falling_factorial', - 'log_inv_logit', - 'log_inv_logit_diff', - 'log_mix', - 'log_modified_bessel_first_kind', - 'log_rising_factorial', - 'log_softmax', - 'log_sum_exp', - 'logit', - 'machine_precision', - 'map_rect', - 'matrix_exp', - 'matrix_exp_multiply', - 'matrix_power', - 'max', - 'mdivide_left_spd', - 'mdivide_left_tri_low', - 'mdivide_right_spd', - 'mdivide_right_tri_low', - 'mean', - 'min', - 'modified_bessel_first_kind', - 'modified_bessel_second_kind', - 'multiply_log', - 'multiply_lower_tri_self_transpose', - 'negative_infinity', - 'norm', - 'not_a_number', - 'num_elements', - 'ode_adams', - 'ode_adams_tol', - 'ode_adjoint_tol_ctl', - 'ode_bdf', - 'ode_bdf_tol', - 'ode_ckrk', - 'ode_ckrk_tol', - 'ode_rk45', - 'ode_rk45_tol', - 'one_hot_array', - 'one_hot_int_array', - 'one_hot_row_vector', - 'one_hot_vector', - 'ones_array', - 'ones_int_array', - 'ones_row_vector', - 'ones_vector', - 'owens_t', - 'polar', - 'positive_infinity', - 'pow', - 'print', - 'prod', - 'proj', - 'qr_Q', - 'qr_R', - 'qr_thin_Q', - 'qr_thin_R', - 'quad_form', - 'quad_form_diag', - 'quad_form_sym', - 'quantile', - 'rank', - 'reduce_sum', - 'reject', - 'rep_array', - 'rep_matrix', - 'rep_row_vector', - 'rep_vector', - 'reverse', - 'rising_factorial', - 'round', - 'row', - 'rows', - 'rows_dot_product', - 'rows_dot_self', - 'scale_matrix_exp_multiply', - 'sd', - 'segment', - 'sin', - 'singular_values', - 'sinh', - 'size', - 'softmax', - 'sort_asc', - 'sort_desc', - 'sort_indices_asc', - 'sort_indices_desc', - 'sqrt', - 'square', - 'squared_distance', - 'step', - 'sub_col', - 'sub_row', - 'sum', - 'svd_U', - 'svd_V', - 'symmetrize_from_lower_tri', - 'tail', - 'tan', - 'tanh', - 'target', - 'tcrossprod', - 'tgamma', - 'to_array_1d', - 'to_array_2d', - 'to_complex', - 'to_matrix', - 'to_row_vector', - 'to_vector', - 'trace', - 'trace_gen_quad_form', - 'trace_quad_form', - 'trigamma', - 'trunc', - 'uniform_simplex', - 'variance', - 'zeros_array', - 'zeros_int_array', - 'zeros_row_vector' + const FUNCTIONS = [ + 'Phi', + 'Phi_approx', + 'abs', + 'acos', + 'acosh', + 'add_diag', + 'algebra_solver', + 'algebra_solver_newton', + 'append_array', + 'append_col', + 'append_row', + 'asin', + 'asinh', + 'atan', + 'atan2', + 'atanh', + 'bessel_first_kind', + 'bessel_second_kind', + 'binary_log_loss', + 'binomial_coefficient_log', + 'block', + 'cbrt', + 'ceil', + 'chol2inv', + 'cholesky_decompose', + 'choose', + 'col', + 'cols', + 'columns_dot_product', + 'columns_dot_self', + 'conj', + 'cos', + 'cosh', + 'cov_exp_quad', + 'crossprod', + 'csr_extract_u', + 'csr_extract_v', + 'csr_extract_w', + 'csr_matrix_times_vector', + 'csr_to_dense_matrix', + 'cumulative_sum', + 'determinant', + 'diag_matrix', + 'diag_post_multiply', + 'diag_pre_multiply', + 'diagonal', + 'digamma', + 'dims', + 'distance', + 'dot_product', + 'dot_self', + 'eigenvalues_sym', + 'eigenvectors_sym', + 'erf', + 'erfc', + 'exp', + 'exp2', + 'expm1', + 'fabs', + 'falling_factorial', + 'fdim', + 'floor', + 'fma', + 'fmax', + 'fmin', + 'fmod', + 'gamma_p', + 'gamma_q', + 'generalized_inverse', + 'get_imag', + 'get_lp', + 'get_real', + 'head', + 'hmm_hidden_state_prob', + 'hmm_marginal', + 'hypot', + 'identity_matrix', + 'inc_beta', + 'int_step', + 'integrate_1d', + 'integrate_ode', + 'integrate_ode_adams', + 'integrate_ode_bdf', + 'integrate_ode_rk45', + 'inv', + 'inv_Phi', + 'inv_cloglog', + 'inv_logit', + 'inv_sqrt', + 'inv_square', + 'inverse', + 'inverse_spd', + 'is_inf', + 'is_nan', + 'lambert_w0', + 'lambert_wm1', + 'lbeta', + 'lchoose', + 'ldexp', + 'lgamma', + 'linspaced_array', + 'linspaced_int_array', + 'linspaced_row_vector', + 'linspaced_vector', + 'lmgamma', + 'lmultiply', + 'log', + 'log1m', + 'log1m_exp', + 'log1m_inv_logit', + 'log1p', + 'log1p_exp', + 'log_determinant', + 'log_diff_exp', + 'log_falling_factorial', + 'log_inv_logit', + 'log_inv_logit_diff', + 'log_mix', + 'log_modified_bessel_first_kind', + 'log_rising_factorial', + 'log_softmax', + 'log_sum_exp', + 'logit', + 'machine_precision', + 'map_rect', + 'matrix_exp', + 'matrix_exp_multiply', + 'matrix_power', + 'max', + 'mdivide_left_spd', + 'mdivide_left_tri_low', + 'mdivide_right_spd', + 'mdivide_right_tri_low', + 'mean', + 'min', + 'modified_bessel_first_kind', + 'modified_bessel_second_kind', + 'multiply_log', + 'multiply_lower_tri_self_transpose', + 'negative_infinity', + 'norm', + 'not_a_number', + 'num_elements', + 'ode_adams', + 'ode_adams_tol', + 'ode_adjoint_tol_ctl', + 'ode_bdf', + 'ode_bdf_tol', + 'ode_ckrk', + 'ode_ckrk_tol', + 'ode_rk45', + 'ode_rk45_tol', + 'one_hot_array', + 'one_hot_int_array', + 'one_hot_row_vector', + 'one_hot_vector', + 'ones_array', + 'ones_int_array', + 'ones_row_vector', + 'ones_vector', + 'owens_t', + 'polar', + 'positive_infinity', + 'pow', + 'print', + 'prod', + 'proj', + 'qr_Q', + 'qr_R', + 'qr_thin_Q', + 'qr_thin_R', + 'quad_form', + 'quad_form_diag', + 'quad_form_sym', + 'quantile', + 'rank', + 'reduce_sum', + 'reject', + 'rep_array', + 'rep_matrix', + 'rep_row_vector', + 'rep_vector', + 'reverse', + 'rising_factorial', + 'round', + 'row', + 'rows', + 'rows_dot_product', + 'rows_dot_self', + 'scale_matrix_exp_multiply', + 'sd', + 'segment', + 'sin', + 'singular_values', + 'sinh', + 'size', + 'softmax', + 'sort_asc', + 'sort_desc', + 'sort_indices_asc', + 'sort_indices_desc', + 'sqrt', + 'square', + 'squared_distance', + 'step', + 'sub_col', + 'sub_row', + 'sum', + 'svd_U', + 'svd_V', + 'symmetrize_from_lower_tri', + 'tail', + 'tan', + 'tanh', + 'target', + 'tcrossprod', + 'tgamma', + 'to_array_1d', + 'to_array_2d', + 'to_complex', + 'to_matrix', + 'to_row_vector', + 'to_vector', + 'trace', + 'trace_gen_quad_form', + 'trace_quad_form', + 'trigamma', + 'trunc', + 'uniform_simplex', + 'variance', + 'zeros_array', + 'zeros_int_array', + 'zeros_row_vector' ]; - const DISTRIBUTIONS = [ - 'bernoulli', - 'bernoulli_logit', - 'bernoulli_logit_glm', - 'beta', - 'beta_binomial', - 'beta_proportion', - 'binomial', - 'binomial_logit', - 'categorical', - 'categorical_logit', - 'categorical_logit_glm', - 'cauchy', - 'chi_square', - 'dirichlet', - 'discrete_range', - 'double_exponential', - 'exp_mod_normal', - 'exponential', - 'frechet', - 'gamma', - 'gaussian_dlm_obs', - 'gumbel', - 'hmm_latent', - 'hypergeometric', - 'inv_chi_square', - 'inv_gamma', - 'inv_wishart', - 'lkj_corr', - 'lkj_corr_cholesky', - 'logistic', - 'lognormal', - 'multi_gp', - 'multi_gp_cholesky', - 'multi_normal', - 'multi_normal_cholesky', - 'multi_normal_prec', - 'multi_student_t', - 'multinomial', - 'multinomial_logit', - 'neg_binomial', - 'neg_binomial_2', - 'neg_binomial_2_log', - 'neg_binomial_2_log_glm', - 'normal', - 'normal_id_glm', - 'ordered_logistic', - 'ordered_logistic_glm', - 'ordered_probit', - 'pareto', - 'pareto_type_2', - 'poisson', - 'poisson_log', - 'poisson_log_glm', - 'rayleigh', - 'scaled_inv_chi_square', - 'skew_double_exponential', - 'skew_normal', - 'std_normal', - 'student_t', - 'uniform', - 'von_mises', - 'weibull', - 'wiener', - 'wishart' - ]; - - const BLOCK_COMMENT = hljs.COMMENT( + const DISTRIBUTIONS = [ + 'bernoulli', + 'bernoulli_logit', + 'bernoulli_logit_glm', + 'beta', + 'beta_binomial', + 'beta_proportion', + 'binomial', + 'binomial_logit', + 'categorical', + 'categorical_logit', + 'categorical_logit_glm', + 'cauchy', + 'chi_square', + 'dirichlet', + 'discrete_range', + 'double_exponential', + 'exp_mod_normal', + 'exponential', + 'frechet', + 'gamma', + 'gaussian_dlm_obs', + 'gumbel', + 'hmm_latent', + 'hypergeometric', + 'inv_chi_square', + 'inv_gamma', + 'inv_wishart', + 'lkj_corr', + 'lkj_corr_cholesky', + 'logistic', + 'lognormal', + 'multi_gp', + 'multi_gp_cholesky', + 'multi_normal', + 'multi_normal_cholesky', + 'multi_normal_prec', + 'multi_student_t', + 'multinomial', + 'multinomial_logit', + 'neg_binomial', + 'neg_binomial_2', + 'neg_binomial_2_log', + 'neg_binomial_2_log_glm', + 'normal', + 'normal_id_glm', + 'ordered_logistic', + 'ordered_logistic_glm', + 'ordered_probit', + 'pareto', + 'pareto_type_2', + 'poisson', + 'poisson_log', + 'poisson_log_glm', + 'rayleigh', + 'scaled_inv_chi_square', + 'skew_double_exponential', + 'skew_normal', + 'std_normal', + 'student_t', + 'uniform', + 'von_mises', + 'weibull', + 'wiener', + 'wishart' + ]; + + const BLOCK_COMMENT = hljs.COMMENT( /\/\*/, /\*\//, { @@ -387,19 +387,19 @@ export default function(hljs) { ] }; - var numberRegex = new RegExp([ + const numberRegex = new RegExp([ // Comes from @RunDevelopment accessed 11/29/2021 at // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 - - // start of big noncapture group which + + // start of big noncapture group which // 1. gets numbers that are by themselves // 2. numbers that are separated by _ // 3. numbers that are separted by . - /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)/ + /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)/, // grabs scientific notation // grabs complex numbers with i - ,/(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/ - ].map(function(r) {return r.source}).join(''), 'i'); + /(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/ + ].map(function(r) { return r.source; }).join(''), 'i'); return { name: 'Stan', @@ -415,11 +415,11 @@ export default function(hljs) { INCLUDE, hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, - { - className: 'symbol', - begin: /\s(pi|e|sqrt2|log2|log10)(?=\()/, - relevance: 0 - }, + { + className: 'symbol', + begin: /\s(pi|e|sqrt2|log2|log10)(?=\()/, + relevance: 0 + }, { // hack: in range constraints, lower must follow either , or < // or @@ -450,52 +450,44 @@ export default function(hljs) { }, { // highlights the 'T' in T[,] for only Stan language distributrions - begin: [ - /~\s*/, - hljs.regex.either(...DISTRIBUTIONS), - /(?:\(\))/, - /\s*T(?=\s*\[)/ - ], - beginScope: { - 2: "built_in", - 4: "keyword" - } - }, - { + begin: [ + /~\s*/, + hljs.regex.either(...DISTRIBUTIONS), + /(?:\(\))/, + /\s*T(?=\s*\[)/ + ], + beginScope: { + 2: "built_in", + 4: "keyword" + } + }, + { // highlights distributrions that end with special endings - className: 'built_in', - begin: /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ - }, + className: 'built_in', + begin: /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ + }, { // cannot ever have these endings with the ~ begin: [ /~\s*/, /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ ], - beginScope: { - 2: "illegal" - } + beginScope: { 2: "illegal" } }, { // highlights user defined distributions - begin: [ - /~/, - /\s*/, - /\w+(?=\s*[\(.*\)])/ - ], - beginScope: { - 3: "built_in" - }, - }, + begin: [ + /~/, + /\s*/, + /\w+(?=\s*[\(.*\)])/ + ], + beginScope: { 3: "built_in" }, + }, { className: 'number', variants: [ - { - begin: numberRegex - }, - { - begin: /\.\d+(?:[eE][+-]?\d+)?\b/ - } + { begin: numberRegex }, + { begin: /\.\d+(?:[eE][+-]?\d+)?\b/ } ], relevance: 0 }, From 0f181b0694e98f6931ea9655dfbd40a6a6da16af Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Mon, 6 Dec 2021 19:19:20 -0500 Subject: [PATCH 21/31] update test output --- test/markup/stan/default.expect.txt | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/markup/stan/default.expect.txt b/test/markup/stan/default.expect.txt index 42e5382e37..1a722a197f 100644 --- a/test/markup/stan/default.expect.txt +++ b/test/markup/stan/default.expect.txt @@ -1,5 +1,5 @@ functions { - #include normal_copula.stanfunctions + #include normal_copula.stanfunctions } data { int<lower=0> N; @@ -9,8 +9,8 @@ transformed data { complex zi = 1+3.14i; zi = zi * 0i; - complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); - real x = get_real(3i - 40e-3i + 1e10i); + complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); + real x = get_real(3i - 40e-3i + 1e10i); }› parameters { array[K - 1] real mu; @@ -18,23 +18,23 @@ cholesky_factor_corr[K] L; } model { - target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]); - target += gumbel_lpdf(x[ : , 2] | mu[2], sigma[2]); - target += lognormal_lpdf(x[ : , 3] | mu[3], sigma[3]); - target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]); + target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]); + target += gumbel_lpdf(x[ : , 2] | mu[2], sigma[2]); + target += lognormal_lpdf(x[ : , 3] | mu[3], sigma[3]); + target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]); { matrix[K, N] y; - for (n in 1 : N) { - y[1, n] = inv_Phi(normal_cdf(x[n, 1] | mu[1], sigma[1])); - y[2, n] = inv_Phi(gumbel_cdf(x[n, 2] | mu[2], sigma[2])); - y[3, n] = inv_Phi(lognormal_cdf(x[n, 3] | mu[3], sigma[3])); - y[4, n] = inv_Phi(weibull_cdf(x[n, 4] | sigma[4], sigma[5])); + for (n in 1 : N) { + y[1, n] = inv_Phi(normal_cdf(x[n, 1] | mu[1], sigma[1])); + y[2, n] = inv_Phi(gumbel_cdf(x[n, 2] | mu[2], sigma[2])); + y[3, n] = inv_Phi(lognormal_cdf(x[n, 3] | mu[3], sigma[3])); + y[4, n] = inv_Phi(weibull_cdf(x[n, 4] | sigma[4], sigma[5])); } - y ~ multi_normal(L); + y ~ multi_normal(L); } } generated quantities { - matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); + matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); } \ No newline at end of file From 54f14dc459f86f1275be2f8d940e75182efa3b63 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Mon, 6 Dec 2021 19:25:10 -0500 Subject: [PATCH 22/31] simplify --- src/languages/stan.js | 70 +++++++++++------------------ test/markup/stan/default.expect.txt | 22 ++++----- 2 files changed, 37 insertions(+), 55 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index a95ab8f3eb..b0ec9fb050 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -7,6 +7,7 @@ Category: scientific */ export default function(hljs) { + const regex = hljs.regex; // variable names cannot conflict with block identifiers const BLOCKS = [ 'functions', @@ -29,7 +30,7 @@ export default function(hljs) { 'return' ]; - const VAR_TYPES = [ + const TYPES = [ 'array', 'complex', 'int', @@ -377,7 +378,6 @@ export default function(hljs) { className: 'meta', begin: /#include\b/, end: /$/, - relevance: 0, // relevance comes from keywords contains: [ { match: /[a-z][a-z-.]+/, @@ -387,19 +387,12 @@ export default function(hljs) { ] }; - const numberRegex = new RegExp([ - // Comes from @RunDevelopment accessed 11/29/2021 at - // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 - - // start of big noncapture group which - // 1. gets numbers that are by themselves - // 2. numbers that are separated by _ - // 3. numbers that are separted by . - /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)/, - // grabs scientific notation - // grabs complex numbers with i - /(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/ - ].map(function(r) { return r.source; }).join(''), 'i'); + const RANGE_CONSTRAINTS = [ + "lower", + "upper", + "offset", + "multiplier" + ]; return { name: 'Stan', @@ -407,7 +400,8 @@ export default function(hljs) { keywords: { $pattern: hljs.IDENT_RE, title: BLOCKS, - keyword: STATEMENTS.concat(VAR_TYPES), + type: TYPES, + keyword: STATEMENTS, built_in: FUNCTIONS }, contains: [ @@ -421,28 +415,8 @@ export default function(hljs) { relevance: 0 }, { - // hack: in range constraints, lower must follow either , or < - // or - begin: /[<,]\s*lower\s*=/, - keywords: 'lower' - }, - { - // hack: in range constraints, upper must follow either , or < - // or - begin: /[<,]\s*upper\s*=/, - keywords: 'upper' - }, - { - // hack: in range constraints, upper must follow either , or < - // or - begin: /[<,]\s*offset\s*=/, - keywords: 'offset' - }, - { - // hack: in range constraints, upper must follow either , or < - // or - begin: /[<,]\s*multiplier\s*=/, - keywords: 'multiplier' + begin: regex.concat(/[<,]\s*/, regex.either(...RANGE_CONSTRAINTS), /\s*=/), + keywords: RANGE_CONSTRAINTS }, { className: 'keyword', @@ -485,17 +459,25 @@ export default function(hljs) { }, { className: 'number', - variants: [ - { begin: numberRegex }, - { begin: /\.\d+(?:[eE][+-]?\d+)?\b/ } - ], + begin: regex.concat( + // Comes from @RunDevelopment accessed 11/29/2021 at + // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 + + // start of big noncapture group which + // 1. gets numbers that are by themselves + // 2. numbers that are separated by _ + // 3. numbers that are separted by . + /(?:\b\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\B\.\d+(?:_\d+)*)/, + // grabs scientific notation + // grabs complex numbers with i + /(?:[eE][+-]?\d+(?:_\d+)*)?i?(?!\w)/ + ), relevance: 0 }, { className: 'string', begin: '"', - end: '"', - relevance: 0 + end: '"' } ] }; diff --git a/test/markup/stan/default.expect.txt b/test/markup/stan/default.expect.txt index 1a722a197f..39fc99e4f5 100644 --- a/test/markup/stan/default.expect.txt +++ b/test/markup/stan/default.expect.txt @@ -2,20 +2,20 @@ #include normal_copula.stanfunctions } data { - int<lower=0> N; - int K; - matrix[N, K] x; + int<lower=0> N; + int K; + matrix[N, K] x; } transformed data { - complex zi = 1+3.14i; + complex zi = 1+3.14i; zi = zi * 0i; - complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); - real x = get_real(3i - 40e-3i + 1e10i); + complex yi = to_complex(0, 1.1) + to_complex(0.0, 2.2) + to_complex(); + real x = get_real(3i - 40e-3i + 1e10i); }› parameters { - array[K - 1] real mu; - array[K + 1] real<lower=0> sigma; - cholesky_factor_corr[K] L; + array[K - 1] real mu; + array[K + 1] real<lower=0> sigma; + cholesky_factor_corr[K] L; } model { target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]); @@ -24,7 +24,7 @@ target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]); { - matrix[K, N] y; + matrix[K, N] y; for (n in 1 : N) { y[1, n] = inv_Phi(normal_cdf(x[n, 1] | mu[1], sigma[1])); y[2, n] = inv_Phi(gumbel_cdf(x[n, 2] | mu[2], sigma[2])); @@ -36,5 +36,5 @@ } } generated quantities { - matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); + matrix[K, K] Sigma = multiply_lower_tri_self_transpose(L); } \ No newline at end of file From fc8419e054e63bd10b20d1cb099f41d27f08a763 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Tue, 7 Dec 2021 13:35:24 -0500 Subject: [PATCH 23/31] clean up grammar --- src/languages/stan.js | 50 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index b0ec9fb050..448e59f15b 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -7,7 +7,7 @@ Category: scientific */ export default function(hljs) { - const regex = hljs.regex; + const regex = regex; // variable names cannot conflict with block identifiers const BLOCKS = [ 'functions', @@ -51,7 +51,7 @@ export default function(hljs) { // to get the functions list // clone the [stan-docs repo](https://github.com/stan-dev/docs) - // then cd into it and run this bash script https://gist.github.com/spinkney/b876a13ca63370364749622120fa71d6 + // then cd into it and run this bash script https://gist.github.com/joshgoebel/dcd33f82d4059a907c986049893843cf // // the output files are // distributions_quoted.txt @@ -367,7 +367,7 @@ export default function(hljs) { relevance: 0, contains: [ { - className: 'doctag', + scope: 'doctag', match: /@(return|param)/ } ] @@ -375,13 +375,13 @@ export default function(hljs) { ); const INCLUDE = { - className: 'meta', + scope: 'meta', begin: /#include\b/, end: /$/, contains: [ { - match: /[a-z][a-z-.]+/, - className: 'string' + match: /[a-z][a-z-._]+/, + scope: 'string' }, hljs.C_LINE_COMMENT_MODE ] @@ -410,56 +410,56 @@ export default function(hljs) { hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, { - className: 'symbol', - begin: /\s(pi|e|sqrt2|log2|log10)(?=\()/, + scope: 'symbol', + match: /\s(pi|e|sqrt2|log2|log10)(?=\()/, relevance: 0 }, { - begin: regex.concat(/[<,]\s*/, regex.either(...RANGE_CONSTRAINTS), /\s*=/), + match: regex.concat(/[<,]\s*/, regex.either(...RANGE_CONSTRAINTS), /\s*=/), keywords: RANGE_CONSTRAINTS }, { - className: 'keyword', - begin: /\btarget\s*/, + scope: 'keyword', + match: /\btarget\s*/, }, { // highlights the 'T' in T[,] for only Stan language distributrions - begin: [ + match: [ /~\s*/, - hljs.regex.either(...DISTRIBUTIONS), + regex.either(...DISTRIBUTIONS), /(?:\(\))/, /\s*T(?=\s*\[)/ ], - beginScope: { + scope: { 2: "built_in", 4: "keyword" } }, { // highlights distributrions that end with special endings - className: 'built_in', - begin: /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ + scope: 'built_in', + match: /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ }, { // cannot ever have these endings with the ~ - begin: [ + match: [ /~\s*/, /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ ], - beginScope: { 2: "illegal" } + scope: { 2: "illegal" } }, { // highlights user defined distributions - begin: [ + match: [ /~/, /\s*/, /\w+(?=\s*[\(.*\)])/ ], - beginScope: { 3: "built_in" }, + scope: { 3: "built_in" }, }, { - className: 'number', - begin: regex.concat( + scope: 'number', + match: regex.concat( // Comes from @RunDevelopment accessed 11/29/2021 at // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 @@ -475,9 +475,9 @@ export default function(hljs) { relevance: 0 }, { - className: 'string', - begin: '"', - end: '"' + scope: 'string', + begin: /"/, + end: /"/ } ] }; From aa553ed7e7750c97f9e0dee9c0e7841fd02d5c51 Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 14 Dec 2021 07:28:30 -0500 Subject: [PATCH 24/31] make udf title.function, address review comments --- src/languages/stan.js | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index 448e59f15b..a328f5b053 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -7,7 +7,7 @@ Category: scientific */ export default function(hljs) { - const regex = regex; + const regex = hljs.regex; // variable names cannot conflict with block identifiers const BLOCKS = [ 'functions', @@ -410,7 +410,7 @@ export default function(hljs) { hljs.HASH_COMMENT_MODE, BLOCK_COMMENT, { - scope: 'symbol', + scope: 'built_in', match: /\s(pi|e|sqrt2|log2|log10)(?=\()/, relevance: 0 }, @@ -436,30 +436,36 @@ export default function(hljs) { } }, { - // highlights distributrions that end with special endings + // highlights distributions that end with special endings scope: 'built_in', - match: /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ - }, - { - // cannot ever have these endings with the ~ - match: [ - /~\s*/, - /\w+(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ - ], - scope: { 2: "illegal" } + keywords: DISTRIBUTIONS, + begin: regex.concat(/\w*/, regex.either(...DISTRIBUTIONS), /(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/) }, { - // highlights user defined distributions - match: [ + // highlights user defined distributions after ~ + begin: [ /~/, /\s*/, /\w+(?=\s*[\(.*\)])/ ], - scope: { 3: "built_in" }, + scope: { 3: "title.function" }, + }, + { + // highlights user defined distributions with special endings + scope: 'title.function', + begin: /\w*(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ + }, + { + // cannot ever have these endings with the ~ + begin: [ + /~\s*/, + /\w*(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ + ], + scope: { 2: "no-highlight" } }, { scope: 'number', - match: regex.concat( + begin: regex.concat( // Comes from @RunDevelopment accessed 11/29/2021 at // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 From 45840d80f50e829396d69223e21395f5f7a0af37 Mon Sep 17 00:00:00 2001 From: spinkney Date: Tue, 14 Dec 2021 07:29:49 -0500 Subject: [PATCH 25/31] change begin to match --- src/languages/stan.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index a328f5b053..d5a2428839 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -457,7 +457,7 @@ export default function(hljs) { }, { // cannot ever have these endings with the ~ - begin: [ + match: [ /~\s*/, /\w*(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ ], @@ -465,7 +465,7 @@ export default function(hljs) { }, { scope: 'number', - begin: regex.concat( + match: regex.concat( // Comes from @RunDevelopment accessed 11/29/2021 at // https://github.com/PrismJS/prism/blob/c53ad2e65b7193ab4f03a1797506a54bbb33d5a2/components/prism-stan.js#L56 From bc70b15c58dc65200aa1599b061b533194e98675 Mon Sep 17 00:00:00 2001 From: Sean Pinkney Date: Tue, 14 Dec 2021 10:15:43 -0500 Subject: [PATCH 26/31] fix user defined distribution --- src/languages/stan.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index d5a2428839..8452db83b9 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -442,13 +442,22 @@ export default function(hljs) { begin: regex.concat(/\w*/, regex.either(...DISTRIBUTIONS), /(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/) }, { - // highlights user defined distributions after ~ + // highlights distributions after ~ begin: [ /~/, /\s*/, - /\w+(?=\s*[\(.*\)])/ + regex.concat(regex.either(...DISTRIBUTIONS), /(?=\s*[\(.*\)])/) + ], + scope: { 3: "built_in" } + }, + { + // highlights user defined distributions after ~ + begin: [ + /~/, + /\s*\w+(?=\s*[\(.*\)])/, + '(?!.*/\b(' + regex.either(...DISTRIBUTIONS) + ')\b)' ], - scope: { 3: "title.function" }, + scope: { 2: "title.function" } }, { // highlights user defined distributions with special endings From f210d0b1a3afe9c7cebfe591488ecd5cbc6be75d Mon Sep 17 00:00:00 2001 From: Sean Pinkney Date: Tue, 14 Dec 2021 12:07:07 -0500 Subject: [PATCH 27/31] fix `target` keyword match --- src/languages/stan.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index 8452db83b9..a4c8b732ca 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -420,7 +420,7 @@ export default function(hljs) { }, { scope: 'keyword', - match: /\btarget\s*/, + match: /\btarget\s*(?=\+=)/, }, { // highlights the 'T' in T[,] for only Stan language distributrions From 14e9109b0ff105361b0ded1ddf6d44472088977f Mon Sep 17 00:00:00 2001 From: Sean Pinkney Date: Tue, 14 Dec 2021 12:11:10 -0500 Subject: [PATCH 28/31] protect against highlighting _lpdf after ~ and remove scope --- src/languages/stan.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index a4c8b732ca..af20b2e7dd 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -435,6 +435,14 @@ export default function(hljs) { 4: "keyword" } }, + { + // cannot ever have these endings with the ~ + match: [ + /~\s*/, + /\w*(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ + ], + scope: { } + }, { // highlights distributions that end with special endings scope: 'built_in', @@ -464,14 +472,6 @@ export default function(hljs) { scope: 'title.function', begin: /\w*(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ }, - { - // cannot ever have these endings with the ~ - match: [ - /~\s*/, - /\w*(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ - ], - scope: { 2: "no-highlight" } - }, { scope: 'number', match: regex.concat( From ea7752a45cf6ccf58b08d24c8eea60a92288a59c Mon Sep 17 00:00:00 2001 From: Sean Pinkney Date: Tue, 14 Dec 2021 13:03:58 -0500 Subject: [PATCH 29/31] Update src/languages/stan.js Co-authored-by: Josh Goebel --- src/languages/stan.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index af20b2e7dd..fb59b935a5 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -420,7 +420,7 @@ export default function(hljs) { }, { scope: 'keyword', - match: /\btarget\s*(?=\+=)/, + match: /\btarget(?=\s*\+=)/, }, { // highlights the 'T' in T[,] for only Stan language distributrions From 118adcdb46e25cacfde10e5fab2561fc717f50fd Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Tue, 14 Dec 2021 16:55:34 -0500 Subject: [PATCH 30/31] do not worry about invalid code --- src/languages/stan.js | 8 -------- test/markup/stan/default.expect.txt | 10 +++++----- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/languages/stan.js b/src/languages/stan.js index fb59b935a5..1636359f22 100644 --- a/src/languages/stan.js +++ b/src/languages/stan.js @@ -435,14 +435,6 @@ export default function(hljs) { 4: "keyword" } }, - { - // cannot ever have these endings with the ~ - match: [ - /~\s*/, - /\w*(_lpdf|_lupdf|_lpmf|_cdf|_lcdf|_lccdf|_qf)(?=\s*[\(.*\)])/ - ], - scope: { } - }, { // highlights distributions that end with special endings scope: 'built_in', diff --git a/test/markup/stan/default.expect.txt b/test/markup/stan/default.expect.txt index 39fc99e4f5..aeb9552aa5 100644 --- a/test/markup/stan/default.expect.txt +++ b/test/markup/stan/default.expect.txt @@ -1,5 +1,5 @@ functions { - #include normal_copula.stanfunctions + #include normal_copula.stanfunctions } data { int<lower=0> N; @@ -18,10 +18,10 @@ cholesky_factor_corr[K] L; } model { - target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]); - target += gumbel_lpdf(x[ : , 2] | mu[2], sigma[2]); - target += lognormal_lpdf(x[ : , 3] | mu[3], sigma[3]); - target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]); + target += normal_lpdf(x[ : , 1] | mu[1], sigma[1]); + target += gumbel_lpdf(x[ : , 2] | mu[2], sigma[2]); + target += lognormal_lpdf(x[ : , 3] | mu[3], sigma[3]); + target += weibull_lpdf(x[ : , 4] | sigma[4], sigma[5]); { matrix[K, N] y; From 7aa2b3d7999e5933b99667ec02ce4e5191cc4760 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Tue, 14 Dec 2021 17:14:12 -0500 Subject: [PATCH 31/31] revert to es2016, no longer using flatMap --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 8e2f9baeff..0e20167ca0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { /* Basic Options */ - "target": "es2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ + "target": "es2016", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ "module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ "allowJs": true, /* Allow javascript files to be compiled. */