def ansisClassBlock(strs, blocks, fout): print("is class") num = len(blocks) start = 0 for i in range(0, num): line = blocks[i].strip() if line and not comm.isComment(line): start = i break print("blocks[start] = ", blocks[start]) desc = blocks[start].strip() + " 行应该跟{,请注意代码格式" comm.assertStr( blocks[start].find('{') != -1 and blocks[start].find('}') == -1, desc) classname = extractClassName(blocks[start]) brief = extractClassBrief(blocks, start) writeClassToFile(classname, brief, fout) newblocks = [] newstrs = "" for i in range(1 + start, num): line = blocks[i] if comm.isComment(line): newblocks.append(line) elif comm.isStatement(line): newblocks.append(line) newstrs = newstrs + comm.rmComment(line) if newstrs.count("{") == newstrs.count("}"): print(newstrs) print("###") print(newblocks) ansisBlock(newstrs, newblocks, fout, classname) newstrs = "" newblocks.clear() print('\n###################\n\n\n\n') else: newblocks.append(line) newstrs = newstrs + comm.rmComment(line) if newstrs.count("{") == newstrs.count( "}") and newstrs.count('{') > 0: print(newstrs) print("###") print(newblocks) ansisBlock(newstrs, newblocks, fout, classname) newstrs = "" newblocks.clear() print('\n###################\n\n\n\n') if newstrs.count('{') < newstrs.count("}"): newstrs = "" newblocks.clear() print('\n###################\n\n\n\n')
def writeClassToFile(clsname, brief, fout): fout.write('## ' + clsname + '\n\n') fout.write('*类描述*\n\n') comm.assertStr(brief, clsname + "类 无描述信息") fout.write(brief + "\n") fout.write('\n\n')
def ansisStructBlock(strs, blocks, fout, classname=None): name = extractName(strs).strip() print("name = ", name) comm.assertStr(name, " 没有找到结构体名") brief = extractBrief(blocks) print("brief = ", brief) comm.assertStr(brief, " 没有描述信息") assert valid(blocks) items = extractStructEle(blocks) print("struct items = ", items) writeStructToFile(brief, name, items, fout)
def extractCommentParams(block): word = "\\param " tword = "\\tparam " note = "" start = -1 num = len(block) params = [] for id in range(0, num): line = block[id].strip() if word in line: #上一个提取完成 if len(note.strip()) > 0: params.append(note.strip()) note = "" start = id + 1 note += line[line.find(word) + len(word):] elif tword in line: #上一个提取完成 if len(note.strip()) > 0: params.append(note.strip()) note = "" start = id + 1 note += line[line.find(tword) + len(tword):] elif id == start and not isContainKeyword( block[id]) and line.startswith("//"): start += 1 note += line.strip('/') if len(note.strip()) > 0: params.append(note.strip()) kvs = [] for param in params: #print(param) comm.assertStr(param.find(' ') != -1, " 提取注释参数失败,没有注释") tup = (param[:param.find(' ')].strip(), param[param.find(' ') + 1:].strip(' \n\t-')) kvs.append(tup) #print("comment params = " , kvs) return kvs
def ansisEnumBlock(strs, blocks, fout, classname=None): name = extractName(strs).strip() print("name = ", name) comm.assertStr(name, "枚举名不能为空") #assert name brief = extractBrief(blocks) print("brief = ", brief) comm.assertStr(brief, "枚举描述不能为空") #assert brief assert valid(blocks) items = extractEnumEle(blocks) print("enum items = ", items) writeEnumToFile(brief, name, items, fout)
def ansisFunctionBlock(strs, blocks, fout, classname=None): strs = parseBody(strs).strip() # pos = strs.find('{') # if pos != -1: # strs = strs[:pos] print("is function", strs) # commentParams = extractCommentParams(block) # rets = extractCommentRet(block) brief = extractBrief(blocks) comm.assertStr(brief, "无描述信息") print("brief = ", brief) realParams = parseRealParams(strs) print("realParams = ", realParams) paramItems = parseParamsItem(realParams) print("param items = ", paramItems) funcname = parseFuncName(strs) print("funcname = ", funcname) if classname: funcname = classname + "::" + funcname hasRetV = hasReturnValue(strs) commentParamItems = extractCommentParams(blocks) print("comment param items = ", commentParamItems) mergeParamItems = mergeParams(commentParamItems, paramItems) print(mergeParams) commentRets = extractCommentRet(blocks) if hasRetV: comm.assertStr(len(commentRets) > 0, " 返回值需要注释") writeFunctionToFile(funcname, strs, brief, mergeParamItems, commentRets, fout) else: writeFunctionToFile(funcname, strs, brief, mergeParamItems, None, fout)
def mergeParams(commentParams, params): comm.assertStr(len(commentParams) == len(params), "参数和注释信息不符合") resultVals = [] for commentVal, desc in commentParams: for typ, realVal in params: if realVal == commentVal: tvd = (typ, realVal, desc) comm.assertStr(typ, " 类型不能为空") comm.assertStr(realVal, " 需要声明变量") comm.assertStr(desc, " 需要注释") resultVals.append(tvd) break print(resultVals) comm.assertStr(len(commentParams) == len(resultVals), "参数和注释信息不符合") return resultVals
def parseParamsItem(params): params = params.strip("()") ps = params.split(',') #print(ps) kv = [] for param in ps: param = param.strip() if len(param) > 0 and "[" not in param: comm.assertStr(param.find(' ') != -1, " 提取注释参数失败,参数需要注释") tup = (param[:param.rfind(' ')].strip(), param[param.rfind(' '):].strip()) kv.append(tup) elif len(param.strip()) > 0 and "[" in param: comm.assertStr(param.find(' ') != -1, " 提取注释参数失败,参数需要注释") tup = (param[:param.rfind(' ')].strip() + "[]", param[param.rfind(' '):param.rfind('[')].strip()) kv.append(tup) return kv
def extractEnumEle(blocks): start = -1 end = -1 num = len(blocks) for i in range(0, num): line = blocks[i].strip() if line.startswith("//"): continue else: #print(line) line = comm.rmComment(line) #print(line) if "{" in line: start = i #print("start = ", start) elif "}" in line: end = i #print("start = ", start) #print("end = ", end) assert start != -1 assert end != -1 #comm.assertStr( start < end, " 格式错误,枚举类型应该包含{,},且在不同行") comm.assertStr(start < end, " ") keyword = '///<' items = [] for i in range(start + 1, end): line = blocks[i].strip() if not line or line.startswith("//"): continue comm.assertStr(line.find(keyword) != -1, line + "需要注释") des = line[line.find(keyword) + len(keyword):] comm.assertStr(des, line + "需要注释") line = comm.rmComment(line) strs = line.split() #print(strs) comm.assertStr(len(strs) > 2, "枚举项格式为 *a = 1 ///< a*") key = strs[0] value = strs[2].strip(',') kvd = (key, value, des) items.append(kvd) return items
def ansisOtherBlock(strs, blocks, fout, classname=None): if containsKeyWord(strs): print("声明语句") else: comm.assertStr(valid(strs, blocks), " 缺少注释") name = parseVarName(strs) if classname: name = classname + "::" + name print("变量名称为", name) comm.assertStr(name, " 变量名不能为空") desc = extractComment(blocks) comm.assertStr(desc, " 变量缺少注释") writeVarToFile(name, desc, fout)
def extractStructEle(blocks): start = -1 end = -1 num = len(blocks) for i in range(0, num): line = blocks[i].strip() if line.startswith("//"): continue else: line = comm.rmComment(line) if "{" in line: start = i elif "}" in line: end = i assert start != -1 assert end != -1 comm.assertStr(start < end, " 格式错误,枚举类型应该包含{,},且在不同行") keyword = '///<' items = [] for i in range(start + 1, end): line = blocks[i].strip() if line == "": continue elif line.startswith("//"): continue # print("#############", line) comm.assertStr(line.find(keyword) != -1, " 没有找到 ///< 关键字") des = line[line.find(keyword) + len(keyword):] comm.assertStr(des, " 没有注释") note = comm.rmComment(line) if '[' in note: # 数组 typ, val = extractArrayEle(note) tvd = (typ, val, des) items.append(tvd) else: typ, val = extractValEle(note) tvd = (typ, val, des) items.append(tvd) return items