mirror of
https://github.com/bminor/binutils-gdb.git
synced 2025-12-25 16:57:52 +00:00
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>
This commit is contained in:
@@ -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
|
||||
global current_range
|
||||
|
||||
if current_range is not None:
|
||||
all_ranges.append(current_range)
|
||||
current_range = None
|
||||
|
||||
|
||||
def process_codepoint(val):
|
||||
global range_start
|
||||
global range_end
|
||||
global upper_delta
|
||||
global lower_delta
|
||||
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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user