Samples development. Tools to convert from ret_if like defines.
[apps/native/sample/sample-core-components.git] / tool / development / convert_ret_if_like_expressions.py
1 #!/usr/bin/python
2
3 import os
4 import re
5
6 import pyparsing
7 from pyparsing import Word, Literal, alphanums, alphas, Suppress, quotedString, Forward, \
8                                           Group, Optional, delimitedList, Regex, oneOf, OneOrMore, operatorPrecedence, \
9                                         opAssoc
10
11
12 LPAR, RPAR, SEMI = map(Suppress, "();")
13
14 identifier = Word(alphanums+"_-", alphanums+"_") + Optional(Literal("->")+Word(alphas+"_", alphanums+"_"))
15
16 RET_IF_TEMPLATE = """if ({0}) {{
17         dlog_print(DLOG_ERROR, LOG_TAG, "%s() return", __FUNCTION__);
18         return;
19 }}"""
20
21 RETV_IF_TEMPLATE = """if ({0}) {{
22         dlog_print(DLOG_ERROR, LOG_TAG, "%s() return", __FUNCTION__);
23         return {1};
24 }}"""
25
26 GOTO_IF_TEMPLATE = """if ({0}) {{
27         dlog_print(DLOG_ERROR, LOG_TAG, "%s() ->goto", __FUNCTION__);
28         goto {1};
29 }}"""
30
31 BREAK_IF_TEMPLATE = """if ({0}) {{
32         dlog_print(DLOG_ERROR, LOG_TAG, "%s() -> break", __FUNCTION__);
33         break;
34 }}"""
35
36 CONTINUE_IF_TEMPLATE = """if ({0}) {{
37         dlog_print(DLOG_ERROR, LOG_TAG, "%s() -> continue", __FUNCTION__);
38         continue;
39 }}"""
40
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
46
47 def getTabCountFromCharIndex(fileContent, index):
48         ret = 0
49         while fileContent[index] == "\t":
50                 ret = ret + 1
51                 index = index -1
52         return ret
53
54 def getContentForTemplate(template, tabCount, *args):
55         ret = template.format(*args)
56         ret = re.sub("^", "\t" * tabCount, ret, flags = re.MULTILINE)
57         return ret
58
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)
69         
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:]
85         return ret
86
87
88 filesToCheck = []
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))
93
94 for fileName in filesToCheck:
95         print "######checking fileName:", fileName
96         f = open(fileName, "r")
97         fileContent = f.read()
98         f.close()
99         newFileContent = replaceRetInFileContent(fileContent)
100         f = open(fileName, "w")
101         f.write(newFileContent)
102         f.close()