]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/FdfParserLite.py
BaseTools: use single RegExp for token matching
[mirror_edk2.git] / BaseTools / Source / Python / Common / FdfParserLite.py
index df287414db6f6c847669001ff47eae27a79218d6..255ca9633aa180abe4fd2603bc23ebee109fe08e 100644 (file)
@@ -22,6 +22,7 @@ import CommonDataClass.FdfClass
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
 from Common.RangeExpression import RangeExpression\r
+from Common.GlobalData import *\r
 \r
 ##define T_CHAR_SPACE                ' '\r
 ##define T_CHAR_NULL                 '\0'\r
@@ -47,6 +48,9 @@ InputMacroDict = {}
 # All Macro values when parsing file, not replace existing Macro\r
 AllMacroList = []\r
 \r
+FileExtensionPattern = re.compile(r'([a-zA-Z][a-zA-Z0-9]*)')\r
+TokenFindPattern = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)')\r
+\r
 def GetRealFileLine (File, Line):\r
     \r
     InsertedLines = 0\r
@@ -353,7 +357,7 @@ class FdfParser(object):
                         if Profile.FileName == File and Profile.MacroName == Name and Profile.DefinedAtLine <= Line:\r
                             Value = Profile.MacroValue\r
                             \r
-                if Value != None:\r
+                if Value is not None:\r
                     Str = Str.replace('$(' + Name + ')', Value)\r
                     MacroEnd = MacroStart + len(Value) \r
                 \r
@@ -676,8 +680,8 @@ class FdfParser(object):
         FileLineTuple = GetRealFileLine(self.FileName, Line)\r
         if Name in InputMacroDict:\r
             MacroValue = InputMacroDict[Name]\r
-            if Op == None:\r
-                if Value == 'Bool' and MacroValue == None or MacroValue.upper() == 'FALSE':\r
+            if Op is None:\r
+                if Value == 'Bool' and MacroValue is None or MacroValue.upper() == 'FALSE':\r
                     return False\r
                 return True\r
             elif Op == '!=':\r
@@ -691,7 +695,7 @@ class FdfParser(object):
                 else:\r
                     return False\r
             else:\r
-                if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(MacroValue) or (MacroValue != None and MacroValue.isdigit())):\r
+                if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(MacroValue) or (MacroValue is not None and MacroValue.isdigit())):\r
                     InputVal = long(Value, 0)\r
                     MacroVal = long(MacroValue, 0)\r
                     if Op == '>':\r
@@ -721,8 +725,8 @@ class FdfParser(object):
                 \r
         for Profile in AllMacroList:\r
             if Profile.FileName == FileLineTuple[0] and Profile.MacroName == Name and Profile.DefinedAtLine <= FileLineTuple[1]:\r
-                if Op == None:\r
-                    if Value == 'Bool' and Profile.MacroValue == None or Profile.MacroValue.upper() == 'FALSE':\r
+                if Op is None:\r
+                    if Value == 'Bool' and Profile.MacroValue is None or Profile.MacroValue.upper() == 'FALSE':\r
                         return False\r
                     return True\r
                 elif Op == '!=':\r
@@ -736,7 +740,7 @@ class FdfParser(object):
                     else:\r
                         return False\r
                 else:\r
-                    if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(Profile.MacroValue) or (Profile.MacroValue != None and Profile.MacroValue.isdigit())):\r
+                    if (self.__IsHex(Value) or Value.isdigit()) and (self.__IsHex(Profile.MacroValue) or (Profile.MacroValue is not None and Profile.MacroValue.isdigit())):\r
                         InputVal = long(Value, 0)\r
                         MacroVal = long(Profile.MacroValue, 0)\r
                         if Op == '>':\r
@@ -932,7 +936,7 @@ class FdfParser(object):
         \r
         if not self.__GetNextToken():\r
             return False\r
-        if RangeExpression.RegGuidPattern.match(self.__Token) != None:\r
+        if gGuidPattern.match(self.__Token) is not None:\r
             return True\r
         else:\r
             self.__UndoToken()\r
@@ -1451,7 +1455,7 @@ class FdfParser(object):
             pass\r
         \r
         for Item in Obj.BlockSizeList:\r
-            if Item[0] == None or Item[1] == None:\r
+            if Item[0] is None or Item[1] is None:\r
                 raise Warning("expected block statement for Fd Section", self.FileName, self.CurrentLineNumber)\r
 \r
         return True\r
@@ -2091,8 +2095,7 @@ class FdfParser(object):
 \r
                 \r
         if self.__GetNextToken():\r
-            p = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)')\r
-            if p.match(self.__Token):\r
+            if TokenFindPattern.match(self.__Token):\r
                 FfsInfObj.KeyStringList.append(self.__Token)\r
                 if not self.__IsToken(","):\r
                     return\r
@@ -2101,7 +2104,7 @@ class FdfParser(object):
                 return\r
                 \r
             while self.__GetNextToken():\r
-                if not p.match(self.__Token):\r
+                if not TokenFindPattern.match(self.__Token):\r
                     raise Warning("expected KeyString \"Target_Tag_Arch\" At Line ", self.FileName, self.CurrentLineNumber)\r
                 FfsInfObj.KeyStringList.append(self.__Token)\r
     \r
@@ -2249,12 +2252,11 @@ class FdfParser(object):
     def __GetFileOpts(self, FfsFileObj):\r
         \r
         if self.__GetNextToken():\r
-            Pattern = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)')\r
-            if Pattern.match(self.__Token):\r
+            if TokenFindPattern.match(self.__Token):\r
                 FfsFileObj.KeyStringList.append(self.__Token)\r
                 if self.__IsToken(","):\r
                     while self.__GetNextToken():\r
-                        if not Pattern.match(self.__Token):\r
+                        if not TokenFindPattern.match(self.__Token):\r
                             raise Warning("expected KeyString \"Target_Tag_Arch\" At Line ", self.FileName, self.CurrentLineNumber)\r
                         FfsFileObj.KeyStringList.append(self.__Token)\r
 \r
@@ -2420,7 +2422,7 @@ class FdfParser(object):
             \r
             FvImageSectionObj = CommonDataClass.FdfClass.FvImageSectionClassObject()\r
             FvImageSectionObj.Alignment = AlignValue\r
-            if FvObj != None:\r
+            if FvObj is not None:\r
                 FvImageSectionObj.Fv = FvObj\r
                 FvImageSectionObj.FvName = None\r
             else:\r
@@ -2842,8 +2844,7 @@ class FdfParser(object):
             \r
         Ext = ""\r
         if self.__GetNextToken():\r
-            Pattern = re.compile(r'([a-zA-Z][a-zA-Z0-9]*)')\r
-            if Pattern.match(self.__Token):\r
+            if FileExtensionPattern.match(self.__Token):\r
                 Ext = self.__Token                            \r
                 return '.' + Ext    \r
             else:\r
@@ -2900,12 +2901,11 @@ class FdfParser(object):
         \r
         KeyStringList = []\r
         if self.__GetNextToken():\r
-            Pattern = re.compile(r'([a-zA-Z0-9\-]+|\$\(TARGET\)|\*)_([a-zA-Z0-9\-]+|\$\(TOOL_CHAIN_TAG\)|\*)_([a-zA-Z0-9\-]+|\$\(ARCH\)|\*)')\r
-            if Pattern.match(self.__Token):\r
+            if TokenFindPattern.match(self.__Token):\r
                 KeyStringList.append(self.__Token)\r
                 if self.__IsToken(","):\r
                     while self.__GetNextToken():\r
-                        if not Pattern.match(self.__Token):\r
+                        if not TokenFindPattern.match(self.__Token):\r
                             raise Warning("expected KeyString \"Target_Tag_Arch\" At Line ", self.FileName, self.CurrentLineNumber)\r
                         KeyStringList.append(self.__Token)\r
 \r
@@ -2940,7 +2940,7 @@ class FdfParser(object):
             Rule.CheckSum = CheckSum\r
             Rule.Fixed = Fixed\r
             Rule.KeyStringList = KeyStringList\r
-            if KeepReloc != None:\r
+            if KeepReloc is not None:\r
                 Rule.KeepReloc = KeepReloc\r
             \r
             while True:\r
@@ -2967,7 +2967,7 @@ class FdfParser(object):
             Rule.Fixed = Fixed\r
             Rule.FileExtension = Ext\r
             Rule.KeyStringList = KeyStringList\r
-            if KeepReloc != None:\r
+            if KeepReloc is not None:\r
                 Rule.KeepReloc = KeepReloc\r
             \r
             return Rule\r
@@ -3010,7 +3010,7 @@ class FdfParser(object):
             Rule.Fixed = Fixed\r
             Rule.FileName = self.__Token\r
             Rule.KeyStringList = KeyStringList\r
-            if KeepReloc != None:\r
+            if KeepReloc is not None:\r
                 Rule.KeepReloc = KeepReloc\r
             return Rule\r
         \r
@@ -3147,7 +3147,7 @@ class FdfParser(object):
                     EfiSectionObj.KeepReloc = False\r
                 else:\r
                     EfiSectionObj.KeepReloc = True\r
-                if Obj.KeepReloc != None and Obj.KeepReloc != EfiSectionObj.KeepReloc:\r
+                if Obj.KeepReloc is not None and Obj.KeepReloc != EfiSectionObj.KeepReloc:\r
                     raise Warning("Section type %s has reloc strip flag conflict with Rule At Line %d" % (EfiSectionObj.SectionType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber)\r
             else:\r
                 raise Warning("Section type %s could not have reloc strip flag At Line %d" % (EfiSectionObj.SectionType, self.CurrentLineNumber), self.FileName, self.CurrentLineNumber)\r
@@ -3469,7 +3469,7 @@ class FdfParser(object):
             raise Warning("expected Component version At Line ", self.FileName, self.CurrentLineNumber)\r
 \r
         Pattern = re.compile('-$|[0-9]{0,1}[0-9]{1}\.[0-9]{0,1}[0-9]{1}')\r
-        if Pattern.match(self.__Token) == None:\r
+        if Pattern.match(self.__Token) is None:\r
             raise Warning("Unknown version format At line ", self.FileName, self.CurrentLineNumber)\r
         CompStatementObj.CompVer = self.__Token\r
         \r
@@ -3542,7 +3542,7 @@ class FdfParser(object):
             for elementRegion in FdObj.RegionList:\r
                 if elementRegion.RegionType == 'FV':\r
                     for elementRegionData in elementRegion.RegionDataList:\r
-                        if elementRegionData != None and elementRegionData.upper() not in FvList:\r
+                        if elementRegionData is not None and elementRegionData.upper() not in FvList:\r
                             FvList.append(elementRegionData.upper())\r
         return FvList\r
     \r
@@ -3559,9 +3559,9 @@ class FdfParser(object):
         \r
         for FfsObj in FvObj.FfsList:\r
             if isinstance(FfsObj, FfsFileStatement.FileStatement):\r
-                if FfsObj.FvName != None and FfsObj.FvName.upper() not in RefFvList:\r
+                if FfsObj.FvName is not None and FfsObj.FvName.upper() not in RefFvList:\r
                     RefFvList.append(FfsObj.FvName.upper())\r
-                elif FfsObj.FdName != None and FfsObj.FdName.upper() not in RefFdList:\r
+                elif FfsObj.FdName is not None and FfsObj.FdName.upper() not in RefFdList:\r
                     RefFdList.append(FfsObj.FdName.upper())\r
                 else:\r
                     self.__GetReferencedFdFvTupleFromSection(FfsObj, RefFdList, RefFvList)    \r
@@ -3582,9 +3582,9 @@ class FdfParser(object):
         while SectionStack != []:\r
             SectionObj = SectionStack.pop()\r
             if isinstance(SectionObj, FvImageSection.FvImageSection):\r
-                if SectionObj.FvName != None and SectionObj.FvName.upper() not in FvList:\r
+                if SectionObj.FvName is not None and SectionObj.FvName.upper() not in FvList:\r
                     FvList.append(SectionObj.FvName.upper())\r
-                if SectionObj.Fv != None and SectionObj.Fv.UiFvName != None and SectionObj.Fv.UiFvName.upper() not in FvList:\r
+                if SectionObj.Fv is not None and SectionObj.Fv.UiFvName is not None and SectionObj.Fv.UiFvName.upper() not in FvList:\r
                     FvList.append(SectionObj.Fv.UiFvName.upper())\r
                     self.__GetReferencedFdFvTuple(SectionObj.Fv, FdList, FvList)\r
             \r