diff --git a/tools/genast.py b/tools/genast.py index e6f6960..32ec14c 100755 --- a/tools/genast.py +++ b/tools/genast.py @@ -1,12 +1,29 @@ #!/usr/bin/env python3 # pylint: disable=missing-function-docstring,missing-class-docstring,missing-module-docstring,line-too-long import abc +import re import sys import time from pathlib import Path from typing import Self +def snake_case(s: str): + # This insane regex comes from ChatGPT. I have no idea what the original + # source is, doing a verbatim search on Google and DDG doesn't yield any + # useful results. + # + # Here is the breakdown of how it works: + # (? _index_assign + # (?=[A-Z]) - this is a lookahead, asserting that the next character is + # uppercase. It does not consume the character, so doing a .sub() + # on this will not replace it. + return re.sub(r"(? str: - return f"fn visit_{self.name.lower()}_{self.base.lower()}(&mut self, {self.base.lower()}: &{self.name}{self.base}) -> Result<(), Error>;" + return f"fn visit_{snake_case(self.name + self.base)}(&mut self, {snake_case(self.base)}: &{self.name}{self.base}) -> Result<(), Error>;" class GenerateEnum(Generator): @@ -130,7 +147,7 @@ class GenerateEnum(Generator): + "{" ) sb.indent() - sb.line(f"visitor.visit_{self.name.lower()}_{self.base.lower()}(self)") + sb.line(f"visitor.visit_{snake_case(self.name + self.base)}(self)") sb.dedent() sb.line("}") @@ -144,7 +161,7 @@ class GenerateEnum(Generator): return str(sb) def generate_visitor(self) -> str: - return f"fn visit_{self.name.lower()}_{self.base.lower()}(&mut self, {self.base.lower()}: &{self.name}{self.base}) -> Result<(), Error>;" + return f"fn visit_{snake_case(self.name)}(&mut self, {snake_case(self.base)}: &{self.name}{self.base}) -> Result<(), Error>;" class GenerateGroup(Generator):