STRA-338 S12b: fns and impl blocks of 59 cold oversize modules move into child modules (model-format, gateway, desktop-core, leaf libraries)
scripts/dev/split_module.py (lexer-based, with 16 self-tests) moves the top-level fn and impl items of 59 oversize module files in control-plane, desktop-core, model-format, gateway-text, leleka-memory, experimental-ref, context, serving-policy, tokenizer, backend-api, quant-core, harmony, igpu, libc-shim, npu-service and runtime-client into foo/<name>.rs groups of about 250 lines; types, constants, macros, nested modules and tests stay, every path stays valid through pub use re-exports, restricted visibilities keep their scope one level down, include paths and super:: paths are shifted. Not touched: leleka-runtime, cuda-driver, native-cpu-expert, sampler-ref and cpu-* (hot path, next wave) and the files kar-off-ab edits. The baseline drops from 311 to 263 oversize files and from 273,600 to about 233,000 lines; the path-include ratchet ignores dir/name.rs child declarations.
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
"""Top-level items of a Rust file: where each starts and ends, what it is, and what must change when it moves."""
|
||||
from dataclasses import dataclass
|
||||
|
||||
from .lexer import tokenize
|
||||
|
||||
OPEN, CLOSE = "{([", "})]"
|
||||
BRACE_ITEMS = {"fn", "impl", "trait", "mod", "struct", "enum", "union", "macro_rules", "extern"}
|
||||
SEMI_ITEMS = {"use", "const", "static", "type", "crate"}
|
||||
|
||||
|
||||
@dataclass
|
||||
class Item:
|
||||
start: int # first character carried by the item: the comments and blanks before it
|
||||
head: int # first character of the item proper (after attributes and doc comments)
|
||||
end: int # one past the last character
|
||||
kind: str # fn impl or other
|
||||
name: str
|
||||
attrs: str # text of the outer attributes
|
||||
inherent: bool = False
|
||||
|
||||
|
||||
def significant(tokens):
|
||||
return [t for t in tokens if t.kind not in ("ws", "comment")]
|
||||
|
||||
|
||||
def _skip_bracket(sig, k):
|
||||
"""Index after the balanced `[ ... ]` group whose `[` is at sig[k]."""
|
||||
depth = 0
|
||||
while k < len(sig):
|
||||
depth += (sig[k].text == "[") - (sig[k].text == "]")
|
||||
k += 1
|
||||
if depth == 0:
|
||||
break
|
||||
return k
|
||||
|
||||
|
||||
def _skip_attrs(sig, k):
|
||||
"""Index of the first token after the outer attributes starting at k."""
|
||||
while k + 1 < len(sig) and sig[k].text == "#" and sig[k + 1].text == "[":
|
||||
k = _skip_bracket(sig, k + 1)
|
||||
return k
|
||||
|
||||
|
||||
def _skip_visibility(sig, k):
|
||||
if sig[k].text != "pub":
|
||||
return k
|
||||
k += 1
|
||||
if sig[k].text == "(":
|
||||
while sig[k].text != ")":
|
||||
k += 1
|
||||
k += 1
|
||||
return k
|
||||
|
||||
|
||||
def _keyword(sig, k):
|
||||
"""The keyword naming the item whose first token (after attributes and visibility) is sig[k], and its index."""
|
||||
while sig[k].text in ("unsafe", "async", "default", "const", "extern"):
|
||||
word, nxt = sig[k].text, sig[k + 1]
|
||||
if word == "const" and nxt.text not in ("fn", "unsafe", "async", "extern"):
|
||||
break
|
||||
if word == "extern":
|
||||
skip = 2 if nxt.kind == "string" else 1
|
||||
if sig[k + skip].text not in ("fn", "unsafe"):
|
||||
break
|
||||
k += skip
|
||||
continue
|
||||
k += 1
|
||||
return sig[k].text, k
|
||||
|
||||
|
||||
def _ends_with_brace(sig, k, bang_brace):
|
||||
"""Index of the last token when the `}` at sig[k] closes a braced item (a macro call may be followed by `;`)."""
|
||||
return k + 1 if bang_brace and k + 1 < len(sig) and sig[k + 1].text == ";" else k
|
||||
|
||||
|
||||
def _item_end(sig, k, keyword):
|
||||
"""Index in `sig` of the last token of the item whose keyword is at sig[k]."""
|
||||
depth, semi_only = 0, keyword in SEMI_ITEMS
|
||||
bang_brace = keyword not in BRACE_ITEMS | SEMI_ITEMS and sig[k + 1].text == "!"
|
||||
while k < len(sig):
|
||||
text = sig[k].text
|
||||
depth += (text in OPEN) - (text in CLOSE)
|
||||
if depth == 0 and (text == ";" or text == "}" and not semi_only):
|
||||
return k if text == ";" else _ends_with_brace(sig, k, bang_brace)
|
||||
k += 1
|
||||
return len(sig) - 1
|
||||
|
||||
|
||||
def _impl_name(sig, k):
|
||||
"""`Foo` for `impl Foo`, `Trait_for_Foo` for `impl<T> Trait for Foo`; at most three identifiers."""
|
||||
j, depth, words = k + 1, 0, []
|
||||
while j < len(sig) and sig[j].text not in ("{", "where"):
|
||||
depth += (sig[j].text == "<") - (sig[j].text == ">")
|
||||
if depth == 0 and sig[j].kind == "ident" and sig[j].text != "for":
|
||||
words.append(sig[j].text)
|
||||
elif depth == 0 and sig[j].text == "for":
|
||||
words.append("for")
|
||||
j += 1
|
||||
return "_".join(words[:3]) or "impl"
|
||||
|
||||
|
||||
def _name_of(sig, k, keyword):
|
||||
if keyword == "impl":
|
||||
return _impl_name(sig, k)
|
||||
return sig[k + 1].text if k + 1 < len(sig) and sig[k + 1].kind == "ident" else keyword
|
||||
|
||||
|
||||
def _is_inherent(sig, k, end):
|
||||
"""An `impl` is inherent when no `for` appears before its opening brace."""
|
||||
for j in range(k, end):
|
||||
if sig[j].text == "{":
|
||||
return True
|
||||
if sig[j].text == "for":
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def preamble_end(text, tokens):
|
||||
"""End of the file header: `//!` docs, `#![...]` inner attributes and the blanks between them."""
|
||||
sig = significant(tokens)
|
||||
end, k = 0, 0
|
||||
for t in tokens:
|
||||
if t.kind == "comment" and t.text.startswith(("//!", "/*!")) or t.kind == "ws":
|
||||
end = t.end
|
||||
elif t.kind != "comment" or t.text.startswith("//"):
|
||||
break
|
||||
while k + 2 < len(sig) and sig[k].text == "#" and sig[k + 1].text == "!":
|
||||
k = _skip_bracket(sig, k + 2)
|
||||
end = max(end, sig[k - 1].end)
|
||||
return end, k
|
||||
|
||||
|
||||
def parse_items(text):
|
||||
"""Top-level items of `text` in order; the text between items is carried by the item that follows it."""
|
||||
tokens = tokenize(text)
|
||||
sig = significant(tokens)
|
||||
previous_end, k = preamble_end(text, tokens)
|
||||
items = []
|
||||
while k < len(sig):
|
||||
attrs_end = _skip_attrs(sig, k)
|
||||
if attrs_end >= len(sig):
|
||||
break
|
||||
keyword, at = _keyword(sig, _skip_visibility(sig, attrs_end))
|
||||
last = _item_end(sig, at, keyword)
|
||||
kind = keyword if keyword in ("fn", "impl") else "other"
|
||||
items.append(Item(previous_end, sig[attrs_end].start, sig[last].end, kind, _name_of(sig, at, keyword),
|
||||
text[sig[k].start:sig[attrs_end].start], kind == "impl" and _is_inherent(sig, at, last)))
|
||||
previous_end, k = sig[last].end, last + 1
|
||||
return items
|
||||
Reference in New Issue
Block a user