|
| 1 | +#!/usr/bin/env python |
| 2 | +# Build the gh-pages |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import re |
| 7 | +import sys |
| 8 | + |
| 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 | +lint_subheadline = re.compile(r'''^\*\*([\w\s]+?)[:?.!]?\*\*(.*)''') |
| 14 | + |
| 15 | +conf_template = """ |
| 16 | +This lint has the following configuration variables: |
| 17 | +
|
| 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) |
| 43 | + |
| 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 | + |
| 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'] = {} |
| 73 | + |
| 74 | + last_section = None |
| 75 | + |
| 76 | + for line in comment: |
| 77 | + if len(line.strip()) == 0: |
| 78 | + continue |
| 79 | + |
| 80 | + match = re.match(lint_subheadline, line) |
| 81 | + if match: |
| 82 | + last_section = match.groups()[0] |
| 83 | + if match: |
| 84 | + text = match.groups()[1] |
| 85 | + else: |
| 86 | + text = line |
| 87 | + |
| 88 | + 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 |
| 150 | + |
| 151 | + |
| 152 | +def main(): |
| 153 | + lints = parse_path() |
| 154 | + info("got %s lints" % len(lints)) |
| 155 | + |
| 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 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + main() |
0 commit comments