diff options
author | Simon Marchi <simon.marchi@efficios.com> | 2024-04-25 13:26:11 -0400 |
---|---|---|
committer | Simon Marchi <simon.marchi@efficios.com> | 2024-04-25 14:34:42 -0400 |
commit | d3c2603167baf9f6fea006f8b747b8186f89b177 (patch) | |
tree | 0edec227d265d84a69350d96ec62cee9116f2a08 | |
parent | 5b9707eb872ad4cb50c98d396d16f110070a44ca (diff) | |
download | binutils-d3c2603167baf9f6fea006f8b747b8186f89b177.zip binutils-d3c2603167baf9f6fea006f8b747b8186f89b177.tar.gz binutils-d3c2603167baf9f6fea006f8b747b8186f89b177.tar.bz2 |
gdb: add type annotations to ada-unicode.py
Add type annotations to ada-unicode.py, just enough to make pyright
happy:
$ pyright --version
pyright 1.1.359
$ pyright ada-unicode.py
0 errors, 0 warnings, 0 informations
Introduce a `Range` class instead of using separate variables and
tuples, to make the code and type annotations a bit cleaner.
When running ada-unicode.py, I get a diff for ada-casefold.h, but I get
the same diff before and after this patch, so that is a separate issue.
Change-Id: I0d8975a57f9fb115703178ae197dc6b6b8b4eb7a
Approved-By: Tom Tromey <tom@tromey.com>
-rwxr-xr-x | gdb/ada-unicode.py | 98 |
1 files changed, 61 insertions, 37 deletions
diff --git a/gdb/ada-unicode.py b/gdb/ada-unicode.py index 06c5de0..f128dee 100755 --- a/gdb/ada-unicode.py +++ b/gdb/ada-unicode.py @@ -25,41 +25,59 @@ import gdbcopyright -# The start of the current range of case-conversions we are -# processing. If RANGE_START is None, then we're outside of a range. -range_start = None -# End of the current range. -range_end = None -# The delta between RANGE_START and the upper-case variant of that -# character. -upper_delta = None -# The delta between RANGE_START and the lower-case variant of that -# character. -lower_delta = None + +class Range: + def __init__(self, range_start: int, upper_delta: int, lower_delta: int): + self._range_start = range_start + self._range_end = range_start + self._upper_delta = upper_delta + self._lower_delta = lower_delta + + # The start of the range. + @property + def range_start(self): + return self._range_start + + # The end of the range. + @property + def range_end(self): + return self._range_end + + @range_end.setter + def range_end(self, val: int): + self._range_end = val + + # The delta between RANGE_START and the upper-case variant of that + # character. + @property + def upper_delta(self): + return self._upper_delta + + # The delta between RANGE_START and the lower-case variant of that + # character. + @property + def lower_delta(self): + return self._lower_delta + + +# The current range we are processing. If None, then we're outside of a range. +current_range: Range | None = None # All the ranges found and completed so far. -# Each entry is a tuple of the form (START, END, UPPER_DELTA, LOWER_DELTA). -all_ranges = [] +all_ranges: list[Range] = [] def finish_range(): - global range_start - global range_end - global upper_delta - global lower_delta - if range_start is not None: - all_ranges.append((range_start, range_end, upper_delta, lower_delta)) - range_start = None - range_end = None - upper_delta = None - lower_delta = None - - -def process_codepoint(val): - global range_start - global range_end - global upper_delta - global lower_delta + global current_range + + if current_range is not None: + all_ranges.append(current_range) + current_range = None + + +def process_codepoint(val: int): + global current_range + c = chr(val) low = c.lower() up = c.upper() @@ -74,13 +92,16 @@ def process_codepoint(val): return updelta = ord(up) - val lowdelta = ord(low) - val - if range_start is not None and (updelta != upper_delta or lowdelta != lower_delta): + + if current_range is not None and ( + updelta != current_range.upper_delta or lowdelta != current_range.lower_delta + ): finish_range() - if range_start is None: - range_start = val - upper_delta = updelta - lower_delta = lowdelta - range_end = val + + if current_range is None: + current_range = Range(val, updelta, lowdelta) + + current_range.range_end = val for c in range(0, 0x10FFFF): @@ -93,4 +114,7 @@ with open("ada-casefold.h", "w") as f: ) print("", file=f) for r in all_ranges: - print(f" {{{r[0]}, {r[1]}, {r[2]}, {r[3]}}},", file=f) + print( + f" {{{r.range_start}, {r.range_end}, {r.upper_delta}, {r.lower_delta}}},", + file=f, + ) |