src/codes.bend

bolt@1.19.0.0 · 100 lines · 0 definitions

1# src/codes: the stable id of every rule. A bolt.bend still names the slug
2# (`wrap`); a diagnostic shows the code (`S003`) and, in an editor, the
3# source `bolt(group:slug)`. Numbers are assigned once, in the group order
4# of src/README.md: do not renumber a rule, and do not reuse a retired code
5# (L004 was `quantify`, now `closed` itself; C001 was `shadow` and U005 was
6# `nat`, whose failures bend 2.0.25 no longer has; U014 was `argv`, retired
7# before its first release, since every reader already drops the program).
8import Base
9import bolt@1.19.0.0/src/lazy/lazy.bend as Lazy
10
11# one rule: the slug bolt.bend names, its code, its group
12type Entry is Data:
13  Entry{slug: String, id: String, grp: String}
14
15# every rule, in group order
16def table() -> List<&2, Entry>:
17  [
18    Entry{"hole", "C002", "correctness"},
19    Entry{"pick", "C003", "correctness"},
20    Entry{"put", "C004", "correctness"},
21    Entry{"arms", "C005", "correctness"},
22    Entry{"escape", "C006", "correctness"},
23    Entry{"twice", "C007", "correctness"},
24    Entry{"strings", "C008", "correctness"},
25    Entry{"chars", "C009", "correctness"},
26    Entry{"foreign", "C010", "correctness"},
27    Entry{"setting", "C011", "correctness"},
28    Entry{"unused", "U001", "suspicious"},
29    Entry{"strict", "U002", "suspicious"},
30    Entry{"eager", "U003", "suspicious"},
31    Entry{"concat", "U004", "suspicious"},
32    Entry{"fuel", "U006", "suspicious"},
33    Entry{"index", "U007", "suspicious"},
34    Entry{"table", "U008", "suspicious"},
35    Entry{"hoist", "U009", "suspicious"},
36    Entry{"ring", "U010", "suspicious"},
37    Entry{"rewalk", "U011", "suspicious"},
38    Entry{"unit", "U012", "suspicious"},
39    Entry{"scan", "U013", "suspicious"},
40    Entry{"thunk", "U015", "suspicious"},
41    Entry{"fromrev", "U016", "suspicious"},
42    Entry{"doc", "S001", "style"},
43    Entry{"space", "S002", "style"},
44    Entry{"wrap", "S003", "style"},
45    Entry{"param", "S004", "style"},
46    Entry{"noqa", "S005", "style"},
47    Entry{"coverage", "L001", "laws"},
48    Entry{"closed", "L002", "laws"},
49    Entry{"unsafe", "L003", "laws"},
50    Entry{"trace", "L005", "laws"},
51    Entry{"tail", "P001", "pedantic"}]
52
53# this row is the slug
54def find.same(+row: Entry, +slug: String) -> Bool:
55  Entry{+s, _id, _grp} = row
56  String.eq(s, slug)
57
58# the row for a slug
59def find(rows: List<&2, Entry>, +slug: String) -> Maybe<&2, Entry>:
60  match rows:
61    case Nil{}:
62      None{}
63    case Con{+row, rest}:
64      Lazy.stop(Maybe<&2, Entry>, find.same(row, slug), Some{row}, _u => find(rest, slug))
65
66# the stable code; the slug itself when it is not a rule
67def code.at(mm: Maybe<&2, Entry>, +slug: String) -> String:
68  match mm:
69    case None{}:
70      slug
71    case Some{Entry{_s, +id, _grp}}:
72      id
73
74# the stable code; the slug itself when it is not a rule
75def code(+slug: String) -> String:
76  code.at(find(table(), slug), slug)
77
78# the group; empty when the slug is not a rule
79def group.at(mm: Maybe<&2, Entry>) -> String:
80  match mm:
81    case None{}:
82      ""
83    case Some{Entry{_s, _id, +grp}}:
84      grp
85
86# the group; empty when the slug is not a rule
87def group(+slug: String) -> String:
88  group.at(find(table(), slug))
89
90# `bolt(group:slug)`; `bolt` when the slug is not a rule
91def source.at(mm: Maybe<&2, Entry>) -> String:
92  match mm:
93    case None{}:
94      "bolt"
95    case Some{Entry{+s, _id, +grp}}:
96      "bolt(" ++ grp ++ ":" ++ s ++ ")"
97
98# `bolt(group:slug)`; `bolt` when the slug is not a rule
99def source(+slug: String) -> String:
100  source.at(find(table(), slug))