def functionCuts(token): """Returns list of cut names assigned to a function.""" cuts = [] f = tmGrammar.Function_Item() if not tmGrammar.Function_parser(token, f): raise ValueError(token) for name in tmGrammar.Function_getCuts(f): cuts.append(name) return cuts
def functionObjects(token): """Returns list of object dicts assigned to a function.""" objects = [] f = tmGrammar.Function_Item() if not tmGrammar.Function_parser(token, f): raise ValueError(token) for token in tmGrammar.Function_getObjects(f): if isObject(token): objects.append(toObject(token)) return objects
def functionObjectsCuts(token): """Returns lists of cuts assigned to function objects. Index ist object number. >>> functionObjectsCuts("comb{MU0[MU-QLTY_HQ,MU-ETA_2p1],MU0[MU-QLTY_OPEN]}") ['MU-QLTY_HQ,MU-ETA_2p1', 'MU-QLTY_OPEN'] """ cuts = [] f = tmGrammar.Function_Item() if not tmGrammar.Function_parser(token, f): raise ValueError(token) # Note: returns strings containting list of cuts. for names in tmGrammar.Function_getObjectCuts(f): cuts.append(names.split(',') if names else []) return cuts
def validate(self, tokens): for token in tokens: if not isFunction(token): continue name = token.split( '{')[0].strip() # fetch function name, eg "dist{...}[...]" if not name in (tmGrammar.dist, tmGrammar.dist_orm): continue f = tmGrammar.Function_Item() if not tmGrammar.Function_parser(token, f): raise AlgorithmSyntaxError(f.message) objects = functionObjects(token) if name == tmGrammar.dist and len(objects) != 2: message = f"Function {name}{{...}} requires excactly two object requirements.\n" \ f"Invalid expression near `{token}`" raise AlgorithmSyntaxError(message)
def validate(self, tokens): for token in tokens: if not isFunction(token): continue f = tmGrammar.Function_Item() if not tmGrammar.Function_parser(token, f): raise AlgorithmSyntaxError(f.message) # Test for applied CHGCOR cuts if not list( filter(lambda name: name.startswith(tmGrammar.CHGCOR), functionCuts(token))): continue objects = functionObjects(token) for i in range(len(objects)): if objects[i].type != tmGrammar.MU: name = token.split('{')[0] # TODO: get function name message = f"All object requirements of function {name}{{...}} must be of type `{tmGrammar.MU}` when applying a `{tmGrammar.CHGCOR}` cut.\n" \ f"Invalid expression near `{token}`" raise AlgorithmSyntaxError(message, token)
def validate(self, tokens): for token in tokens: if not isFunction(token): continue name = token.split( '{')[0].strip() # fetch function name, eg "dist{...}[...]" if not name in (tmGrammar.comb, tmGrammar.comb_orm): continue f = tmGrammar.Function_Item() if not tmGrammar.Function_parser(token, f): raise AlgorithmSyntaxError(f.message) objects = functionObjects(token) sameBxRange = len(objects) if name == tmGrammar.comb_orm: sameBxRange -= 1 # exclude last object for i in range(sameBxRange): if int(objects[i].bx_offset) != int(objects[0].bx_offset): message = f"All object requirements of function {name}{{...}} must be of same bunch crossing offset.\n" \ f"Invalid expression near `{token}`" # TODO differentiate! raise AlgorithmSyntaxError(message, token)
def loadFunction(self, token): """Load dialog by values from function. Will raise a ValueError if string *token* is not a valid object. """ f = tmGrammar.Function_Item() if not tmGrammar.Function_parser(AlgorithmFormatter.compress(token), f): raise ValueError(token) self.functionComboBox.setCurrentIndex(self.functionComboBox.findData(f.name)) objects = list(tmGrammar.Function_getObjects(f)) for i, cuts in enumerate(tmGrammar.Function_getObjectCuts(f)): if cuts: objects[i] = "{object}[{cuts}]".format(object=objects[i], cuts=cuts) for helper in self.objectHelpers: if helper.index < len(objects): helper.lineEdit.setText(objects[helper.index]) else: helper.lineEdit.clear() for cut in self.cutModel._items: if cut.data().name in tmGrammar.Function_getCuts(f): cut.setCheckState(QtCore.Qt.Checked) self.updateInfoText() # refresh
def toFunctionItem(self, token): item = tmGrammar.Function_Item() if not tmGrammar.Function_parser(token, item): message = f"Invalid function statement `{item.message}`" raise AlgorithmSyntaxError(message, token) return item
tmGrammar.Algorithm_Logic.clear() tmGrammar.Algorithm_parser(expression) for token in tmGrammar.Algorithm_Logic.getTokens(): if token in tmGrammar.gateName: print token, " is a Gate" continue; isMatched = False for name in tmGrammar.functionName: if name == token[:len(name)]: print token, " is a function" isMatched = True o = tmGrammar.Function_Item() tmGrammar.Function_parser(token, o) o._print() break for name in tmGrammar.objectName: if name == token[:len(name)]: print token, " is an object" isMatched = True o = tmGrammar.Object_Item() tmGrammar.Object_parser(token, o) o._print() break if isMatched: continue print "failed to parse '", token, "'"