Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace TreeAssociator with a function #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions codebasin/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

from codebasin import CodeBase, file_parser, platform, preprocessor
from codebasin.language import FileLanguage
from codebasin.preprocessor import CodeNode
from codebasin.walkers.tree_associator import TreeAssociator
from codebasin.platform import Platform
from codebasin.preprocessor import CodeNode, Node, Visit

log = logging.getLogger(__name__)

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

def associate(self, filename: str, platform: Platform):
"""
Update the association for the provided filename and platform.
"""
tree = self.get_tree(filename)
association = self.get_map(filename)
branch_taken = []

def associator(node: Node) -> Visit:
association[node].add(platform.name)
active = node.evaluate_for_platform(
platform=platform,
filename=filename,
state=self,
)

# Ensure we only descend into one branch of an if/else/endif.
if node.is_start_node():
branch_taken.append(active)
elif node.is_cont_node():
if branch_taken[-1]:
return Visit.NEXT_SIBLING
branch_taken[-1] = active
elif node.is_end_node():
branch_taken.pop()

if active:
return Visit.NEXT
else:
return Visit.NEXT_SIBLING

tree.visit(associator)


def find(
rootdir,
Expand Down Expand Up @@ -181,18 +214,9 @@ def find(
)
if include_file:
state.insert_file(include_file)

associator = TreeAssociator(
state.get_tree(include_file),
state.get_map(include_file),
)
associator.walk(file_platform, state)
state.associate(include_file, file_platform)

# Process the file, to build a list of associate nodes
associator = TreeAssociator(
state.get_tree(e["file"]),
state.get_map(e["file"]),
)
associator.walk(file_platform, state)
state.associate(e["file"], file_platform)

return state
8 changes: 1 addition & 7 deletions codebasin/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import numpy as np

from codebasin import util
from codebasin.walkers.tree_associator import TreeAssociator

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -909,12 +908,7 @@ def evaluate_for_platform(self, **kwargs):
# irrespective of file extension.
lang = kwargs["state"].langs[kwargs["filename"]]
kwargs["state"].insert_file(include_file, lang)

associator = TreeAssociator(
kwargs["state"].get_tree(include_file),
kwargs["state"].get_map(include_file),
)
associator.walk(kwargs["platform"], kwargs["state"])
kwargs["state"].associate(include_file, kwargs["platform"])

if not include_file:
filename = kwargs["filename"]
Expand Down
61 changes: 0 additions & 61 deletions codebasin/walkers/tree_associator.py

This file was deleted.

Loading