diff --git a/src/code_.py b/src/code_.py index dab97af9f221e608ccf8e92f3e917c3df907825b..79683779e6243d8cba624e03b4e1a0e732485714 100644 --- a/src/code_.py +++ b/src/code_.py @@ -1,4 +1,4 @@ -from .outils import remove_comments, get_parenthese_count, get_curly_count, get_code_until_parenthese_count +from .outils import remove_comments, get_parenthese_count, get_curly_count, get_code_until_parenthese_count, extract_curly from .bioflowinsighterror import BioFlowInsightError import re from . import constant @@ -20,6 +20,7 @@ class Code: self.code_wo_comments = re.sub(constant.DOUBLE_BACKSLAPSH_JUMP, ' ', self.code_wo_comments) self.code_wo_comments = re.sub(constant.BACKSLAPSH_JUMP, ' ', self.code_wo_comments) self.code_wo_comments = self.code_wo_comments.replace("||", "$OR$") + self.code_wo_comments = self.add_triple_quote_inside_map(self.code_wo_comments ) self.code_wo_comments = self.turn_single_condition_into_multiline(self.code_wo_comments) self.code_wo_comments = self.rewrite_ternary_operation_to_normal_condition(self.code_wo_comments) self.code_wo_comments = self.rewrite_jump_dot(self.code_wo_comments) @@ -121,6 +122,26 @@ class Code: code = code.replace(old, new) return code + #This function takes the code and adds '''...''' inside the map operator + def add_triple_quote_inside_map(self, code): + searching = True + while(searching): + searching = False + #TODO -> do the same with flatMap -> 668 + patterns = [r"\.\s*map\s*{", r"\.\s*flatMap\s*{"] + for word in ["map", "flatMap"]: + pattern = r"\.\s*"+word+r"\s*{" + for match in re.finditer(pattern, code): + start_map, end = match.span(0) + end_map = extract_curly(code, end) + old = code[start_map:end_map] + new_code = f".{word}_modified {{ '''\n{code[end:end_map-1]}\n''' }}" + code = code.replace(old, new_code) + searching = True + break + + return code + def add_to_ternary_operation_dico(self, old, new): self.origin.add_to_ternary_operation_dico(old, new) diff --git a/src/constant.py b/src/constant.py index e1e55a52090db6c6ee5ed82382e71352c5ff2f5e..6993cdd25a23673e677bb46eb4cbcc0f0b9a3362 100644 --- a/src/constant.py +++ b/src/constant.py @@ -13,7 +13,7 @@ ILLEGAL_IMPORTS = ["groovy", "java"] LIST_AS = ["as ", "As ", "AS ", "aS "] LIST_OPERATORS = ["distinct", "filter", "first", "last", "randomSample", "take", "unique", - "until","buffer","collate","collect","flatten","flatMap","groupBy","groupTuple","map","reduce","toList","toSortedList","transpose", + "until","buffer","collate","collect","flatten","flatMap", "flatMap_modified","groupBy","groupTuple","map","map_modified","reduce","toList","toSortedList","transpose", "splitCsv","splitFasta","splitFastq","splitText", "cross","collectFile","combine","concat","join","merge","mix","phase","spread","tap", "branch","choice","multiMap","into","separate","tap", @@ -109,7 +109,7 @@ DOUBLE_DOT_TUPLE = r"\(\s*\w+\s*(,\s*\w+\s*)+\)\s*=\s*([^\?\n]+)\s*\?([^\n]+)" END_OPERATOR = r' *(\(|{)' ILLEGAL_CHARCTER_BEFORE_POTENTIAL_CHANNELS = r"\w|\'|\"|\." ILLEGAL_CHARCTER_AFTER_POTENTIAL_CHANNELS = r"\w" -MERGE_OPERATIONS = r'\.\s*((merge|mix|concat|spread|join|phase|cross|combine|fromList|collect|fromPath|value|from|map|fromFilePairs)\s*(\(|\{))'#I've added map to this list cause channels can appear in map can concatenating channels -> it's a strange way of doing it +MERGE_OPERATIONS = r'\.\s*((merge|mix|concat|spread|join|phase|cross|combine|fromList|collect|fromPath|value|from|map|map_modified|fromFilePairs)\s*(\(|\{))'#I've added map to this list cause channels can appear in map can concatenating channels -> it's a strange way of doing it OPERATOR_IN_PIPE = r"\w+ *{[^}]*}|\w+ *\([^\)]*\)|\w+" SET_OPERATORS = ["choice", "separate", "tap", "into", "set"] TUPLE_EQUALS = r'\( *\w+( *, *\w+)+ *\) *=\s*(\w+)\s*\.' diff --git a/src/outils.py b/src/outils.py index bcaa57ea602ba64e86bbc9b6761ded47331114c4..c5d67695d13e2158b28d616d5fdf0ff1ec142bb2 100644 --- a/src/outils.py +++ b/src/outils.py @@ -1354,7 +1354,7 @@ def get_channels_to_add_in_false_conditions(body, emitted_channels): #This function removes the empty conditions def remove_empty_conditions(code): - pattern = r"(if *\(.+\)|else)\s*{(\s*)}" + pattern = r"(else +if *\(.+\)|if *\(.+\)|else)\s*{(\s*)}" def replace(text, pattern): def replacer(match): return match.group(0).replace(match.group(0), match.group(2))