X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FCommon%2FExpression.py;h=09240de6978180050be22a554293ce86b38f9593;hp=e76f09c367c19a102d06a6ee94d4061a4e33fcc3;hb=e52aed0d855218f23cbd94629561eb4155936cec;hpb=b31501c90abcea74c03e86a3bd9f6190cb4e1ddd diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py index e76f09c367..09240de697 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -40,20 +40,23 @@ ERR_ARRAY_ELE = 'This must be HEX value for NList or Array: [%s].' ERR_EMPTY_EXPR = 'Empty expression is not allowed.' ERR_IN_OPERAND = 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).' +__ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$') +_ReLabel = re.compile('LABEL\((\w+)\)') +_ReOffset = re.compile('OFFSET_OF\((\w+)\)') + ## SplitString # Split string to list according double quote # For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn'] # def SplitString(String): # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi' - Str = String RetList = [] InSingleQuote = False InDoubleQuote = False Item = '' - for i, ch in enumerate(Str): + for i, ch in enumerate(String): if ch == '"' and not InSingleQuote: - if Str[i - 1] != '\\': + if String[i - 1] != '\\': InDoubleQuote = not InDoubleQuote if not InDoubleQuote: Item += String[i] @@ -64,7 +67,7 @@ def SplitString(String): RetList.append(Item) Item = '' elif ch == "'" and not InDoubleQuote: - if Str[i - 1] != '\\': + if String[i - 1] != '\\': InSingleQuote = not InSingleQuote if not InSingleQuote: Item += String[i] @@ -84,27 +87,26 @@ def SplitString(String): def SplitPcdValueString(String): # There might be escaped comma in GUID() or DEVICE_PATH() or " " # or ' ' or L' ' or L" " - Str = String RetList = [] InParenthesis = 0 InSingleQuote = False InDoubleQuote = False Item = '' - for i, ch in enumerate(Str): + for i, ch in enumerate(String): if ch == '(': InParenthesis += 1 - if ch == ')': + elif ch == ')': if InParenthesis: InParenthesis -= 1 else: raise BadExpression(ERR_STRING_TOKEN % Item) - if ch == '"' and not InSingleQuote: + elif ch == '"' and not InSingleQuote: if String[i-1] != '\\': InDoubleQuote = not InDoubleQuote - if ch == "'" and not InDoubleQuote: + elif ch == "'" and not InDoubleQuote: if String[i-1] != '\\': InSingleQuote = not InSingleQuote - if ch == ',': + elif ch == ',': if InParenthesis or InSingleQuote or InDoubleQuote: Item += String[i] continue @@ -119,14 +121,10 @@ def SplitPcdValueString(String): RetList.append(Item) return RetList -def IsValidCString(Str): - ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$') - if not ValidString.match(Str): - return False - return True +def IsValidCName(Str): + return True if __ValidString.match(Str) else False def BuildOptionValue(PcdValue, GuidDict): - IsArray = False if PcdValue.startswith('H'): InputValue = PcdValue[1:] elif PcdValue.startswith("L'") or PcdValue.startswith("'"): @@ -136,8 +134,6 @@ def BuildOptionValue(PcdValue, GuidDict): else: InputValue = PcdValue if IsFieldValueAnArray(InputValue): - IsArray = True - if IsArray: try: PcdValue = ValueExpressionEx(InputValue, 'VOID*', GuidDict)(True) except: @@ -219,8 +215,6 @@ class ValueExpression(object): NonLetterOpLst = ['+', '-', '*', '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':'] PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$') - HexPattern = re.compile(r'0[xX][0-9a-fA-F]+$') - RegGuidPattern = re.compile(r'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') SymbolPattern = re.compile("(" "\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|" @@ -672,7 +666,7 @@ class ValueExpression(object): self._LiteralToken.endswith('}'): return True - if self.HexPattern.match(self._LiteralToken): + if gHexPattern.match(self._LiteralToken): Token = self._LiteralToken[2:] if not Token: self._LiteralToken = '0x0' @@ -731,7 +725,7 @@ class ValueExpression(object): self._Token = '' if Expr: Ch = Expr[0] - Match = self.RegGuidPattern.match(Expr) + Match = gGuidPattern.match(Expr) if Match and not Expr[Match.end():Match.end()+1].isalnum() \ and Expr[Match.end():Match.end()+1] != '_': self._Idx += Match.end() @@ -904,17 +898,15 @@ class ValueExpressionEx(ValueExpression): PcdValueList = SplitPcdValueString(PcdValue.strip()[1:-1]) LabelDict = {} NewPcdValueList = [] - ReLabel = re.compile('LABEL\((\w+)\)') - ReOffset = re.compile('OFFSET_OF\((\w+)\)') LabelOffset = 0 - for Index, Item in enumerate(PcdValueList): + for Item in PcdValueList: # compute byte offset of every LABEL - LabelList = ReLabel.findall(Item) - Item = ReLabel.sub('', Item) + LabelList = _ReLabel.findall(Item) + Item = _ReLabel.sub('', Item) Item = Item.strip() if LabelList: for Label in LabelList: - if not IsValidCString(Label): + if not IsValidCName(Label): raise BadExpression('%s is not a valid c variable name' % Label) if Label not in LabelDict.keys(): LabelDict[Label] = str(LabelOffset) @@ -933,15 +925,15 @@ class ValueExpressionEx(ValueExpression): except: LabelOffset = LabelOffset + 1 - for Index, Item in enumerate(PcdValueList): + for Item in PcdValueList: # for LABEL parse Item = Item.strip() try: - Item = ReLabel.sub('', Item) + Item = _ReLabel.sub('', Item) except: pass try: - OffsetList = ReOffset.findall(Item) + OffsetList = _ReOffset.findall(Item) except: pass for Offset in OffsetList: @@ -999,7 +991,7 @@ class ValueExpressionEx(ValueExpression): Item = '0x%x' % TmpValue if type(TmpValue) != type('') else TmpValue if ItemSize == 0: ItemValue, ItemSize = ParseFieldValue(Item) - if not (Item.startswith('"') or Item.startswith('L') or Item.startswith('{')) and ItemSize > 1: + if Item[0] not in ['"','L','{'] and ItemSize > 1: raise BadExpression("Byte array number %s should less than 0xFF." % Item) else: ItemValue = ParseFieldValue(Item)[0]