diff --git a/src/code_.py b/src/code_.py index 98417e078b06d01ba089d8651517ef78a458bbf9..cd62a1bb4e494753719fe1908e9310321ed701a0 100644 --- a/src/code_.py +++ b/src/code_.py @@ -1,4 +1,4 @@ -from .outils import remove_comments, get_parenthese_count, get_curly_count +from .outils import remove_comments, get_parenthese_count, get_curly_count, get_code_until_parenthese_count from .bioflowinsighterror import BioFlowInsightError import re from . import constant @@ -32,12 +32,22 @@ class Code: raise BioFlowInsightError(f"The presence of '{bit_of_code}' is detected{self.get_string_line(bit_of_code)}.", num = 1,origin=self) + #This methods turns a single line condition into a muli line conditions def turn_single_condition_into_multiline(self, code): + + pattern = r"(if *\()(.+)\n" to_replace = [] - for match in re.finditer(r"if *\((.+)\) *([^{\n]+)\n", code): - old = match.group(0) - new = f"if ({match.group(1)}) {{\n{match.group(2)}\n}}\n" - to_replace.append((old, new)) + for match in re.finditer(pattern, code): + all = match.group(0) + extarcted = match.group(2).strip() + + if(extarcted!="" and extarcted[-1] not in ["{", "}"]): + start, end = match.span(1) + extracted_condition = get_code_until_parenthese_count(code=code[end:], val=-1) + condition = extracted_condition[:-1] + body = extarcted.replace(extracted_condition, "") + new = f"if ({condition}) {{\n{body}\n}}\n" + to_replace.append((all, new)) for r in to_replace: old, new = r code = code.replace(old, new)