Add the new parsing functions and a sample file for test it
authorduplo <duplo@38d2e660-2303-0410-9eaa-f027e97ec537>
Thu, 12 Feb 2009 19:27:29 +0000 (19:27 +0000)
committerduplo <duplo@38d2e660-2303-0410-9eaa-f027e97ec537>
Thu, 12 Feb 2009 19:27:29 +0000 (19:27 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2344 38d2e660-2303-0410-9eaa-f027e97ec537

wizard/newParser.py [new file with mode: 0644]
wizard/test/to_parse.h [new file with mode: 0644]

diff --git a/wizard/newParser.py b/wizard/newParser.py
new file mode 100644 (file)
index 0000000..02035c7
--- /dev/null
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+# encoding: utf-8
+#
+# Copyright 2009 Develer S.r.l. (http://www.develer.com/)
+# All rights reserved.
+#
+# $Id:$
+#
+# Author: Lorenzo Berni <duplo@develer.com>
+#
+
+import re
+import DefineException
+
+def getCommentList(string):
+    commentList = re.findall(r"/\*{2}\s*([^*]*\*(?:[^/*][^*]*\*+)*)/", string)
+    commentList = [re.findall(r"^\s*\* *(.*?)$", comment, re.MULTILINE) for comment in commentList]
+    return commentList
+
+def loadModuleDefinition(first_comment):
+    toBeParsed = False
+    moduleDefinition = {}
+    for num, line in enumerate(first_comment):
+        index = line.find("$WIZ$")
+        if index != -1:
+            toBeParsed = True
+            try:
+                exec line[index + len("$WIZ$ "):] in {}, moduleDefinition
+            except:
+                raise ParseError(num, line[index:])
+        elif line.find("\\brief") != -1:
+            moduleDefinition["module_description"] = line[line.find("\\brief") + len("\\brief "):]
+    moduleDict = {}
+    if "module_name" in moduleDefinition.keys():
+        moduleDict[moduleDefinition["module_name"]] = {}
+        if "module_depends" in moduleDefinition.keys():
+            if type(moduleDefinition["module_depends"]) == str:
+                moduleDefinition["module_depends"] = (moduleDefinition["module_depends"],)
+            moduleDict[moduleDefinition["module_name"]]["depends"] = moduleDefinition["module_depends"]
+        else:
+            moduleDict[moduleDefinition["module_name"]]["depends"] = ()
+        if "module_configuration" in moduleDefinition.keys():
+            moduleDict[moduleDefinition["module_name"]]["configuration"] = moduleDefinition["module_configuration"]
+        else:
+            moduleDict[moduleDefinition["module_name"]]["configuration"] = ""
+        if "module_description" in moduleDefinition.keys():
+            moduleDict[moduleDefinition["module_name"]]["description"] = moduleDefinition["module_description"]
+    return toBeParsed, moduleDict
+
+def loadDefineList(commentList):
+    defineList = {}
+    for comment in commentList:
+        for num, line in enumerate(comment):
+            index = line.find("$WIZ$")
+            if index != -1:
+                try:
+                    exec line[index + len("$WIZ$ "):] in {}, defineList
+                except:
+                    raise ParseError(num, line[index:])
+    for key, value in defineList.items():
+        if type(value) == str:
+            defineList[key] = (value,)
+    return defineList
+
+class ParseError(Exception):
+    def __init__(self, line_number, line):
+        Exception.__init__(self)
+        self.line_number = line_number
+        self.line = line
+
+def main():
+    try:
+        commentList = getCommentList(open("test/to_parse.h", "r").read())
+        print loadModuleDefinition(commentList[0])
+        print loadDefineList(commentList[1:])
+    except ParseError, err:
+        print "Error: line %d - %s" % (err.line_number, err.line)
+
+if __name__ == '__main__':
+    main()
\ No newline at end of file
diff --git a/wizard/test/to_parse.h b/wizard/test/to_parse.h
new file mode 100644 (file)
index 0000000..3e90532
--- /dev/null
@@ -0,0 +1,28 @@
+/**
+ * Header comment
+ * with * asterisks *
+ * this comment has the module define statements
+ *
+ * Edit me for test the new parser
+ * \brief This is a testing module
+ * $WIZ$ module_name = "name"
+ * $WIZ$ module_depends = "module1", "module2"
+ * $WIZ$ module_configuration = "config_file.h"
+ */
+
+/**
+ * List to be parsed
+ * $WIZ$ to_be_parsed = "BLA", "BLABLA", "BLABLA_BLA"
+ */
+
+/**
+ *
+ * Commento to be ignored
+ *
+ */
+
+/**
+ * Comment with list to be parsed
+ *
+ * $WIZ$ another_list_to_be_parsed = "BLABLABLA_BLA"
+ */
\ No newline at end of file