Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Stan #2490

Merged
merged 3 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,10 @@
"title": "SQL",
"owner": "multipetros"
},
"stan": {
"title": "Stan",
"owner": "RunDevelopment"
},
"iecst": {
"title": "Structured Text (IEC 61131-3)",
"owner": "serhioromano"
Expand Down
49 changes: 49 additions & 0 deletions components/prism-stan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// https://mc-stan.org/docs/2_24/reference-manual/bnf-grammars.html

Prism.languages.stan = {
'comment': /\/\/.*|\/\*[\s\S]*?\*\/|#(?!include).*/,
'string': {
// String literals can contain spaces and any printable ASCII characters except for " and \
// https://mc-stan.org/docs/2_24/reference-manual/print-statements-section.html#string-literals
pattern: /"[\x20\x21\x23-\x5B\x5D-\x7E]*"/,
greedy: true
},
'directive': {
pattern: /^([ \t]*)#include\b.*/m,
lookbehind: true,
alias: 'property'
},

'function-arg': {
pattern: /(\b(?:algebra_solver|integrate_1d|integrate_ode|integrate_ode_bdf|integrate_ode_rk45|map_rect)\s*\(\s*)[a-zA-Z]\w*/,
lookbehind: true,
alias: 'function'
},
'constraint': {
pattern: /(\b(?:int|matrix|real|row_vector|vector)\s*)<[^<>]*>/,
lookbehind: true,
inside: {
'expression': {
pattern: /(=\s*)(?:(?!\s*(?:>$|,\s*\w+\s*=))[\s\S])+/,
lookbehind: true,
inside: null // see below
},
'property': /\b[a-z]\w*(?=\s*=)/i,
'operator': /=/,
'punctuation': /^<|>$|[,]/
}
},
'keyword': [
/\b(?:break|cholesky_factor_corr|cholesky_factor_cov|continue|corr_matrix|cov_matrix|data|else|for|functions|generated|if|in|increment_log_prob|int|matrix|model|ordered|parameters|positive_ordered|print|quantities|real|reject|return|row_vector|simplex|target|transformed|unit_vector|vector|void|while)\b/,
// these are functions that are known to take another function as their first argument.
/\b(?:algebra_solver|integrate_1d|integrate_ode|integrate_ode_bdf|integrate_ode_rk45|map_rect)\b/
],
'function': /\b[a-z]\w*(?=\s*\()/i,
'number': /(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:E[+-]?\d+)?\b/i,
'boolean': /\b(?:false|true)\b/,

'operator': /<-|\.[*/]=?|\|\|?|&&|[!=<>+\-*/]=?|['^%~?:]/,
'punctuation': /[()\[\]{},;]/
};

Prism.languages.stan.constraint.inside.expression.inside = Prism.languages.stan;
1 change: 1 addition & 0 deletions components/prism-stan.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/prism-stan.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h2>Full example</h2>
<pre><code>// source: https://github.com/stan-dev/example-models/blob/8a6964135560f54f52695ccd4d2492a8067f0c30/misc/linear-regression/regression_std.stan

// normal mixture, unknown proportion and means, known variance
// p(y|mu,theta) = theta * Normal(y|mu[1],1) + (1-theta) * Normal(y|mu[2],1);

data {
int&lt;lower=0> N;
real y[N];
}
parameters {
real&lt;lower=0,upper=1> theta;
real mu[2];
}
model {
theta ~ uniform(0,1); // equivalently, ~ beta(1,1);
for (k in 1:2)
mu[k] ~ normal(0,10);
for (n in 1:N)
target += log_mix(theta, normal_lpdf(y[n]|mu[1],1.0), normal_lpdf(y[n]|mu[2],1.0));
}</code></pre>
17 changes: 17 additions & 0 deletions tests/languages/stan/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# comment
// comment
/*
comment
*/

----------------------------------------------------

[
["comment", "# comment"],
["comment", "// comment"],
["comment", "/*\ncomment\n*/"]
]

----------------------------------------------------

Checks for comments.
176 changes: 176 additions & 0 deletions tests/languages/stan/constraint_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
real<lower=a> b;

real<lower=a, upper=b> theta;

int<lower=1> T;

matrix<multiplier=5>[3, 4] B;

row_vector<lower=-1,upper=1>[10] u;

row_vector<offset=-42,multiplier=3>[3] u;

real<lower=min(y), upper=max(y)> phi;

real<offset=mu,multiplier=sigma> x;

----------------------------------------------------

[
["keyword", "real"],
["constraint", [
["punctuation", "<"],
["property", "lower"],
["operator", "="],
["expression", [
"a"
]],
["punctuation", ">"]
]],
" b",
["punctuation", ";"],

["keyword", "real"],
["constraint", [
["punctuation", "<"],
["property", "lower"],
["operator", "="],
["expression", [
"a"
]],
["punctuation", ","],
["property", "upper"],
["operator", "="],
["expression", [
"b"
]],
["punctuation", ">"]
]],
" theta",
["punctuation", ";"],

["keyword", "int"],
["constraint", [
["punctuation", "<"],
["property", "lower"],
["operator", "="],
["expression", [
["number", "1"]
]],
["punctuation", ">"]
]],
" T",
["punctuation", ";"],

["keyword", "matrix"],
["constraint", [
["punctuation", "<"],
["property", "multiplier"],
["operator", "="],
["expression", [
["number", "5"]
]],
["punctuation", ">"]
]],
["punctuation", "["],
["number", "3"],
["punctuation", ","],
["number", "4"],
["punctuation", "]"],
" B",
["punctuation", ";"],

["keyword", "row_vector"],
["constraint", [
["punctuation", "<"],
["property", "lower"],
["operator", "="],
["expression", [
["operator", "-"],
["number", "1"]
]],
["punctuation", ","],
["property", "upper"],
["operator", "="],
["expression", [
["number", "1"]
]],
["punctuation", ">"]
]],
["punctuation", "["],
["number", "10"],
["punctuation", "]"],
" u",
["punctuation", ";"],

["keyword", "row_vector"],
["constraint", [
["punctuation", "<"],
["property", "offset"],
["operator", "="],
["expression", [
["operator", "-"],
["number", "42"]
]],
["punctuation", ","],
["property", "multiplier"],
["operator", "="],
["expression", [
["number", "3"]
]],
["punctuation", ">"]
]],
["punctuation", "["],
["number", "3"],
["punctuation", "]"],
" u",
["punctuation", ";"],

["keyword", "real"],
["constraint", [
["punctuation", "<"],
["property", "lower"],
["operator", "="],
["expression", [
["function", "min"],
["punctuation", "("],
"y",
["punctuation", ")"]
]],
["punctuation", ","],
["property", "upper"],
["operator", "="],
["expression", [
["function", "max"],
["punctuation", "("],
"y",
["punctuation", ")"]
]],
["punctuation", ">"]
]],
" phi",
["punctuation", ";"],

["keyword", "real"],
["constraint", [
["punctuation", "<"],
["property", "offset"],
["operator", "="],
["expression", [
"mu"
]],
["punctuation", ","],
["property", "multiplier"],
["operator", "="],
["expression", [
"sigma"
]],
["punctuation", ">"]
]],
" x",
["punctuation", ";"]
]

----------------------------------------------------

Checks for type constraints.
14 changes: 14 additions & 0 deletions tests/languages/stan/directive_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include my-std-normal.stan // definition of standard normal
#include my-std-normal.stan

----------------------------------------------------

[
["directive", "#include my-std-normal.stan "],
["comment", "// definition of standard normal"],
["directive", "#include my-std-normal.stan"]
]

----------------------------------------------------

Checks for include directives.
89 changes: 89 additions & 0 deletions tests/languages/stan/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
break
cholesky_factor_corr
cholesky_factor_cov
continue
corr_matrix
cov_matrix
data
else
for
functions
generated
if
in
increment_log_prob
int
matrix
model
ordered
parameters
positive_ordered
print
quantities
real
reject
return
row_vector
simplex
target
transformed
unit_vector
vector
void
while

algebra_solver
integrate_1d
integrate_ode
integrate_ode_bdf
integrate_ode_rk45
map_rect

----------------------------------------------------

[
["keyword", "break"],
["keyword", "cholesky_factor_corr"],
["keyword", "cholesky_factor_cov"],
["keyword", "continue"],
["keyword", "corr_matrix"],
["keyword", "cov_matrix"],
["keyword", "data"],
["keyword", "else"],
["keyword", "for"],
["keyword", "functions"],
["keyword", "generated"],
["keyword", "if"],
["keyword", "in"],
["keyword", "increment_log_prob"],
["keyword", "int"],
["keyword", "matrix"],
["keyword", "model"],
["keyword", "ordered"],
["keyword", "parameters"],
["keyword", "positive_ordered"],
["keyword", "print"],
["keyword", "quantities"],
["keyword", "real"],
["keyword", "reject"],
["keyword", "return"],
["keyword", "row_vector"],
["keyword", "simplex"],
["keyword", "target"],
["keyword", "transformed"],
["keyword", "unit_vector"],
["keyword", "vector"],
["keyword", "void"],
["keyword", "while"],

["keyword", "algebra_solver"],
["keyword", "integrate_1d"],
["keyword", "integrate_ode"],
["keyword", "integrate_ode_bdf"],
["keyword", "integrate_ode_rk45"],
["keyword", "map_rect"]
]

----------------------------------------------------

Checks for keywords.
Loading