From 60a4982cd7cb3b00b41a3899f69b76347021e498 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Sat, 29 Jul 2023 20:44:21 +0100 Subject: [PATCH] Argument clinic: cleanup the `DLSParser` class --- Tools/clinic/clinic.py | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 3501c16a567ccc..b04f22d7b0c67b 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -44,7 +44,6 @@ NamedTuple, NoReturn, Protocol, - TypeGuard, TypeVar, cast, overload, @@ -4389,7 +4388,7 @@ def dedent(self, line: str) -> str: return line[indent:] -StateKeeper = Callable[[str | None], None] +StateKeeper = Callable[[str], None] ConverterArgs = dict[str, Any] class ParamState(enum.IntEnum): @@ -4611,9 +4610,7 @@ def parse(self, block: Block) -> None: fail('Tab characters are illegal in the Clinic DSL.\n\t' + repr(line), line_number=block_start) self.state(line) - self.next(self.state_terminal) - self.state(None) - + self.do_post_block_processing_cleanup() block.output.extend(self.clinic.language.render(self.clinic, block.signatures)) if self.preserve_output: @@ -4622,10 +4619,7 @@ def parse(self, block: Block) -> None: block.output = self.saved_output @staticmethod - def valid_line(line: str | None) -> TypeGuard[str]: - if line is None: - return False - + def valid_line(line: str) -> bool: # ignore comment-only lines if line.lstrip().startswith('#'): return False @@ -4651,7 +4645,7 @@ def next( if line is not None: self.state(line) - def state_dsl_start(self, line: str | None) -> None: + def state_dsl_start(self, line: str) -> None: # self.block = self.ClinicOutputBlock(self) if not self.valid_line(line): return @@ -4669,7 +4663,7 @@ def state_dsl_start(self, line: str | None) -> None: self.next(self.state_modulename_name, line) - def state_modulename_name(self, line: str | None) -> None: + def state_modulename_name(self, line: str) -> None: # looking for declaration, which establishes the leftmost column # line should be # modulename.fnname [as c_basename] [-> return annotation] @@ -4858,7 +4852,7 @@ def state_modulename_name(self, line: str | None) -> None: # separate boolean state variables.) The states are defined in the # ParamState class. - def state_parameters_start(self, line: str | None) -> None: + def state_parameters_start(self, line: str) -> None: if not self.valid_line(line): return @@ -4880,7 +4874,7 @@ def to_required(self) -> None: for p in self.function.parameters.values(): p.group = -p.group - def state_parameter(self, line: str | None) -> None: + def state_parameter(self, line: str) -> None: assert isinstance(self.function, Function) if not self.valid_line(line): @@ -5263,7 +5257,7 @@ def parse_slash(self, function: Function) -> None: "positional-only parameters, which is unsupported.") p.kind = inspect.Parameter.POSITIONAL_ONLY - def state_parameter_docstring_start(self, line: str | None) -> None: + def state_parameter_docstring_start(self, line: str) -> None: assert self.indent.margin is not None, "self.margin.infer() has not yet been called to set the margin" self.parameter_docstring_indent = len(self.indent.margin) assert self.indent.depth == 3 @@ -5272,9 +5266,7 @@ def state_parameter_docstring_start(self, line: str | None) -> None: # every line of the docstring must start with at least F spaces, # where F > P. # these F spaces will be stripped. - def state_parameter_docstring(self, line: str | None) -> None: - assert line is not None - + def state_parameter_docstring(self, line: str) -> None: stripped = line.strip() if stripped.startswith('#'): return @@ -5302,9 +5294,8 @@ def state_parameter_docstring(self, line: str | None) -> None: last_parameter.docstring = new_docstring # the final stanza of the DSL is the docstring. - def state_function_docstring(self, line: str | None) -> None: + def state_function_docstring(self, line: str) -> None: assert self.function is not None - assert line is not None if self.group: fail("Function " + self.function.name + " has a ] without a matching [.") @@ -5573,12 +5564,10 @@ def add_parameter(text: str) -> None: return docstring - def state_terminal(self, line: str | None) -> None: + def do_post_block_processing_cleanup(self) -> None: """ Called when processing the block is done. """ - assert not line - if not self.function: return