02035c79f8c9a4112b0041d4259963a68c2729d2
[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     return toBeParsed, moduleDict
49
50 def loadDefineList(commentList):
51     defineList = {}
52     for comment in commentList:
53         for num, line in enumerate(comment):
54             index = line.find("$WIZ$")
55             if index != -1:
56                 try:
57                     exec line[index + len("$WIZ$ "):] in {}, defineList
58                 except:
59                     raise ParseError(num, line[index:])
60     for key, value in defineList.items():
61         if type(value) == str:
62             defineList[key] = (value,)
63     return defineList
64
65 class ParseError(Exception):
66     def __init__(self, line_number, line):
67         Exception.__init__(self)
68         self.line_number = line_number
69         self.line = line
70
71 def main():
72     try:
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)
78
79 if __name__ == '__main__':
80     main()