@skillstech/thunderlang 0.5.9 → 0.5.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/dist/core.cjs +1563 -152
- package/dist/index.cjs +1077 -113
- package/dist/learn.cjs +1518 -130
- package/package.json +1 -1
- package/src/core.d.ts +1 -1
- package/src/core.mjs +1 -1
- package/src/emit.mjs +1 -1
- package/src/model.mjs +43 -19
- package/src/read-repo.d.mts +7 -0
- package/src/read-repo.mjs +21 -0
- package/src/read.mjs +29 -8
- package/src/same.mjs +2 -2
- package/src/sql.mjs +387 -0
- package/src/steps.mjs +500 -22
- package/src/things-langs.mjs +352 -0
- package/src/thunderize.mjs +3 -4
- package/src/visual.mjs +1 -1
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
// Things (classes, records, and choices) in Java, C#, Kotlin, PHP, Go, Rust, and Ruby, so Big Picture shows what an
|
|
2
|
+
// app is made of in every language Thunder reads, not only Python and TypeScript. Each reader gives the same shape
|
|
3
|
+
// as model.mjs's pythonThings/tsThings: { kind: 'thing' | 'record' | 'choices', name, file, lines, doc?, knows,
|
|
4
|
+
// choices, memberRanges? }. `memberRanges` are extra line ranges whose functions belong to the Thing (Go methods
|
|
5
|
+
// with a receiver, Rust impl blocks), because those languages write methods outside the type itself.
|
|
6
|
+
|
|
7
|
+
/** Index of the `}` that closes the `{` at `open`, skipping strings, chars, and comments. -1 if unclosed. */
|
|
8
|
+
function braceEnd(src, open) {
|
|
9
|
+
let depth = 0;
|
|
10
|
+
for (let i = open; i < src.length; i++) {
|
|
11
|
+
const c = src[i];
|
|
12
|
+
if (c === '/' && src[i + 1] === '/') { const n = src.indexOf('\n', i); if (n < 0) return -1; i = n; continue; }
|
|
13
|
+
if (c === '/' && src[i + 1] === '*') { const n = src.indexOf('*/', i + 2); if (n < 0) return -1; i = n + 1; continue; }
|
|
14
|
+
if (c === '"' || c === '`') { const q = c; for (i++; i < src.length && src[i] !== q; i++) if (src[i] === '\\') i++; continue; }
|
|
15
|
+
if (c === "'" && /^'(?:\\.|[^\\'])'/.test(src.slice(i, i + 4))) { i = src.indexOf("'", i + 1 + (src[i + 1] === '\\' ? 1 : 0)) ; continue; }
|
|
16
|
+
if (c === '{') depth++;
|
|
17
|
+
else if (c === '}' && --depth === 0) return i;
|
|
18
|
+
}
|
|
19
|
+
return -1;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Line-start offsets once per source, so every line lookup is a binary search. */
|
|
23
|
+
function lineFinder(src) {
|
|
24
|
+
const starts = [0];
|
|
25
|
+
for (let i = src.indexOf('\n'); i >= 0; i = src.indexOf('\n', i + 1)) starts.push(i + 1);
|
|
26
|
+
return (idx) => { let lo = 0, hi = starts.length - 1; while (lo < hi) { const mid = (lo + hi + 1) >> 1; if (starts[mid] <= idx) lo = mid; else hi = mid - 1; } return lo + 1; };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The doc comment just above a declaration (/** … *\/, ///, or # lines), first paragraph, or null. */
|
|
30
|
+
function docAbove(src, idx) {
|
|
31
|
+
// Strip attributes and annotations right before the declaration, one at a time (a single regex with nested
|
|
32
|
+
// whitespace repeats backtracks without end on long whitespace runs).
|
|
33
|
+
let before = src.slice(Math.max(0, idx - 1200), idx).trimEnd();
|
|
34
|
+
for (let n = 0; n < 20; n++) {
|
|
35
|
+
const next = before.replace(/(?:@\w+(?:\([^)]*\))?|#\[[^\]]*\]|\[[A-Z]\w*(?:\([^)]*\))?\])$/, '').trimEnd();
|
|
36
|
+
if (next === before) break;
|
|
37
|
+
before = next;
|
|
38
|
+
}
|
|
39
|
+
// The doc comment that ends right before the declaration: the last /** in the text, not the first.
|
|
40
|
+
let m = /\*\/\s*$/.test(before) ? before.slice(before.lastIndexOf('/**')).match(/^\/\*\*([\s\S]*?)\*\/\s*$/) : null;
|
|
41
|
+
if (m) return m[1].split('\n').map((l) => l.replace(/^\s*\*\s?/, '').trim()).filter((l) => l && !l.startsWith('@')).join(' ').split(/\s{2,}/)[0] || null;
|
|
42
|
+
// Line comments right above (/// , //, #): walk back line by line (a regex over the run backtracks badly).
|
|
43
|
+
const lines = before.split('\n');
|
|
44
|
+
const run = [];
|
|
45
|
+
for (let k = lines.length - 1; k >= 0 && /^\s*(?:\/\/\/?|#(?!\[))/.test(lines[k]); k--) run.unshift(lines[k]);
|
|
46
|
+
if (run.length) {
|
|
47
|
+
const text = run.map((l) => l.replace(/^\s*(?:\/\/\/?|#)\s?/, '').trim()).filter(Boolean).join(' ').replace(/<\/?summary>/g, '').trim();
|
|
48
|
+
return text && !/^(frozen_string_literal|!|typed:)/.test(text) ? text : null;
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The body of a brace block with nested blocks removed: the type's own declarations, not its methods' bodies. */
|
|
54
|
+
function ownLevel(body) {
|
|
55
|
+
let depth = 0, own = '';
|
|
56
|
+
for (const c of body) { if (c === '{') depth++; if (depth === 0) own += c; if (c === '}') depth--; }
|
|
57
|
+
return own;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Split a parameter list at top-level commas. */
|
|
61
|
+
function splitParams(s) {
|
|
62
|
+
const out = [];
|
|
63
|
+
let depth = 0, cur = '';
|
|
64
|
+
for (const c of String(s)) {
|
|
65
|
+
if ('(<[{'.includes(c)) depth++;
|
|
66
|
+
if (')>]}'.includes(c)) depth--;
|
|
67
|
+
if (c === ',' && depth === 0) { out.push(cur.trim()); cur = ''; } else cur += c;
|
|
68
|
+
}
|
|
69
|
+
if (cur.trim()) out.push(cur.trim());
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Go doc comments start with the type's name ("Library lends books…"); read them without it ("Lends books…"). */
|
|
74
|
+
function goDoc(src, at, name) {
|
|
75
|
+
const d = docAbove(src, at);
|
|
76
|
+
if (!d) return null;
|
|
77
|
+
const t = d.replace(new RegExp(`^${name}\\s+(?:is|holds|represents)\\s+`), '').replace(new RegExp(`^${name}\\s+`), '');
|
|
78
|
+
return t.charAt(0).toUpperCase() + t.slice(1);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const MODIFIERS = /^(?:(?:public|private|protected|internal|static|final|readonly|const|volatile|transient|override|open|abstract|sealed|lateinit|required|unsafe|mut|pub(?:\([^)]*\))?|var|val|let)\s+)*/;
|
|
82
|
+
|
|
83
|
+
export function makeThingReaders({ words, kindWords, startValue }) {
|
|
84
|
+
const know = (name, type, start, params, line = null) => ({ name, label: words(name.replace(/^[_$]+|_$/g, '')), kind: type ? kindWords(type) : null, start: start != null && start !== '' ? startValue(start, params) : null, line });
|
|
85
|
+
|
|
86
|
+
/** Java, C#, Kotlin, PHP: brace-bodied classes. */
|
|
87
|
+
function curlyThings(src, file, language) {
|
|
88
|
+
const lineAt = lineFinder(src);
|
|
89
|
+
const out = [];
|
|
90
|
+
const kw = {
|
|
91
|
+
java: /(?:^|\n)[ \t]*(?:@\w+(?:\([^)]*\))?\s*)*(?:(?:public|private|protected|static|final|abstract|sealed|non-sealed)\s+)*(class|enum|record)\s+(\w+)\s*(?:<[^>{]*>)?\s*(\([^)]*\))?[^{;]*\{/g,
|
|
92
|
+
csharp: /(?:^|\n)[ \t]*(?:\[[^\]]*\]\s*)*(?:(?:public|private|protected|internal|static|sealed|abstract|partial|readonly|file)\s+)*(class|struct|enum|record(?:\s+(?:class|struct))?)\s+(\w+)\s*(?:<[^>{(]*>)?\s*(\([^)]*\))?[^{;]*(?:\{|;)/g,
|
|
93
|
+
kotlin: /(?:^|\n)[ \t]*(?:@\w+(?:\([^)]*\))?\s*)*(?:(?:public|private|protected|internal|open|abstract|sealed|final|data|enum|value|inline)\s+)*(class|object)\s+(\w+)\s*(?:<[^>{(]*>)?\s*(?:(?:private|internal|public)?\s*constructor\s*)?(\((?:[^()]|\([^()]*\))*\))?[^{\n]*(\{)?/g,
|
|
94
|
+
php: /(?:^|\n)[ \t]*(?:#\[[^\]]*\]\s*)*(?:(?:final|abstract|readonly)\s+)*(class|enum)\s+(\w+)(?:\s*:\s*\w+)?[^{;]*\{/g,
|
|
95
|
+
}[language];
|
|
96
|
+
if (!kw) return out;
|
|
97
|
+
let m;
|
|
98
|
+
while ((m = kw.exec(src))) {
|
|
99
|
+
const declAt = m.index + (m[0].startsWith('\n') ? 1 : 0);
|
|
100
|
+
const head = m[0];
|
|
101
|
+
const kindWord = m[1];
|
|
102
|
+
const name = m[2];
|
|
103
|
+
const ctorParams = m[3] && m[3].startsWith('(') ? m[3].slice(1, -1) : '';
|
|
104
|
+
const isData = language === 'kotlin' ? /\bdata\s+class\b|\bvalue\s+class\b/.test(head) : false;
|
|
105
|
+
const isEnum = kindWord === 'enum' || (language === 'kotlin' && /\benum\s+class\b/.test(head));
|
|
106
|
+
const isRecord = !isEnum && (/^record/.test(kindWord) || isData || (language === 'csharp' && kindWord === 'struct'));
|
|
107
|
+
const open = head.trimEnd().endsWith('{') ? m.index + head.trimEnd().length - 1 : -1;
|
|
108
|
+
const close = open >= 0 ? braceEnd(src, open) : -1;
|
|
109
|
+
if (open >= 0 && close < 0) continue;
|
|
110
|
+
const body = open >= 0 ? src.slice(open + 1, close) : '';
|
|
111
|
+
const thing = {
|
|
112
|
+
kind: isEnum ? 'choices' : isRecord ? 'record' : 'thing', name, file,
|
|
113
|
+
lines: [lineAt(declAt), lineAt(close >= 0 ? close : m.index + head.length)],
|
|
114
|
+
...(docAbove(src, declAt) ? { doc: docAbove(src, declAt) } : {}), knows: [], choices: [],
|
|
115
|
+
};
|
|
116
|
+
// Constructor parameters in the header: Java/C# records, Kotlin primary constructors, C# primary constructors.
|
|
117
|
+
const params = {};
|
|
118
|
+
for (const p of splitParams(ctorParams)) {
|
|
119
|
+
let pm;
|
|
120
|
+
if (language === 'kotlin' && (pm = p.match(/^(?:@\w+\s+)*(?:(?:private|public|protected|internal|override|open)\s+)*(val|var)?\s*(\w+)\s*:\s*([^=]+?)(?:\s*=\s*(.+))?$/))) {
|
|
121
|
+
params[pm[2]] = pm[4] ?? null;
|
|
122
|
+
if (pm[1]) thing.knows.push(know(pm[2], pm[3], pm[4], {}, null));
|
|
123
|
+
} else if ((pm = p.match(/^(?:@\w+\s+)*(?:final\s+)?([\w.<>[\],? ]+?)\s+(\w+)(?:\s*=\s*(.+))?$/))) {
|
|
124
|
+
params[pm[2]] = pm[3] ?? null;
|
|
125
|
+
if (/^record/.test(kindWord) || (language === 'csharp' && /^record/.test(kindWord))) thing.knows.push(know(pm[2], pm[1], pm[3], {}, null));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (isEnum) {
|
|
129
|
+
// Doc comments between the values (/// <summary>…, /** … */) are not values.
|
|
130
|
+
const own = ownLevel(body.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/[^\n]*/g, ''));
|
|
131
|
+
const list = language === 'php' ? [...own.matchAll(/\bcase\s+(\w+)(?:\s*=\s*([^;]+))?;/g)].map((x) => [x[1], x[2]]) : own.split(';')[0].split(',').map((x) => x.trim().match(/^(?:@\w+\s+)*(\w+)\s*(?:\((.*)\))?(?:\s*=\s*(.+))?/s)).filter(Boolean).map((x) => [x[1], x[2] ?? x[3]]);
|
|
132
|
+
for (const [n, v] of list) if (n && !/^(public|private|protected|fun|val|var|companion)$/.test(n)) thing.choices.push(v && /^\s*["']/.test(v) ? v.trim().replace(/^["'](.*)["']$/s, '$1') : words(n));
|
|
133
|
+
out.push(thing);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const own = ownLevel(body);
|
|
137
|
+
for (const raw of own.split(/[;\n]/)) {
|
|
138
|
+
const l = raw.trim().replace(/^(?:@\w+(?:\([^)]*\))?\s*)+/, '').replace(/^#\[[^\]]*\]\s*/, '');
|
|
139
|
+
if (!l || /^(\/\/|\/\*|\*|import|package|use|return|case|default|enum\s|class\s|interface\s|fun\s|function\s|def\s|init\b|constructor|companion|object\s)/.test(l) || /\)\s*$/.test(l) && !/=/.test(l)) continue;
|
|
140
|
+
let f;
|
|
141
|
+
if (language === 'kotlin' && (f = l.match(/^(?:(?:private|public|protected|internal|override|open|lateinit|const)\s+)*(?:val|var)\s+(\w+)\s*(?::\s*([^=]+?))?\s*(?:=\s*(.+))?$/))) {
|
|
142
|
+
thing.knows.push(know(f[1], f[2], f[3], params));
|
|
143
|
+
} else if (language === 'php' && (f = l.match(/^(?:(?:public|private|protected|static|readonly)\s+)+(?:\??[\w\\]+\s+)?\$(\w+)(?:\s*=\s*(.+))?$/))) {
|
|
144
|
+
const type = l.match(/^(?:(?:public|private|protected|static|readonly)\s+)+(\??[\w\\]+)\s+\$/)?.[1];
|
|
145
|
+
thing.knows.push(know(f[1], type, f[2], params));
|
|
146
|
+
} else if (language === 'csharp' && (f = l.match(/^(?:(?:public|private|protected|internal|static|readonly|required|virtual|override|const)\s+)*([\w.<>[\],? ]+?)\s+(\w+)\s*\{\s*(?:get|init|set)[^}]*\}(?:\s*=\s*(.+))?$/))) {
|
|
147
|
+
thing.knows.push(know(f[2], f[1], f[3], params));
|
|
148
|
+
} else if ((language === 'java' || language === 'csharp') && (f = l.match(/^(?:(?:public|private|protected|internal|static|final|readonly|transient|volatile|const)\s+)*([\w.<>[\],? ]+?)\s+(\w+)(?:\s*=\s*(.+))?$/)) && !/^(new|return|throw|if|for|while)$/.test(f[1].trim())) {
|
|
149
|
+
if (/\bstatic\b/.test(l) && /\bfinal\b|\bconst\b/.test(l)) continue; // constants belong to the type, not each Thing
|
|
150
|
+
thing.knows.push(know(f[2], f[1], f[3], params));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// Starting values set in the constructor: this.x = …; / $this->x = …; / x = … (Kotlin init blocks).
|
|
154
|
+
const ctorRe = language === 'php' ? /function\s+__construct\s*\(([^)]*)\)\s*\{/ : language === 'kotlin' ? /\binit\s*\{/ : new RegExp(`(?:public|private|protected|internal)?\\s*${name}\\s*\\(([^)]*)\\)\\s*(?::\\s*(?:this|base)\\([^)]*\\)\\s*)?\\{`);
|
|
155
|
+
const ctor = body.match(ctorRe);
|
|
156
|
+
if (ctor) {
|
|
157
|
+
for (const p of splitParams(ctor[1] || '')) {
|
|
158
|
+
const pm = language === 'php' ? p.match(/^(?:(public|private|protected)(?:\s+readonly)?\s+)?(?:\??[\w\\]+\s+)?\$(\w+)(?:\s*=\s*(.+))?$/) : p.match(/^(?:final\s+)?(?:[\w.<>[\],? ]+?)\s+(\w+)(?:\s*=\s*(.+))?$/);
|
|
159
|
+
if (!pm) continue;
|
|
160
|
+
if (language === 'php') { params[pm[2]] = pm[3] ?? null; if (pm[1] && !thing.knows.some((k) => k.name === pm[2])) thing.knows.push(know(pm[2], p.match(/(\??[\w\\]+)\s+\$/)?.[1], pm[3], {})); }
|
|
161
|
+
else params[pm[1]] = pm[2] ?? null;
|
|
162
|
+
}
|
|
163
|
+
const cOpen = body.indexOf('{', ctor.index + ctor[0].length - 1);
|
|
164
|
+
const cBody = body.slice(cOpen + 1, braceEnd(body, cOpen));
|
|
165
|
+
const assign = language === 'php' ? /\$this->(\w+)\s*=\s*([^;\n]+)/g : language === 'kotlin' ? /(?:this\.)?(\w+)\s*=\s*([^\n;]+)/g : /(?:this\.)?(\w+)\s*=\s*([^;\n]+);/g;
|
|
166
|
+
for (const a of cBody.matchAll(assign)) {
|
|
167
|
+
const k = thing.knows.find((x) => x.name === a[1] || x.name === `_${a[1]}` || x.name.replace(/^_/, '') === a[1]);
|
|
168
|
+
const value = a[2].trim().replace(/^\$/, '');
|
|
169
|
+
if (k) { if (!k.start) k.start = startValue(value, params); }
|
|
170
|
+
else if (language === 'php' || /^this\./.test(a[0])) thing.knows.push(know(a[1], null, value, params));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
out.push(thing);
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Go: structs are Things when they have methods (func (r *T) …), records otherwise; typed const blocks are choices. */
|
|
179
|
+
function goThings(src, file) {
|
|
180
|
+
const lineAt = lineFinder(src);
|
|
181
|
+
const out = [];
|
|
182
|
+
const byName = new Map();
|
|
183
|
+
for (const m of src.matchAll(/(?:^|\n)type\s+(\w+)\s+struct\s*\{/g)) {
|
|
184
|
+
const at = m.index + (m[0].startsWith('\n') ? 1 : 0);
|
|
185
|
+
const open = m.index + m[0].length - 1;
|
|
186
|
+
const close = braceEnd(src, open);
|
|
187
|
+
if (close < 0) continue;
|
|
188
|
+
const t = { kind: 'record', name: m[1], file, lines: [lineAt(at), lineAt(close)], ...(goDoc(src, at, m[1]) ? { doc: goDoc(src, at, m[1]) } : {}), knows: [], choices: [], memberRanges: [] };
|
|
189
|
+
for (const raw of src.slice(open + 1, close).split('\n')) {
|
|
190
|
+
const f = raw.replace(/\/\/.*$/, '').replace(/`[^`]*`/g, '').trim().match(/^([A-Za-z_]\w*(?:\s*,\s*[A-Za-z_]\w*)*)\s+(\S.*)$/);
|
|
191
|
+
if (f) for (const n of f[1].split(',')) t.knows.push(know(n.trim(), f[2].trim(), null, {}));
|
|
192
|
+
}
|
|
193
|
+
out.push(t);
|
|
194
|
+
byName.set(m[1], t);
|
|
195
|
+
}
|
|
196
|
+
// Methods with a receiver belong to their type; a type with methods can do things, so it is a Thing.
|
|
197
|
+
for (const m of src.matchAll(/(?:^|\n)func\s*\(\s*\w*\s*\*?\s*(\w+)(?:\[[^\]]*\])?\s*\)\s*\w+/g)) {
|
|
198
|
+
const t = byName.get(m[1]);
|
|
199
|
+
if (!t) continue;
|
|
200
|
+
const at = m.index + (m[0].startsWith('\n') ? 1 : 0);
|
|
201
|
+
const open = src.indexOf('{', at);
|
|
202
|
+
const close = open >= 0 ? braceEnd(src, open) : -1;
|
|
203
|
+
t.kind = 'thing';
|
|
204
|
+
t.memberRanges.push([lineAt(at), lineAt(close >= 0 ? close : at)]);
|
|
205
|
+
}
|
|
206
|
+
// Starting values from a constructor: func NewLibrary(...) *Library { return &Library{MaxBooks: 3, ...} }.
|
|
207
|
+
for (const m of src.matchAll(/func\s+New(\w+)\s*\(([^)]*)\)[^{]*\{/g)) {
|
|
208
|
+
const t = byName.get(m[1]);
|
|
209
|
+
if (!t) continue;
|
|
210
|
+
const open = m.index + m[0].length - 1;
|
|
211
|
+
const body = src.slice(open, braceEnd(src, open) + 1);
|
|
212
|
+
const params = Object.fromEntries(splitParams(m[2]).map((p) => [p.split(/\s+/)[0], null]));
|
|
213
|
+
const litAt = body.search(new RegExp(`&?${m[1]}\\s*\\{`));
|
|
214
|
+
const litOpen = litAt >= 0 ? body.indexOf('{', litAt) : -1;
|
|
215
|
+
const litBody = litOpen >= 0 ? body.slice(litOpen + 1, braceEnd(body, litOpen)) : '';
|
|
216
|
+
for (const kv of splitParams(litBody.replace(/\n/g, ','))) {
|
|
217
|
+
const f = kv.match(/^(\w+)\s*:\s*(.+)$/);
|
|
218
|
+
const k = f && t.knows.find((x) => x.name === f[1]);
|
|
219
|
+
if (k && !k.start) k.start = startValue(f[2].trim(), params);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// A named type with a typed const block is a set of choices: type Status int; const ( OnShelf Status = iota … ).
|
|
223
|
+
for (const m of src.matchAll(/(?:^|\n)type\s+(\w+)\s+(?:int|int8|int16|int32|uint|uint8|string)\b/g)) {
|
|
224
|
+
const name = m[1];
|
|
225
|
+
const block = src.match(new RegExp(`const\\s*\\(([^)]*\\b${name}\\b[^)]*)\\)`));
|
|
226
|
+
if (!block) continue;
|
|
227
|
+
const at = m.index + (m[0].startsWith('\n') ? 1 : 0);
|
|
228
|
+
const choices = [];
|
|
229
|
+
let inType = false;
|
|
230
|
+
for (const raw of block[1].split('\n')) {
|
|
231
|
+
const l = raw.replace(/\/\/.*$/, '').trim();
|
|
232
|
+
if (!l) continue;
|
|
233
|
+
const f = l.match(/^(\w+)(?:\s+(\w+))?(?:\s*=\s*(.+))?$/);
|
|
234
|
+
if (!f) continue;
|
|
235
|
+
if (f[2]) inType = f[2] === name;
|
|
236
|
+
if (!inType) continue;
|
|
237
|
+
const label = f[3] && /^"/.test(f[3].trim()) ? f[3].trim().slice(1, -1) : words(f[1].replace(new RegExp(`^${name}`), '') || f[1]);
|
|
238
|
+
choices.push(label);
|
|
239
|
+
}
|
|
240
|
+
if (choices.length) out.push({ kind: 'choices', name, file, lines: [lineAt(at), lineAt(at)], ...(goDoc(src, at, name) ? { doc: goDoc(src, at, name) } : {}), knows: [], choices });
|
|
241
|
+
}
|
|
242
|
+
return out;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Rust: structs and enums; impl blocks give a struct its methods (a Thing), otherwise it is a record. */
|
|
246
|
+
function rustThings(src, file) {
|
|
247
|
+
const lineAt = lineFinder(src);
|
|
248
|
+
const out = [];
|
|
249
|
+
const byName = new Map();
|
|
250
|
+
for (const m of src.matchAll(/(?:^|\n)[ \t]*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?(struct|enum)\s+(\w+)\s*(?:<[^>{]*>)?\s*(?:where[^{]*)?(\{|\(|;)/g)) {
|
|
251
|
+
const at = m.index + (m[0].startsWith('\n') ? 1 : 0);
|
|
252
|
+
const doc = docAbove(src, at);
|
|
253
|
+
if (m[3] !== '{') {
|
|
254
|
+
// Tuple struct (struct Meters(f64);) or unit struct: a record with no named facts.
|
|
255
|
+
const t = { kind: 'record', name: m[2], file, lines: [lineAt(at), lineAt(at)], ...(doc ? { doc } : {}), knows: [], choices: [], memberRanges: [] };
|
|
256
|
+
out.push(t); byName.set(m[2], t); continue;
|
|
257
|
+
}
|
|
258
|
+
const open = m.index + m[0].length - 1;
|
|
259
|
+
const close = braceEnd(src, open);
|
|
260
|
+
if (close < 0) continue;
|
|
261
|
+
const body = ownLevel(src.slice(open + 1, close));
|
|
262
|
+
const t = { kind: m[1] === 'enum' ? 'choices' : 'record', name: m[2], file, lines: [lineAt(at), lineAt(close)], ...(doc ? { doc } : {}), knows: [], choices: [], memberRanges: [] };
|
|
263
|
+
if (m[1] === 'enum') {
|
|
264
|
+
for (const v of splitParams(body.replace(/\/\/[^\n]*/g, '').replace(/#\[[^\]]*\]/g, ''))) { const n = v.trim().match(/^(\w+)/); if (n) t.choices.push(words(n[1])); }
|
|
265
|
+
} else {
|
|
266
|
+
for (const f of splitParams(body.replace(/\/\/[^\n]*/g, '').replace(/#\[[^\]]*\]/g, ''))) { const fm = f.trim().match(/^(?:pub(?:\([^)]*\))?\s+)?(\w+)\s*:\s*(.+)$/s); if (fm) t.knows.push(know(fm[1], fm[2].trim(), null, {})); }
|
|
267
|
+
}
|
|
268
|
+
out.push(t); byName.set(m[2], t);
|
|
269
|
+
}
|
|
270
|
+
for (const m of src.matchAll(/(?:^|\n)[ \t]*impl(?:\s*<[^>]*>)?\s+(?:[\w:]+(?:<[^>]*>)?\s+for\s+)?(\w+)(?:<[^>]*>)?\s*(?:where[^{]*)?\{/g)) {
|
|
271
|
+
const t = byName.get(m[1]);
|
|
272
|
+
if (!t) continue;
|
|
273
|
+
const at = m.index + (m[0].startsWith('\n') ? 1 : 0);
|
|
274
|
+
const close = braceEnd(src, m.index + m[0].length - 1);
|
|
275
|
+
if (close < 0) continue;
|
|
276
|
+
if (/\bfn\s+\w+\s*(?:<[^>]*>)?\s*\(\s*&?(?:mut\s+)?self\b/.test(src.slice(at, close))) { if (t.kind === 'record') t.kind = 'thing'; }
|
|
277
|
+
t.memberRanges.push([lineAt(at), lineAt(close)]);
|
|
278
|
+
// Starting values from fn new() -> Self { Self { field: value, … } }.
|
|
279
|
+
// (Brace matching, not one big regex: a lazy match across a large impl block backtracks without end.)
|
|
280
|
+
const block = src.slice(at, close);
|
|
281
|
+
const fnNew = block.match(/fn\s+new\s*\(([^)]*)\)[^{;]*\{/);
|
|
282
|
+
const fnOpen = fnNew ? fnNew.index + fnNew[0].length - 1 : -1;
|
|
283
|
+
const fnBody = fnOpen >= 0 ? block.slice(fnOpen + 1, braceEnd(block, fnOpen)) : '';
|
|
284
|
+
const litAt = fnBody.search(new RegExp(`\\b(?:Self|${m[1]})\\s*\\{`));
|
|
285
|
+
const litOpen = litAt >= 0 ? fnBody.indexOf('{', litAt) : -1;
|
|
286
|
+
const ctor = litOpen >= 0 ? [null, fnNew[1], fnBody.slice(litOpen + 1, braceEnd(fnBody, litOpen))] : null;
|
|
287
|
+
if (ctor) {
|
|
288
|
+
const params = Object.fromEntries(splitParams(ctor[1]).map((p) => [p.split(':')[0].trim(), null]));
|
|
289
|
+
for (const kv of splitParams(ctor[2].replace(/\n/g, ','))) {
|
|
290
|
+
const f = kv.match(/^(\w+)(?:\s*:\s*(.+))?$/);
|
|
291
|
+
const k = f && t.knows.find((x) => x.name === f[1]);
|
|
292
|
+
if (k && !k.start) k.start = startValue((f[2] ?? f[1]).trim().replace(/^Vec::new\(\)$|^vec!\[\]$|^HashMap::new\(\)$|^String::new\(\)$/, '[]'), params);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Ruby: class … end (by indentation), attr_* facts, initialize assignments, Struct.new records. */
|
|
300
|
+
function rubyThings(src, file) {
|
|
301
|
+
const lines = src.split('\n');
|
|
302
|
+
const out = [];
|
|
303
|
+
for (let i = 0; i < lines.length; i++) {
|
|
304
|
+
const struct = lines[i].match(/^\s*(\w+)\s*=\s*(?:Struct|Data)\.(?:new|define)\(([^)]*)\)/);
|
|
305
|
+
if (struct) {
|
|
306
|
+
const t = { kind: 'record', name: struct[1], file, lines: [i + 1, i + 1], knows: [], choices: [] };
|
|
307
|
+
for (const s of struct[2].split(',')) { const n = s.trim().match(/^:(\w+)/); if (n) t.knows.push(know(n[1], null, null, {}, i + 1)); }
|
|
308
|
+
out.push(t);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const m = lines[i].match(/^(\s*)class\s+(\w+)/);
|
|
312
|
+
if (!m) continue;
|
|
313
|
+
const indent = m[1].length;
|
|
314
|
+
let end = i;
|
|
315
|
+
for (let j = i + 1; j < lines.length; j++) if (new RegExp(`^\\s{${indent}}end\\b`).test(lines[j]) && lines[j].match(/^\s*/)[0].length === indent) { end = j; break; }
|
|
316
|
+
if (end === i) continue;
|
|
317
|
+
const doc = (() => { const d = []; for (let k = i - 1; k >= 0 && /^\s*#/.test(lines[k]); k--) d.unshift(lines[k].replace(/^\s*#\s?/, '').trim()); return d.join(' ').trim() || null; })();
|
|
318
|
+
const t = { kind: 'thing', name: m[2], file, lines: [i + 1, end + 1], ...(doc && !/frozen_string_literal/.test(doc) ? { doc } : {}), knows: [], choices: [] };
|
|
319
|
+
const body = lines.slice(i + 1, end);
|
|
320
|
+
for (let k = 0; k < body.length; k++) {
|
|
321
|
+
const a = body[k].match(/^\s*attr_(?:reader|accessor|writer)\s+(.+)$/);
|
|
322
|
+
if (a) for (const s of a[1].split(',')) { const n = s.trim().match(/^:(\w+)/); if (n && !t.knows.some((x) => x.name === n[1])) t.knows.push(know(n[1], null, null, {}, i + 2 + k)); }
|
|
323
|
+
}
|
|
324
|
+
const init = body.findIndex((l) => /^\s*def\s+initialize\b/.test(l));
|
|
325
|
+
if (init >= 0) {
|
|
326
|
+
const sig = body[init].match(/def\s+initialize\s*\(?([^)]*)\)?/);
|
|
327
|
+
const params = {};
|
|
328
|
+
for (const p of splitParams(sig?.[1] || '')) { const pm = p.match(/^(\w+):?\s*(?:=\s*)?(.*)$/); if (pm) params[pm[1]] = pm[2] || null; }
|
|
329
|
+
const ii = body[init].match(/^\s*/)[0].length;
|
|
330
|
+
for (let k = init + 1; k < body.length; k++) {
|
|
331
|
+
if (body[k].trim() && body[k].match(/^\s*/)[0].length <= ii) break;
|
|
332
|
+
const a = body[k].trim().match(/^@(\w+)\s*=\s*(.+)$/);
|
|
333
|
+
if (!a) continue;
|
|
334
|
+
const kn = t.knows.find((x) => x.name === a[1]);
|
|
335
|
+
if (kn) { if (!kn.start) kn.start = startValue(a[2], params); }
|
|
336
|
+
else t.knows.push(know(a[1], null, a[2], params, i + 2 + k));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (!t.knows.length && !body.some((l) => /^\s*def\s/.test(l))) t.kind = 'record';
|
|
340
|
+
out.push(t);
|
|
341
|
+
}
|
|
342
|
+
return out;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return function thingsFor(language, src, file) {
|
|
346
|
+
if (language === 'go') return goThings(src, file);
|
|
347
|
+
if (language === 'rust') return rustThings(src, file);
|
|
348
|
+
if (language === 'ruby') return rubyThings(src, file);
|
|
349
|
+
if (['java', 'csharp', 'kotlin', 'php'].includes(language)) return curlyThings(src, file, language);
|
|
350
|
+
return [];
|
|
351
|
+
};
|
|
352
|
+
}
|
package/src/thunderize.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// its path, and an index (APP.thunder-text) reads the app from the top: areas, files, actions, rules. Browser-safe:
|
|
3
3
|
// it takes [{ path, source }] and returns text; the CLI writes the files.
|
|
4
4
|
import { readCode } from './read.mjs';
|
|
5
|
-
import { readableLanguage } from './read-repo.mjs';
|
|
5
|
+
import { readableLanguage, readingOf } from './read-repo.mjs';
|
|
6
6
|
import { thunderText, visualFor } from './visual.mjs';
|
|
7
7
|
|
|
8
8
|
export const THUNDERIZE_SCHEMA = 'thunderize-v1';
|
|
@@ -11,8 +11,7 @@ export const THUNDER_TEXT_EXT = '.thunder-text';
|
|
|
11
11
|
const LANGUAGE_NAMES = { typescript: 'TypeScript', javascript: 'JavaScript', python: 'Python', go: 'Go', rust: 'Rust', csharp: 'C#', java: 'Java', kotlin: 'Kotlin', php: 'PHP', ruby: 'Ruby' };
|
|
12
12
|
|
|
13
13
|
/** One file in Thunder Text: a header, then every action, rule, and function in order, then how it can fail. */
|
|
14
|
-
export function thunderizeFile(path, source, language = readableLanguage(path)) {
|
|
15
|
-
const reading = readCode(source, { language, file: path });
|
|
14
|
+
export function thunderizeFile(path, source, language = readableLanguage(path), reading = readCode(source, { language, file: path })) {
|
|
16
15
|
const main = reading.items.filter((i) => i.kind === 'decision' || i.kind === 'function');
|
|
17
16
|
const failures = [...new Set(reading.items.filter((i) => i.kind === 'error').map((i) => i.name))];
|
|
18
17
|
const counts = { actions: 0, rules: 0, functions: 0, notReadYet: 0, failures: failures.length };
|
|
@@ -47,7 +46,7 @@ export function thunderizeFiles(files, { name = 'app' } = {}) {
|
|
|
47
46
|
const language = readableLanguage(f.path);
|
|
48
47
|
if (!language) { skipped.push({ path: f.path, reason: 'not a language Thunder reads yet' }); continue; }
|
|
49
48
|
if (!f.source) { skipped.push({ path: f.path, reason: f.size ? 'too large to read' : 'empty' }); continue; }
|
|
50
|
-
try { out.push(thunderizeFile(f.path, f.source, language)); }
|
|
49
|
+
try { out.push(thunderizeFile(f.path, f.source, language, readingOf(f, language))); }
|
|
51
50
|
catch (e) { skipped.push({ path: f.path, reason: `could not be read: ${e.message}` }); }
|
|
52
51
|
}
|
|
53
52
|
out.sort((a, b) => a.path.localeCompare(b.path));
|
package/src/visual.mjs
CHANGED
|
@@ -130,7 +130,7 @@ export const LANGUAGES = {
|
|
|
130
130
|
kotlin: { name: 'Kotlin', status: 'available', action: 'Spring annotations', decision: 'a fun with when or if returning fixed answers', doc: '/** KDoc */', 'data-shape': 'data class, enum class (planned)' },
|
|
131
131
|
php: { name: 'PHP', status: 'available', action: '#[Route] (Symfony)', decision: 'a function that returns fixed answers', doc: '/** PHPDoc */', 'data-shape': 'class, enum (planned)' },
|
|
132
132
|
ruby: { name: 'Ruby', status: 'available', action: 'Sinatra get/post blocks (Rails routes.rb planned)', decision: 'a def that returns fixed answers', doc: '# comment above the def', 'data-shape': 'class, Struct (planned)' },
|
|
133
|
-
sql: { name: 'SQL', status: '
|
|
133
|
+
sql: { name: 'SQL', status: 'available', action: '(none: SQL is reached through the app)', decision: 'CHECK and UNIQUE constraints; IF in functions and procedures', doc: '-- comment above, COMMENT ON', 'data-shape': 'CREATE TABLE / VIEW', event: 'CREATE TRIGGER', steps: 'SELECT / INSERT / UPDATE / DELETE: which data, filtered by which conditions, changed how' },
|
|
134
134
|
};
|
|
135
135
|
|
|
136
136
|
/** Rules every Thunder view follows. A view that breaks one is wrong, however good it looks. */
|