@@ -4618,7 +4618,7 @@ def parse(self, block: Block) -> None:
4618
4618
fail (f'Tab characters are illegal in the Clinic DSL: { line !r} ' ,
4619
4619
line_number = block_start )
4620
4620
try :
4621
- function = self .handle_line (function , line )
4621
+ function = self .handle_line (function = function , line = line )
4622
4622
except ClinicError as exc :
4623
4623
exc .lineno = line_number
4624
4624
raise
@@ -4631,7 +4631,16 @@ def parse(self, block: Block) -> None:
4631
4631
fail ("'preserve' only works for blocks that don't produce any output!" )
4632
4632
block .output = self .saved_output
4633
4633
4634
- def handle_line (self , function : Function | None , line : str ) -> Function | None :
4634
+ def handle_line (
4635
+ self ,
4636
+ / ,
4637
+ * ,
4638
+ function : Function | None ,
4639
+ line : str ,
4640
+ new_state : DSLParserState | None = None
4641
+ ) -> Function | None :
4642
+ if new_state :
4643
+ self .state = new_state
4635
4644
match function :
4636
4645
case None :
4637
4646
match self .state :
@@ -4697,10 +4706,13 @@ def handle_dsl_start(self, line: str) -> Function | None:
4697
4706
fail (str (e ))
4698
4707
return None
4699
4708
4700
- self .state = DSLParserState .MODULENAME_NAME
4701
- return self .handle_modulename_name (line )
4709
+ return self .handle_line (
4710
+ function = None ,
4711
+ line = line ,
4712
+ new_state = DSLParserState .MODULENAME_NAME
4713
+ )
4702
4714
4703
- def handle_modulename_name (self , line : str ) -> Function | None :
4715
+ def handle_modulename_name (self , line : str ) -> Function :
4704
4716
# looking for declaration, which establishes the leftmost column
4705
4717
# line should be
4706
4718
# modulename.fnname [as c_basename] [-> return annotation]
@@ -4717,9 +4729,6 @@ def handle_modulename_name(self, line: str) -> Function | None:
4717
4729
# this line is permitted to start with whitespace.
4718
4730
# we'll call this number of spaces F (for "function").
4719
4731
4720
- if not self .valid_line (line ):
4721
- return None
4722
-
4723
4732
self .indent .infer (line )
4724
4733
4725
4734
# are we cloning?
@@ -4900,13 +4909,16 @@ def handle_parameters_start(self, function: Function, line: str) -> Function:
4900
4909
if self .valid_line (line ):
4901
4910
# if this line is not indented, we have no parameters
4902
4911
if not self .indent .infer (line ):
4903
- self .state = DSLParserState .FUNCTION_DOCSTRING
4904
- self .handle_function_docstring (function = function , line = line )
4912
+ self .handle_line (
4913
+ function = function ,
4914
+ line = line ,
4915
+ new_state = DSLParserState .FUNCTION_DOCSTRING
4916
+ )
4905
4917
else :
4906
4918
self .parameter_continuation = ''
4907
- self .state = DSLParserState . PARAMETER
4908
- self . handle_parameter ( function = function , line = line )
4909
-
4919
+ self .handle_line (
4920
+ function = function , line = line , new_state = DSLParserState . PARAMETER
4921
+ )
4910
4922
return function
4911
4923
4912
4924
def to_required (self , function : Function ) -> None :
@@ -4930,13 +4942,19 @@ def handle_parameter(self, function: Function, line: str) -> Function:
4930
4942
indent = self .indent .infer (line )
4931
4943
if indent == - 1 :
4932
4944
# we outdented, must be to definition column
4933
- self .state = DSLParserState .FUNCTION_DOCSTRING
4934
- return self .handle_function_docstring (function = function , line = line )
4945
+ self .handle_line (
4946
+ function = function , line = line , new_state = DSLParserState .FUNCTION_DOCSTRING
4947
+ )
4948
+ return function
4935
4949
4936
4950
if indent == 1 :
4937
4951
# we indented, must be to new parameter docstring column
4938
- self .state = DSLParserState .PARAMETER_DOCSTRING_START
4939
- return self .handle_parameter_docstring_start (function = function , line = line )
4952
+ self .handle_line (
4953
+ function = function ,
4954
+ line = line ,
4955
+ new_state = DSLParserState .PARAMETER_DOCSTRING_START
4956
+ )
4957
+ return function
4940
4958
4941
4959
line = line .rstrip ()
4942
4960
if line .endswith ('\\ ' ):
@@ -5332,8 +5350,10 @@ def handle_parameter_docstring_start(
5332
5350
assert self .indent .margin is not None , "self.margin.infer() has not yet been called to set the margin"
5333
5351
self .parameter_docstring_indent = len (self .indent .margin )
5334
5352
assert self .indent .depth == 3
5335
- self .state = DSLParserState .PARAMETER_DOCSTRING
5336
- return self .handle_parameter_docstring (function = function , line = line )
5353
+ self .handle_line (
5354
+ function = function , line = line , new_state = DSLParserState .PARAMETER_DOCSTRING
5355
+ )
5356
+ return function
5337
5357
5338
5358
def docstring_append (self , obj : Function | Parameter , line : str ) -> None :
5339
5359
"""Add a rstripped line to the current docstring."""
@@ -5364,11 +5384,15 @@ def handle_parameter_docstring(
5364
5384
assert self .indent .depth < 3
5365
5385
if self .indent .depth == 2 :
5366
5386
# back to a parameter
5367
- self .state = DSLParserState .PARAMETER
5368
- return self .handle_parameter (function = function , line = line )
5369
- assert self .indent .depth == 1
5370
- self .state = DSLParserState .FUNCTION_DOCSTRING
5371
- return self .handle_function_docstring (function = function , line = line )
5387
+ self .handle_line (
5388
+ function = function , line = line , new_state = DSLParserState .PARAMETER
5389
+ )
5390
+ else :
5391
+ assert self .indent .depth == 1
5392
+ self .handle_line (
5393
+ function = function , line = line , new_state = DSLParserState .FUNCTION_DOCSTRING
5394
+ )
5395
+ return function
5372
5396
5373
5397
assert function .parameters
5374
5398
last_param = next (reversed (function .parameters .values ()))
0 commit comments