X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FCommon%2FExpression.py;h=320f0015009b3c28e16562cda31116b179597e8d;hb=7da06eeede10fc8b908327b375bcdf36707ff7b5;hp=bcb27ec11fd596c75f5ce5c91220b24afd7d0ab9;hpb=ae4cc2b084a67d671a116808ac82e59ab770afc0;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py index bcb27ec11f..320f001500 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -40,6 +40,10 @@ 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'] @@ -91,18 +95,18 @@ def SplitPcdValueString(String): 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 @@ -117,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("'"): @@ -134,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: @@ -217,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+|" @@ -670,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' @@ -729,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() @@ -839,7 +835,7 @@ class ValueExpressionEx(ValueExpression): elif Item.startswith('UINT64'): ItemSize = 8 ValueType = 'UINT64' - elif Item.startswith('"') or Item.startswith("'") or Item.startswith('L'): + elif Item[0] in ['"',"'",'L']: ItemSize = 0 ValueType = 'VOID*' else: @@ -849,7 +845,7 @@ class ValueExpressionEx(ValueExpression): if ItemSize == 0: try: - tmpValue = int(Item, 16) if Item.upper().startswith('0X') else int(Item, 0) + tmpValue = int(Item, 0) if tmpValue > 255: raise BadExpression("Byte array number %s should less than 0xFF." % Item) except BadExpression, Value: @@ -861,7 +857,7 @@ class ValueExpressionEx(ValueExpression): ItemValue = ParseFieldValue(Item)[0] if type(ItemValue) == type(''): - ItemValue = int(ItemValue, 16) if ItemValue.startswith('0x') else int(ItemValue) + ItemValue = int(ItemValue, 0) TmpValue = (ItemValue << (Size * 8)) | TmpValue Size = Size + ItemSize @@ -902,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) @@ -931,23 +925,24 @@ 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 + # replace each offset, except errors for Offset in OffsetList: - if Offset in LabelDict.keys(): - Re = re.compile('OFFSET_OF\(%s\)' % Offset) - Item = Re.sub(LabelDict[Offset], Item) - else: + try: + Item = Item.replace('OFFSET_OF({})'.format(Offset),LabelDict[Offset]) + except: raise BadExpression('%s not defined' % Offset) + NewPcdValueList.append(Item) AllPcdValueList = [] @@ -997,7 +992,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]