Change the comment style for the modules and the list, and use the newParser to parse...
[bertos.git] / wizard / newParser.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2009 Develer S.r.l. (http://www.develer.com/)
5 # All rights reserved.
6 #
7 # $Id:$
8 #
9 # Author: Lorenzo Berni <duplo@develer.com>
10 #
11
12 import re
13 import DefineException
14
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]
18     return commentList
19
20 def loadModuleDefinition(first_comment):
21     toBeParsed = False
22     moduleDefinition = {}
23     for num, line in enumerate(first_comment):
24         index = line.find("$WIZ$")
25         if index != -1:
26             toBeParsed = True
27             try:
28                 exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
29             except:
30                 raise ParseError(num, line[index:])
31         elif line.find("\\brief") != -1:
32             moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
33     moduleDict = {}
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"]
40         else:
41             moduleDict[moduleDefinition["module_name"]]["depends"] = ()
42         if "module_configuration" in moduleDefinition.keys():
43             moduleDict[moduleDefinition["module_name"]]["configuration"] = moduleDefinition["module_configuration"]
44         else:
45             moduleDict[moduleDefinition["module_name"]]["configuration"] = ""
46         if "module_description" in moduleDefinition.keys():
47             moduleDict[moduleDefinition["module_name"]]["description"] = moduleDefinition["module_description"]
48         moduleDict[moduleDefinition["module_name"]]["enabled"] = False
49     return toBeParsed, moduleDict
50
51 def loadDefineLists(commentList):
52     defineList = {}
53     for comment in commentList:
54         for num, line in enumerate(comment):
55             index = line.find("$WIZ$")
56             if index != -1:
57                 try:
58                     exec line[index + len("$WIZ$ "):] in {}, defineList
59                 except:
60                     raise ParseError(num, line[index:])
61     for key, value in defineList.items():
62         if type(value) == str:
63             defineList[key] = (value,)
64     return defineList
65
66 class ParseError(Exception):
67     def __init__(self, line_number, line):
68         Exception.__init__(self)
69         self.line_number = line_number
70         self.line = line
71
72 def main():
73     try:
74         defineLists = {}
75         modules = {}
76         commentList = getCommentList(open("test/to_parse.h", "r").read())
77         toBeParsedm, moduleInfo = loadModuleDefinition(commentList[0])
78         modules.update(moduleInfo)
79         if toBeParsed:
80             defineLists.update(loadDefineList(commentList[1:]))
81         print modules
82         print defineLists
83     except ParseError, err:
84         print "Error: line %d - %s" % (err.line_number, err.line)
85
86 if __name__ == '__main__':
87     main()