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