]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/Expression.py
BaseTools: Expression - remove variable
[mirror_edk2.git] / BaseTools / Source / Python / Common / Expression.py
index 79dc83efc3d5670d9dc278bc06d488402d359a56..4f0f377f37880a2eb9981ba52c778f4a9d93bc90 100644 (file)
@@ -40,20 +40,21 @@ ERR_ARRAY_ELE           = 'This must be HEX value for NList or Array: [%s].'
 ERR_EMPTY_EXPR          = 'Empty expression is not allowed.'\r
 ERR_IN_OPERAND          = 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).'\r
 \r
+__ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')\r
+\r
 ## SplitString\r
 #  Split string to list according double quote\r
 #  For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']\r
 #\r
 def SplitString(String):\r
     # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'\r
-    Str = String\r
     RetList = []\r
     InSingleQuote = False\r
     InDoubleQuote = False\r
     Item = ''\r
-    for i, ch in enumerate(Str):\r
+    for i, ch in enumerate(String):\r
         if ch == '"' and not InSingleQuote:\r
-            if Str[i - 1] != '\\':\r
+            if String[i - 1] != '\\':\r
                 InDoubleQuote = not InDoubleQuote\r
             if not InDoubleQuote:\r
                 Item += String[i]\r
@@ -64,7 +65,7 @@ def SplitString(String):
                 RetList.append(Item)\r
                 Item = ''\r
         elif ch == "'" and not InDoubleQuote:\r
-            if Str[i - 1] != '\\':\r
+            if String[i - 1] != '\\':\r
                 InSingleQuote = not InSingleQuote\r
             if not InSingleQuote:\r
                 Item += String[i]\r
@@ -84,27 +85,26 @@ def SplitString(String):
 def SplitPcdValueString(String):\r
     # There might be escaped comma in GUID() or DEVICE_PATH() or " "\r
     # or ' ' or L' ' or L" "\r
-    Str = String\r
     RetList = []\r
     InParenthesis = 0\r
     InSingleQuote = False\r
     InDoubleQuote = False\r
     Item = ''\r
-    for i, ch in enumerate(Str):\r
+    for i, ch in enumerate(String):\r
         if ch == '(':\r
             InParenthesis += 1\r
-        if ch == ')':\r
+        elif ch == ')':\r
             if InParenthesis:\r
                 InParenthesis -= 1\r
             else:\r
                 raise BadExpression(ERR_STRING_TOKEN % Item)\r
-        if ch == '"' and not InSingleQuote:\r
+        elif ch == '"' and not InSingleQuote:\r
             if String[i-1] != '\\':\r
                 InDoubleQuote = not InDoubleQuote\r
-        if ch == "'" and not InDoubleQuote:\r
+        elif ch == "'" and not InDoubleQuote:\r
             if String[i-1] != '\\':\r
                 InSingleQuote = not InSingleQuote\r
-        if ch == ',':\r
+        elif ch == ',':\r
             if InParenthesis or InSingleQuote or InDoubleQuote:\r
                 Item += String[i]\r
                 continue\r
@@ -119,14 +119,10 @@ def SplitPcdValueString(String):
         RetList.append(Item)\r
     return RetList\r
 \r
-def IsValidCString(Str):\r
-    ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')\r
-    if not ValidString.match(Str):\r
-        return False\r
-    return True\r
+def IsValidCName(Str):\r
+    return True if __ValidString.match(Str) else False\r
 \r
 def BuildOptionValue(PcdValue, GuidDict):\r
-    IsArray = False\r
     if PcdValue.startswith('H'):\r
         InputValue = PcdValue[1:]\r
     elif PcdValue.startswith("L'") or PcdValue.startswith("'"):\r
@@ -136,8 +132,6 @@ def BuildOptionValue(PcdValue, GuidDict):
     else:\r
         InputValue = PcdValue\r
     if IsFieldValueAnArray(InputValue):\r
-        IsArray = True\r
-    if IsArray:\r
         try:\r
             PcdValue = ValueExpressionEx(InputValue, 'VOID*', GuidDict)(True)\r
         except:\r
@@ -909,11 +903,12 @@ class ValueExpressionEx(ValueExpression):
                         LabelOffset = 0\r
                         for Index, Item in enumerate(PcdValueList):\r
                             # compute byte offset of every LABEL\r
-                            Item = Item.strip()\r
                             LabelList = ReLabel.findall(Item)\r
+                            Item = ReLabel.sub('', Item)\r
+                            Item = Item.strip()\r
                             if LabelList:\r
                                 for Label in LabelList:\r
-                                    if not IsValidCString(Label):\r
+                                    if not IsValidCName(Label):\r
                                         raise BadExpression('%s is not a valid c variable name' % Label)\r
                                     if Label not in LabelDict.keys():\r
                                         LabelDict[Label] = str(LabelOffset)\r