Find bad comments in one pass, add awareness of TEMPLATE

This commit is contained in:
disinvite 2023-10-29 18:51:02 -04:00
parent a9300dd605
commit cb7e223b63
3 changed files with 15 additions and 21 deletions

View File

@ -5,7 +5,6 @@
from isledecomp.dir import walk_source_dir
from isledecomp.parser import find_code_blocks
from isledecomp.parser.util import (
match_offset_comment,
is_exact_offset_comment
)
@ -16,16 +15,6 @@ def sig_truncate(sig: str) -> str:
return f"{sig[:47]}{'...' if len(sig) >= 50 else ''}"
def get_inexact_offset_comments(stream: TextIO) -> [tuple]:
"""Read the file stream and return the line number and string
for any offset comments that don't exactly match the template."""
return ([
(line_no, line.strip())
for line_no, line in enumerate(stream)
if match_offset_comment(line) and not is_exact_offset_comment(line)
])
def check_file(filename: str, verbose: bool = False) -> bool:
"""Open and read the given file, then check whether the code blocks
are in order. If verbose, print each block."""
@ -35,7 +24,10 @@ def check_file(filename: str, verbose: bool = False) -> bool:
# TODO: Should combine these checks if/when we refactor.
# This is just for simplicity / proof of concept.
f.seek(os.SEEK_SET, 0)
bad_comments = get_inexact_offset_comments(f)
bad_comments = [(block.start_line, block.offset_comment)
for block in code_blocks
if not is_exact_offset_comment(block.offset_comment)]
just_offsets = [block.offset for block in code_blocks]
sorted_offsets = sorted(just_offsets)

View File

@ -26,6 +26,7 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]:
blocks = []
offset = None
offset_comment = None
function_sig = None
start_line = None
state = ReaderState.WANT_OFFSET
@ -39,7 +40,8 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]:
block = CodeBlock(offset=offset,
signature=function_sig,
start_line=start_line,
end_line=line_no)
end_line=line_no,
offset_comment=offset_comment)
blocks.append(block)
state = ReaderState.WANT_OFFSET
@ -50,7 +52,8 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]:
block = CodeBlock(offset=offset,
signature=function_sig,
start_line=start_line,
end_line=line_no - 1)
end_line=line_no - 1,
offset_comment=offset_comment)
blocks.append(block)
state = ReaderState.WANT_OFFSET
@ -67,6 +70,7 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]:
match = match_offset_comment(line)
if match is not None:
offset = int(match, 16)
offset_comment = line.strip()
start_line = line_no
state = ReaderState.WANT_SIG

View File

@ -4,20 +4,18 @@
CodeBlock = namedtuple('CodeBlock',
['offset', 'signature', 'start_line', 'end_line'])
FunctionOffset = namedtuple('FunctionOffset',
['raw', 'address', 'is_stub'])
['offset', 'signature', 'start_line', 'end_line',
'offset_comment'])
# To match a reasonable variance of formatting for the offset comment
offsetCommentRegex = re.compile(r'//\s*OFFSET:\s*\w+\s+(?:0x)?([a-f0-9]+)',
offsetCommentRegex = re.compile(r'//\s*OFFSET:\s*\w+\s+(?:0x)?([a-f0-9]+)(\s+STUB)?',
flags=re.I)
# To match the exact syntax (text upper case, hex lower case, with spaces)
# that is used in most places
offsetCommentExactRegex = re.compile(r'^// OFFSET: [A-Z0-9]+ (0x[a-f0-9]+)(?: STUB)?$')
# TODO: template and stub mutually exclusive?
offsetCommentExactRegex = re.compile(r'^// OFFSET: [A-Z0-9]+ (0x[a-f0-9]+)(?: STUB| TEMPLATE)?$')
def is_blank_or_comment(line: str) -> bool: