Added support for Stan (#2490)

This commit is contained in:
Michael Schmidt 2020-08-12 13:10:38 +02:00 committed by GitHub
parent bf115f4799
commit 2da2beba24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 512 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@ -1070,6 +1070,10 @@
"title": "SQL",
"owner": "multipetros"
},
"stan": {
"title": "Stan",
"owner": "RunDevelopment"
},
"iecst": {
"title": "Structured Text (IEC 61131-3)",
"owner": "serhioromano"

49
components/prism-stan.js Normal file
View File

@ -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
components/prism-stan.min.js vendored Normal file
View File

@ -0,0 +1 @@
Prism.languages.stan={comment:/\/\/.*|\/\*[\s\S]*?\*\/|#(?!include).*/,string:{pattern:/"[\x20\x21\x23-\x5B\x5D-\x7E]*"/,greedy:!0},directive:{pattern:/^([ \t]*)#include\b.*/m,lookbehind:!0,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:!0,alias:"function"},constraint:{pattern:/(\b(?:int|matrix|real|row_vector|vector)\s*)<[^<>]*>/,lookbehind:!0,inside:{expression:{pattern:/(=\s*)(?:(?!\s*(?:>$|,\s*\w+\s*=))[\s\S])+/,lookbehind:!0,inside:null},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/,/\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;

21
examples/prism-stan.html Normal file
View File

@ -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>

View File

@ -0,0 +1,17 @@
# comment
// comment
/*
comment
*/
----------------------------------------------------
[
["comment", "# comment"],
["comment", "// comment"],
["comment", "/*\ncomment\n*/"]
]
----------------------------------------------------
Checks for comments.

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -0,0 +1,29 @@
0
1
24567898765
0.0
1.0
3.14
2.7e3
2E-5
1.23e+3
----------------------------------------------------
[
["number", "0"],
["number", "1"],
["number", "24567898765"],
["number", "0.0"],
["number", "1.0"],
["number", "3.14"],
["number", "2.7e3"],
["number", "2E-5"],
["number", "1.23e+3"]
]
----------------------------------------------------
Checks for numbers.

View File

@ -0,0 +1,52 @@
+ - * /
+= -= *= /=
== != < <= > >=
! || &&
<- =
.* .*= ./ ./=
| ' ^ % ~ ? :
----------------------------------------------------
[
["operator", "+"],
["operator", "-"],
["operator", "*"],
["operator", "/"],
["operator", "+="],
["operator", "-="],
["operator", "*="],
["operator", "/="],
["operator", "=="],
["operator", "!="],
["operator", "<"],
["operator", "<="],
["operator", ">"],
["operator", ">="],
["operator", "!"],
["operator", "||"],
["operator", "&&"],
["operator", "<-"],
["operator", "="],
["operator", ".*"],
["operator", ".*="],
["operator", "./"],
["operator", "./="],
["operator", "|"],
["operator", "'"],
["operator", "^"],
["operator", "%"],
["operator", "~"],
["operator", "?"],
["operator", ":"]
]
----------------------------------------------------
Checks for operators.

View File

@ -0,0 +1,19 @@
( ) [ ] { }
, ;
----------------------------------------------------
[
["punctuation", "("],
["punctuation", ")"],
["punctuation", "["],
["punctuation", "]"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ","],
["punctuation", ";"]
]
----------------------------------------------------
Checks for punctuation.

View File

@ -0,0 +1,40 @@
""
"foo bar"
print("log density before =", target());
print("u[", n, "] = ", u[n]);
----------------------------------------------------
[
["string", "\"\""],
["string", "\"foo bar\""],
["keyword", "print"],
["punctuation", "("],
["string", "\"log density before =\""],
["punctuation", ","],
["keyword", "target"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ")"],
["punctuation", ";"],
["keyword", "print"],
["punctuation", "("],
["string", "\"u[\""],
["punctuation", ","],
" n",
["punctuation", ","],
["string", "\"] = \""],
["punctuation", ","],
" u",
["punctuation", "["],
"n",
["punctuation", "]"],
["punctuation", ")"],
["punctuation", ";"]
]
----------------------------------------------------
Checks for strings.