Added more error check
authorjwang36 <jwang36@7335b38e-4728-0410-8992-fb3ffe349368>
Fri, 4 Jan 2008 02:07:25 +0000 (02:07 +0000)
committerjwang36 <jwang36@7335b38e-4728-0410-8992-fb3ffe349368>
Fri, 4 Jan 2008 02:07:25 +0000 (02:07 +0000)
git-svn-id: https://buildtools.tianocore.org/svn/buildtools/trunk/BaseTools@941 7335b38e-4728-0410-8992-fb3ffe349368

Source/Python/AutoGen/GenDepex.py

index 7769427..14091d3 100644 (file)
@@ -83,7 +83,7 @@ class DependencyExpression:
     # op code that should not be the last one\r
     NonEndingOpcode = ["AND", "OR"]\r
     # op code must not present at the same time\r
-    ExclusiveOpcode = ["BEFORE", "AFTER"]\r
+    ExclusiveOpcode = ["SOR", "BEFORE", "AFTER"]\r
     # op code that should be the first one if it presents\r
     AboveAllOpcode = ["SOR"]\r
 \r
@@ -112,6 +112,9 @@ class DependencyExpression:
         self.GetPostfixNotation()\r
         self.ValidateOpcode()\r
 \r
+    def __str__(self):\r
+        return " ".join(self.TokenList)\r
+\r
     ## Split the expression string into token list\r
     def GetExpressionTokenList(self):\r
         self.TokenList = self.TokenPattern.findall(self.ExpressionString)\r
@@ -119,10 +122,17 @@ class DependencyExpression:
     ## Convert token list into postfix notation\r
     def GetPostfixNotation(self):\r
         Stack = []\r
+        LastToken = 'AND'\r
         for Token in self.TokenList:\r
             if Token == "(":\r
+                if LastToken not in self.SupportedOpcode:\r
+                    EdkLogger.error("GenDepex", PARSER_ERROR, "Invalid dependency expression: missing operator",\r
+                                    ExtraData=str(self))\r
                 Stack.append(Token)\r
             elif Token == ")":\r
+                if '(' not in Stack:\r
+                    EdkLogger.error("GenDepex", PARSER_ERROR, "Invalid dependency expression: mismatched parentheses",\r
+                                    ExtraData=str(self))\r
                 while len(Stack) > 0:\r
                     if Stack[-1] == '(':\r
                         Stack.pop()\r
@@ -136,11 +146,25 @@ class DependencyExpression:
                 Stack.append(Token)\r
                 self.OpcodeList.append(Token)\r
             else:\r
-                if Token not in self.Opcode[self.Phase]:\r
+                # not OP, take it as GUID\r
+                if Token not in self.SupportedOpcode:\r
+                    if LastToken not in self.SupportedOpcode + ['(', ')']:\r
+                        EdkLogger.error("GenDepex", PARSER_ERROR, "Invalid dependency expression: missing operator",\r
+                                        ExtraData=str(self))\r
                     self.PostfixNotation.append("PUSH")\r
-                else:\r
+                # check if OP is valid in this phase\r
+                elif Token in self.Opcode[self.Phase]:\r
                     self.OpcodeList.append(Token)\r
+                else:\r
+                    EdkLogger.error("GenDepex", PARSER_ERROR, \r
+                                    "Opcode=%s doesn't supported in %s stage " % (Op, self.Phase))\r
                 self.PostfixNotation.append(Token)\r
+            LastToken = Token\r
+\r
+        # there should not be parentheses in Stack\r
+        if '(' in Stack or ')' in Stack:\r
+            EdkLogger.error("GenDepex", PARSER_ERROR, "Invalid dependency expression: mismatched parentheses",\r
+                            ExtraData=str(self))\r
         while len(Stack) > 0:\r
             self.PostfixNotation.append(Stack.pop())\r
         self.PostfixNotation.append("END")\r
@@ -195,7 +219,7 @@ class DependencyExpression:
         Buffer.close()\r
         return FileChangeFlag\r
 \r
-versionNumber = "0.01"\r
+versionNumber = "0.02"\r
 __version__ = "%prog Version " + versionNumber\r
 __copyright__ = "Copyright (c) 2007, Intel Corporation  All rights reserved."\r
 __usage__ = "%prog [options] [dependency_expression_file]"\r
@@ -242,7 +266,10 @@ def Main():
             DxsString = open(DxsFile, 'r').read().replace("\n", " ").replace("\r", " ")\r
             DxsString = re.compile("DEPENDENCY_START(.+)DEPENDENCY_END").findall(DxsString)[0]\r
         elif Option.Expression != "":\r
-            DxsString = Option.Expression\r
+            if Option.Expression[0] == '"':\r
+                DxsString = Option.Expression[1:-1]\r
+            else:\r
+                DxsString = Option.Expression\r
         else:\r
             print "No expression string or file given"\r
             return 1\r