|
14 | 14 |
|
15 | 15 | from codebasin import CodeBase, file_parser, platform, preprocessor
|
16 | 16 | from codebasin.language import FileLanguage
|
17 |
| -from codebasin.preprocessor import CodeNode |
18 |
| -from codebasin.walkers.tree_associator import TreeAssociator |
| 17 | +from codebasin.platform import Platform |
| 18 | +from codebasin.preprocessor import CodeNode, Node, Visit |
19 | 19 |
|
20 | 20 | log = logging.getLogger(__name__)
|
21 | 21 |
|
@@ -105,6 +105,39 @@ def get_setmap(self, codebase: CodeBase) -> dict[frozenset, int]:
|
105 | 105 | setmap[platform] += node.num_lines
|
106 | 106 | return setmap
|
107 | 107 |
|
| 108 | + def associate(self, filename: str, platform: Platform): |
| 109 | + """ |
| 110 | + Update the association for the provided filename and platform. |
| 111 | + """ |
| 112 | + tree = self.get_tree(filename) |
| 113 | + association = self.get_map(filename) |
| 114 | + branch_taken = [] |
| 115 | + |
| 116 | + def associator(node: Node) -> Visit: |
| 117 | + association[node].add(platform.name) |
| 118 | + active = node.evaluate_for_platform( |
| 119 | + platform=platform, |
| 120 | + filename=filename, |
| 121 | + state=self, |
| 122 | + ) |
| 123 | + |
| 124 | + # Ensure we only descend into one branch of an if/else/endif. |
| 125 | + if node.is_start_node(): |
| 126 | + branch_taken.append(active) |
| 127 | + elif node.is_cont_node(): |
| 128 | + if branch_taken[-1]: |
| 129 | + return Visit.NEXT_SIBLING |
| 130 | + branch_taken[-1] = active |
| 131 | + elif node.is_end_node(): |
| 132 | + branch_taken.pop() |
| 133 | + |
| 134 | + if active: |
| 135 | + return Visit.NEXT |
| 136 | + else: |
| 137 | + return Visit.NEXT_SIBLING |
| 138 | + |
| 139 | + tree.visit(associator) |
| 140 | + |
108 | 141 |
|
109 | 142 | def find(
|
110 | 143 | rootdir,
|
@@ -181,18 +214,9 @@ def find(
|
181 | 214 | )
|
182 | 215 | if include_file:
|
183 | 216 | state.insert_file(include_file)
|
184 |
| - |
185 |
| - associator = TreeAssociator( |
186 |
| - state.get_tree(include_file), |
187 |
| - state.get_map(include_file), |
188 |
| - ) |
189 |
| - associator.walk(file_platform, state) |
| 217 | + state.associate(include_file, file_platform) |
190 | 218 |
|
191 | 219 | # Process the file, to build a list of associate nodes
|
192 |
| - associator = TreeAssociator( |
193 |
| - state.get_tree(e["file"]), |
194 |
| - state.get_map(e["file"]), |
195 |
| - ) |
196 |
| - associator.walk(file_platform, state) |
| 220 | + state.associate(e["file"], file_platform) |
197 | 221 |
|
198 | 222 | return state
|
0 commit comments