-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_patterns.py
46 lines (36 loc) · 1.24 KB
/
gen_patterns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
"""Create the regexes to use in the Atom language-stan grammar.
This is a hack until I translate it into coffescript script that directly
outputs the .cson grammar.
"""
import json
def kw2re(x):
"""Convert a list of keywords to a regex."""
return r'(%s)' % '|'.join(sorted(list(set(x))))
def patterns(filename):
"""Print patterns."""
with open(filename, "r") as f:
data = json.load(f)
functions = [
k for k, v in data['functions'].items()
if not k.startswith('operator') and not v['deprecated']
and not v['keyword']
]
deprecated_functions = data['deprecated']
distributions = [
v['sampling'] for k, v in data['functions'].items() if v['sampling']
]
print("functions: \n" + r'"match": "\\b' + kw2re(functions) + r'\\b",')
print()
print("distributions: \n" + r'"match": "(~)(\\s*)' + kw2re(distributions)
+ r'\\b",')
print()
print("deprecated_functions: \n" + r'"match": "\\b([A-Za-z0-9][A-Za-z0-9_]*_log|' +
kw2re(deprecated_functions)[1:] + r'\\b",')
print()
def main():
"""Command line interface."""
src = 'stan-language-definitions/stan_lang.json'
patterns(src)
if __name__ == '__main__':
main()