@@ -2870,26 +2870,36 @@ def __validate_mfsg(line: Line, string_idx: int) -> STResult[None]:
2870
2870
return None
2871
2871
2872
2872
2873
- def get_first_unmatched_rpar_idx (leaves : List [Leaf ]) -> Result [int , ValueError ]:
2874
- unmatched_parens = 0
2875
- for (i , leaf ) in enumerate (leaves ):
2876
- if leaf .type == token .LPAR :
2877
- unmatched_parens += 1
2878
- continue
2873
+ class StringStripperMixin (StringTransformerMixin ):
2874
+ @abstractmethod
2875
+ def _do_match (self , line : Line ) -> STResult [str ]:
2876
+ pass
2877
+
2878
+ @abstractmethod
2879
+ def _do_transform (self , line : Line , string_idx : int ) -> Iterator [STResult [Line ]]:
2880
+ pass
2879
2881
2880
- if leaf .type == token .RPAR and unmatched_parens == 0 :
2881
- return i
2882
+ @staticmethod
2883
+ def get_first_unmatched_rpar_idx (leaves : List [Leaf ]) -> Result [int , ValueError ]:
2884
+ unmatched_parens = 0
2885
+ for (i , leaf ) in enumerate (leaves ):
2886
+ if leaf .type == token .LPAR :
2887
+ unmatched_parens += 1
2888
+ continue
2882
2889
2883
- if leaf .type == token .RPAR :
2884
- if unmatched_parens > 0 :
2885
- unmatched_parens -= 1
2886
- else :
2887
- return ValueError ("Found RPAR that matches LPAR from the past!" )
2890
+ if leaf .type == token .RPAR and unmatched_parens == 0 :
2891
+ return i
2892
+
2893
+ if leaf .type == token .RPAR :
2894
+ if unmatched_parens > 0 :
2895
+ unmatched_parens -= 1
2896
+ else :
2897
+ return ValueError ("Found RPAR that matches LPAR from the past!" )
2888
2898
2889
- return ValueError ("No RPAR found!" )
2899
+ return ValueError ("No RPAR found!" )
2890
2900
2891
2901
2892
- class StringArgCommaStripper (StringTransformerMixin ):
2902
+ class StringArgCommaStripper (StringStripperMixin ):
2893
2903
def _do_match (self , line : Line ) -> STResult [str ]:
2894
2904
regex_result = self ._regex_match (
2895
2905
line ,
@@ -2948,7 +2958,7 @@ def _do_transform(self, line: Line, string_idx: int) -> Iterator[STResult[Line]]
2948
2958
new_line = line .clone ()
2949
2959
new_line .comments = line .comments .copy ()
2950
2960
2951
- idx_result = get_first_unmatched_rpar_idx (line .leaves [string_idx + 2 :])
2961
+ idx_result = self . get_first_unmatched_rpar_idx (line .leaves [string_idx + 2 :])
2952
2962
if isinstance (idx_result , ValueError ):
2953
2963
raise RuntimeError (
2954
2964
f"Logic Error. { self .__class__ .__name__ } was unable to find the ending"
@@ -2975,7 +2985,7 @@ def _do_transform(self, line: Line, string_idx: int) -> Iterator[STResult[Line]]
2975
2985
yield new_line
2976
2986
2977
2987
2978
- class StringParensStripper (StringTransformerMixin ):
2988
+ class StringParensStripper (StringStripperMixin ):
2979
2989
def _do_match (self , line : Line ) -> STResult [str ]:
2980
2990
regex_result = self ._regex_match (
2981
2991
line ,
@@ -3028,7 +3038,7 @@ def _do_match(self, line: Line) -> STResult[str]:
3028
3038
return string_value
3029
3039
3030
3040
def _do_transform (self , line : Line , string_idx : int ) -> Iterator [STResult [Line ]]:
3031
- idx_result = get_first_unmatched_rpar_idx (line .leaves [string_idx + 1 :])
3041
+ idx_result = self . get_first_unmatched_rpar_idx (line .leaves [string_idx + 1 :])
3032
3042
if isinstance (idx_result , ValueError ):
3033
3043
raise RuntimeError (
3034
3044
f"Logic Error. { self .__class__ .__name__ } was unable to find the ending"
0 commit comments