def mix_results(resultll, productionset): """ Mix n sets of results """ #We mix all results into final result midlist = _create_combined_list(resultll) finallist = [] for combination in midlist: if len(combination) == 1: finallist.append(combination[0]) else: left_pos = [x.leftpos for x in combination if x.leftpos is not None][0] right_pos = [x.rightpos for x in combination if x.rightpos is not None][-1] #Creates a node with all elements, and originals nodes are the childs of the new node symbollist = [] compoundword = "" for element in combination: compoundword += element.content symbollist += element.symbollist finalresult = ParseTree(left_pos, right_pos, symbollist, compoundword, combination[0].production, valid = all([x for x in combination])) #Add childs to result. FIXME El problema es que estamos añadiendo como hijos del nuevo los elementos ya creados rightside = [] for child in combination: assert(child != finalresult) #Avoid recursion finalresult.append_child(child) rightside += child.symbollist #Creating the rightside of the production to guess the full production #FIXME doesn't work with terminals try: finalresult.production = productionset.getProductionsBySide(rightside, "right") except IndexError: finalresult.production = None finally: finallist.append(finalresult) #rule found; we add binded together version return finallist
def mix_results(resultll:list, productionset): """ Mix n sets of results """ from pydsl.Grammar.Tree import ParseTree production = None for resultl in resultll: assert(isinstance(resultl, TypeCheckList) and resultl.instancetype == ParseTree) midlist = [] #All blocks combinations are stored here firstindex = 0 while firstindex < len(resultll) and len(resultll[firstindex]) == 0: firstindex += 1 if firstindex == len(resultll): return [] validsets = 1 #Processing head set firstresultl = resultll[firstindex] for result in firstresultl: if not(isinstance(result, ParseTree)): raise TypeError if result.leftpos == 0: midlist.append([result]) elif result.leftpos == None: raise Exception #FIXME:What's the right thing to do here? #Processing Tail sets for resultl in resultll[firstindex + 1:]: #For each result list if len(resultl) == 0: continue for result in resultl: #for each result tmp = [] for middleresult in midlist: #combinamos todos los elementos con los ya apuntados en la lista intermedia #Here we mix every result with intermediate results list lastresult = middleresult[-1] if lastresult.production != result.production: pass if result.rightpos == None: result.rightpos = lastresult.rightpos result.leftpos = lastresult.rightpos if lastresult.rightpos == None or result.leftpos == None: tmp.append(middleresult + [ParseTree(result.leftpos, result.rightpos, \ result.symbollist, result.content, result.production, \ TypeCheckList(ParseTree, result.childlist), result.valid)]) elif lastresult.rightpos == result.leftpos: tmp.append(middleresult + [ParseTree(result.leftpos, result.rightpos, \ result.symbollist, result.content, result.production, \ TypeCheckList(ParseTree, result.childlist), result.valid)]) midlist += tmp validsets += 1 #eliminamos los resultados intermedios que no contienen tantos elementos como hemos procesado. Es decir, no se ha encontrado una combinacion valida en la ultima mezcla #Removes all results that have less elements than the number of valid sets for element in midlist[:]: if len(element) != validsets: midlist.remove(element) #Combinamos resultados en la lista final #We mix all results into final result finallist = TypeCheckList(ParseTree) for middleresult in midlist: if len(middleresult) == 1: finallist.append(middleresult[0]) elif middleresult[0].leftpos != None and middleresult[-1].rightpos != None: #mezclamos la coleccion y dejamos los originales como hijos #Creates a node with all elements, and originals nodes are the childs of the new node symbollist = [] for element in middleresult: compoundword += element.content symbollist += element.symbollist finalresult = ParseTree(middleresult[0].leftpos, middleresult[-1].rightpos, symbollist, compoundword, middleresult[0].production, valid = result.valid) psl = middleresult[0].production #Add childs to result. FIXME El problema es que estamos añadiendo como hijos del nuevo los elementos ya creados error = False rightside = [] for child in middleresult: assert(child != finalresult) finalresult.append_child(child) if child.production: rightside += child.production.leftside if not child.valid: finalresult.valid = False #valid status propagates upwards #if error: # print([str(x.production) for x in middleresult]) # continue if productionset: try: finalresult.production = productionset.getProductionsBySide(rightside, "right") except IndexError: finallist += middleresult #rule not found: we add it unprocessed (non joined version) else: finallist.append(finalresult) #rule found; we add binded together version else: raise Exception return finallist