hivecode-mcp 0.8.0 → 0.11.0
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/README.md +116 -68
- package/core.js +62 -13
- package/hive-mcp.js +458 -48
- package/icr.js +502 -35
- package/lang-brace.js +240 -174
- package/lang-js.js +642 -418
- package/lang-json.js +184 -0
- package/lang-python.js +360 -149
- package/lang-ruby.js +230 -0
- package/lang-toml.js +136 -0
- package/lang-yaml.js +151 -0
- package/package.json +97 -70
- package/sync.js +220 -10
- package/MCP.md +0 -77
package/lang-python.js
CHANGED
|
@@ -1,149 +1,360 @@
|
|
|
1
|
-
// Python language provider for ICR — indentation-based structural splitter.
|
|
2
|
-
// Implements the ICR provider contract { id, exts, parses, units, declaredNames,
|
|
3
|
-
// splitUnit, parsesUnit } so the shared merge engine merges Python by structure:
|
|
4
|
-
// top-level defs/classes/statements are units keyed by name; editing different
|
|
5
|
-
// methods of the same class merges via splitUnit. Heuristic (tracks #-comments,
|
|
6
|
-
// triple-quoted and normal strings, bracket depth) — not a full parser; parses()
|
|
7
|
-
// is an approximate balanced-delimiter check.
|
|
8
|
-
|
|
9
|
-
function scannerAt(src) {
|
|
10
|
-
const n = src.length
|
|
11
|
-
return function at(i) {
|
|
12
|
-
if (src[i] === '#') { let j = i + 1; while (j < n && src[j] !== '\n') j++; return j }
|
|
13
|
-
for (const q of ['"""', "'''"]) if (src.startsWith(q, i)) { let j = i + 3; while (j < n && !src.startsWith(q, j)) { if (src[j] === '\\') j++; j++ } return Math.min(n, j + 3) }
|
|
14
|
-
if (src[i] === '"' || src[i] === "'") { const q = src[i]; let j = i + 1; while (j < n) { if (src[j] === '\\') { j += 2; continue } if (src[j] === q) { j++; break } if (src[j] === '\n') break; j++ } return j }
|
|
15
|
-
return -1
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Line records: { start, end (incl newline), indent, blank, text }
|
|
20
|
-
function lines(src) {
|
|
21
|
-
const out = []
|
|
22
|
-
let i = 0
|
|
23
|
-
while (i < src.length) {
|
|
24
|
-
let e = src.indexOf('\n', i); if (e < 0) e = src.length; else e = e + 1
|
|
25
|
-
const raw = src.slice(i, e)
|
|
26
|
-
const trimmed = raw.replace(/\r?\n$/, '')
|
|
27
|
-
let indent = 0; while (indent < trimmed.length && (trimmed[indent] === ' ' || trimmed[indent] === '\t')) indent++
|
|
28
|
-
const blank = trimmed.trim() === '' || trimmed.trim().startsWith('#')
|
|
29
|
-
out.push({ start: i, end: e, indent, blank, text: raw })
|
|
30
|
-
i = e
|
|
31
|
-
}
|
|
32
|
-
return out
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const HEAD = /^\s*(?:async\s+def|def)\s+([A-Za-z_]\w*)/
|
|
36
|
-
const CLS = /^\s*class\s+([A-Za-z_]\w*)/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
1
|
+
// Python language provider for ICR — indentation-based structural splitter.
|
|
2
|
+
// Implements the ICR provider contract { id, exts, parses, units, declaredNames,
|
|
3
|
+
// splitUnit, parsesUnit } so the shared merge engine merges Python by structure:
|
|
4
|
+
// top-level defs/classes/statements are units keyed by name; editing different
|
|
5
|
+
// methods of the same class merges via splitUnit. Heuristic (tracks #-comments,
|
|
6
|
+
// triple-quoted and normal strings, bracket depth) — not a full parser; parses()
|
|
7
|
+
// is an approximate balanced-delimiter check.
|
|
8
|
+
|
|
9
|
+
function scannerAt(src) {
|
|
10
|
+
const n = src.length
|
|
11
|
+
return function at(i) {
|
|
12
|
+
if (src[i] === '#') { let j = i + 1; while (j < n && src[j] !== '\n') j++; return j }
|
|
13
|
+
for (const q of ['"""', "'''"]) if (src.startsWith(q, i)) { let j = i + 3; while (j < n && !src.startsWith(q, j)) { if (src[j] === '\\') j++; j++ } return Math.min(n, j + 3) }
|
|
14
|
+
if (src[i] === '"' || src[i] === "'") { const q = src[i]; let j = i + 1; while (j < n) { if (src[j] === '\\') { j += 2; continue } if (src[j] === q) { j++; break } if (src[j] === '\n') break; j++ } return j }
|
|
15
|
+
return -1
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Line records: { start, end (incl newline), indent, blank, text }
|
|
20
|
+
function lines(src) {
|
|
21
|
+
const out = []
|
|
22
|
+
let i = 0
|
|
23
|
+
while (i < src.length) {
|
|
24
|
+
let e = src.indexOf('\n', i); if (e < 0) e = src.length; else e = e + 1
|
|
25
|
+
const raw = src.slice(i, e)
|
|
26
|
+
const trimmed = raw.replace(/\r?\n$/, '')
|
|
27
|
+
let indent = 0; while (indent < trimmed.length && (trimmed[indent] === ' ' || trimmed[indent] === '\t')) indent++
|
|
28
|
+
const blank = trimmed.trim() === '' || trimmed.trim().startsWith('#')
|
|
29
|
+
out.push({ start: i, end: e, indent, blank, text: raw })
|
|
30
|
+
i = e
|
|
31
|
+
}
|
|
32
|
+
return out
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const HEAD = /^\s*(?:async\s+def|def)\s+([A-Za-z_]\w*)/
|
|
36
|
+
const CLS = /^\s*class\s+([A-Za-z_]\w*)/
|
|
37
|
+
// Continuation clauses of a COMPOUND statement. They are not independent statements —
|
|
38
|
+
// an `else:` only means something relative to its `if`/`for`/`try` — so blocks() must
|
|
39
|
+
// attach them to the preceding unit. Keying them separately let two sides' restructured
|
|
40
|
+
// if/else halves interleave into adjacent dangling `else:` blocks (ConGra, transformers
|
|
41
|
+
// file_utils.py: output had `else:` twice in a row — ast-invalid, heuristics blind).
|
|
42
|
+
const COMPOUND_CONT = /^\s*(?:else\b|elif\b|except\b|finally\b)/
|
|
43
|
+
|
|
44
|
+
function keyOf(text) {
|
|
45
|
+
let m = text.match(HEAD); if (m) return 'def:' + m[1]
|
|
46
|
+
m = text.match(CLS); if (m) return 'class:' + m[1]
|
|
47
|
+
return 'stmt:' + text.split('\n')[0].trim().replace(/\s+/g, ' ')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Split src into units whose heads sit at exactly `base` indentation. A def/class
|
|
51
|
+
// head owns the following blank + more-indented lines (its body). A run of leading
|
|
52
|
+
// @decorator lines attaches to the head that follows. Everything else is a
|
|
53
|
+
// one-logical-line statement (continued while bracket depth > 0).
|
|
54
|
+
function blocks(src, base = 0) {
|
|
55
|
+
const L = lines(src)
|
|
56
|
+
const at = scannerAt(src)
|
|
57
|
+
const units = []
|
|
58
|
+
let i = 0
|
|
59
|
+
// find bracket depth over a line range (to keep continued statements together)
|
|
60
|
+
const bracketDelta = (s, e) => {
|
|
61
|
+
let d = 0, k = s
|
|
62
|
+
while (k < e) { const j = at(k); if (j > k) { k = j; continue } const c = src[k]; if (c === '(' || c === '[' || c === '{') d++; else if (c === ')' || c === ']' || c === '}') d--; k++ }
|
|
63
|
+
return d
|
|
64
|
+
}
|
|
65
|
+
while (i < L.length) {
|
|
66
|
+
while (i < L.length && L[i].text.trim() === '') i++ // skip ONLY pure blank lines, NOT comments
|
|
67
|
+
if (i >= L.length) break
|
|
68
|
+
if (L[i].indent !== base) { // stray deeper line (shouldn't happen at top) — treat as its own stmt
|
|
69
|
+
const s = L[i].start; let e = L[i].end; i++
|
|
70
|
+
units.push({ start: s, end: e })
|
|
71
|
+
continue
|
|
72
|
+
}
|
|
73
|
+
const start = L[i].start
|
|
74
|
+
// consume leading decorators
|
|
75
|
+
let isDecorator = /^\s*@/.test(L[i].text)
|
|
76
|
+
while (i < L.length && L[i].indent === base && /^\s*@/.test(L[i].text)) i++
|
|
77
|
+
const headIdx = i
|
|
78
|
+
const head = i < L.length ? L[i] : null
|
|
79
|
+
if (head && (HEAD.test(head.text) || CLS.test(head.text) || isDecorator)) {
|
|
80
|
+
// a block: consume the header line, then its body (blank or deeper-indented lines)
|
|
81
|
+
i++ // header line
|
|
82
|
+
while (i < L.length && (L[i].blank || L[i].indent > base)) i++
|
|
83
|
+
// Trailing PURE-BLANK lines are GAP text, not part of the unit — if the unit owned
|
|
84
|
+
// them, appending a new unit after the last one would mutate the last unit's text
|
|
85
|
+
// and raise a false same-key conflict (found by multilang fuzz). Trailing comments
|
|
86
|
+
// stay attached (comment-attachment convention).
|
|
87
|
+
let last = i - 1
|
|
88
|
+
while (last > headIdx && L[last].text.trim() === '') last--
|
|
89
|
+
units.push({ start, end: L[last].end })
|
|
90
|
+
} else if (head) {
|
|
91
|
+
// simple statement: consume continued lines while bracket depth remains open
|
|
92
|
+
let e = head.end, depth = bracketDelta(head.start, head.end); i++
|
|
93
|
+
while (i < L.length && (depth > 0 || /\\\s*$/.test(L[i - 1].text.replace(/\r?\n$/, '')))) { depth += bracketDelta(L[i].start, L[i].end); e = L[i].end; i++ }
|
|
94
|
+
// COMPOUND statement (if/for/while/try/with/else/…): the logical line ends with ':',
|
|
95
|
+
// so this header OWNS its indented body — one unit, like def/class. Without this the
|
|
96
|
+
// body lines become stray per-line units and the clause structure can't survive a
|
|
97
|
+
// structured merge coherently.
|
|
98
|
+
const logicalEndsColon = (() => {
|
|
99
|
+
const at = scannerAt(src)
|
|
100
|
+
let lastC = '', k = start
|
|
101
|
+
while (k < e) { const j = at(k); if (j > k) { if (src[k] !== '#') lastC = 's'; k = Math.min(e, j); continue } const c = src[k]; if (c !== ' ' && c !== '\t' && c !== '\r' && c !== '\n') lastC = c; k++ }
|
|
102
|
+
return lastC === ':'
|
|
103
|
+
})()
|
|
104
|
+
if (logicalEndsColon) {
|
|
105
|
+
const bodyHead = i - 1
|
|
106
|
+
while (i < L.length && (L[i].blank || L[i].indent > base)) i++
|
|
107
|
+
let last = i - 1
|
|
108
|
+
while (last > bodyHead && L[last].text.trim() === '') last--
|
|
109
|
+
e = L[last].end
|
|
110
|
+
}
|
|
111
|
+
// Continuation clause (else/elif/except/finally): not an independent statement —
|
|
112
|
+
// extend the PREVIOUS unit so the whole compound statement is one mergeable unit.
|
|
113
|
+
if (COMPOUND_CONT.test(head.text) && units.length) units[units.length - 1].end = e
|
|
114
|
+
else units.push({ start, end: e })
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return units
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function keyed(raw, src) {
|
|
121
|
+
const seen = new Map(), out = []
|
|
122
|
+
for (const u of raw) {
|
|
123
|
+
const text = src.slice(u.start, u.end)
|
|
124
|
+
let k = keyOf(text)
|
|
125
|
+
const c = (seen.get(k) || 0) + 1; seen.set(k, c)
|
|
126
|
+
if (c > 1) k += '#' + c
|
|
127
|
+
out.push({ key: k, text, start: u.start, end: u.end })
|
|
128
|
+
}
|
|
129
|
+
return out
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function balanced(src) {
|
|
133
|
+
const at = scannerAt(src)
|
|
134
|
+
const stack = []
|
|
135
|
+
const pair = { ')': '(', ']': '[', '}': '{' }
|
|
136
|
+
let i = 0
|
|
137
|
+
while (i < src.length) {
|
|
138
|
+
const j = at(i); if (j > i) { if (j > src.length) return false; i = j; continue }
|
|
139
|
+
const c = src[i]
|
|
140
|
+
if (c === '(' || c === '[' || c === '{') stack.push(c)
|
|
141
|
+
else if (pair[c]) { if (stack.pop() !== pair[c]) return false }
|
|
142
|
+
i++
|
|
143
|
+
}
|
|
144
|
+
return stack.length === 0
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// INDENTATION validity — the half of Python syntax that delimiter balance cannot see.
|
|
148
|
+
// Walks LOGICAL lines (joining bracket continuations, backslash continuations, and
|
|
149
|
+
// multi-line strings) and enforces Python's block rules: a line ending with ':' must
|
|
150
|
+
// open a deeper-indented block; an indent may only appear after such an opener; every
|
|
151
|
+
// dedent must land on an indent level already on the stack. This is what lets parses()
|
|
152
|
+
// refuse a merge that spliced statements into the wrong block — balance alone passes it.
|
|
153
|
+
// The first line's indent is taken as the baseline so unit fragments validate too.
|
|
154
|
+
function indentsOk(src) {
|
|
155
|
+
const at = scannerAt(src)
|
|
156
|
+
const n = src.length
|
|
157
|
+
let stack = null // [indent levels]; seeded from the first logical line
|
|
158
|
+
let i = 0, prevOpened = false
|
|
159
|
+
while (i < n) {
|
|
160
|
+
let indent = 0
|
|
161
|
+
while (i < n && (src[i] === ' ' || src[i] === '\t')) { indent++; i++ }
|
|
162
|
+
if (i >= n) break
|
|
163
|
+
if (src[i] === '\r' || src[i] === '\n') { i++; continue } // blank line
|
|
164
|
+
if (src[i] === '#') { i = Math.min(n, at(i)); continue } // comment-only line
|
|
165
|
+
if (stack == null) stack = [indent] // baseline (fragment-friendly)
|
|
166
|
+
if (prevOpened) {
|
|
167
|
+
if (indent <= stack[stack.length - 1]) return false // ':' promised a block; none came
|
|
168
|
+
stack.push(indent)
|
|
169
|
+
} else if (indent > stack[stack.length - 1]) {
|
|
170
|
+
return false // indent without an opener
|
|
171
|
+
} else if (indent < stack[stack.length - 1]) {
|
|
172
|
+
while (stack.length > 1 && stack[stack.length - 1] > indent) stack.pop()
|
|
173
|
+
if (stack[stack.length - 1] !== indent) return false // dedent to a level never opened
|
|
174
|
+
}
|
|
175
|
+
// scan to the end of this LOGICAL line (brackets, backslash, strings span physical lines)
|
|
176
|
+
let depth = 0, last = ''
|
|
177
|
+
while (i < n) {
|
|
178
|
+
const j = at(i)
|
|
179
|
+
if (j > i) { if (src[i] !== '#') last = 's'; i = Math.min(n, j); continue } // string counts as content; comment doesn't
|
|
180
|
+
const c = src[i]
|
|
181
|
+
if (c === '(' || c === '[' || c === '{') depth++
|
|
182
|
+
else if (c === ')' || c === ']' || c === '}') depth--
|
|
183
|
+
if (c === '\n') {
|
|
184
|
+
if (depth > 0 || last === '\\') { if (last === '\\') last = ''; i++; continue } // continuation
|
|
185
|
+
i++; break
|
|
186
|
+
}
|
|
187
|
+
if (c !== ' ' && c !== '\t' && c !== '\r') last = c
|
|
188
|
+
i++
|
|
189
|
+
}
|
|
190
|
+
prevOpened = last === ':'
|
|
191
|
+
}
|
|
192
|
+
return !prevOpened // must not end still promising a block
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Generic lexer for the token-level inner merge + the intent layer. Identifiers,
|
|
196
|
+
// numbers, whole strings are tokens; comments live in the gaps (a comment-only edit
|
|
197
|
+
// is a no-op at this tier, same contract as the JS provider).
|
|
198
|
+
function lexTokens(src) {
|
|
199
|
+
const at = scannerAt(src)
|
|
200
|
+
const out = []
|
|
201
|
+
const n = src.length
|
|
202
|
+
let i = 0
|
|
203
|
+
while (i < n) {
|
|
204
|
+
const c = src[i]
|
|
205
|
+
if (c === ' ' || c === '\t' || c === '\r' || c === '\n') { i++; continue }
|
|
206
|
+
const j = at(i)
|
|
207
|
+
if (j > i) {
|
|
208
|
+
if (c === '#') { i = j; continue } // comment → gap
|
|
209
|
+
out.push({ start: i, end: j, k: src.slice(i, j) }); i = j; continue // string → one token
|
|
210
|
+
}
|
|
211
|
+
if (/[A-Za-z_]/.test(c)) { let e = i + 1; while (e < n && /\w/.test(src[e])) e++; out.push({ start: i, end: e, k: src.slice(i, e) }); i = e; continue }
|
|
212
|
+
if (/[0-9]/.test(c)) { let e = i + 1; while (e < n && /[\w.]/.test(src[e])) e++; out.push({ start: i, end: e, k: src.slice(i, e) }); i = e; continue }
|
|
213
|
+
out.push({ start: i, end: i + 1, k: c }); i++
|
|
214
|
+
}
|
|
215
|
+
return out
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Token stream for the token-level MERGE tier, in an indentation language. Unlike
|
|
219
|
+
// lexTokens (used by the intent layer, where whitespace is noise), this emits a NEWLINE
|
|
220
|
+
// token for every real line break — a statement boundary. Without it a token splice can
|
|
221
|
+
// weld two statements onto one physical line (`x = f(y)if z:`), which balance+indent
|
|
222
|
+
// checks read as a single logical line and wrongly accept — a parse-guarantee violation
|
|
223
|
+
// found by the ConGra corpus (ast.parse rejects it). With newlines as tokens, any spliced
|
|
224
|
+
// run that crosses a line carries its break, so statements can never fuse. Newlines inside
|
|
225
|
+
// strings/comments stay inside those tokens; a bare `\n` between brackets is still emitted
|
|
226
|
+
// (harmless: it only ever makes the token diff finer, never coarser).
|
|
227
|
+
function lexTokensNL(src) {
|
|
228
|
+
const at = scannerAt(src)
|
|
229
|
+
const out = []
|
|
230
|
+
const n = src.length
|
|
231
|
+
let i = 0, depth = 0, contBackslash = false
|
|
232
|
+
while (i < n) {
|
|
233
|
+
const c = src[i]
|
|
234
|
+
if (c === '\n') {
|
|
235
|
+
// Emit a NEWLINE token only at a real statement boundary: not inside brackets, not
|
|
236
|
+
// after a line-continuation backslash. Inside brackets a newline is insignificant
|
|
237
|
+
// (like whitespace) — emitting it there corrupts multi-line-call token interleaving.
|
|
238
|
+
if (depth === 0 && !contBackslash) out.push({ start: i, end: i + 1, k: '\n' })
|
|
239
|
+
contBackslash = false; i++; continue
|
|
240
|
+
}
|
|
241
|
+
if (c === ' ' || c === '\t' || c === '\r') { i++; continue }
|
|
242
|
+
const j = at(i)
|
|
243
|
+
if (j > i) {
|
|
244
|
+
contBackslash = false
|
|
245
|
+
if (c === '#') { i = j; continue }
|
|
246
|
+
out.push({ start: i, end: j, k: src.slice(i, j) }); i = j; continue
|
|
247
|
+
}
|
|
248
|
+
if (c === '\\') { contBackslash = true; i++; continue }
|
|
249
|
+
contBackslash = false
|
|
250
|
+
if (c === '(' || c === '[' || c === '{') depth++
|
|
251
|
+
else if (c === ')' || c === ']' || c === '}') { if (depth > 0) depth-- }
|
|
252
|
+
if (/[A-Za-z_]/.test(c)) { let e = i + 1; while (e < n && /\w/.test(src[e])) e++; out.push({ start: i, end: e, k: src.slice(i, e) }); i = e; continue }
|
|
253
|
+
if (/[0-9]/.test(c)) { let e = i + 1; while (e < n && /[\w.]/.test(src[e])) e++; out.push({ start: i, end: e, k: src.slice(i, e) }); i = e; continue }
|
|
254
|
+
out.push({ start: i, end: i + 1, k: c }); i++
|
|
255
|
+
}
|
|
256
|
+
return out
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Reference-position identifier (not attribute access `.name`)?
|
|
260
|
+
function isRefToken(src, t) {
|
|
261
|
+
if (!/^[A-Za-z_]\w*$/.test(t.k)) return false
|
|
262
|
+
let p = t.start - 1
|
|
263
|
+
while (p >= 0 && (src[p] === ' ' || src[p] === '\t')) p--
|
|
264
|
+
return !(p >= 0 && src[p] === '.')
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// PER-STATEMENT bracket nesting — the soundness backstop the whole-file `balanced()`
|
|
268
|
+
// (global) and `indentsOk()` (indentation) both miss. A token merge can splice a result
|
|
269
|
+
// that is GLOBALLY balanced yet LOCALLY mis-nested: side A reformats `f(x)` to span lines
|
|
270
|
+
// and a concurrent edit detaches the `)`, so `( ... { ... )` lands in one statement and a
|
|
271
|
+
// stray `)` balances the count elsewhere. In VALID Python every logical statement is
|
|
272
|
+
// independently balanced AND properly nested, so we check exactly that: segment the token
|
|
273
|
+
// stream at depth-0 NEWLINE tokens (which lexTokensNL only emits between statements) and
|
|
274
|
+
// require each segment's brackets to nest correctly. Sound — it never rejects valid Python
|
|
275
|
+
// — and it closes the token-merge corruption class the ConGra corpus exposed via ast.parse.
|
|
276
|
+
function stmtBracketsOk(src) {
|
|
277
|
+
const pair = { ')': '(', ']': '[', '}': '{' }
|
|
278
|
+
let stack = []
|
|
279
|
+
for (const t of lexTokensNL(src)) {
|
|
280
|
+
if (t.k === '\n') { if (stack.length) return false; continue } // statement boundary: must be closed
|
|
281
|
+
if (t.k === '(' || t.k === '[' || t.k === '{') stack.push(t.k)
|
|
282
|
+
else if (pair[t.k]) { if (stack.pop() !== pair[t.k]) return false }
|
|
283
|
+
}
|
|
284
|
+
return stack.length === 0
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export const python = {
|
|
288
|
+
id: 'python',
|
|
289
|
+
exts: ['.py', '.pyi'],
|
|
290
|
+
parses: (src) => { const s = String(src == null ? '' : src); return balanced(s) && stmtBracketsOk(s) && indentsOk(s) },
|
|
291
|
+
parsesUnit: (src) => { const s = String(src); return balanced(s) && indentsOk(s) },
|
|
292
|
+
units: (src) => keyed(blocks(String(src), 0), String(src)),
|
|
293
|
+
declaredNames: (src) => {
|
|
294
|
+
const names = new Set()
|
|
295
|
+
for (const u of keyed(blocks(String(src), 0), String(src))) {
|
|
296
|
+
const m = u.key.match(/^(?:def|class):([A-Za-z_]\w*)/)
|
|
297
|
+
if (m) names.add(m[1])
|
|
298
|
+
}
|
|
299
|
+
return names
|
|
300
|
+
},
|
|
301
|
+
// --- intent layer (heuristic but honest): dangling refs, rename, token merge ---
|
|
302
|
+
// Every identifier used in reference position (skips `.attribute` access). Approximate
|
|
303
|
+
// and OVER-inclusive — which only makes the dangling check MORE cautious: a deleted-
|
|
304
|
+
// but-still-referenced def/class is surfaced as a conflict, never shipped broken.
|
|
305
|
+
usedIdentifiers: (src) => {
|
|
306
|
+
src = String(src)
|
|
307
|
+
const used = new Set()
|
|
308
|
+
for (const t of lexTokens(src)) if (isRefToken(src, t)) used.add(t.k)
|
|
309
|
+
return used
|
|
310
|
+
},
|
|
311
|
+
// The def/class text with its own name excluded, so renamed twins compare equal.
|
|
312
|
+
// trimEnd(): a unit owns its trailing blank lines, and splicing can change how many
|
|
313
|
+
// survive — trailing whitespace must not defeat the rename match.
|
|
314
|
+
declBody: (src, name) => {
|
|
315
|
+
src = String(src)
|
|
316
|
+
for (const u of keyed(blocks(src, 0), src)) {
|
|
317
|
+
const m = u.key.match(/^(?:def|class):([A-Za-z_]\w*)/)
|
|
318
|
+
if (!m || m[1] !== name) continue
|
|
319
|
+
return u.text.replace(name, '').trimEnd()
|
|
320
|
+
}
|
|
321
|
+
return null
|
|
322
|
+
},
|
|
323
|
+
// Rewrite reference-position uses of oldName (never `.oldName` attribute access).
|
|
324
|
+
// The engine only accepts the rewrite if the result still passes parses().
|
|
325
|
+
renameRefs: (src, oldName, newName) => {
|
|
326
|
+
src = String(src)
|
|
327
|
+
const spots = []
|
|
328
|
+
for (const t of lexTokens(src)) if (t.k === oldName && isRefToken(src, t)) spots.push(t)
|
|
329
|
+
let out = src
|
|
330
|
+
for (const t of spots.reverse()) out = out.slice(0, t.start) + newName + out.slice(t.end)
|
|
331
|
+
return out
|
|
332
|
+
},
|
|
333
|
+
// Finest granularity: token stream for the token-level inner merge. NEWLINE-aware
|
|
334
|
+
// (lexTokensNL) so a token splice can never weld two statements onto one line — the
|
|
335
|
+
// parse guarantee holds even where the indentation heuristic alone would be fooled.
|
|
336
|
+
tokenize: (src) => lexTokensNL(String(src)),
|
|
337
|
+
// Descend into a class/def body so two agents editing DIFFERENT methods merge.
|
|
338
|
+
splitUnit: (text) => {
|
|
339
|
+
const src = String(text)
|
|
340
|
+
const L = lines(src)
|
|
341
|
+
let h = 0; while (h < L.length && (L[h].blank || /^\s*@/.test(L[h].text))) h++
|
|
342
|
+
if (h >= L.length) return null
|
|
343
|
+
// Only descend into a CLASS (named methods → mergeable). A def body is bare
|
|
344
|
+
// statements with no stable keys; descending would falsely merge two edits to the
|
|
345
|
+
// same function, so we don't — same-def edits become a clean conflict instead.
|
|
346
|
+
if (!CLS.test(L[h].text)) return null
|
|
347
|
+
const headEnd = L[h].end
|
|
348
|
+
// body = everything after the header line; its base indent is the first body line's indent
|
|
349
|
+
let b = h + 1; while (b < L.length && L[b].blank) b++
|
|
350
|
+
if (b >= L.length) return null
|
|
351
|
+
const bodyIndent = L[b].indent
|
|
352
|
+
const sig = src.slice(0, headEnd)
|
|
353
|
+
const body = src.slice(headEnd)
|
|
354
|
+
const inner = keyed(blocks(body, bodyIndent), body)
|
|
355
|
+
// spliceable: inner units carry byte ranges in `body`, so the engine rebuilds the
|
|
356
|
+
// class body by splicing merged methods back in — comments/blank lines between
|
|
357
|
+
// methods survive verbatim instead of being reflowed by a join.
|
|
358
|
+
return { sig, open: '', close: '', body, units: inner, join: '', spliceable: true }
|
|
359
|
+
},
|
|
360
|
+
}
|