Another enum

This commit is contained in:
Nyall Dawson 2024-08-13 10:53:37 +10:00
parent d72f69f89e
commit a33c97db40

View File

@ -29,12 +29,11 @@ class PrependType(Enum):
MakePrivate = auto() MakePrivate = auto()
# Constants class MultiLineType(Enum):
STRICT = 10 NotMultiline = auto()
UNSTRICT = 11 Method = auto()
MULTILINE_NO = 20 ConditionalStatement = auto()
MULTILINE_METHOD = 21
MULTILINE_CONDITIONAL_STATEMENT = 22
# Parse command-line arguments # Parse command-line arguments
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -81,7 +80,7 @@ class Context:
self.sip_run: bool = False self.sip_run: bool = False
self.header_code: bool = False self.header_code: bool = False
self.access: List[Visibility] = [Visibility.Public] self.access: List[Visibility] = [Visibility.Public]
self.multiline_definition: int = MULTILINE_NO self.multiline_definition: MultiLineType = MultiLineType.NotMultiline
self.classname: List[str] = [] self.classname: List[str] = []
self.class_and_struct: List[str] = [] self.class_and_struct: List[str] = []
self.declared_classes: List[str] = [] self.declared_classes: List[str] = []
@ -1039,12 +1038,12 @@ def fix_annotations(line):
# Remove argument # Remove argument
if 'SIP_PYARGREMOVE' in line: if 'SIP_PYARGREMOVE' in line:
dbg_info("remove arg") dbg_info("remove arg")
if CONTEXT.multiline_definition != MULTILINE_NO: if CONTEXT.multiline_definition != MultiLineType.NotMultiline:
prev_line = CONTEXT.output.pop().rstrip() prev_line = CONTEXT.output.pop().rstrip()
# Update multi line status # Update multi line status
parenthesis_balance = prev_line.count('(') - prev_line.count(')') parenthesis_balance = prev_line.count('(') - prev_line.count(')')
if parenthesis_balance == 1: if parenthesis_balance == 1:
CONTEXT.multiline_definition = MULTILINE_NO CONTEXT.multiline_definition = MultiLineType.NotMultiline
# Concatenate with above line to bring previous commas # Concatenate with above line to bring previous commas
line = f"{prev_line} {line.lstrip()}\n" line = f"{prev_line} {line.lstrip()}\n"
@ -1387,7 +1386,7 @@ while CONTEXT.line_idx < CONTEXT.line_count:
if re.search(r'SIP_SKIP|SIP_PYTHON_SPECIAL_', CONTEXT.current_line): if re.search(r'SIP_SKIP|SIP_PYTHON_SPECIAL_', CONTEXT.current_line):
dbg_info('SIP SKIP!') dbg_info('SIP SKIP!')
# if multiline definition, remove previous lines # if multiline definition, remove previous lines
if CONTEXT.multiline_definition != MULTILINE_NO: if CONTEXT.multiline_definition != MultiLineType.NotMultiline:
dbg_info('SIP_SKIP with MultiLine') dbg_info('SIP_SKIP with MultiLine')
opening_line = '' opening_line = ''
while not re.match(r'^[^()]*\(([^()]*\([^()]*\)[^()]*)*[^()]*$', while not re.match(r'^[^()]*\(([^()]*\([^()]*\)[^()]*)*[^()]*$',
@ -1396,7 +1395,7 @@ while CONTEXT.line_idx < CONTEXT.line_count:
if len(CONTEXT.output) < 1: if len(CONTEXT.output) < 1:
exit_with_error('could not reach opening definition') exit_with_error('could not reach opening definition')
dbg_info("removed multiline definition of SIP_SKIP method") dbg_info("removed multiline definition of SIP_SKIP method")
CONTEXT.multiline_definition = MULTILINE_NO CONTEXT.multiline_definition = MultiLineType.NotMultiline
# also skip method body if there is one # also skip method body if there is one
detect_and_remove_following_body_or_initializerlist() detect_and_remove_following_body_or_initializerlist()
@ -2099,7 +2098,7 @@ while CONTEXT.line_idx < CONTEXT.line_count:
# Remove keywords # Remove keywords
if CONTEXT.is_override_or_make_private != PrependType.NoPrepend: if CONTEXT.is_override_or_make_private != PrependType.NoPrepend:
# Handle multiline definition to add virtual keyword or make private on opening line # Handle multiline definition to add virtual keyword or make private on opening line
if CONTEXT.multiline_definition != MULTILINE_NO: if CONTEXT.multiline_definition != MultiLineType.NotMultiline:
rolling_line = CONTEXT.current_line rolling_line = CONTEXT.current_line
rolling_line_idx = CONTEXT.line_idx rolling_line_idx = CONTEXT.line_idx
dbg_info( dbg_info(
@ -2190,7 +2189,7 @@ while CONTEXT.line_idx < CONTEXT.line_count:
CONTEXT.current_line): CONTEXT.current_line):
# support Docstring for template based classes in SIP 4.19.7+ # support Docstring for template based classes in SIP 4.19.7+
CONTEXT.comment_template_docstring = True CONTEXT.comment_template_docstring = True
elif (CONTEXT.multiline_definition == MULTILINE_NO and elif (CONTEXT.multiline_definition == MultiLineType.NotMultiline and
(re.search(r'//', CONTEXT.current_line) or (re.search(r'//', CONTEXT.current_line) or
re.match(r'^\s*typedef ', CONTEXT.current_line) or re.match(r'^\s*typedef ', CONTEXT.current_line) or
re.search(r'\s*struct ', CONTEXT.current_line) or re.search(r'\s*struct ', CONTEXT.current_line) or
@ -2258,7 +2257,7 @@ while CONTEXT.line_idx < CONTEXT.line_count:
write_output("PSI", f"{CONTEXT.python_signature}\n") write_output("PSI", f"{CONTEXT.python_signature}\n")
# multiline definition (parenthesis left open) # multiline definition (parenthesis left open)
if CONTEXT.multiline_definition != MULTILINE_NO: if CONTEXT.multiline_definition != MultiLineType.NotMultiline:
dbg_info("on multiline") dbg_info("on multiline")
# https://regex101.com/r/DN01iM/4 # https://regex101.com/r/DN01iM/4
# TODO - original regex is incompatible with python -- it was: # TODO - original regex is incompatible with python -- it was:
@ -2268,7 +2267,7 @@ while CONTEXT.line_idx < CONTEXT.line_count:
CONTEXT.current_line): CONTEXT.current_line):
dbg_info("ending multiline") dbg_info("ending multiline")
# remove potential following body # remove potential following body
if CONTEXT.multiline_definition != MULTILINE_CONDITIONAL_STATEMENT and not re.search( if CONTEXT.multiline_definition != MultiLineType.ConditionalStatement and not re.search(
r'(\{.*}|;)\s*(//.*)?$', r'(\{.*}|;)\s*(//.*)?$',
CONTEXT.current_line): CONTEXT.current_line):
dbg_info("remove following body of multiline def") dbg_info("remove following body of multiline def")
@ -2277,16 +2276,16 @@ while CONTEXT.line_idx < CONTEXT.line_count:
# add missing semi column # add missing semi column
CONTEXT.output.pop() CONTEXT.output.pop()
write_output("MLT", f"{last_line};\n") write_output("MLT", f"{last_line};\n")
CONTEXT.multiline_definition = MULTILINE_NO CONTEXT.multiline_definition = MultiLineType.NotMultiline
else: else:
continue continue
elif re.match(r'^[^()]+\([^()]*(?:\([^()]*\)[^()]*)*[^)]*$', elif re.match(r'^[^()]+\([^()]*(?:\([^()]*\)[^()]*)*[^)]*$',
CONTEXT.current_line): CONTEXT.current_line):
dbg_info(f"Multiline detected:: {CONTEXT.current_line}") dbg_info(f"Multiline detected:: {CONTEXT.current_line}")
if re.match(r'^\s*((else )?if|while|for) *\(', CONTEXT.current_line): if re.match(r'^\s*((else )?if|while|for) *\(', CONTEXT.current_line):
CONTEXT.multiline_definition = MULTILINE_CONDITIONAL_STATEMENT CONTEXT.multiline_definition = MultiLineType.ConditionalStatement
else: else:
CONTEXT.multiline_definition = MULTILINE_METHOD CONTEXT.multiline_definition = MultiLineType.Method
continue continue
# write comment # write comment