Exemple #1
0
 def visit_sealed_class(self, node):
     variants = []
     accessors = []
     tag_checkers = []
     camel_node_name = camel_case(node.name)
     for member, var in node.class_assignments.items():
         member_id = get_id(member)
         camel_member_id = camel_case(member_id)
         lower_member_id = member_id.lower()
         typename = node.declarations_with_defaults.get(member_id, None)
         if typename == None:
             variants.append(f"{member_id},")
         else:
             typename, _ = typename
             variants.append(f"{member_id}({typename}),")
         tag_check = textwrap.dedent(f"""
                 fn is_{lower_member_id}(&self) -> bool {{
                     matches!(*self, {camel_node_name}::{camel_member_id}(_))
                 }}""")
         tag_checkers.append(tag_check)
         accessor = textwrap.dedent(f"""
                 fn {lower_member_id}(&self) -> Option<&{typename}>{{
                     if let {camel_node_name}::{camel_member_id}(val) = self {{
                         Some(val)
                     }} else {{
                         None
                     }}
                 }}""")
         accessors.append(accessor)
     body_str = "\n".join(variants)
     impl_str = "\n".join(accessors) + "\n" + "\n".join(tag_checkers)
     return f"enum {camel_node_name} {{ {body_str} }}\n\n impl {camel_node_name} {{ {impl_str} }}\n\n"
Exemple #2
0
 def visit_Name(self, node):
     if hasattr(node, "scopes") and is_global(node):
         old_name = get_id(node)
         new_name = camel_case(old_name)
         if old_name != new_name:
             rename(node.scopes[-1], old_name, new_name)
     return node
Exemple #3
0
 def visit_FunctionDef(self, node):
     if hasattr(node, "scopes") and isinstance(node.scopes[-2], ast.Module):
         old_name = get_id(node)
         if old_name is not None:
             if "_" in old_name:
                 new_name = camel_case(old_name)
             else:
                 new_name = capitalize_first(old_name)
             if old_name != new_name:
                 rename(node.scopes[-2], old_name, new_name)
     return node