Skip to content

Instantly share code, notes, and snippets.

@deanm0000
Last active June 7, 2024 15:00
Show Gist options
  • Save deanm0000/e16b94e3d635fcf9fca4d3fd5fc6444a to your computer and use it in GitHub Desktop.
Save deanm0000/e16b94e3d635fcf9fca4d3fd5fc6444a to your computer and use it in GitHub Desktop.
add eprint everywhere
from pathlib import Path
import re
rootpath = Path("./polars/crates")
for p in rootpath.rglob("*.rs"):
with p.open() as f:
filestr = f.read()
if filestr.find("fn") == -1:
continue
filestr = filestr.splitlines()
impl = None
impl_brackets = 0
fn = []
cur_fn = None
impl_mult = False
in_macro_struct = False
macro_struct_brackets = 0
eprints = []
for i, line in enumerate(filestr):
# if i==1315:
# break
line = line.split("//")[0]
line = re.sub(r"<.+?>", "", line)
if (
len(macsplit := line.split("macro_rules!")) > 1
or len(macsplit := line.split("struct")) > 1
):
in_macro_struct = True
macro_struct_brackets = len(macsplit[1]) - len(macsplit[1].replace("{", ""))
macro_struct_brackets -= len(macsplit[1]) - len(
macsplit[1].replace("}", "")
)
if in_macro_struct is True:
macro_struct_brackets += len(line) - len(line.replace("{", ""))
macro_struct_brackets -= len(line) - len(line.replace("}", ""))
if macro_struct_brackets == 0:
in_macro_struct = False
continue
if impl_mult is True:
if len(line.replace("<", "")) > len(line.replace(">", "")):
impl = line.split(">", maxsplit=1)[1].strip()
impl = impl.replace("<", " ").split(" ")[0]
impl_mult = False
if len(implsplt := line.split("impl")) > 1 and (
line.find("fn") == -1 or line.find("fn") > line.find("impl")
):
if len(implsplt[1]) > 0 and implsplt[1][0] != "_":
impl = implsplt[1].strip()
if len(forsplt := impl.split("for")) > 1:
impl = forsplt[1].split(" ")[0].strip()
elif impl[0] == "<" and impl.find(">") != -1:
impl = impl.split(">", maxsplit=1)[1].strip()
impl = impl.replace("<", " ").split(" ")[0]
elif len(impl) == len(impl.replace("{", "")) > 0:
impl_mult = True
else:
impl = impl.split(" ")[0]
else:
impl_mult = True
if impl is not None:
impl_brackets += len(line) - len(line.replace("{", ""))
impl_brackets -= len(line) - len(line.replace("}", ""))
if impl_brackets == 0:
impl = None
if (
len(
linesplit := re.sub("(?<!(._|: |.\.))fn", "REALFN", line).split(
"REALFN"
)
)
> 1
and line.find("const fn") == -1
and line[-1] != ";"
and not (
"{" in line and len(line.replace("{", "")) == len(line.replace("}", ""))
)
):
after_fn = linesplit[1]
fnname = after_fn.split("(")[0].strip()
fn_status = [fnname, len(after_fn) - len(after_fn.replace("{", ""))]
if fn_status[1] == 0:
fn_status[1] = None
else:
eprints.append((i + 1, impl, fnname))
fn.append(fn_status)
if len(fn) > 0 and fn[-1][1] is None:
if line.find("{") != -1:
eprints.append((i + 1, impl, fn[-1][0]))
fn[-1][1] = len(line) - len(line.replace("{", ""))
fn[-1][1] -= len(line) - len(line.replace("}", ""))
if fn[-1][1] == 0:
fn.pop(-1)
elif len(line) > 0 and (line[-1].strip() == ";" or line[-1].strip() == ","):
fn.pop(-1)
if len(eprints) > 0:
counter = 1
for i, impl, fn in eprints[::-1]:
adj_i = i + len(eprints) - counter
pname = str(p).split("crates", maxsplit=1)[1]
counter += 1
if impl is None:
impl = ""
else:
impl = f"impl {impl}"
if "{" in impl or "}" in impl:
impl = impl.replace("{", "").replace("}", "")
print(f"bracket in {pname} {adj_i} {impl} {fn}")
fn = f"fn {fn.split('<')[0]}"
fn = fn.replace("<", "").replace(">", "")
filestr.insert(i, f'eprintln!("{pname} {adj_i} {impl} {fn}");')
filestr = "\n".join(filestr)
with p.open("w") as f:
f.write(filestr)
print(f"changed {p}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment