Skip to content

Commit dd1f7e7

Browse files
authored
Merge pull request #125 from Pennycook/tree-associator
Replace TreeAssociator with a function
2 parents 154dd7f + 60e9410 commit dd1f7e7

File tree

3 files changed

+38
-81
lines changed

3 files changed

+38
-81
lines changed

codebasin/finder.py

+37-13
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
from codebasin import CodeBase, file_parser, platform, preprocessor
1616
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
1919

2020
log = logging.getLogger(__name__)
2121

@@ -105,6 +105,39 @@ def get_setmap(self, codebase: CodeBase) -> dict[frozenset, int]:
105105
setmap[platform] += node.num_lines
106106
return setmap
107107

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+
108141

109142
def find(
110143
rootdir,
@@ -181,18 +214,9 @@ def find(
181214
)
182215
if include_file:
183216
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)
190218

191219
# 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)
197221

198222
return state

codebasin/preprocessor.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import numpy as np
2020

2121
from codebasin import util
22-
from codebasin.walkers.tree_associator import TreeAssociator
2322

2423
log = logging.getLogger(__name__)
2524

@@ -909,12 +908,7 @@ def evaluate_for_platform(self, **kwargs):
909908
# irrespective of file extension.
910909
lang = kwargs["state"].langs[kwargs["filename"]]
911910
kwargs["state"].insert_file(include_file, lang)
912-
913-
associator = TreeAssociator(
914-
kwargs["state"].get_tree(include_file),
915-
kwargs["state"].get_map(include_file),
916-
)
917-
associator.walk(kwargs["platform"], kwargs["state"])
911+
kwargs["state"].associate(include_file, kwargs["platform"])
918912

919913
if not include_file:
920914
filename = kwargs["filename"]

codebasin/walkers/tree_associator.py

-61
This file was deleted.

0 commit comments

Comments
 (0)