|
| 1 | +from collections import defaultdict, Counter |
| 2 | +from dataclasses import dataclass |
| 3 | +from tree_sitter import Node |
| 4 | +from .ast_util import AST |
| 5 | + |
| 6 | + |
| 7 | +class TypeExtractor(AST): |
| 8 | + """Static analyzer for Haskell type signatures. |
| 9 | + NOTE: this analyzer works on the body of a type signature only, |
| 10 | + i.e. the part after the `=>` symbol if it has constraints, |
| 11 | + or otherwise after the `::` symbol. |
| 12 | + The constraints (if any) are handled in other modules. |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, code: str): |
| 16 | + super().__init__(code) |
| 17 | + self.constructors: dict[str, Counter] = defaultdict(Counter) |
| 18 | + self.names: set[str] = set() |
| 19 | + |
| 20 | + self._analysis_types() |
| 21 | + |
| 22 | + @property |
| 23 | + def type_constructors(self) -> dict[str, int]: |
| 24 | + """Get a mapping of type constructor names to their maximum observed arity (i.e. number of parameters).""" |
| 25 | + return {k: max(v.keys()) for k, v in self.constructors.items()} |
| 26 | + |
| 27 | + def _analysis_types(self): |
| 28 | + """analysis types in the function signature to fill out self.constructors and self.names""" |
| 29 | + sigs = self.get_all_nodes_of_type(self.root, "signature") |
| 30 | + functions = self.get_all_nodes_of_type(sigs[0], "function") |
| 31 | + if len(functions) > 0: |
| 32 | + self._visit(functions[0]) |
| 33 | + |
| 34 | + def _collect_from_tuple(self, node: Node): |
| 35 | + # record tuple arity if you care: arity = count of element children |
| 36 | + # then continue walking children |
| 37 | + for ch in node.named_children: |
| 38 | + self._visit(ch) |
| 39 | + |
| 40 | + def _visit(self, n: Node): |
| 41 | + t = n.type |
| 42 | + |
| 43 | + if t == "apply": |
| 44 | + # Count this application chain once, at the top-most 'apply' only. |
| 45 | + parent = n.parent |
| 46 | + if not ( |
| 47 | + parent |
| 48 | + and parent.type == "apply" |
| 49 | + and parent.child_by_field_name("constructor") is n |
| 50 | + ): |
| 51 | + apply_chain = _peel_apply_chain(n) |
| 52 | + ctor_name = self.get_src_from_node(apply_chain.constructor) |
| 53 | + self.constructors[ctor_name][apply_chain.arity] += 1 |
| 54 | + # Recurse into children so we also catch nested names/applications. |
| 55 | + for ch in n.named_children: |
| 56 | + self._visit(ch) |
| 57 | + return |
| 58 | + |
| 59 | + if t == "constructor": |
| 60 | + # Zero-arity constructor occurrence (e.g., `Int`) not part of an apply |
| 61 | + parent = n.parent |
| 62 | + if not ( |
| 63 | + parent |
| 64 | + and parent.type == "apply" |
| 65 | + and parent.child_by_field_name("constructor") is n |
| 66 | + ): |
| 67 | + name_node = n.child_by_field_name("name") or ( |
| 68 | + n.named_children[0] if n.named_children else None |
| 69 | + ) |
| 70 | + if name_node: |
| 71 | + constructor_name = self.get_src_from_node(name_node) |
| 72 | + self.constructors[constructor_name][0] += 1 |
| 73 | + # still walk inside |
| 74 | + for ch in n.named_children: |
| 75 | + self._visit(ch) |
| 76 | + return |
| 77 | + |
| 78 | + if t == "tuple": |
| 79 | + self._collect_from_tuple(n) |
| 80 | + return |
| 81 | + |
| 82 | + if t == "name": |
| 83 | + # Treat as a plain type variable/name when not under a constructor role. |
| 84 | + p = n.parent |
| 85 | + # If its parent is 'constructor', it's part of a constructor; skip here. |
| 86 | + if p is None or p.type != "constructor": |
| 87 | + self.names.add(self.get_src_from_node(n)) |
| 88 | + return |
| 89 | + |
| 90 | + # default: recurse |
| 91 | + for ch in n.named_children: |
| 92 | + self._visit(ch) |
| 93 | + |
| 94 | + |
| 95 | +@dataclass |
| 96 | +class TypeApplyChain: |
| 97 | + constructor: Node |
| 98 | + arity: int |
| 99 | + arguments: list[Node] |
| 100 | + |
| 101 | + |
| 102 | +def _peel_apply_chain(node: Node) -> TypeApplyChain: |
| 103 | + """ |
| 104 | + Given an (apply ...) subtree, walk left through nested apply nodes to |
| 105 | + find the root constructor name and count how many arguments were applied. |
| 106 | + # Returns (arity, arg_nodes_list, constructor_node). |
| 107 | + """ |
| 108 | + args = [] |
| 109 | + arity = 0 |
| 110 | + cur = node |
| 111 | + while cur.type == "apply": |
| 112 | + arity += 1 |
| 113 | + arg = cur.child_by_field_name("argument") |
| 114 | + if arg is not None: |
| 115 | + args.append(arg) |
| 116 | + # could be 'constructor' or another 'apply' |
| 117 | + next_level = cur.child_by_field_name("constructor") |
| 118 | + if not next_level: |
| 119 | + break |
| 120 | + cur = next_level |
| 121 | + |
| 122 | + # now cur is either a 'constructor' node or a 'name' (rare) |
| 123 | + ctor_node = cur |
| 124 | + return TypeApplyChain(constructor=ctor_node, arity=arity, arguments=args) |
0 commit comments