X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FCommon%2FExpression.py;h=30711cedd784f83922430c39b95a7343f09a0473;hp=bcb27ec11fd596c75f5ce5c91220b24afd7d0ab9;hb=663b9e061ed1b48e562159e51333e996f1efc830;hpb=ae4cc2b084a67d671a116808ac82e59ab770afc0 diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py index bcb27ec11f..30711cedd7 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -40,6 +40,8 @@ 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]*$') + ## 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 +93,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 +119,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 +132,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 +213,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 +664,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 +723,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() @@ -912,7 +906,7 @@ class ValueExpressionEx(ValueExpression): 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) @@ -997,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]