def showSearchResult(self): self.resultNum = len(self.satisfiedNodes) self.currentIndex = 0 self.chosenNum = 0 self.findResultsList.clearContents() self.findResultsList.setColumnCount(self.columnNum) self.findResultsList.setColumnWidth(0, 20) self.findResultsList.setRowCount(self.resultNum) self.findResultsList.setHorizontalHeaderLabels(['', '初始值', '替换值']) argName = self.argInput.itemText(self.argInput.currentIndex()) searchValue = self.searchInput.text() replaceValue = self.replaceInput.text() for i in range(self.resultNum): self.contentType, originContent = self.satisfiedNodes[i].getArgsTypeAndValue(argName) originContent = str(originContent) replaceContent = originContent.replace(searchValue, replaceValue) suc, replaceContent = util.parseValue(self.contentType, replaceContent) replaceContent = str(replaceContent) if suc else '非法(不会进行替换)' # checkItem = QTableWidgetItem('') # checkItem.setFlags(Qt.ItemIsUserCheckable) # checkItem.setCheckable(True) checkItem = QCheckBox() checkItem.stateChanged.connect(self.updateChosenNums) self.findResultsList.setCellWidget(i, 0, checkItem) self.findResultsList.setItem(i, 1, QTableWidgetItem(originContent)) self.findResultsList.setItem(i, 2, QTableWidgetItem(replaceContent))
def replace(self): DialogLogger.info('replace values') if not self.chosenNum: QMessageBox.warning(None, 'Warning', '替换时没有选中任何内容', QMessageBox.Ok) # super(ReplaceDialog, self).accept() return argName = self.argInput.itemText(self.argInput.currentIndex()) searchValue = self.searchInput.text() replaceValue = self.replaceInput.text() for i in range(self.resultNum): checkBox = self.findResultsList.cellWidget(i, 0) if checkBox.isChecked(): node = self.satisfiedNodes[i] inputItem = node.getInputItemByName(argName) if inputItem: contentType, contentValue = inputItem.itemContent.contentType, inputItem.itemContent.contentValue suc, value = util.parseValue(contentType, str(contentValue).replace(searchValue, replaceValue)) if suc: inputItem.doneChangeContentValue(value, mode='set') self._reset() self.findNodesByArgsValue()
def updateReplaceValues(self, replaceValue): for i in range(self.resultNum): originContent = self.findResultsList.item(i, 1).text() suc, replaceContent = util.parseValue(self.contentType, originContent.replace(self.searchInput.text(), replaceValue)) replaceContent = str(replaceContent) if suc else '非法(不会进行替换)' self.findResultsList.item(i, 2).setText(replaceContent)
def videoURL(self, url): print self.name + ' url -> ' + url try: # url = urllib2.build_opener(urllib2.HTTPRedirectHandler).open(url).url vidid=re.compile('http://www.hostingcup.com/(.+?)').findall(url) if (len(vidid) == 0): return '', 'Error extracting video_id from URL' newURL = "http://vidpe.com/%s" % vidid[0] req = urllib2.Request(url) req.add_header('User-Agent', _UserAgent_) req.add_header('Referer', 'http://www.hostingcup.net/') page = urllib2.urlopen(req);response=page.read();page.close() link = ''.join(response.splitlines()).replace('\t','') params = re.compile("return p\}\(\'(.+?)\',36,(.+?),\'(.+?)\'").findall(link) result = parseValue(params[0][0], 36, int(params[0][1]), params[0][2].split('|')) result = result.replace('\\','').replace('"','\'') movielink = re.compile("s1.addVariable\(\'file\',\'(.+?)\'\);").findall(result)[0] print self.name + '->' + movielink return movielink, '' except: pass
def parseValue(self): text = str(self.newInput.text()) print('user input', text) return util.parseValue(self.contentType, text)
def parse_line(self, cleaned_line=None, is_complete_line=True): """Turns a cleaned up line into its parsed components, ready for the assembler to handle""" if cleaned_line == None: cleaned_line = self._cleaned_line if cleaned_line == "": return [] LINE = cleaned_line.split(" ") # split into individual components # parsing stack parse_stack = [] ind = 0 if is_complete_line: # if not a reserved word at beginning of line, then it is a label if not LINE[0].lower() in util.RESERVED_FLAT: parse_stack.append({ "type": util.DATA_TYPES.LABEL, "label": LINE[0], "varname": LINE[0] }) # indicate this is a label, so parser doesnt mess with it ind += 1 elif LINE[0].lower() in util.CONDITIONAL_SYMBOLS: # is a conditional statement that affets assembler process if LINE[0].lower() == "if": parse_stack.append({ "type": util.DATA_TYPES.CONDITIONAL_IF, "condition": LINE[1] }) ind += 2 elif LINE[0].lower() == "endif": parse_stack.append( {"type": util.DATA_TYPES.CONDITIONAL_ENDIF}) ind += 1 elif LINE[0].lower() in util.INCLUDE_SYMBOLS: # is an include statement, so rest of line is a file filename = "".join(LINE[1:]).replace("\"", "").replace("\'", "") parse_stack.append({ "type": util.DATA_TYPES.INCLUDE, "filename": filename }) ind += len(LINE) elif LINE[0].lower() in util.GLOBAL_SYMBOLS: # is a global variable identifier for item in LINE[1:]: if not (item in util.SEPARATOR_SYMBOLS): parse_stack.append({ "type": util.DATA_TYPES.GLOBAL, "varname": item }) ind += len(LINE) elif LINE[0].lower() in util.EXTERNAL_SYMBOLS: # is an external variable identifier for item in LINE[1:]: if not (item in util.SEPARATOR_SYMBOLS): parse_stack.append({ "type": util.DATA_TYPES.EXTERNAL, "varname": item }) ind += len(LINE) else: #print("IN SUB LINE: " + cleaned_line) pass while ind < len(LINE): # iterate through LINE to parse item = LINE[ind] if util.isValue(item): # item is a raw value, so turn it into one parse_stack.append({ "type": util.DATA_TYPES.VALUE, "value": util.parseValue(item) }) elif item in "+-([": # if unsure about status of symbol if item in "([": # if ambiguous separator #print("STACK BEFORE: " + str(parse_stack)) if parse_stack[-1]["type"] in (util.DATA_TYPES.OPCODE, util.DATA_TYPES.EQU): was_op = False if parse_stack[-1]["type"] == util.DATA_TYPES.OPCODE: was_op = True off = 1 numparens = 1 ended = False # parse where end of this paren is while ind + off < len(LINE): if item == "(": if LINE[ind + off] == "(": numparens += 1 elif LINE[ind + off] == ")": numparens -= 1 elif item == "[": if LINE[ind + off] == "[": numparens += 1 elif LINE[ind + off] == "]": numparens -= 1 if numparens == 0: ended = True break off += 1 # unbalanced parens if not ended: raise LineException(self.line_number, "unbalanced parens", self._file_name) #print("STACK: " + str(parse_stack)) sub_parsed = self.parse_line(" ".join( LINE[ind + 1:ind + off]), is_complete_line=False) if was_op: if item == "(": parse_stack.append( {"type": util.DATA_TYPES.INDIRECT_START}) elif item == "[": parse_stack.append({ "type": util.DATA_TYPES.INDIRECT_LONG_START }) else: parse_stack.append({ "type": util.DATA_TYPES.OPERATOR, "operator": "(" }) for p in sub_parsed: parse_stack.append(p) if was_op: if item == "(": parse_stack.append( {"type": util.DATA_TYPES.INDIRECT_END}) elif item == "[": parse_stack.append({ "type": util.DATA_TYPES.INDIRECT_LONG_END }) else: parse_stack.append({ "type": util.DATA_TYPES.OPERATOR, "operator": ")" }) #print("AFTER: " + str(parse_stack)) ind += off else: # just a priority paren parse_stack.append({ "type": util.DATA_TYPES.OPERATOR, "operator": "(" }) elif item in "+-": isarith = True # default is arithetic symbol unless conditions met if item == "-" or item == "+": # check to see if this is a pos/neg or if it is an arithmetic +/- # is pos/neg iff : # #1. next item is a value # 2. prev item is NOT a value, variable, expression # 3. if prev item is an operator, must not be closed paren # or if start of expression if ind != len(LINE) - 1 and ind != 0: #if util.isValue(LINE[ind+1]): # next item is value if parse_stack[-1][ "type"] != util.DATA_TYPES.VALUE: # prev item not value if parse_stack[-1][ "type"] != util.DATA_TYPES.VARIABLE: # prev item not variable if parse_stack[-1][ "type"] != util.DATA_TYPES.EXPRESSION: # prev item not expression # fits all criterion to be pos/neg isarith = False if parse_stack[-1][ "type"] == util.DATA_TYPES.OPERATOR: # prev item is an operator, check if it is a closed parenthesis if parse_stack[-1][ "operator"] == ")": # if prev item is closed paren, must be an operator isarith = True if parse_stack[-1][ "operator"] == "]": # if prev item is closed paren, must be an operator isarith = True else: isarith = False if not isarith: ''' # base value is next if util.isValue(LINE[ind+1]): val = util.parseValue(LINE[ind+1]) if item == "-": # if pos, no change is needed # however, if neg, need to turn to negative value val = -1*val parse_stack.append({"type": util.DATA_TYPES.VALUE, "value": val}) ind += 1 # skip over next item else: # negative of a variable, so push -1 * variable parse_stack.append({"type": util.DATA_TYPES.VALUE, "value": -1}) parse_stack.append({"type": util.DATA_TYPES.OPERATOR, "operator": "*"}) ''' if item == "-": parse_stack.append({ "type": util.DATA_TYPES.VALUE, "value": -1 }) parse_stack.append({ "type": util.DATA_TYPES.OPERATOR, "operator": "*" }) elif item == "+": parse_stack.append({ "type": util.DATA_TYPES.VALUE, "value": 1 }) parse_stack.append({ "type": util.DATA_TYPES.OPERATOR, "operator": "*" }) else: # is an arithmetic op parse_stack.append({ "type": util.DATA_TYPES.OPERATOR, "operator": item }) elif item in util.ARITHMETIC_SYMBOLS: # item is an operator parse_stack.append({ "type": util.DATA_TYPES.OPERATOR, "operator": item }) elif item in util.REGISTER_SYMBOLS: # item is a register reg = util.NONE if item in util.REGA_SYMBOLS: reg = "a" elif item in util.REGX_SYMBOLS: reg = "x" elif item in util.REGY_SYMBOLS: reg = "y" elif item in util.REGS_SYMBOLS: reg = "s" if reg != util.NONE: parse_stack.append({ "type": util.DATA_TYPES.REGISTER, "register": reg }) if reg == "s": if len(parse_stack) >= 4: if parse_stack[-4][ "type"] == util.DATA_TYPES.OPCODE or parse_stack[ -4]["type"] == util.DATA_TYPES.INDIRECT_START: parse_stack.insert( len(parse_stack) - 3, { "type": util.DATA_TYPES.TYPE, "valtype": "sr", "size": 1 }) elif item in util.SEPARATOR_SYMBOLS: # item is a separator, pretty simple parse_stack.append({"type": util.DATA_TYPES.SEPARATOR}) elif item in util.OPCODE_SYMBOLS: # item is an opode mnemonic parse_stack.append({ "type": util.DATA_TYPES.OPCODE, "opcode": item.lower(), "size": 1, "reg": util.OPCODE_REGS[item.lower()] }) elif item in util.TYPE_SYMBOLS: # item is a designator for a type itemtype = util.NONE if item == "<": itemtype = "dp" size = 1 elif item == "!": itemtype = "addr" size = 2 elif item == ">": itemtype = "long" size = 3 elif item == "#": itemtype = "const" size = None elif item == util.BANK_CHAR: itemtype = "bank" size = None #1 elif item == util.OFFSET_CHAR: itemtype = "offset" size = None #2 elif item == util.HIGH_CHAR: itemtype = "high" size = None #1 elif item == util.LOW_CHAR: itemtype = "low" size = None #1 elif item == "$": itemtype = "constaddr" size = None is_equ = False if itemtype == "constaddr": if parse_stack != []: if parse_stack[-1]["type"] == util.DATA_TYPES.EQU: top = parse_stack.pop() if parse_stack != []: if parse_stack[-1] == top: parse_stack = parse_stack[:-1] parse_stack.append({ "type": util.DATA_TYPES.LABEL, "varname": top["varname"], "label": top["varname"], "is_near": True }) is_equ = True if not is_equ: if itemtype != util.NONE: if itemtype != "constaddr": parse_stack.append({ "type": util.DATA_TYPES.TYPE, "valtype": itemtype, "size": size }) else: near_label = "NEAR_VAR" + str( self.get_line_num()) + "$" if parse_stack[0]["type"] != util.DATA_TYPES.LABEL: parse_stack = [{ "type": util.DATA_TYPES.LABEL, "label": near_label, "varname": near_label }] + parse_stack near_label = parse_stack[0]["label"] LINE[ind] = near_label ind -= 1 ''' if itemtype == "constaddr": print(parse_stack) ''' elif item in util.GLOBAL_SYMBOLS: # item is an identifier for a global variable parse_stack.append({"type": util.DATA_TYPES.GLOBAL}) elif item in util.EXTERNAL_SYMBOLS: # item is an identifier for an external variable parse_stack.append({"type": util.DATA_TYPES.EXTERNAL}) elif item in util.INCLUDE_SYMBOLS: # item is an identifier for an included file parse_stack.append({"type": util.DATA_TYPES.INCLUDE}) elif item in util.SECTION_SYMBOLS: # item is an identifier for a section parse_stack.append({"type": util.DATA_TYPES.SECTION}) if ind + 1 < len(LINE): parse_stack[-1]["SECTION_CLASS"] = LINE[ind + 1] ind += 1 else: parse_stack[-1]["SECTION_CLASS"] = "REL" elif item in util.COMN_SYMBOLS: # item is an identifier for a COMN section lbl = "COMN" parse_stack.append({ "type": util.DATA_TYPES.LABEL, "label": lbl, "varname": lbl }) parse_stack.append({"type": util.DATA_TYPES.SECTION}) if ind + 1 < len(LINE): parse_stack[-1]["SECTION_CLASS"] = LINE[ind + 1] ind += 1 else: parse_stack[-1]["SECTION_CLASS"] = "REL" elif item in util.GROUP_SYMBOLS: # item is an identifier for a group parse_stack.append({"type": util.DATA_TYPES.GROUP}) if ind + 1 < len(LINE): parse_stack[-1]["SECTION_GROUP"] = LINE[ind + 1] ind += 1 elif item in util.ORG_SYMBOLS: # item is an identifier for an org specifier lbl = self._file_name.split(".")[0] parse_stack.append({ "type": util.DATA_TYPES.LABEL, "label": lbl, "varname": lbl }) parse_stack.append({"type": util.DATA_TYPES.ORG}) if LINE[ind + 1].lower() == "$": ind += 1 #parse_stack[-1]["offset"] = util.parseValue(LINE[ind+1].replace("h", "").replace("H", "") + "h") #ind += 1 elif item in util.DBANK_SYMBOLS: # item is an identifier for a dbank instruction parse_stack.append({"type": util.DATA_TYPES.DATA_BANK}) sub_parsed = self.parse_line(" ".join(LINE[ind + 1:]), is_complete_line=False) parse_stack[-1]["bank"] = [] for p in sub_parsed: parse_stack[-1]["bank"].append(p) ind += len(sub_parsed) elif item in util.DPAGE_SYMBOLS: # item is an identifier for a dpage instruction parse_stack.append({"type": util.DATA_TYPES.DATA_PAGE}) sub_parsed = self.parse_line(" ".join(LINE[ind + 1:]), is_complete_line=False) parse_stack[-1]["page"] = [] for p in sub_parsed: parse_stack[-1]["page"].append(p) ind += len(sub_parsed) elif item in util.END_SYMBOLS: # item is an identifier for an END instruction parse_stack.append({"type": util.DATA_TYPES.END}) elif item in util.PROCESSOR_SYMBOLS: # item is an identifier for a processor flag parse_stack.append({ "type": util.DATA_TYPES.PFLAG, "flag": item.lower() }) elif item in util.EQU_SYMBOLS: # item is an identifier for an EQU identifier try: var = parse_stack.pop()["varname"] parse_stack.append({ "type": util.DATA_TYPES.EQU, "varname": var, "label": var }) except: raise LineException( self.get_line_num(), "EQU prev does not have varname. \n" + self.get_raw(), self.get_file_name()) #print(str(LINE), str(parse_stack)) elif item in util.DATA_SYMBOLS: # item is an identifier for byte data if item in util.BYTE_SYMBOLS: datatype = util.DATA_TYPES.DBYTE elif item in util.WORD_SYMBOLS: datatype = util.DATA_TYPES.DWORD elif item in util.LONG_SYMBOLS: datatype = util.DATA_TYPES.DLONG parse_stack.append({"type": datatype}) elif item in util.PSEG_SYMBOLS: # item is an identifier for a PROGRAM section lbl = "P" + self._file_name.split(".")[0] parse_stack.append({ "type": util.DATA_TYPES.LABEL, "label": lbl, "varname": lbl }) parse_stack.append({"type": util.DATA_TYPES.SECTION}) if ind + 1 < len(LINE): parse_stack[-1]["SECTION_CLASS"] = LINE[ind + 1] ind += 1 else: parse_stack[-1]["SECTION_CLASS"] = "REL" elif item in util.DSEG_SYMBOLS: # item is an identifier for a DATA section lbl = "D" + self._file_name.split(".")[0] parse_stack.append({ "type": util.DATA_TYPES.LABEL, "label": lbl, "varname": lbl }) parse_stack.append({"type": util.DATA_TYPES.SECTION}) if ind + 1 < len(LINE): parse_stack[-1]["SECTION_CLASS"] = LINE[ind + 1] ind += 1 else: parse_stack[-1]["SECTION_CLASS"] = "REL" elif item in util.STORAGE_DIRECTIVE_SYMBOLS: # item is a storage directive identifier try: var = parse_stack.pop()["varname"] parse_stack.append({ "type": util.DATA_TYPES.STORAGE_DIRECTIVE, "storage_size": 0, "varname": var, "label": var }) except: raise LineException( self.get_line_num(), "Storage Directive prev does not have varname. \n" + self.get_raw(), self.get_file_name()) elif item in util.MACRO_SYMBOLS: # item is an identifier for a macro if parse_stack == []: raise LineException( self.get_line_num(), "Macro is not named.\n\t" + self.get_raw(), self.get_file_name()) if not (parse_stack[-1]["type"] in (util.DATA_TYPES.LABEL, util.DATA_TYPES.VARIABLE)): raise LineException( self.get_line_num(), "Improper format for macro.\n\t" + self.get_raw(), self.get_file_name()) top = parse_stack[-1] parse_stack = parse_stack[:-1] parse_stack.append({ "type": util.DATA_TYPES.MACRO, "varname": top["varname"], "label": top["label"] }) elif item in util.END_MACRO_SYMBOLS: # item is an identifier for an ENDM instruction parse_stack.append({"type": util.DATA_TYPES.END_MACRO}) else: # item is a variable if item[-1] == "$": # "near" variable size = 2 if parse_stack != []: if parse_stack[-1]["type"] == util.DATA_TYPES.OPCODE: if parse_stack[-1]["opcode"].lower() in ( "bcc", "blt", "bcs", "bge", "beq", "bmi", "bne", "bpl", "bra", "bvc", "bvs"): size = 1 if parse_stack[-1]["type"] == util.DATA_TYPES.TYPE: size = 0 parse_stack.append({ "type": util.DATA_TYPES.VARIABLE, "varname": item, "label": item, "vartype": util.DATA_TYPES.NEARVAR, "size": size, "is_near": True }) else: # normal variable name, possibly is_normal = True size = 0 if parse_stack != []: if parse_stack[-1]["type"] == util.DATA_TYPES.OPCODE: if parse_stack[-1]["opcode"].lower() in ( "bcc", "blt", "bcs", "bge", "beq", "bmi", "bne", "bpl", "bra", "bvc", "bvs", "brl", "per"): # "near" variable is_normal = False size = 1 if parse_stack[-1]["opcode"].lower() in ( "brl", "per"): size = 2 if parse_stack[-1][ "type"] == util.DATA_TYPES.TYPE: size = 0 parse_stack.append({ "type": util.DATA_TYPES.VARIABLE, "varname": item, "label": item, "vartype": util.DATA_TYPES.NEARVAR, "size": size, "is_near": True }) if is_normal: if parse_stack != []: if parse_stack[-1]["type"] != util.DATA_TYPES.TYPE: size = 2 parse_stack.append({ "type": util.DATA_TYPES.VARIABLE, "varname": item, "label": item, "vartype": util.DATA_TYPES.NORMALVAR, "size": size }) # mercy its over thank god ind += 1 for p in range(len(parse_stack)): if not ("size" in parse_stack[p]): parse_stack[p]["size"] = 0 if parse_stack[p]["type"] == util.DATA_TYPES.LABEL: if not "is_near" in parse_stack[p]: parse_stack[p]["is_near"] = False ind = 0 while ind < len(parse_stack): ''' if parse_stack[ind]["type"] == util.DATA_TYPES.OPCODE: if parse_stack[ind]["opcode"].lower() in ("mvp", "mvn"): i = ind+1 while i < len(parse_stack): no_inc = False if parse_stack[i]["type"] == util.DATA_TYPES.TYPE: if not parse_stack[i]["valtype"] in (util.BANK_CHAR, util.OFFSET_CHAR, util.HIGH_CHAR, util.LOW_CHAR): parse_stack = parse_stack[:i] + parse_stack[i+1:] no_inc = True if not no_inc: i += 1 ''' if parse_stack[ind]["type"] == util.DATA_TYPES.STORAGE_DIRECTIVE: for i in range(ind + 1, len(parse_stack)): parse_stack[i]["size"] = 0 elif parse_stack[ind]["type"] == util.DATA_TYPES.ORG: parse_stack[ind + 1]["size"] = 0 ''' elif parse_stack[ind]["type"] == util.DATA_TYPES.OPCODE: if parse_stack[ind]["opcode"].lower() in ("bcc", "blt", "bcs", "bge", "beq", "bmi", "bne", "bpl", "bra", "bvc", "bvs", "brl", "per"): if self.get_file_name().lower() == "kart-pers.asm": print(self.get_raw(), parse_stack) parse_stack[ind+1]["size"] ''' ind += 1 return parse_stack