Skip to content

Commit 2da2beb

Browse files
Added support for Stan (#2490)
1 parent bf115f4 commit 2da2beb

13 files changed

+512
-1
lines changed

components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

+4
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,10 @@
10701070
"title": "SQL",
10711071
"owner": "multipetros"
10721072
},
1073+
"stan": {
1074+
"title": "Stan",
1075+
"owner": "RunDevelopment"
1076+
},
10731077
"iecst": {
10741078
"title": "Structured Text (IEC 61131-3)",
10751079
"owner": "serhioromano"

components/prism-stan.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// https://mc-stan.org/docs/2_24/reference-manual/bnf-grammars.html
2+
3+
Prism.languages.stan = {
4+
'comment': /\/\/.*|\/\*[\s\S]*?\*\/|#(?!include).*/,
5+
'string': {
6+
// String literals can contain spaces and any printable ASCII characters except for " and \
7+
// https://mc-stan.org/docs/2_24/reference-manual/print-statements-section.html#string-literals
8+
pattern: /"[\x20\x21\x23-\x5B\x5D-\x7E]*"/,
9+
greedy: true
10+
},
11+
'directive': {
12+
pattern: /^([ \t]*)#include\b.*/m,
13+
lookbehind: true,
14+
alias: 'property'
15+
},
16+
17+
'function-arg': {
18+
pattern: /(\b(?:algebra_solver|integrate_1d|integrate_ode|integrate_ode_bdf|integrate_ode_rk45|map_rect)\s*\(\s*)[a-zA-Z]\w*/,
19+
lookbehind: true,
20+
alias: 'function'
21+
},
22+
'constraint': {
23+
pattern: /(\b(?:int|matrix|real|row_vector|vector)\s*)<[^<>]*>/,
24+
lookbehind: true,
25+
inside: {
26+
'expression': {
27+
pattern: /(=\s*)(?:(?!\s*(?:>$|,\s*\w+\s*=))[\s\S])+/,
28+
lookbehind: true,
29+
inside: null // see below
30+
},
31+
'property': /\b[a-z]\w*(?=\s*=)/i,
32+
'operator': /=/,
33+
'punctuation': /^<|>$|[,]/
34+
}
35+
},
36+
'keyword': [
37+
/\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/,
38+
// these are functions that are known to take another function as their first argument.
39+
/\b(?:algebra_solver|integrate_1d|integrate_ode|integrate_ode_bdf|integrate_ode_rk45|map_rect)\b/
40+
],
41+
'function': /\b[a-z]\w*(?=\s*\()/i,
42+
'number': /(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:E[+-]?\d+)?\b/i,
43+
'boolean': /\b(?:false|true)\b/,
44+
45+
'operator': /<-|\.[*/]=?|\|\|?|&&|[!=<>+\-*/]=?|['^%~?:]/,
46+
'punctuation': /[()\[\]{},;]/
47+
};
48+
49+
Prism.languages.stan.constraint.inside.expression.inside = Prism.languages.stan;

components/prism-stan.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-stan.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<h2>Full example</h2>
2+
<pre><code>// source: https://github.com/stan-dev/example-models/blob/8a6964135560f54f52695ccd4d2492a8067f0c30/misc/linear-regression/regression_std.stan
3+
4+
// normal mixture, unknown proportion and means, known variance
5+
// p(y|mu,theta) = theta * Normal(y|mu[1],1) + (1-theta) * Normal(y|mu[2],1);
6+
7+
data {
8+
int&lt;lower=0> N;
9+
real y[N];
10+
}
11+
parameters {
12+
real&lt;lower=0,upper=1> theta;
13+
real mu[2];
14+
}
15+
model {
16+
theta ~ uniform(0,1); // equivalently, ~ beta(1,1);
17+
for (k in 1:2)
18+
mu[k] ~ normal(0,10);
19+
for (n in 1:N)
20+
target += log_mix(theta, normal_lpdf(y[n]|mu[1],1.0), normal_lpdf(y[n]|mu[2],1.0));
21+
}</code></pre>
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# comment
2+
// comment
3+
/*
4+
comment
5+
*/
6+
7+
----------------------------------------------------
8+
9+
[
10+
["comment", "# comment"],
11+
["comment", "// comment"],
12+
["comment", "/*\ncomment\n*/"]
13+
]
14+
15+
----------------------------------------------------
16+
17+
Checks for comments.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
real<lower=a> b;
2+
3+
real<lower=a, upper=b> theta;
4+
5+
int<lower=1> T;
6+
7+
matrix<multiplier=5>[3, 4] B;
8+
9+
row_vector<lower=-1,upper=1>[10] u;
10+
11+
row_vector<offset=-42,multiplier=3>[3] u;
12+
13+
real<lower=min(y), upper=max(y)> phi;
14+
15+
real<offset=mu,multiplier=sigma> x;
16+
17+
----------------------------------------------------
18+
19+
[
20+
["keyword", "real"],
21+
["constraint", [
22+
["punctuation", "<"],
23+
["property", "lower"],
24+
["operator", "="],
25+
["expression", [
26+
"a"
27+
]],
28+
["punctuation", ">"]
29+
]],
30+
" b",
31+
["punctuation", ";"],
32+
33+
["keyword", "real"],
34+
["constraint", [
35+
["punctuation", "<"],
36+
["property", "lower"],
37+
["operator", "="],
38+
["expression", [
39+
"a"
40+
]],
41+
["punctuation", ","],
42+
["property", "upper"],
43+
["operator", "="],
44+
["expression", [
45+
"b"
46+
]],
47+
["punctuation", ">"]
48+
]],
49+
" theta",
50+
["punctuation", ";"],
51+
52+
["keyword", "int"],
53+
["constraint", [
54+
["punctuation", "<"],
55+
["property", "lower"],
56+
["operator", "="],
57+
["expression", [
58+
["number", "1"]
59+
]],
60+
["punctuation", ">"]
61+
]],
62+
" T",
63+
["punctuation", ";"],
64+
65+
["keyword", "matrix"],
66+
["constraint", [
67+
["punctuation", "<"],
68+
["property", "multiplier"],
69+
["operator", "="],
70+
["expression", [
71+
["number", "5"]
72+
]],
73+
["punctuation", ">"]
74+
]],
75+
["punctuation", "["],
76+
["number", "3"],
77+
["punctuation", ","],
78+
["number", "4"],
79+
["punctuation", "]"],
80+
" B",
81+
["punctuation", ";"],
82+
83+
["keyword", "row_vector"],
84+
["constraint", [
85+
["punctuation", "<"],
86+
["property", "lower"],
87+
["operator", "="],
88+
["expression", [
89+
["operator", "-"],
90+
["number", "1"]
91+
]],
92+
["punctuation", ","],
93+
["property", "upper"],
94+
["operator", "="],
95+
["expression", [
96+
["number", "1"]
97+
]],
98+
["punctuation", ">"]
99+
]],
100+
["punctuation", "["],
101+
["number", "10"],
102+
["punctuation", "]"],
103+
" u",
104+
["punctuation", ";"],
105+
106+
["keyword", "row_vector"],
107+
["constraint", [
108+
["punctuation", "<"],
109+
["property", "offset"],
110+
["operator", "="],
111+
["expression", [
112+
["operator", "-"],
113+
["number", "42"]
114+
]],
115+
["punctuation", ","],
116+
["property", "multiplier"],
117+
["operator", "="],
118+
["expression", [
119+
["number", "3"]
120+
]],
121+
["punctuation", ">"]
122+
]],
123+
["punctuation", "["],
124+
["number", "3"],
125+
["punctuation", "]"],
126+
" u",
127+
["punctuation", ";"],
128+
129+
["keyword", "real"],
130+
["constraint", [
131+
["punctuation", "<"],
132+
["property", "lower"],
133+
["operator", "="],
134+
["expression", [
135+
["function", "min"],
136+
["punctuation", "("],
137+
"y",
138+
["punctuation", ")"]
139+
]],
140+
["punctuation", ","],
141+
["property", "upper"],
142+
["operator", "="],
143+
["expression", [
144+
["function", "max"],
145+
["punctuation", "("],
146+
"y",
147+
["punctuation", ")"]
148+
]],
149+
["punctuation", ">"]
150+
]],
151+
" phi",
152+
["punctuation", ";"],
153+
154+
["keyword", "real"],
155+
["constraint", [
156+
["punctuation", "<"],
157+
["property", "offset"],
158+
["operator", "="],
159+
["expression", [
160+
"mu"
161+
]],
162+
["punctuation", ","],
163+
["property", "multiplier"],
164+
["operator", "="],
165+
["expression", [
166+
"sigma"
167+
]],
168+
["punctuation", ">"]
169+
]],
170+
" x",
171+
["punctuation", ";"]
172+
]
173+
174+
----------------------------------------------------
175+
176+
Checks for type constraints.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include my-std-normal.stan // definition of standard normal
2+
#include my-std-normal.stan
3+
4+
----------------------------------------------------
5+
6+
[
7+
["directive", "#include my-std-normal.stan "],
8+
["comment", "// definition of standard normal"],
9+
["directive", "#include my-std-normal.stan"]
10+
]
11+
12+
----------------------------------------------------
13+
14+
Checks for include directives.
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
break
2+
cholesky_factor_corr
3+
cholesky_factor_cov
4+
continue
5+
corr_matrix
6+
cov_matrix
7+
data
8+
else
9+
for
10+
functions
11+
generated
12+
if
13+
in
14+
increment_log_prob
15+
int
16+
matrix
17+
model
18+
ordered
19+
parameters
20+
positive_ordered
21+
print
22+
quantities
23+
real
24+
reject
25+
return
26+
row_vector
27+
simplex
28+
target
29+
transformed
30+
unit_vector
31+
vector
32+
void
33+
while
34+
35+
algebra_solver
36+
integrate_1d
37+
integrate_ode
38+
integrate_ode_bdf
39+
integrate_ode_rk45
40+
map_rect
41+
42+
----------------------------------------------------
43+
44+
[
45+
["keyword", "break"],
46+
["keyword", "cholesky_factor_corr"],
47+
["keyword", "cholesky_factor_cov"],
48+
["keyword", "continue"],
49+
["keyword", "corr_matrix"],
50+
["keyword", "cov_matrix"],
51+
["keyword", "data"],
52+
["keyword", "else"],
53+
["keyword", "for"],
54+
["keyword", "functions"],
55+
["keyword", "generated"],
56+
["keyword", "if"],
57+
["keyword", "in"],
58+
["keyword", "increment_log_prob"],
59+
["keyword", "int"],
60+
["keyword", "matrix"],
61+
["keyword", "model"],
62+
["keyword", "ordered"],
63+
["keyword", "parameters"],
64+
["keyword", "positive_ordered"],
65+
["keyword", "print"],
66+
["keyword", "quantities"],
67+
["keyword", "real"],
68+
["keyword", "reject"],
69+
["keyword", "return"],
70+
["keyword", "row_vector"],
71+
["keyword", "simplex"],
72+
["keyword", "target"],
73+
["keyword", "transformed"],
74+
["keyword", "unit_vector"],
75+
["keyword", "vector"],
76+
["keyword", "void"],
77+
["keyword", "while"],
78+
79+
["keyword", "algebra_solver"],
80+
["keyword", "integrate_1d"],
81+
["keyword", "integrate_ode"],
82+
["keyword", "integrate_ode_bdf"],
83+
["keyword", "integrate_ode_rk45"],
84+
["keyword", "map_rect"]
85+
]
86+
87+
----------------------------------------------------
88+
89+
Checks for keywords.

0 commit comments

Comments
 (0)