1
1
#!/usr/bin/env python
2
2
# Build the gh-pages
3
3
4
- import json
5
- import os
6
4
import re
7
5
import sys
6
+ import json
8
7
8
+ from lintlib import parse_all , log
9
9
10
- level_re = re .compile (r'''(Forbid|Deny|Warn|Allow)''' )
11
- conf_re = re .compile (r'''define_Conf! {\n([^}]*)\n}''' , re .MULTILINE )
12
- confvar_re = re .compile (r'''/// Lint: (\w+). (.*).*\n *\("([^"]*)", (?:[^,]*), (.*) => (.*)\),''' )
13
10
lint_subheadline = re .compile (r'''^\*\*([\w\s]+?)[:?.!]?\*\*(.*)''' )
14
11
15
- conf_template = """
12
+ CONF_TEMPLATE = """\
16
13
This lint has the following configuration variables:
17
14
18
- * `%s: %s`: %s (defaults to `%s`).
19
- """
20
-
21
-
22
- # TODO: actual logging
23
- def warn (* args ):
24
- print (args )
25
-
26
-
27
- def debug (* args ):
28
- print (args )
29
-
30
-
31
- def info (* args ):
32
- print (args )
33
-
34
-
35
- def parse_path (p = "clippy_lints/src" ):
36
- lints = []
37
- for f in os .listdir (p ):
38
- if f .endswith (".rs" ):
39
- parse_file (lints , os .path .join (p , f ))
40
-
41
- conf = parse_conf (p )
42
- info (conf )
15
+ * `%s: %s`: %s (defaults to `%s`)."""
43
16
44
- for lint_id in conf :
45
- lint = next (l for l in lints if l ['id' ] == lint_id )
46
- if lint :
47
- lint ['docs' ]['Configuration' ] = (conf_template % conf [lint_id ]).strip ()
48
17
49
- return lints
50
-
51
-
52
- def parse_conf (p ):
53
- c = {}
54
- with open (p + '/utils/conf.rs' ) as f :
55
- f = f .read ()
56
-
57
- m = re .search (conf_re , f )
58
- m = m .groups ()[0 ]
59
-
60
- m = re .findall (confvar_re , m )
61
-
62
- for (lint , doc , name , default , ty ) in m :
63
- c [lint .lower ()] = (name , ty , doc , default )
64
-
65
- return c
66
-
67
-
68
- def parseLintDef (level , comment , name ):
69
- lint = {}
70
- lint ['id' ] = name
71
- lint ['level' ] = level
72
- lint ['docs' ] = {}
18
+ def parse_lint_def (lint ):
19
+ lint_dict = {}
20
+ lint_dict ['id' ] = lint .name
21
+ lint_dict ['level' ] = lint .level
22
+ lint_dict ['docs' ] = {}
73
23
74
24
last_section = None
75
25
76
- for line in comment :
26
+ for line in lint . doc :
77
27
if len (line .strip ()) == 0 :
78
28
continue
79
29
@@ -86,77 +36,29 @@ def parseLintDef(level, comment, name):
86
36
text = line
87
37
88
38
if not last_section :
89
- warn ("Skipping comment line as it was not preceded by a heading" )
90
- debug ("in lint `%s`, line `%s`" % name , line )
91
-
92
- lint ['docs' ][last_section ] = (lint ['docs' ].get (last_section , "" ) + "\n " + text ).strip ()
93
-
94
- return lint
95
-
96
-
97
- def parse_file (d , f ):
98
- last_comment = []
99
- comment = True
100
-
101
- with open (f ) as rs :
102
- for line in rs :
103
- if comment :
104
- if line .startswith ("///" ):
105
- if line .startswith ("/// " ):
106
- last_comment .append (line [4 :])
107
- else :
108
- last_comment .append (line [3 :])
109
- elif line .startswith ("declare_lint!" ):
110
- comment = False
111
- deprecated = False
112
- restriction = False
113
- elif line .startswith ("declare_restriction_lint!" ):
114
- comment = False
115
- deprecated = False
116
- restriction = True
117
- elif line .startswith ("declare_deprecated_lint!" ):
118
- comment = False
119
- deprecated = True
120
- else :
121
- last_comment = []
122
- if not comment :
123
- l = line .strip ()
124
- m = re .search (r"pub\s+([A-Z_][A-Z_0-9]*)" , l )
125
-
126
- if m :
127
- name = m .group (1 ).lower ()
128
-
129
- # Intentionally either a never looping or infinite loop
130
- while not deprecated and not restriction :
131
- m = re .search (level_re , line )
132
- if m :
133
- level = m .group (0 )
134
- break
135
-
136
- line = next (rs )
137
-
138
- if deprecated :
139
- level = "Deprecated"
140
- elif restriction :
141
- level = "Allow"
142
-
143
- info ("found %s with level %s in %s" % (name , level , f ))
144
- d .append (parseLintDef (level , last_comment , name = name ))
145
- last_comment = []
146
- comment = True
147
- if "}" in l :
148
- warn ("Warning: Missing Lint-Name in" , f )
149
- comment = True
39
+ log .warn ("Skipping comment line as it was not preceded by a heading" )
40
+ log .debug ("in lint `%s`, line `%s`" , lint .name , line )
41
+
42
+ lint_dict ['docs' ][last_section ] = \
43
+ (lint_dict ['docs' ].get (last_section , "" ) + "\n " + text ).strip ()
44
+
45
+ return lint_dict
150
46
151
47
152
48
def main ():
153
- lints = parse_path ()
154
- info ("got %s lints" % len (lints ))
49
+ lintlist , configs = parse_all ()
50
+ lints = {}
51
+ for lint in lintlist :
52
+ lints [lint .name ] = parse_lint_def (lint )
53
+ if lint .name in configs :
54
+ lints [lint .name ]['docs' ]['Configuration' ] = \
55
+ CONF_TEMPLATE % configs [lint .name ]
56
+
57
+ outfile = sys .argv [1 ] if len (sys .argv ) > 1 else "util/gh-pages/lints.json"
58
+ with open (outfile , "w" ) as fp :
59
+ json .dump (list (lints .values ()), fp , indent = 2 )
60
+ log .info ("wrote JSON for great justice" )
155
61
156
- outdir = sys .argv [1 ] if len (sys .argv ) > 1 else "util/gh-pages/lints.json"
157
- with open (outdir , "w" ) as file :
158
- json .dump (lints , file , indent = 2 )
159
- info ("wrote JSON for great justice" )
160
62
161
63
if __name__ == "__main__" :
162
64
main ()
0 commit comments