|
1 | 1 | import re |
| 2 | +from itertools import chain |
2 | 3 | import requests |
3 | 4 |
|
4 | | -FUNCTION_SIGNATURES_FILE = "https://raw.githubusercontent.com/stan-dev/stan/develop/src/stan/lang/function_signatures.h" |
| 5 | + |
| 6 | +# Path to stan-vim syntax file |
5 | 7 | SYNTAX_FILE = "syntax/stan.vim" |
| 8 | + |
| 9 | +# Syntax declarations |
| 10 | +SYNTAX_HIGHLIGHTS = [ |
| 11 | + "syntax keyword stanFunction", |
| 12 | + "syntax keyword stanConstant", |
| 13 | + "syntax keyword stanSpecial", |
| 14 | +] |
| 15 | + |
| 16 | +# URL for Stan function signatures file |
| 17 | +FUNCTION_SIGNATURES_FILE = "https://raw.githubusercontent.com/stan-dev/stan/develop/src/stan/lang/function_signatures.h" |
| 18 | + |
| 19 | +# Functions that Stan uses to register function signatures |
6 | 20 | STAN_ADD_FUNCTIONS = [ |
7 | 21 | "add", |
8 | 22 | "add_unary", |
|
20 | 34 | msg = "Could not request Stan file." |
21 | 35 | raise RuntimeError(msg) |
22 | 36 |
|
23 | | -# Read syntax file |
| 37 | +# Read stan-vim syntax file |
24 | 38 | with open(SYNTAX_FILE, "r") as f: |
25 | 39 | syntax_file = f.read() |
26 | 40 |
|
27 | | -# Find all functions from Stan file |
| 41 | +# Find functions declarations from Stan file |
28 | 42 | add_functions = "|".join(STAN_ADD_FUNCTIONS) |
29 | 43 | regex = r"(?<=[{}]\(['\"])(\w+)(?=['\"])".format(add_functions) |
30 | | -stan_functions = set(re.findall(regex, stan_file)) |
31 | | -# Remove _log functions |
| 44 | +matches = set(re.findall(regex, stan_file)) |
32 | 45 | stan_functions = { |
33 | | - function for function in stan_functions if not function.endswith("_log") |
| 46 | + # Remove matches that end with "_log" |
| 47 | + match |
| 48 | + for match in matches |
| 49 | + if not match.endswith("_log") |
34 | 50 | } |
35 | | -# Add bare distribution names |
36 | 51 | stan_functions = stan_functions | { |
37 | | - function[:-4] for function in stan_functions if function.endswith("_rng") |
| 52 | + # Add bare distribution names |
| 53 | + function[:-4] |
| 54 | + for function in stan_functions |
| 55 | + if function.endswith("_rng") |
38 | 56 | } |
39 | 57 |
|
40 | | -regex1 = r"(?<=syntax keyword stanFunction )(.*)" |
41 | | -regex2 = r"(?<=syntax keyword stanConstant )(.*)" |
42 | | -matches = re.findall(regex1, syntax_file) + re.findall(regex2, syntax_file) |
43 | | - |
44 | | -# Flatten |
45 | | -highlighted_functions = set([word for match in matches for word in match.split()]) |
| 58 | +# Find highlighted words from stan-vim syntax file |
| 59 | +regexes = [r"(?<={} )(.*)".format(declaration) for declaration in SYNTAX_HIGHLIGHTS] |
| 60 | +matches = list(chain(*[re.findall(regex, syntax_file) for regex in regexes])) |
| 61 | +highlighted_functions = set(chain(*[match.split() for match in matches])) |
46 | 62 |
|
47 | 63 | missing = stan_functions - highlighted_functions |
48 | 64 | unnecessary = highlighted_functions - stan_functions |
|
0 commit comments