From 347e575487c36c7050996dd1c4347b3f0a6d1d4e Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Fri, 26 Jul 2024 20:16:51 +0000 Subject: [PATCH 1/2] Update Stan syntax highlighting --- gui/src/app/FileEditor/TextEditor.tsx | 1 - gui/src/app/FileEditor/stanLang.ts | 1305 ++++++++++++++++--------- 2 files changed, 836 insertions(+), 470 deletions(-) diff --git a/gui/src/app/FileEditor/TextEditor.tsx b/gui/src/app/FileEditor/TextEditor.tsx index 2eb3a34d..0156df67 100644 --- a/gui/src/app/FileEditor/TextEditor.tsx +++ b/gui/src/app/FileEditor/TextEditor.tsx @@ -161,7 +161,6 @@ const TextEditor: FunctionComponent = ({ defaultLanguage={language} onChange={handleChange} onMount={(editor, _) => setEditor(editor)} - theme="vs-stan" options={{ readOnly, domReadOnly: readOnly, diff --git a/gui/src/app/FileEditor/stanLang.ts b/gui/src/app/FileEditor/stanLang.ts index adf2e417..173cd341 100644 --- a/gui/src/app/FileEditor/stanLang.ts +++ b/gui/src/app/FileEditor/stanLang.ts @@ -1,489 +1,856 @@ -// from: https://github.com/highlightjs/highlight.js/blob/main/src/languages/stan.js +/* eslint-disable no-useless-escape */ -import { Monaco } from "@monaco-editor/react"; +// See https://microsoft.github.io/monaco-editor/monarch.html +// Adapted in part from https://github.com/WardBrian/vscode-stan-extension/blob/main/lang/syntaxes/stan.json -const highlightJsData = () => { - const BLOCKS = [ - "functions", - "model", - "data", - "parameters", - "quantities", - "transformed", - "generated", - ]; +import { Monaco } from "@monaco-editor/react"; +import { languages } from "monaco-editor"; - const STATEMENTS = [ - "for", - "in", - "if", - "else", - "while", - "break", - "continue", - "return", - ]; +const BLOCKS = [ + "functions", + "model", + "data", + "parameters", + "generated quantities", + "transformed data", + "transformed parameters", +]; - const TYPES = [ - "array", - "tuple", - "complex", - "int", - "real", - "vector", - "complex_vector", - "ordered", - "positive_ordered", - "simplex", - "unit_vector", - "row_vector", - "complex_row_vector", - "matrix", - "complex_matrix", - "cholesky_factor_corr|10", - "cholesky_factor_cov|10", - "corr_matrix|10", - "cov_matrix|10", - "void", - ]; +const STATEMENTS = [ + "for", + "in", + "if", + "else", + "while", + "break", + "continue", + "return", +]; - // 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/joshgoebel/dcd33f82d4059a907c986049893843cf - // - // the output files are - // distributions_quoted.txt - // functions_quoted.txt +const TYPES = [ + "array", + "tuple", + "data", + "complex", + "int", + "real", + "vector", + "complex_vector", + "ordered", + "positive_ordered", + "simplex", + "unit_vector", + "row_vector", + "complex_row_vector", + "matrix", + "complex_matrix", + "cholesky_factor_corr|10", + "cholesky_factor_cov|10", + "corr_matrix|10", + "cov_matrix|10", + "void", +]; - const FUNCTIONS = [ - "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", - "block", - "cbrt", - "ceil", - "chol2inv", - "cholesky_decompose", - "choose", - "col", - "cols", - "columns_dot_product", - "columns_dot_self", - "complex_schur_decompose", - "complex_schur_decompose_t", - "complex_schur_decompose_u", - "conj", - "cos", - "cosh", - "cov_exp_quad", - "crossprod", - "csr_extract", - "csr_extract_u", - "csr_extract_v", - "csr_extract_w", - "csr_matrix_times_vector", - "csr_to_dense_matrix", - "cumulative_sum", - "dae", - "dae_tol", - "determinant", - "diag_matrix", - "diagonal", - "diag_post_multiply", - "diag_pre_multiply", - "digamma", - "dims", - "distance", - "dot_product", - "dot_self", - "eigendecompose", - "eigendecompose_sym", - "eigenvalues", - "eigenvalues_sym", - "eigenvectors", - "eigenvectors_sym", - "erf", - "erfc", - "exp", - "exp2", - "expm1", - "falling_factorial", - "fdim", - "fft", - "fft2", - "floor", - "fma", - "fmax", - "fmin", - "fmod", - "gamma_p", - "gamma_q", - "generalized_inverse", - "get_imag", - "get_real", - "head", - "hmm_hidden_state_prob", - "hmm_marginal", - "hypot", - "identity_matrix", - "inc_beta", - "integrate_1d", - "integrate_ode", - "integrate_ode_adams", - "integrate_ode_bdf", - "integrate_ode_rk45", - "int_step", - "inv", - "inv_cloglog", - "inv_erfc", - "inverse", - "inverse_spd", - "inv_fft", - "inv_fft2", - "inv_inc_beta", - "inv_logit", - "inv_Phi", - "inv_sqrt", - "inv_square", - "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", - "logit", - "log_mix", - "log_modified_bessel_first_kind", - "log_rising_factorial", - "log_softmax", - "log_sum_exp", - "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_lower_tri_self_transpose", - "negative_infinity", - "norm", - "norm1", - "norm2", - "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", - "Phi", - "Phi_approx", - "polar", - "positive_infinity", - "pow", - "print", - "prod", - "proj", - "qr", - "qr_Q", - "qr_R", - "qr_thin", - "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", - "svd_U", - "svd_V", - "symmetrize_from_lower_tri", - "tail", - "tan", - "tanh", - "target", - "tcrossprod", - "tgamma", - "to_array_1d", - "to_array_2d", - "to_complex", - "to_int", - "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", + "arg", + "asin", + "asinh", + "atan", + "atan2", + "atanh", + "bernoulli_cdf", + "bernoulli_lccdf", + "bernoulli_lcdf", + "bernoulli_logit_glm_lpmf", + "bernoulli_logit_glm_lupmf", + "bernoulli_logit_glm_rng", + "bernoulli_logit_lpmf", + "bernoulli_logit_lupmf", + "bernoulli_logit_rng", + "bernoulli_lpmf", + "bernoulli_lupmf", + "bernoulli_rng", + "bessel_first_kind", + "bessel_second_kind", + "beta", + "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_lccdf", + "beta_proportion_lcdf", + "beta_proportion_rng", + "beta_rng", + "binary_log_loss", + "binomial_cdf", + "binomial_lccdf", + "binomial_lcdf", + "binomial_logit_glm_lpmf", + "binomial_logit_glm_lupmf", + "binomial_logit_lpmf", + "binomial_logit_lupmf", + "binomial_lpmf", + "binomial_lupmf", + "binomial_rng", + "block", + "categorical_logit_glm_lpmf", + "categorical_logit_glm_lupmf", + "categorical_logit_lpmf", + "categorical_logit_lupmf", + "categorical_logit_rng", + "categorical_lpmf", + "categorical_lupmf", + "categorical_rng", + "cauchy_cdf", + "cauchy_lccdf", + "cauchy_lcdf", + "cauchy_lpdf", + "cauchy_lupdf", + "cauchy_rng", + "cbrt", + "ceil", + "chi_square_cdf", + "chi_square_lccdf", + "chi_square_lcdf", + "chi_square_lpdf", + "chi_square_lupdf", + "chi_square_rng", + "chol2inv", + "cholesky_decompose", + "choose", + "col", + "cols", + "columns_dot_product", + "columns_dot_self", + "complex_schur_decompose", + "complex_schur_decompose_t", + "complex_schur_decompose_u", + "conj", + "cos", + "cosh", + "cov_exp_quad", + "crossprod", + "csr_extract", + "csr_extract_u", + "csr_extract_v", + "csr_extract_w", + "csr_matrix_times_vector", + "csr_to_dense_matrix", + "cumulative_sum", + "dae", + "dae_tol", + "determinant", + "diag_matrix", + "diag_post_multiply", + "diag_pre_multiply", + "diagonal", + "digamma", + "dims", + "dirichlet_lpdf", + "dirichlet_lupdf", + "dirichlet_multinomial_lpmf", + "dirichlet_multinomial_lupmf", + "dirichlet_multinomial_rng", + "dirichlet_rng", + "discrete_range_cdf", + "discrete_range_lccdf", + "discrete_range_lcdf", + "discrete_range_lpmf", + "discrete_range_lupmf", + "discrete_range_rng", + "distance", + "dot_product", + "dot_self", + "double_exponential_cdf", + "double_exponential_lccdf", + "double_exponential_lcdf", + "double_exponential_lpdf", + "double_exponential_lupdf", + "double_exponential_rng", + "e", + "eigendecompose", + "eigendecompose_sym", + "eigenvalues", + "eigenvalues_sym", + "eigenvectors", + "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_lupdf", + "exp_mod_normal_rng", + "expm1", + "exponential_cdf", + "exponential_lccdf", + "exponential_lcdf", + "exponential_lpdf", + "exponential_lupdf", + "exponential_rng", + "falling_factorial", + "fatal_error", + "fdim", + "fft", + "fft2", + "floor", + "fma", + "fmax", + "fmin", + "fmod", + "frechet_cdf", + "frechet_lccdf", + "frechet_lcdf", + "frechet_lpdf", + "frechet_lupdf", + "frechet_rng", + "gamma_cdf", + "gamma_lccdf", + "gamma_lcdf", + "gamma_lpdf", + "gamma_lupdf", + "gamma_p", + "gamma_q", + "gamma_rng", + "gaussian_dlm_obs_lpdf", + "gaussian_dlm_obs_lupdf", + "generalized_inverse", + "get_imag", + "get_real", + "gumbel_cdf", + "gumbel_lccdf", + "gumbel_lcdf", + "gumbel_lpdf", + "gumbel_lupdf", + "gumbel_rng", + "head", + "hmm_hidden_state_prob", + "hmm_latent_rng", + "hmm_marginal", + "hypergeometric_lpmf", + "hypergeometric_lupmf", + "hypergeometric_rng", + "hypot", + "identity_matrix", + "inc_beta", + "int_step", + "integrate_1d", + "integrate_ode", + "integrate_ode_adams", + "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_lupdf", + "inv_chi_square_rng", + "inv_cloglog", + "inv_erfc", + "inv_fft", + "inv_fft2", + "inv_gamma_cdf", + "inv_gamma_lccdf", + "inv_gamma_lcdf", + "inv_gamma_lpdf", + "inv_gamma_lupdf", + "inv_gamma_rng", + "inv_inc_beta", + "inv_logit", + "inv_sqrt", + "inv_square", + "inv_wishart_cholesky_lpdf", + "inv_wishart_cholesky_lupdf", + "inv_wishart_cholesky_rng", + "inv_wishart_lpdf", + "inv_wishart_lupdf", + "inv_wishart_rng", + "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", + "lkj_corr_cholesky_lpdf", + "lkj_corr_cholesky_lupdf", + "lkj_corr_cholesky_rng", + "lkj_corr_lpdf", + "lkj_corr_lupdf", + "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_inv_logit_diff", + "log_mix", + "log_modified_bessel_first_kind", + "log_rising_factorial", + "log_softmax", + "log_sum_exp", + "logistic_cdf", + "logistic_lccdf", + "logistic_lcdf", + "logistic_lpdf", + "logistic_lupdf", + "logistic_rng", + "logit", + "loglogistic_cdf", + "loglogistic_lpdf", + "loglogistic_rng", + "lognormal_cdf", + "lognormal_lccdf", + "lognormal_lcdf", + "lognormal_lpdf", + "lognormal_lupdf", + "lognormal_rng", + "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", + "multi_gp_cholesky_lpdf", + "multi_gp_cholesky_lupdf", + "multi_gp_lpdf", + "multi_gp_lupdf", + "multi_normal_cholesky_lpdf", + "multi_normal_cholesky_lupdf", + "multi_normal_cholesky_rng", + "multi_normal_lpdf", + "multi_normal_lupdf", + "multi_normal_prec_lpdf", + "multi_normal_prec_lupdf", + "multi_normal_rng", + "multi_student_cholesky_t_rng", + "multi_student_t_cholesky_lpdf", + "multi_student_t_cholesky_lupdf", + "multi_student_t_cholesky_rng", + "multi_student_t_lpdf", + "multi_student_t_lupdf", + "multi_student_t_rng", + "multinomial_logit_lpmf", + "multinomial_logit_lupmf", + "multinomial_logit_rng", + "multinomial_lpmf", + "multinomial_lupmf", + "multinomial_rng", + "multiply_lower_tri_self_transpose", + "neg_binomial_2_cdf", + "neg_binomial_2_lccdf", + "neg_binomial_2_lcdf", + "neg_binomial_2_log_glm_lpmf", + "neg_binomial_2_log_glm_lupmf", + "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", + "norm1", + "norm2", + "normal_cdf", + "normal_id_glm_lpdf", + "normal_id_glm_lupdf", + "normal_lccdf", + "normal_lcdf", + "normal_lpdf", + "normal_lupdf", + "normal_rng", + "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", + "ordered_logistic_glm_lpmf", + "ordered_logistic_glm_lupmf", + "ordered_logistic_lpmf", + "ordered_logistic_lupmf", + "ordered_logistic_rng", + "ordered_probit_lpmf", + "ordered_probit_lupmf", + "ordered_probit_rng", + "owens_t", + "pareto_cdf", + "pareto_lccdf", + "pareto_lcdf", + "pareto_lpdf", + "pareto_lupdf", + "pareto_rng", + "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_cdf", + "poisson_lccdf", + "poisson_lcdf", + "poisson_log_glm_lpmf", + "poisson_log_glm_lupmf", + "poisson_log_lpmf", + "poisson_log_lupmf", + "poisson_log_rng", + "poisson_lpmf", + "poisson_lupmf", + "poisson_rng", + "polar", + "positive_infinity", + "pow", + "prod", + "proj", + "qr", + "qr_Q", + "qr_R", + "qr_thin", + "qr_thin_Q", + "qr_thin_R", + "quad_form", + "quad_form_diag", + "quad_form_sym", + "quantile", + "rank", + "rayleigh_cdf", + "rayleigh_lccdf", + "rayleigh_lcdf", + "rayleigh_lpdf", + "rayleigh_lupdf", + "rayleigh_rng", + "reduce_sum", + "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", + "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", + "sd", + "segment", + "sin", + "singular_values", + "sinh", + "size", + "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_cdf", + "skew_normal_lccdf", + "skew_normal_lcdf", + "skew_normal_lpdf", + "skew_normal_lupdf", + "skew_normal_rng", + "softmax", + "sort_asc", + "sort_desc", + "sort_indices_asc", + "sort_indices_desc", + "sqrt", + "sqrt2", + "square", + "squared_distance", + "std_normal_cdf", + "std_normal_lccdf", + "std_normal_lcdf", + "std_normal_log_qf", + "std_normal_lpdf", + "std_normal_lupdf", + "std_normal_qf", + "std_normal_rng", + "step", + "student_t_cdf", + "student_t_lccdf", + "student_t_lcdf", + "student_t_lpdf", + "student_t_lupdf", + "student_t_rng", + "sub_col", + "sub_row", + "sum", + "svd", + "svd_U", + "svd_V", + "symmetrize_from_lower_tri", + "tail", + "tan", + "tanh", + "target", + "tcrossprod", + "tgamma", + "to_array_1d", + "to_array_2d", + "to_complex", + "to_int", + "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_lupdf", + "uniform_rng", + "uniform_simplex", + "variance", + "von_mises_cdf", + "von_mises_lccdf", + "von_mises_lcdf", + "von_mises_lpdf", + "von_mises_lupdf", + "von_mises_rng", + "weibull_cdf", + "weibull_lccdf", + "weibull_lcdf", + "weibull_lpdf", + "weibull_lupdf", + "weibull_rng", + "wiener_lpdf", + "wiener_lupdf", + "wishart_cholesky_lpdf", + "wishart_cholesky_lupdf", + "wishart_cholesky_rng", + "wishart_lpdf", + "wishart_lupdf", + "wishart_rng", + "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", - "inv_wishart_cholesky", - "lkj_corr", - "lkj_corr_cholesky", - "logistic", - "loglogistic", - "lognormal", - "multi_gp", - "multi_gp_cholesky", - "multinomial", - "multinomial_logit", - "multi_normal", - "multi_normal_cholesky", - "multi_normal_prec", - "multi_student_cholesky_t", - "multi_student_t", - "multi_student_t_cholesky", - "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", - "std_normal_log", - "student_t", - "uniform", - "von_mises", - "weibull", - "wiener", - "wishart", - "wishart_cholesky", - ]; +const SPECIAL_FUNCTIONS = ["print", "reject", "fatal_error"]; - // const BLOCK_COMMENT = hljs.COMMENT( - // /\/\*/, - // /\*\//, - // { - // relevance: 0, - // contains: [ - // { - // scope: 'doctag', - // match: /@(return|param)/ - // } - // ] - // } - // ); +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", + "inv_wishart_cholesky", + "lkj_corr", + "lkj_corr_cholesky", + "logistic", + "loglogistic", + "lognormal", + "multi_gp", + "multi_gp_cholesky", + "multinomial", + "multinomial_logit", + "multi_normal", + "multi_normal_cholesky", + "multi_normal_prec", + "multi_student_cholesky_t", + "multi_student_t", + "multi_student_t_cholesky", + "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", + "std_normal_log", + "student_t", + "uniform", + "von_mises", + "weibull", + "wiener", + "wishart", + "wishart_cholesky", +]; - // const INCLUDE = { - // scope: 'meta', - // begin: /#include\b/, - // end: /$/, - // contains: [ - // { - // match: /[a-z][a-z-._]+/, - // scope: 'string' - // }, - // hljs.C_LINE_COMMENT_MODE - // ] - // }; +const RANGE_CONSTRAINTS = ["lower", "upper", "offset", "multiplier"]; - const RANGE_CONSTRAINTS = ["lower", "upper", "offset", "multiplier"]; +const OPERATORS = [ + "=", + ">", + "<", + "!", + "~", + "?", + ":", + "==", + "<=", + ">=", + "!=", + "&&", + "||", + "+", + "-", + "*", + "/", + "\\", + "|", + "^", + "%", + ">>", + "+=", + "-=", + "*=", + "/=", + "&=", + "|=", + "^=", + "%=", +]; - return { - BLOCKS, - STATEMENTS, - FUNCTIONS, - DISTRIBUTIONS, - RANGE_CONSTRAINTS, - TYPES, - }; +export const conf: languages.LanguageConfiguration = { + comments: { + lineComment: "//", + blockComment: ["/*", "*/"], + }, + brackets: [ + ["{", "}"], + ["[", "]"], + ["(", ")"], + ], + autoClosingPairs: [ + { open: "[", close: "]" }, + { open: "{", close: "}" }, + { open: "(", close: ")" }, + { open: "'", close: "'", notIn: ["string", "comment"] }, + { open: '"', close: '"', notIn: ["string"] }, + ], + surroundingPairs: [ + { open: "{", close: "}" }, + { open: "[", close: "]" }, + { open: "(", close: ")" }, + { open: '"', close: '"' }, + { open: "'", close: "'" }, + ], + indentationRules: { + increaseIndentPattern: /(\{[^}"']*$) | (\[[^\]"']*$) | (\([^)"']*$)/, + decreaseIndentPattern: /^\s*(\s*\/[*].*[*]\/\s*)*[}\])]/, + }, + folding: { + markers: { + start: + /^[functions|data|transformed\s+data|parameters|transformed\s+parameters|model|generated\s+quantities]\s\{$/, + end: /^\}$/, + }, + }, + wordPattern: /[a-zA-Z_][a-zA-Z0-9_]*/, }; -const monacoAddStanLang = (monacoInstance: Monaco) => { - console.log("monaco loaded! Setting up stan language"); +export const language = { + defaultToken: "", + tokenPostfix: ".stan", - monacoInstance.editor.defineTheme("vs-stan", { - base: "vs-dark", - inherit: true, - rules: [ - { token: "stanblock", foreground: "#C9A969", fontStyle: "bold" }, // seems like underscores in tokens are not allowed! (took me a while to figure this out) - { token: "stanstatement", foreground: "#A8EEF7" }, - { token: "standistribution", foreground: "#9999FF" }, - { token: "stanfunction", foreground: "ffffaa" }, - { token: "stanrangeconstraint", foreground: "#D48331" }, - { token: "stanoperator", foreground: "#A8EEF7" }, - { token: "stantype", foreground: "#BD9BF8" }, - { token: "identifier", foreground: "#DDDDDD" }, - { token: "number", foreground: "#D48331" }, - { token: "string", foreground: "55ff55" }, - ], - colors: {}, - }); + brackets: [ + { token: "delimiter.curly", open: "{", close: "}" }, + { token: "delimiter.parenthesis", open: "(", close: ")" }, + { token: "delimiter.square", open: "[", close: "]" }, + { token: "delimiter.angle", open: "<", close: ">" }, + ], + + blocks: BLOCKS.join("|"), + functions: [...FUNCTIONS, ...SPECIAL_FUNCTIONS].join("|"), + distributions: DISTRIBUTIONS.join("|"), + keywords: [...RANGE_CONSTRAINTS, ...TYPES, ...STATEMENTS], + + operators: OPERATORS, + symbols: /[=> l.id === "cpp")[0]; + tokenizer: { + root: [ + [/(@blocks)\s*{/, "type.$0"], - if (cppDef) { - (cppDef as any).loader().then((loaded: any) => { - const cppLang = loaded.language; - const hjd = highlightJsData(); - const stanLang = { ...cppLang }; - stanLang.tokenizer = { ...cppLang.tokenizer }; - stanLang.tokenizer.root = [...cppLang.tokenizer.root]; - // stanLang.keywords = [...hjd.BLOCKS, ...hjd.STATEMENTS, ...hjd.DISTRIBUTIONS, ...hjd.FUNCTIONS, ...hjd.RANGE_CONSTRAINTS, ...hjd.TYPES] - stanLang.keywords = []; - stanLang.stan_blocks = hjd.BLOCKS; - stanLang.stan_statements = hjd.STATEMENTS; - stanLang.stan_distributions = hjd.DISTRIBUTIONS; - stanLang.stan_functions = hjd.FUNCTIONS; - stanLang.stan_range_constraints = hjd.RANGE_CONSTRAINTS; - stanLang.stan_types = hjd.TYPES; - stanLang.tokenizer.root = [ - [ - /[a-zA-Z_]\w*/, - { - cases: { - "@stan_blocks": "stanblock", - "@stan_statements": "stanstatement", - "@stan_distributions": "standistribution", - "@stan_functions": "stanfunction", - "@stan_range_constraints": "stanrangeconstraint", - "@stan_types": "stantype", - "@default": "identifier", - }, + [/(@functions)\s*\(/, "tag.function.$0"], + [/~\s*(@distributions)\s*\(/, "tag.distribution.$1"], + + // identifiers and keywords + [ + /[a-zA-Z_]\w*/, + { + cases: { + "@keywords": { token: "keyword.$0" }, + "@default": "identifier", + }, + }, + ], + + // whitespace + { include: "@whitespace" }, + + // delimiters and operators + [/[{}()<>\[\]]/, "@brackets"], + [ + /@symbols/, + { + cases: { + "@operators": "delimiter", + "@default": "", }, - ], - [/~|:|\+=|=|<|>/, "stanoperator"], - ...stanLang.tokenizer.root, - ]; - monacoInstance.languages.register({ id: "stan" }); - monacoInstance.languages.setMonarchTokensProvider("stan", stanLang); - }); - } + }, + ], + + // numbers + [/\d*\d+[eE]([\-+]?\d+)?i?/, "number.float"], + [/\d*\.\d+([eE][\-+]?\d+)?i?/, "number.float"], + [/\d[\d']*\di?/, "number"], + [/\di?/, "number"], + + // strings + [/"/, "string", "@string"], + + // characters + [/'[^\\']'/, "string"], + [/'/, "string.invalid"], + ], + + whitespace: [ + [/[ \t\r\n]+/, ""], + [/\/\*/, "comment", "@comment"], + [/\/\/.*\\$/, "comment", "@linecomment"], + [/\/\/.*$/, "comment"], + ], + + comment: [ + [/[^\/*]+/, "comment"], + [/\*\//, "comment", "@pop"], + [/[\/*]/, "comment"], + ], + + //For use with continuous line comments + linecomment: [ + [/.*[^\\]$/, "comment", "@pop"], + [/[^]+/, "comment"], + ], + string: [ + [/[^\\"]+/, "string"], + [/"/, "string", "@pop"], + ], + }, +}; + +const monacoAddStanLang = (monacoInstance: Monaco) => { + console.log("monaco loaded! Setting up stan language"); + + monacoInstance.languages.register({ id: "stan" }); + monacoInstance.languages.setMonarchTokensProvider("stan", language); + monacoInstance.languages.setLanguageConfiguration("stan", conf); }; export default monacoAddStanLang; From b7552f624fde471a359c1932a4c3ffc93ec7311c Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Fri, 26 Jul 2024 20:34:51 +0000 Subject: [PATCH 2/2] Simplified decreaseIndent --- gui/src/app/FileEditor/stanLang.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gui/src/app/FileEditor/stanLang.ts b/gui/src/app/FileEditor/stanLang.ts index 173cd341..a0c9a917 100644 --- a/gui/src/app/FileEditor/stanLang.ts +++ b/gui/src/app/FileEditor/stanLang.ts @@ -741,8 +741,8 @@ export const conf: languages.LanguageConfiguration = { { open: "'", close: "'" }, ], indentationRules: { - increaseIndentPattern: /(\{[^}"']*$) | (\[[^\]"']*$) | (\([^)"']*$)/, - decreaseIndentPattern: /^\s*(\s*\/[*].*[*]\/\s*)*[}\])]/, + increaseIndentPattern: /(\{[^}\"\']*$) | (\[[^\]\"\']*$) | (\([^)\"\']*$)/, + decreaseIndentPattern: /^[\}\]\)]/, }, folding: { markers: {