7 from pyparsing import Word, Literal, alphanums, alphas, Suppress, quotedString, Forward, \
8 Group, Optional, delimitedList, Regex, oneOf, OneOrMore, operatorPrecedence, \
12 LPAR, RPAR, SEMI = map(Suppress, "();")
14 identifier = Word(alphanums+"_-", alphanums+"_") + Optional(Literal("->")+Word(alphas+"_", alphanums+"_"))
16 RET_IF_TEMPLATE = """if ({0}) {{
17 dlog_print(DLOG_ERROR, LOG_TAG, "%s() return", __FUNCTION__);
21 RETV_IF_TEMPLATE = """if ({0}) {{
22 dlog_print(DLOG_ERROR, LOG_TAG, "%s() return", __FUNCTION__);
26 GOTO_IF_TEMPLATE = """if ({0}) {{
27 dlog_print(DLOG_ERROR, LOG_TAG, "%s() ->goto", __FUNCTION__);
31 BREAK_IF_TEMPLATE = """if ({0}) {{
32 dlog_print(DLOG_ERROR, LOG_TAG, "%s() -> break", __FUNCTION__);
36 CONTINUE_IF_TEMPLATE = """if ({0}) {{
37 dlog_print(DLOG_ERROR, LOG_TAG, "%s() -> continue", __FUNCTION__);
41 retIf = Literal("ret_if") + LPAR + Group(Optional("!") + identifier) + RPAR
42 retVIf = Literal("retv_if") + LPAR + Group(Optional("!") + identifier) + Literal(",") + identifier + RPAR
43 gotoIf = Literal("goto_if") + LPAR + Group(Optional("!") + identifier) + Literal(",") + identifier + RPAR
44 breakIf = Literal("break_if") + LPAR + Group(Optional("!") + identifier) + Literal(",") + identifier + RPAR
45 continueIf = Literal("continue_if") + LPAR + Group(Optional("!") + identifier) + Literal(",") + identifier + RPAR
47 def getTabCountFromCharIndex(fileContent, index):
49 while fileContent[index] == "\t":
54 def getContentForTemplate(template, tabCount, *args):
55 ret = template.format(*args)
56 ret = re.sub("^", "\t" * tabCount, ret, flags = re.MULTILINE)
59 def replaceRetInFileContent(fileContent):
60 ret = fileContent.replace("#include \"log.h\"\n", "")
61 compiledHAPI = re.compile("\\bHAPI\\s")
62 compiledDebug = re.compile("\\b_D\(")
63 compiledError = re.compile("\\b_E\(")
64 compiledWarning = re.compile("\\b_W\(")
65 ret = compiledHAPI.sub("", ret)
66 ret = compiledDebug.sub("dlog_print(DLOG_DEBUG, LOG_TAG, ", ret)
67 ret = compiledWarning.sub("dlog_print(DLOG_WARNING, LOG_TAG, ", ret)
68 ret = compiledError.sub("dlog_print(DLOG_ERROR, LOG_TAG, ", ret)
70 for t, s, e in reversed(list(retIf.parseWithTabs().scanString(ret))):
71 tabCount = getTabCountFromCharIndex(ret, s - 1)
72 ret = ret[:s-1] + getContentForTemplate(RET_IF_TEMPLATE, tabCount, "".join(t[1])) + ret[e:]
73 for t, s, e in reversed(list(retVIf.parseWithTabs().scanString(ret))):
74 tabCount = getTabCountFromCharIndex(ret, s - 1)
75 ret = ret[:s-1] + getContentForTemplate(RETV_IF_TEMPLATE, tabCount, "".join(t[1]), "".join(t[3])) + ret[e:]
76 for t, s, e in reversed(list(gotoIf.parseWithTabs().scanString(ret))):
77 tabCount = getTabCountFromCharIndex(ret, s - 1)
78 ret = ret[:s-1] + getContentForTemplate(GOTO_IF_TEMPLATE, tabCount, "".join(t[1]), "".join(t[3])) + ret[e:]
79 for t, s, e in reversed(list(breakIf.parseWithTabs().scanString(ret))):
80 tabCount = getTabCountFromCharIndex(ret, s - 1)
81 ret = ret[:s-1] + getContentForTemplate(BREAK_IF_TEMPLATE, tabCount, "".join(t[1])) + ret[e:]
82 for t, s, e in reversed(list(continueIf.parseWithTabs().scanString(ret))):
83 tabCount = getTabCountFromCharIndex(ret, s - 1)
84 ret = ret[:s-1] + getContentForTemplate(CONTINUE_IF_TEMPLATE, tabCount, "".join(t[1])) + ret[e:]
89 for root, dirs, files in os.walk(os.getcwd()):
90 for fileName in files:
91 if (fileName.endswith(".c") or fileName.endswith(".h")) and not fileName.endswith("log.h"):
92 filesToCheck.append(os.path.join(root, fileName))
94 for fileName in filesToCheck:
95 print "######checking fileName:", fileName
96 f = open(fileName, "r")
97 fileContent = f.read()
99 newFileContent = replaceRetInFileContent(fileContent)
100 f = open(fileName, "w")
101 f.write(newFileContent)