Final renaming and type hints

This commit is contained in:
disinvite 2023-11-20 21:48:38 -05:00
parent 7c65e5bd3e
commit 5c7da9f6b3
5 changed files with 20 additions and 18 deletions

View File

@ -3,7 +3,7 @@
import argparse import argparse
from isledecomp.dir import ( from isledecomp.dir import (
walk_source_dir, walk_source_dir,
file_is_cpp is_file_cpp
) )
from isledecomp.parser import find_code_blocks from isledecomp.parser import find_code_blocks
from isledecomp.parser.util import ( from isledecomp.parser.util import (
@ -92,7 +92,7 @@ def main():
if os.path.isdir(args['target']): if os.path.isdir(args['target']):
files_to_check = list(walk_source_dir(args['target'])) files_to_check = list(walk_source_dir(args['target']))
elif os.path.isfile(args['target']) and file_is_cpp(args['target']): elif os.path.isfile(args['target']) and is_file_cpp(args['target']):
files_to_check = [args['target']] files_to_check = [args['target']]
else: else:
sys.exit('Invalid target') sys.exit('Invalid target')

View File

@ -2,7 +2,7 @@
from typing import Iterator from typing import Iterator
def file_is_cpp(filename: str) -> bool: def is_file_cpp(filename: str) -> bool:
(basefile, ext) = os.path.splitext(filename) (basefile, ext) = os.path.splitext(filename)
return ext.lower() in ('.h', '.cpp') return ext.lower() in ('.h', '.cpp')
@ -14,7 +14,7 @@ def walk_source_dir(source: str, recursive: bool = True) -> Iterator[str]:
source = os.path.abspath(source) source = os.path.abspath(source)
for subdir, dirs, files in os.walk(source): for subdir, dirs, files in os.walk(source):
for file in files: for file in files:
if file_is_cpp(file): if is_file_cpp(file):
yield os.path.join(subdir, file) yield os.path.join(subdir, file)
if not recursive: if not recursive:

View File

@ -8,9 +8,9 @@
is_blank_or_comment, is_blank_or_comment,
match_offset_comment, match_offset_comment,
is_exact_offset_comment, is_exact_offset_comment,
template_function_name, get_template_function_name,
remove_trailing_comment, remove_trailing_comment,
distinct_module, distinct_by_module,
) )
@ -30,16 +30,16 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]:
binary. We expect the result to be ordered by line number because we binary. We expect the result to be ordered by line number because we
are reading the file from start to finish.""" are reading the file from start to finish."""
blocks = [] blocks: List[CodeBlock] = []
offset_matches = [] offset_matches: List[OffsetMatch] = []
function_sig = None function_sig = None
start_line = None start_line = None
end_line = None end_line = None
state = ReaderState.WANT_OFFSET state = ReaderState.WANT_OFFSET
# 1-based to match cvdump and your text-editor # 1-based to match cvdump and your text editor
# I know it says 0, but we will increment before each readline() # I know it says 0, but we will increment before each readline()
line_no = 0 line_no = 0
can_seek = True can_seek = True
@ -50,7 +50,7 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]:
if state == ReaderState.FUNCTION_DONE: if state == ReaderState.FUNCTION_DONE:
# Our list of offset marks could have duplicates on # Our list of offset marks could have duplicates on
# module name, so we'll eliminate those now. # module name, so we'll eliminate those now.
for offset_match in distinct_module(offset_matches): for offset_match in distinct_by_module(offset_matches):
block = CodeBlock(offset=offset_match.address, block = CodeBlock(offset=offset_match.address,
signature=function_sig, signature=function_sig,
start_line=start_line, start_line=start_line,
@ -98,7 +98,7 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]:
elif state == ReaderState.IN_TEMPLATE: elif state == ReaderState.IN_TEMPLATE:
# TEMPLATE functions are a special case. The signature is # TEMPLATE functions are a special case. The signature is
# given on the next line (in a // comment) # given on the next line (in a // comment)
function_sig = template_function_name(line) function_sig = get_template_function_name(line)
start_line = line_no start_line = line_no
end_line = line_no end_line = line_no
state = ReaderState.FUNCTION_DONE state = ReaderState.FUNCTION_DONE

View File

@ -1,6 +1,7 @@
# C++ Parser utility functions and data structures # C++ Parser utility functions and data structures
from __future__ import annotations # python <3.10 compatibility from __future__ import annotations # python <3.10 compatibility
import re import re
from typing import List
from collections import namedtuple from collections import namedtuple
@ -34,7 +35,7 @@
trailingCommentRegex = re.compile(r'(\s*(?://|/\*).*)$') trailingCommentRegex = re.compile(r'(\s*(?://|/\*).*)$')
def template_function_name(line: str) -> str: def get_template_function_name(line: str) -> str:
"""Parse function signature for special TEMPLATE functions""" """Parse function signature for special TEMPLATE functions"""
template_match = templateCommentRegex.match(line) template_match = templateCommentRegex.match(line)
@ -78,7 +79,7 @@ def match_offset_comment(line: str) -> OffsetMatch | None:
comment=line.strip()) comment=line.strip())
def distinct_module(offsets: [OffsetMatch]) -> [OffsetMatch]: def distinct_by_module(offsets: List) -> List:
"""Given a list of offset markers, return a list with distinct """Given a list of offset markers, return a list with distinct
module names. If module names (case-insensitive) are repeated, module names. If module names (case-insensitive) are repeated,
choose the offset that appears first.""" choose the offset that appears first."""

View File

@ -1,10 +1,11 @@
import pytest import pytest
from collections import namedtuple from collections import namedtuple
from typing import List
from isledecomp.parser.util import ( from isledecomp.parser.util import (
is_blank_or_comment, is_blank_or_comment,
match_offset_comment, match_offset_comment,
is_exact_offset_comment, is_exact_offset_comment,
distinct_module, distinct_by_module,
) )
@ -92,7 +93,7 @@ def test_exact_offset_comment(line: str, exact: bool, match):
# Helper for the next test: cut down version of OffsetMatch # Helper for the next test: cut down version of OffsetMatch
MiniOfs = namedtuple('MiniOfs', ['module', 'value']) MiniOfs = namedtuple('MiniOfs', ['module', 'value'])
distinct_module_samples = [ distinct_by_module_samples = [
# empty set # empty set
([], []), ([], []),
# same module name # same module name
@ -107,6 +108,6 @@ def test_exact_offset_comment(line: str, exact: bool, match):
] ]
@pytest.mark.parametrize('sample, expected', distinct_module_samples) @pytest.mark.parametrize('sample, expected', distinct_by_module_samples)
def test_distinct_module(sample: [MiniOfs], expected: [MiniOfs]): def test_distinct_by_module(sample: List[MiniOfs], expected: List[MiniOfs]):
assert distinct_module(sample) == expected assert distinct_by_module(sample) == expected