def__jumpSpace(self, string, loc): while loc < len(string) and string[loc] in self.__spaceChars: loc += 1 return loc
def__getBackSlashNum(self, lua_str, loc): backslash_num = 0 while loc >= 0and lua_str[loc] == '\': loc -= 1 backslash_num += 1 return backslash_num
def__getStringEnd(self, lua_str, loc, quote): result = loc + 1 if lua_str[loc] == quote: end = lua_str.find(quote, loc + 1) if end != loc + 1: at_end = False whilenot at_end: if self.__getBackSlashNum(lua_str, end - 1) % 2 == 0: at_end = True ifnot at_end: end = lua_str.find(quote, end + 1) if end < 0: raise Exception("wrong quotes!") result = end return result
def__getStringConent(self, lua_str, loc): """ return the string in the @para lua_str thee result contain the quotes""" loc = self.__jumpSpace(lua_str, loc) result = None if lua_str[loc] == '"'or lua_str[loc] == "'": end = self.__getStringEnd(lua_str, loc, lua_str[loc]) result = lua_str[loc: end + 1] loc = end + 1 return result, loc
def__removeComment(self, lua_str): result = "" loc = 0 while loc < len(lua_str): if lua_str[loc] == '"'or lua_str[loc] == "'": string, loc = self.__getStringConent(lua_str, loc) result += string # remove comment elif lua_str[loc] == '-': if loc + 1 < len(lua_str) and lua_str[loc + 1] == '-': loc += 2 # multi line comments if loc + 1 < len(lua_str) and lua_str[loc:loc + 2] == '[[': loc = lua_str.find(']]', loc + 1) + 2 # single line comment else: loc = lua_str.find('n', loc + 1) + 1 # add a space after remove the comment result += ' ' else: result += lua_str[loc] loc += 1 # remove 'r' elif lua_str[loc] == 'r': loc += 1 continue else: result += lua_str[loc] loc += 1 return result
def__getRightBrace(self, lua_str, loc): loc = lua_str.find('{', loc) + 1 left_barce_num = 0 while loc < len(lua_str): if lua_str[loc] == '"'or lua_str[loc] == "'": loc = self.__getStringEnd(lua_str, loc, lua_str[loc]) + 1 elif lua_str[loc] == '{': left_barce_num -= 1 loc += 1 elif lua_str[loc] == '}': left_barce_num += 1 if left_barce_num >= 1: break loc += 1 else: loc += 1 if loc == len(lua_str): raise Exception("wrong number of brace") return loc
def__getRightBracket(self, lua_str, loc): loc = lua_str.find('[', loc) + 1 while loc < len(lua_str): if lua_str[loc] == '"'or lua_str[loc] == "'": loc = self.__getStringEnd(lua_str, loc, lua_str[loc]) + 1 elif lua_str[loc] == ']': break else: loc += 1