]> git.lizzy.rs Git - rust.git/blob - util/export.py
Adjust HTML Docs
[rust.git] / util / export.py
1 #!/usr/bin/env python
2
3 import os
4 import re
5 import json
6
7 level_re = re.compile(r'''(Forbid|Deny|Warn|Allow)''')
8 conf_re = re.compile(r'''define_Conf! {\n([^}]*)\n}''', re.MULTILINE)
9 confvar_re = re.compile(r'''/// Lint: (\w+). (.*).*\n *\("([^"]*)", (?:[^,]*), (.*) => (.*)\),''')
10 lint_subheadline = re.compile(r'''^\*\*([\w\s]+)[:?.!]\*\*(.*)''')
11
12 # TODO: actual logging
13 def warn(*args): print(args)
14 def debug(*args): print(args)
15 def info(*args): print(args)
16
17 def parse_path(p="clippy_lints/src"):
18     d = []
19     for f in os.listdir(p):
20         if f.endswith(".rs"):
21             parse_file(d, os.path.join(p, f))
22     return (d, parse_conf(p))
23
24
25 def parse_conf(p):
26     c = {}
27     with open(p + '/utils/conf.rs') as f:
28         f = f.read()
29
30         m = re.search(conf_re, f)
31         m = m.groups()[0]
32
33         m = re.findall(confvar_re, m)
34
35         for (lint, doc, name, default, ty) in m:
36             c[lint.lower()] = (name, ty, doc, default)
37
38     return c
39
40 def parseLintDef(level, comment, name):
41     lint = {}
42     lint['id'] = name
43     lint['level'] = level
44     lint['docs'] = {}
45
46     last_section = None
47
48     for line in comment:
49         if len(line.strip()) == 0:
50             continue
51
52         match = re.match(lint_subheadline, line)
53         if match:
54             last_section = match.groups()[0]
55         if match:
56             text = match.groups()[1]
57         else:
58             text = line
59
60         if not last_section:
61             warn("Skipping comment line as it was not preceded by a heading")
62             debug("in lint `%s`, line `%s`" % name, line)
63
64         lint['docs'][last_section] = (lint['docs'].get(last_section, "") + "\n" + text).strip()
65
66     return lint
67
68 def parse_file(d, f):
69     last_comment = []
70     comment = True
71
72     with open(f) as rs:
73         for line in rs:
74             if comment:
75                 if line.startswith("///"):
76                     if line.startswith("/// "):
77                         last_comment.append(line[4:])
78                     else:
79                         last_comment.append(line[3:])
80                 elif line.startswith("declare_lint!"):
81                     comment = False
82                     deprecated = False
83                     restriction = False
84                 elif line.startswith("declare_restriction_lint!"):
85                     comment = False
86                     deprecated = False
87                     restriction = True
88                 elif line.startswith("declare_deprecated_lint!"):
89                     comment = False
90                     deprecated = True
91                 else:
92                     last_comment = []
93             if not comment:
94                 l = line.strip()
95                 m = re.search(r"pub\s+([A-Z_][A-Z_0-9]*)", l)
96
97                 if m:
98                     name = m.group(1).lower()
99
100                     # Intentionally either a never looping or infinite loop
101                     while not deprecated and not restriction:
102                         m = re.search(level_re, line)
103                         if m:
104                             level = m.group(0)
105                             break
106
107                         line = next(rs)
108
109                     if deprecated:
110                         level = "Deprecated"
111                     elif restriction:
112                         level = "Allow"
113
114                     info("found %s with level %s in %s" % (name, level, f))
115                     d.append(parseLintDef(level, last_comment, name=name))
116                     last_comment = []
117                     comment = True
118                 if "}" in l:
119                     warn("Warning: Missing Lint-Name in", f)
120                     comment = True
121
122 def main():
123     (lints, config) = parse_path()
124     info("got %s lints" % len(lints))
125     with open("util/gh-pages/lints.json", "w") as file:
126         json.dump(lints, file, indent=2)
127         info("wrote JSON for great justice")
128
129 if __name__ == "__main__":
130     main()