4 # Copyright 2009 Develer S.r.l. (http://www.develer.com/)
9 # Author: Lorenzo Berni <duplo@develer.com>
13 import DefineException
15 def getCommentList(string):
16 commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
17 commentList = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in commentList]
20 def loadModuleDefinition(first_comment):
23 for num, line in enumerate(first_comment):
24 index = line.find("$WIZ$")
28 exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
30 raise ParseError(num, line[index:])
31 elif line.find("\\brief") != -1:
32 moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
34 if "module_name" in moduleDefinition.keys():
35 moduleDict[moduleDefinition["module_name"]] = {}
36 if "module_depends" in moduleDefinition.keys():
37 if type(moduleDefinition["module_depends"]) == str:
38 moduleDefinition["module_depends"] = (moduleDefinition["module_depends"],)
39 moduleDict[moduleDefinition["module_name"]]["depends"] = moduleDefinition["module_depends"]
41 moduleDict[moduleDefinition["module_name"]]["depends"] = ()
42 if "module_configuration" in moduleDefinition.keys():
43 moduleDict[moduleDefinition["module_name"]]["configuration"] = moduleDefinition["module_configuration"]
45 moduleDict[moduleDefinition["module_name"]]["configuration"] = ""
46 if "module_description" in moduleDefinition.keys():
47 moduleDict[moduleDefinition["module_name"]]["description"] = moduleDefinition["module_description"]
48 return toBeParsed, moduleDict
50 def loadDefineList(commentList):
52 for comment in commentList:
53 for num, line in enumerate(comment):
54 index = line.find("$WIZ$")
57 exec line[index + len("$WIZ$ "):] in {}, defineList
59 raise ParseError(num, line[index:])
60 for key, value in defineList.items():
61 if type(value) == str:
62 defineList[key] = (value,)
65 class ParseError(Exception):
66 def __init__(self, line_number, line):
67 Exception.__init__(self)
68 self.line_number = line_number
73 commentList = getCommentList(open("test/to_parse.h", "r").read())
74 print loadModuleDefinition(commentList[0])
75 print loadDefineList(commentList[1:])
76 except ParseError, err:
77 print "Error: line %d - %s" % (err.line_number, err.line)
79 if __name__ == '__main__':