gh-104683: Argument clinic: cleanup DLSParser state_foo methods (#107543)

This commit is contained in:
Alex Waygood 2023-08-01 21:42:39 +01:00 committed by GitHub
parent c8872f4285
commit 818c83cf81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -45,7 +45,6 @@ from typing import (
NamedTuple, NamedTuple,
NoReturn, NoReturn,
Protocol, Protocol,
TypeGuard,
TypeVar, TypeVar,
cast, cast,
overload, overload,
@ -4388,7 +4387,7 @@ class IndentStack:
return line[indent:] return line[indent:]
StateKeeper = Callable[[str | None], None] StateKeeper = Callable[[str], None]
ConverterArgs = dict[str, Any] ConverterArgs = dict[str, Any]
class ParamState(enum.IntEnum): class ParamState(enum.IntEnum):
@ -4610,9 +4609,7 @@ class DSLParser:
fail('Tab characters are illegal in the Clinic DSL.\n\t' + repr(line), line_number=block_start) fail('Tab characters are illegal in the Clinic DSL.\n\t' + repr(line), line_number=block_start)
self.state(line) self.state(line)
self.next(self.state_terminal) self.do_post_block_processing_cleanup()
self.state(None)
block.output.extend(self.clinic.language.render(self.clinic, block.signatures)) block.output.extend(self.clinic.language.render(self.clinic, block.signatures))
if self.preserve_output: if self.preserve_output:
@ -4621,10 +4618,7 @@ class DSLParser:
block.output = self.saved_output block.output = self.saved_output
@staticmethod @staticmethod
def valid_line(line: str | None) -> TypeGuard[str]: def valid_line(line: str) -> bool:
if line is None:
return False
# ignore comment-only lines # ignore comment-only lines
if line.lstrip().startswith('#'): if line.lstrip().startswith('#'):
return False return False
@ -4650,7 +4644,7 @@ class DSLParser:
if line is not None: if line is not None:
self.state(line) 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) # self.block = self.ClinicOutputBlock(self)
if not self.valid_line(line): if not self.valid_line(line):
return return
@ -4668,7 +4662,7 @@ class DSLParser:
self.next(self.state_modulename_name, line) 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 # looking for declaration, which establishes the leftmost column
# line should be # line should be
# modulename.fnname [as c_basename] [-> return annotation] # modulename.fnname [as c_basename] [-> return annotation]
@ -4857,7 +4851,7 @@ class DSLParser:
# separate boolean state variables.) The states are defined in the # separate boolean state variables.) The states are defined in the
# ParamState class. # 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): if not self.valid_line(line):
return return
@ -4879,7 +4873,7 @@ class DSLParser:
for p in self.function.parameters.values(): for p in self.function.parameters.values():
p.group = -p.group p.group = -p.group
def state_parameter(self, line: str | None) -> None: def state_parameter(self, line: str) -> None:
assert isinstance(self.function, Function) assert isinstance(self.function, Function)
if not self.valid_line(line): if not self.valid_line(line):
@ -5262,7 +5256,7 @@ class DSLParser:
"positional-only parameters, which is unsupported.") "positional-only parameters, which is unsupported.")
p.kind = inspect.Parameter.POSITIONAL_ONLY 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" 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) self.parameter_docstring_indent = len(self.indent.margin)
assert self.indent.depth == 3 assert self.indent.depth == 3
@ -5271,9 +5265,7 @@ class DSLParser:
# every line of the docstring must start with at least F spaces, # every line of the docstring must start with at least F spaces,
# where F > P. # where F > P.
# these F spaces will be stripped. # these F spaces will be stripped.
def state_parameter_docstring(self, line: str | None) -> None: def state_parameter_docstring(self, line: str) -> None:
assert line is not None
stripped = line.strip() stripped = line.strip()
if stripped.startswith('#'): if stripped.startswith('#'):
return return
@ -5301,9 +5293,8 @@ class DSLParser:
last_parameter.docstring = new_docstring last_parameter.docstring = new_docstring
# the final stanza of the DSL is the 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 self.function is not None
assert line is not None
if self.group: if self.group:
fail("Function " + self.function.name + " has a ] without a matching [.") fail("Function " + self.function.name + " has a ] without a matching [.")
@ -5572,12 +5563,10 @@ class DSLParser:
return docstring 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. Called when processing the block is done.
""" """
assert not line
if not self.function: if not self.function:
return return