X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FCommon%2FExpression.py;h=07ca039a9cf328a4e63185a7db047d5e56636a95;hb=2e351cbe8e190271b3716284fc1076551d005472;hp=79dc83efc3d5670d9dc278bc06d488402d359a56;hpb=8565b5829c1f30408020a4adb37074dba5492378;p=mirror_edk2.git diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py index 79dc83efc3..07ca039a9c 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -2,22 +2,22 @@ # This file is used to parse and evaluate expression in directive or PCD value. # # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.
-# This program and the accompanying materials -# are licensed and made available under the terms and conditions of the BSD License -# which accompanies this distribution. The full text of the license may be found at -# http://opensource.org/licenses/bsd-license.php -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# SPDX-License-Identifier: BSD-2-Clause-Patent ## Import Modules # +from __future__ import print_function +from __future__ import absolute_import from Common.GlobalData import * from CommonDataClass.Exceptions import BadExpression from CommonDataClass.Exceptions import WrnExpression -from Misc import GuidStringToGuidStructureString, ParseFieldValue, IsFieldValueAnArray +from .Misc import GuidStringToGuidStructureString, ParseFieldValue,CopyDict import Common.EdkLogger as EdkLogger import copy +from Common.DataType import * +import sys +from random import sample +import string ERR_STRING_EXPR = 'This operator cannot be used in string expression: [%s].' ERR_SNYTAX = 'Syntax error, the rest of expression cannot be evaluated: [%s].' @@ -25,7 +25,7 @@ ERR_MATCH = 'No matching right parenthesis.' ERR_STRING_TOKEN = 'Bad string token: [%s].' ERR_MACRO_TOKEN = 'Bad macro token: [%s].' ERR_EMPTY_TOKEN = 'Empty token is not allowed.' -ERR_PCD_RESOLVE = 'PCD token cannot be resolved: [%s].' +ERR_PCD_RESOLVE = 'The PCD should be FeatureFlag type or FixedAtBuild type: [%s].' ERR_VALID_TOKEN = 'No more valid token found from rest of string: [%s].' ERR_EXPR_TYPE = 'Different types found in expression.' ERR_OPERATOR_UNSUPPORT = 'Unsupported operator: [%s]' @@ -40,20 +40,26 @@ 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+)\)') +PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_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'] # def SplitString(String): # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi' - Str = String + RanStr = ''.join(sample(string.ascii_letters + string.digits, 8)) + String = String.replace('\\\\', RanStr).strip() 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 +70,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] @@ -79,32 +85,36 @@ def SplitString(String): raise BadExpression(ERR_STRING_TOKEN % Item) if Item: RetList.append(Item) + for i, ch in enumerate(RetList): + if RanStr in ch: + RetList[i] = ch.replace(RanStr,'\\\\') return RetList def SplitPcdValueString(String): # There might be escaped comma in GUID() or DEVICE_PATH() or " " # or ' ' or L' ' or L" " - Str = String + RanStr = ''.join(sample(string.ascii_letters + string.digits, 8)) + String = String.replace('\\\\', RanStr).strip() 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 @@ -117,16 +127,15 @@ def SplitPcdValueString(String): raise BadExpression(ERR_STRING_TOKEN % Item) if Item: RetList.append(Item) + for i, ch in enumerate(RetList): + if RanStr in ch: + RetList[i] = ch.replace(RanStr,'\\\\') 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("'"): @@ -135,13 +144,11 @@ def BuildOptionValue(PcdValue, GuidDict): InputValue = 'L"' + PcdValue[1:] + '"' else: InputValue = PcdValue - if IsFieldValueAnArray(InputValue): - IsArray = True - if IsArray: - try: - PcdValue = ValueExpressionEx(InputValue, 'VOID*', GuidDict)(True) - except: - pass + try: + PcdValue = ValueExpressionEx(InputValue, TAB_VOID, GuidDict)(True) + except: + pass + return PcdValue ## ReplaceExprMacro @@ -154,7 +161,7 @@ def ReplaceExprMacro(String, Macros, ExceptionList = None): InQuote = True MacroStartPos = String.find('$(') if MacroStartPos < 0: - for Pcd in gPlatformPcds.keys(): + for Pcd in gPlatformPcds: if Pcd in String: if Pcd not in gConditionalPcds: gConditionalPcds.append(Pcd) @@ -173,7 +180,7 @@ def ReplaceExprMacro(String, Macros, ExceptionList = None): RetStr += '0' elif not InQuote: Tklst = RetStr.split() - if Tklst and Tklst[-1] in ['IN', 'in'] and ExceptionList and Macro not in ExceptionList: + if Tklst and Tklst[-1] in {'IN', 'in'} and ExceptionList and Macro not in ExceptionList: raise BadExpression(ERR_IN_OPERAND) # Make sure the macro in exception list is encapsulated by double quote # For example: DEFINE ARCH = IA32 X64 @@ -203,7 +210,22 @@ def IntToStr(Value): SupportedInMacroList = ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY'] -class ValueExpression(object): +class BaseExpression(object): + def __init__(self, *args, **kwargs): + super(BaseExpression, self).__init__() + + # Check if current token matches the operators given from parameter + def _IsOperator(self, OpSet): + Idx = self._Idx + self._GetOperator() + if self._Token in OpSet: + if self._Token in self.LogicalOperators: + self._Token = self.LogicalOperators[self._Token] + return True + self._Idx = Idx + return False + +class ValueExpression(BaseExpression): # Logical operator mapping LogicalOperators = { '&&' : 'and', '||' : 'or', @@ -216,11 +238,8 @@ class ValueExpression(object): 'IN' : 'in' } - NonLetterOpLst = ['+', '-', '*', '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':'] + NonLetterOpLst = ['+', '-', TAB_STAR, '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':'] - 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+|" @@ -233,35 +252,36 @@ class ValueExpression(object): def Eval(Operator, Oprand1, Oprand2 = None): WrnExp = None - if Operator not in ["==", "!=", ">=", "<=", ">", "<", "in", "not in"] and \ - (type(Oprand1) == type('') or type(Oprand2) == type('')): + if Operator not in {"==", "!=", ">=", "<=", ">", "<", "in", "not in"} and \ + (isinstance(Oprand1, type('')) or isinstance(Oprand2, type(''))): raise BadExpression(ERR_STRING_EXPR % Operator) - if Operator in ['in', 'not in']: - if type(Oprand1) != type(''): + if Operator in {'in', 'not in'}: + if not isinstance(Oprand1, type('')): Oprand1 = IntToStr(Oprand1) - if type(Oprand2) != type(''): + if not isinstance(Oprand2, type('')): Oprand2 = IntToStr(Oprand2) TypeDict = { type(0) : 0, - type(0L) : 0, + # For python2 long type + type(sys.maxsize + 1) : 0, type('') : 1, type(True) : 2 } EvalStr = '' - if Operator in ["!", "NOT", "not"]: - if type(Oprand1) == type(''): + if Operator in {"!", "NOT", "not"}: + if isinstance(Oprand1, type('')): raise BadExpression(ERR_STRING_EXPR % Operator) EvalStr = 'not Oprand1' - elif Operator in ["~"]: - if type(Oprand1) == type(''): + elif Operator in {"~"}: + if isinstance(Oprand1, type('')): raise BadExpression(ERR_STRING_EXPR % Operator) EvalStr = '~ Oprand1' else: - if Operator in ["+", "-"] and (type(True) in [type(Oprand1), type(Oprand2)]): + if Operator in {"+", "-"} and (type(True) in {type(Oprand1), type(Oprand2)}): # Boolean in '+'/'-' will be evaluated but raise warning WrnExp = WrnExpression(WRN_BOOL_EXPR) - elif type('') in [type(Oprand1), type(Oprand2)] and type(Oprand1)!= type(Oprand2): + elif type('') in {type(Oprand1), type(Oprand2)} and not isinstance(Oprand1, type(Oprand2)): # == between string and number/boolean will always return False, != return True if Operator == "==": WrnExp = WrnExpression(WRN_EQCMP_STR_OTHERS) @@ -274,19 +294,19 @@ class ValueExpression(object): else: raise BadExpression(ERR_RELCMP_STR_OTHERS % Operator) elif TypeDict[type(Oprand1)] != TypeDict[type(Oprand2)]: - if Operator in ["==", "!=", ">=", "<=", ">", "<"] and set((TypeDict[type(Oprand1)], TypeDict[type(Oprand2)])) == set((TypeDict[type(True)], TypeDict[type(0)])): + if Operator in {"==", "!=", ">=", "<=", ">", "<"} and set((TypeDict[type(Oprand1)], TypeDict[type(Oprand2)])) == set((TypeDict[type(True)], TypeDict[type(0)])): # comparison between number and boolean is allowed pass - elif Operator in ['&', '|', '^', "and", "or"] and set((TypeDict[type(Oprand1)], TypeDict[type(Oprand2)])) == set((TypeDict[type(True)], TypeDict[type(0)])): + elif Operator in {'&', '|', '^', "and", "or"} and set((TypeDict[type(Oprand1)], TypeDict[type(Oprand2)])) == set((TypeDict[type(True)], TypeDict[type(0)])): # bitwise and logical operation between number and boolean is allowed pass else: raise BadExpression(ERR_EXPR_TYPE) - if type(Oprand1) == type('') and type(Oprand2) == type(''): - if (Oprand1.startswith('L"') and not Oprand2.startswith('L"')) or \ - (not Oprand1.startswith('L"') and Oprand2.startswith('L"')): + if isinstance(Oprand1, type('')) and isinstance(Oprand2, type('')): + if ((Oprand1.startswith('L"') or Oprand1.startswith("L'")) and (not Oprand2.startswith('L"')) and (not Oprand2.startswith("L'"))) or \ + (((not Oprand1.startswith('L"')) and (not Oprand1.startswith("L'"))) and (Oprand2.startswith('L"') or Oprand2.startswith("L'"))): raise BadExpression(ERR_STRING_CMP % (Oprand1, Operator, Oprand2)) - if 'in' in Operator and type(Oprand2) == type(''): + if 'in' in Operator and isinstance(Oprand2, type('')): Oprand2 = Oprand2.split() EvalStr = 'Oprand1 ' + Operator + ' Oprand2' @@ -297,10 +317,10 @@ class ValueExpression(object): } try: Val = eval(EvalStr, {}, Dict) - except Exception, Excpt: + except Exception as Excpt: raise BadExpression(str(Excpt)) - if Operator in ['and', 'or']: + if Operator in {'and', 'or'}: if Val: Val = True else: @@ -312,8 +332,9 @@ class ValueExpression(object): return Val def __init__(self, Expression, SymbolTable={}): + super(ValueExpression, self).__init__(self, Expression, SymbolTable) self._NoProcess = False - if type(Expression) != type(''): + if not isinstance(Expression, type('')): self._Expr = Expression self._NoProcess = True return @@ -328,7 +349,7 @@ class ValueExpression(object): # # The symbol table including PCD and macro mapping # - self._Symb = copy.deepcopy(SymbolTable) + self._Symb = CopyDict(SymbolTable) self._Symb.update(self.LogicalOperators) self._Idx = 0 self._Len = len(self._Expr) @@ -361,7 +382,7 @@ class ValueExpression(object): Token = self._GetToken() except BadExpression: pass - if type(Token) == type('') and Token.startswith('{') and Token.endswith('}') and self._Idx >= self._Len: + if isinstance(Token, type('')) and Token.startswith('{') and Token.endswith('}') and self._Idx >= self._Len: return self._Expr self._Idx = 0 @@ -369,13 +390,13 @@ class ValueExpression(object): Val = self._ConExpr() RealVal = Val - if type(Val) == type(''): + if isinstance(Val, type('')): if Val == 'L""': Val = False elif not Val: Val = False RealVal = '""' - elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'"): + elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'") and not Val.startswith("'"): Val = True RealVal = '"' + RealVal + '"' @@ -399,94 +420,101 @@ class ValueExpression(object): # Template function to parse binary operators which have same precedence # Expr [Operator Expr]* - def _ExprFuncTemplate(self, EvalFunc, OpLst): + def _ExprFuncTemplate(self, EvalFunc, OpSet): Val = EvalFunc() - while self._IsOperator(OpLst): + while self._IsOperator(OpSet): Op = self._Token if Op == '?': Val2 = EvalFunc() - if self._IsOperator(':'): + if self._IsOperator({':'}): Val3 = EvalFunc() if Val: Val = Val2 else: Val = Val3 continue + # + # PEP 238 -- Changing the Division Operator + # x/y to return a reasonable approximation of the mathematical result of the division ("true division") + # x//y to return the floor ("floor division") + # + if Op == '/': + Op = '//' try: Val = self.Eval(Op, Val, EvalFunc()) - except WrnExpression, Warn: + except WrnExpression as Warn: self._WarnExcept = Warn Val = Warn.result return Val # A [? B]* def _ConExpr(self): - return self._ExprFuncTemplate(self._OrExpr, ['?', ':']) + return self._ExprFuncTemplate(self._OrExpr, {'?', ':'}) # A [|| B]* def _OrExpr(self): - return self._ExprFuncTemplate(self._AndExpr, ["OR", "or", "||"]) + return self._ExprFuncTemplate(self._AndExpr, {"OR", "or", "||"}) # A [&& B]* def _AndExpr(self): - return self._ExprFuncTemplate(self._BitOr, ["AND", "and", "&&"]) + return self._ExprFuncTemplate(self._BitOr, {"AND", "and", "&&"}) # A [ | B]* def _BitOr(self): - return self._ExprFuncTemplate(self._BitXor, ["|"]) + return self._ExprFuncTemplate(self._BitXor, {"|"}) # A [ ^ B]* def _BitXor(self): - return self._ExprFuncTemplate(self._BitAnd, ["XOR", "xor", "^"]) + return self._ExprFuncTemplate(self._BitAnd, {"XOR", "xor", "^"}) # A [ & B]* def _BitAnd(self): - return self._ExprFuncTemplate(self._EqExpr, ["&"]) + return self._ExprFuncTemplate(self._EqExpr, {"&"}) # A [ == B]* def _EqExpr(self): Val = self._RelExpr() - while self._IsOperator(["==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"]): + while self._IsOperator({"==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"}): Op = self._Token - if Op in ["!", "NOT", "not"]: - if not self._IsOperator(["IN", "in"]): + if Op in {"!", "NOT", "not"}: + if not self._IsOperator({"IN", "in"}): raise BadExpression(ERR_REL_NOT_IN) Op += ' ' + self._Token try: Val = self.Eval(Op, Val, self._RelExpr()) - except WrnExpression, Warn: + except WrnExpression as Warn: self._WarnExcept = Warn Val = Warn.result return Val # A [ > B]* def _RelExpr(self): - return self._ExprFuncTemplate(self._ShiftExpr, ["<=", ">=", "<", ">", "LE", "GE", "LT", "GT"]) + return self._ExprFuncTemplate(self._ShiftExpr, {"<=", ">=", "<", ">", "LE", "GE", "LT", "GT"}) def _ShiftExpr(self): - return self._ExprFuncTemplate(self._AddExpr, ["<<", ">>"]) + return self._ExprFuncTemplate(self._AddExpr, {"<<", ">>"}) # A [ + B]* def _AddExpr(self): - return self._ExprFuncTemplate(self._MulExpr, ["+", "-"]) + return self._ExprFuncTemplate(self._MulExpr, {"+", "-"}) # A [ * B]* def _MulExpr(self): - return self._ExprFuncTemplate(self._UnaryExpr, ["*", "/", "%"]) + return self._ExprFuncTemplate(self._UnaryExpr, {TAB_STAR, "/", "%"}) # [!]*A def _UnaryExpr(self): - if self._IsOperator(["!", "NOT", "not"]): + if self._IsOperator({"!", "NOT", "not"}): Val = self._UnaryExpr() try: return self.Eval('not', Val) - except WrnExpression, Warn: + except WrnExpression as Warn: self._WarnExcept = Warn return Warn.result - if self._IsOperator(["~"]): + if self._IsOperator({"~"}): Val = self._UnaryExpr() try: return self.Eval('~', Val) - except WrnExpression, Warn: + except WrnExpression as Warn: self._WarnExcept = Warn return Warn.result return self._IdenExpr() @@ -520,7 +548,7 @@ class ValueExpression(object): if self._Token.startswith('"') or self._Token.startswith('L"'): Flag = 0 for Index in range(len(self._Token)): - if self._Token[Index] in ['"']: + if self._Token[Index] in {'"'}: if self._Token[Index - 1] == '\\': continue Flag += 1 @@ -529,7 +557,7 @@ class ValueExpression(object): if self._Token.startswith("'") or self._Token.startswith("L'"): Flag = 0 for Index in range(len(self._Token)): - if self._Token[Index] in ["'"]: + if self._Token[Index] in {"'"}: if self._Token[Index - 1] == '\\': continue Flag += 1 @@ -557,7 +585,7 @@ class ValueExpression(object): IsArray = IsGuid = False if len(Token.split(',')) == 11 and len(Token.split(',{')) == 2 \ and len(Token.split('},')) == 1: - HexLen = [11,6,6,5,4,4,4,4,4,4,6] + HexLen = [11, 6, 6, 5, 4, 4, 4, 4, 4, 4, 6] HexList= Token.split(',') if HexList[3].startswith('{') and \ not [Index for Index, Hex in enumerate(HexList) if len(Hex) > HexLen[Index]]: @@ -622,21 +650,21 @@ class ValueExpression(object): raise BadExpression(ERR_EMPTY_TOKEN) # PCD token - if self.PcdPattern.match(self._Token): + if PcdPattern.match(self._Token): if self._Token not in self._Symb: Ex = BadExpression(ERR_PCD_RESOLVE % self._Token) Ex.Pcd = self._Token raise Ex self._Token = ValueExpression(self._Symb[self._Token], self._Symb)(True, self._Depth+1) - if type(self._Token) != type(''): + if not isinstance(self._Token, type('')): self._LiteralToken = hex(self._Token) return if self._Token.startswith('"'): self._Token = self._Token[1:-1] - elif self._Token in ["FALSE", "false", "False"]: + elif self._Token in {"FALSE", "false", "False"}: self._Token = False - elif self._Token in ["TRUE", "true", "True"]: + elif self._Token in {"TRUE", "true", "True"}: self._Token = True else: self.__IsNumberToken() @@ -672,7 +700,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' @@ -723,7 +751,7 @@ class ValueExpression(object): if Ch == ')': TmpValue = self._Expr[Idx :self._Idx - 1] TmpValue = ValueExpression(TmpValue)(True) - TmpValue = '0x%x' % int(TmpValue) if type(TmpValue) != type('') else TmpValue + TmpValue = '0x%x' % int(TmpValue) if not isinstance(TmpValue, type('')) else TmpValue break self._Token, Size = ParseFieldValue(Prefix + '(' + TmpValue + ')') return self._Token @@ -731,7 +759,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() @@ -753,7 +781,7 @@ class ValueExpression(object): # Parse operator def _GetOperator(self): self.__SkipWS() - LegalOpLst = ['&&', '||', '!=', '==', '>=', '<='] + self.NonLetterOpLst + ['?',':'] + LegalOpLst = ['&&', '||', '!=', '==', '>=', '<='] + self.NonLetterOpLst + ['?', ':'] self._Token = '' Expr = self._Expr[self._Idx:] @@ -773,7 +801,7 @@ class ValueExpression(object): OpToken = '' for Ch in Expr: if Ch in self.NonLetterOpLst: - if '!' == Ch and OpToken: + if Ch in ['!', '~'] and OpToken: break self._Idx += 1 OpToken += Ch @@ -785,17 +813,6 @@ class ValueExpression(object): self._Token = OpToken return OpToken - # Check if current token matches the operators given from OpList - def _IsOperator(self, OpList): - Idx = self._Idx - self._GetOperator() - if self._Token in OpList: - if self._Token in self.LogicalOperators: - self._Token = self.LogicalOperators[self._Token] - return True - self._Idx = Idx - return False - class ValueExpressionEx(ValueExpression): def __init__(self, PcdValue, PcdType, SymbolTable={}): ValueExpression.__init__(self, PcdValue, SymbolTable) @@ -804,219 +821,219 @@ class ValueExpressionEx(ValueExpression): def __call__(self, RealValue=False, Depth=0): PcdValue = self.PcdValue - try: - PcdValue = ValueExpression.__call__(self, RealValue, Depth) - if self.PcdType == 'VOID*' and (PcdValue.startswith("'") or PcdValue.startswith("L'")): - PcdValue, Size = ParseFieldValue(PcdValue) - PcdValueList = [] - for I in range(Size): - PcdValueList.append('0x%02X'%(PcdValue & 0xff)) - PcdValue = PcdValue >> 8 - PcdValue = '{' + ','.join(PcdValueList) + '}' - elif self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN'] and (PcdValue.startswith("'") or \ - PcdValue.startswith('"') or PcdValue.startswith("L'") or PcdValue.startswith('L"') or PcdValue.startswith('{')): - raise BadExpression - except WrnExpression, Value: - PcdValue = Value.result - except BadExpression, Value: - if self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN']: - PcdValue = PcdValue.strip() - if type(PcdValue) == type('') and PcdValue.startswith('{') and PcdValue.endswith('}'): - PcdValue = SplitPcdValueString(PcdValue[1:-1]) - if type(PcdValue) == type([]): - TmpValue = 0 - Size = 0 - ValueType = '' - for Item in PcdValue: - Item = Item.strip() - if Item.startswith('UINT8'): - ItemSize = 1 - ValueType = 'UINT8' - elif Item.startswith('UINT16'): - ItemSize = 2 - ValueType = 'UINT16' - elif Item.startswith('UINT32'): - ItemSize = 4 - ValueType = 'UINT32' - elif Item.startswith('UINT64'): - ItemSize = 8 - ValueType = 'UINT64' - elif Item.startswith('"') or Item.startswith("'") or Item.startswith('L'): - ItemSize = 0 - ValueType = 'VOID*' - else: - ItemSize = 0 - ValueType = 'UINT8' - Item = ValueExpressionEx(Item, ValueType, self._Symb)(True) - - if ItemSize == 0: - try: - tmpValue = int(Item, 16) if Item.upper().startswith('0X') else int(Item, 0) - if tmpValue > 255: - raise BadExpression("Byte array number %s should less than 0xFF." % Item) - except BadExpression, Value: - raise BadExpression(Value) - except ValueError: - pass - ItemValue, ItemSize = ParseFieldValue(Item) - else: - ItemValue = ParseFieldValue(Item)[0] + if "{CODE(" not in PcdValue: + try: + PcdValue = ValueExpression.__call__(self, RealValue, Depth) + if self.PcdType == TAB_VOID and (PcdValue.startswith("'") or PcdValue.startswith("L'")): + PcdValue, Size = ParseFieldValue(PcdValue) + PcdValueList = [] + for I in range(Size): + PcdValueList.append('0x%02X'%(PcdValue & 0xff)) + PcdValue = PcdValue >> 8 + PcdValue = '{' + ','.join(PcdValueList) + '}' + elif self.PcdType in TAB_PCD_NUMERIC_TYPES and (PcdValue.startswith("'") or \ + PcdValue.startswith('"') or PcdValue.startswith("L'") or PcdValue.startswith('L"') or PcdValue.startswith('{')): + raise BadExpression + except WrnExpression as Value: + PcdValue = Value.result + except BadExpression as Value: + if self.PcdType in TAB_PCD_NUMERIC_TYPES: + PcdValue = PcdValue.strip() + if PcdValue.startswith('{') and PcdValue.endswith('}'): + PcdValue = SplitPcdValueString(PcdValue[1:-1]) + if isinstance(PcdValue, type([])): + TmpValue = 0 + Size = 0 + ValueType = '' + for Item in PcdValue: + Item = Item.strip() + if Item.startswith(TAB_UINT8): + ItemSize = 1 + ValueType = TAB_UINT8 + elif Item.startswith(TAB_UINT16): + ItemSize = 2 + ValueType = TAB_UINT16 + elif Item.startswith(TAB_UINT32): + ItemSize = 4 + ValueType = TAB_UINT32 + elif Item.startswith(TAB_UINT64): + ItemSize = 8 + ValueType = TAB_UINT64 + elif Item[0] in {'"', "'", 'L'}: + ItemSize = 0 + ValueType = TAB_VOID + else: + ItemSize = 0 + ValueType = TAB_UINT8 + Item = ValueExpressionEx(Item, ValueType, self._Symb)(True) + if ItemSize == 0: + try: + tmpValue = int(Item, 0) + if tmpValue > 255: + raise BadExpression("Byte array number %s should less than 0xFF." % Item) + except BadExpression as Value: + raise BadExpression(Value) + except ValueError: + pass + ItemValue, ItemSize = ParseFieldValue(Item) + else: + ItemValue = ParseFieldValue(Item)[0] - if type(ItemValue) == type(''): - ItemValue = int(ItemValue, 16) if ItemValue.startswith('0x') else int(ItemValue) + if isinstance(ItemValue, type('')): + ItemValue = int(ItemValue, 0) - TmpValue = (ItemValue << (Size * 8)) | TmpValue - Size = Size + ItemSize + TmpValue = (ItemValue << (Size * 8)) | TmpValue + Size = Size + ItemSize + else: + try: + TmpValue, Size = ParseFieldValue(PcdValue) + except BadExpression as Value: + raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value)) + if isinstance(TmpValue, type('')): + try: + TmpValue = int(TmpValue) + except: + raise BadExpression(Value) + else: + PcdValue = '0x%0{}X'.format(Size) % (TmpValue) + if TmpValue < 0: + raise BadExpression('Type %s PCD Value is negative' % self.PcdType) + if self.PcdType == TAB_UINT8 and Size > 1: + raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self.PcdType) + if self.PcdType == TAB_UINT16 and Size > 2: + raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self.PcdType) + if self.PcdType == TAB_UINT32 and Size > 4: + raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self.PcdType) + if self.PcdType == TAB_UINT64 and Size > 8: + raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType) else: try: - TmpValue, Size = ParseFieldValue(PcdValue) - except BadExpression, Value: - raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value)) - if type(TmpValue) == type(''): - try: - TmpValue = int(TmpValue) + TmpValue = int(PcdValue) + TmpList = [] + if TmpValue.bit_length() == 0: + PcdValue = '{0x00}' + else: + for I in range((TmpValue.bit_length() + 7) // 8): + TmpList.append('0x%02x' % ((TmpValue >> I * 8) & 0xff)) + PcdValue = '{' + ', '.join(TmpList) + '}' except: - raise BadExpression(Value) - else: - PcdValue = '0x%0{}X'.format(Size) % (TmpValue) - if TmpValue < 0: - raise BadExpression('Type %s PCD Value is negative' % self.PcdType) - if self.PcdType == 'UINT8' and Size > 1: - raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self.PcdType) - if self.PcdType == 'UINT16' and Size > 2: - raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self.PcdType) - if self.PcdType == 'UINT32' and Size > 4: - raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self.PcdType) - if self.PcdType == 'UINT64' and Size > 8: - raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType) - else: - try: - TmpValue = long(PcdValue) - TmpList = [] - if TmpValue.bit_length() == 0: - PcdValue = '{0x00}' - else: - for I in range((TmpValue.bit_length() + 7) / 8): - TmpList.append('0x%02x' % ((TmpValue >> I * 8) & 0xff)) - PcdValue = '{' + ', '.join(TmpList) + '}' - except: - if PcdValue.strip().startswith('{'): - 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): - # compute byte offset of every LABEL - Item = Item.strip() - LabelList = ReLabel.findall(Item) - if LabelList: - for Label in LabelList: - if not IsValidCString(Label): - raise BadExpression('%s is not a valid c variable name' % Label) - if Label not in LabelDict.keys(): - LabelDict[Label] = str(LabelOffset) - if Item.startswith('UINT8'): - LabelOffset = LabelOffset + 1 - elif Item.startswith('UINT16'): - LabelOffset = LabelOffset + 2 - elif Item.startswith('UINT32'): - LabelOffset = LabelOffset + 4 - elif Item.startswith('UINT64'): - LabelOffset = LabelOffset + 8 - else: - try: - ItemValue, ItemSize = ParseFieldValue(Item) - LabelOffset = LabelOffset + ItemSize - except: + if PcdValue.strip().startswith('{'): + PcdValueList = SplitPcdValueString(PcdValue.strip()[1:-1]) + LabelDict = {} + NewPcdValueList = [] + LabelOffset = 0 + for Item in PcdValueList: + # compute byte offset of every LABEL + LabelList = _ReLabel.findall(Item) + Item = _ReLabel.sub('', Item) + Item = Item.strip() + if LabelList: + for Label in LabelList: + if not IsValidCName(Label): + raise BadExpression('%s is not a valid c variable name' % Label) + if Label not in LabelDict: + LabelDict[Label] = str(LabelOffset) + if Item.startswith(TAB_UINT8): LabelOffset = LabelOffset + 1 - - for Index, Item in enumerate(PcdValueList): - # for LABEL parse - Item = Item.strip() - try: - Item = ReLabel.sub('', Item) - except: - pass - try: - OffsetList = ReOffset.findall(Item) - except: - pass - for Offset in OffsetList: - if Offset in LabelDict.keys(): - Re = re.compile('OFFSET_OF\(%s\)' % Offset) - Item = Re.sub(LabelDict[Offset], Item) + elif Item.startswith(TAB_UINT16): + LabelOffset = LabelOffset + 2 + elif Item.startswith(TAB_UINT32): + LabelOffset = LabelOffset + 4 + elif Item.startswith(TAB_UINT64): + LabelOffset = LabelOffset + 8 else: - raise BadExpression('%s not defined' % Offset) - NewPcdValueList.append(Item) - - AllPcdValueList = [] - for Item in NewPcdValueList: - Size = 0 - ValueStr = '' - TokenSpaceGuidName = '' - if Item.startswith('GUID') and Item.endswith(')'): + try: + ItemValue, ItemSize = ParseFieldValue(Item) + LabelOffset = LabelOffset + ItemSize + except: + LabelOffset = LabelOffset + 1 + + for Item in PcdValueList: + # for LABEL parse + Item = Item.strip() try: - TokenSpaceGuidName = re.search('GUID\((\w+)\)', Item).group(1) + Item = _ReLabel.sub('', Item) except: pass - if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb: - Item = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')' - elif TokenSpaceGuidName: - raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName) - Item, Size = ParseFieldValue(Item) - for Index in range(0, Size): - ValueStr = '0x%02X' % (int(Item) & 255) - Item >>= 8 - AllPcdValueList.append(ValueStr) - continue - elif Item.startswith('DEVICE_PATH') and Item.endswith(')'): - Item, Size = ParseFieldValue(Item) - AllPcdValueList.append(Item[1:-1]) - continue - else: - ValueType = "" - if Item.startswith('UINT8'): - ItemSize = 1 - ValueType = "UINT8" - elif Item.startswith('UINT16'): - ItemSize = 2 - ValueType = "UINT16" - elif Item.startswith('UINT32'): - ItemSize = 4 - ValueType = "UINT32" - elif Item.startswith('UINT64'): - ItemSize = 8 - ValueType = "UINT64" - else: - ItemSize = 0 - if ValueType: - TmpValue = ValueExpressionEx(Item, ValueType, self._Symb)(True) - else: - TmpValue = ValueExpressionEx(Item, self.PcdType, self._Symb)(True) - 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: - raise BadExpression("Byte array number %s should less than 0xFF." % Item) + try: + OffsetList = _ReOffset.findall(Item) + except: + pass + # replace each offset, except errors + for Offset in OffsetList: + try: + Item = Item.replace('OFFSET_OF({})'.format(Offset), LabelDict[Offset]) + except: + raise BadExpression('%s not defined' % Offset) + + NewPcdValueList.append(Item) + + AllPcdValueList = [] + for Item in NewPcdValueList: + Size = 0 + ValueStr = '' + TokenSpaceGuidName = '' + if Item.startswith(TAB_GUID) and Item.endswith(')'): + try: + TokenSpaceGuidName = re.search('GUID\((\w+)\)', Item).group(1) + except: + pass + if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb: + Item = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')' + elif TokenSpaceGuidName: + raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName) + Item, Size = ParseFieldValue(Item) + for Index in range(0, Size): + ValueStr = '0x%02X' % (int(Item) & 255) + Item >>= 8 + AllPcdValueList.append(ValueStr) + continue + elif Item.startswith('DEVICE_PATH') and Item.endswith(')'): + Item, Size = ParseFieldValue(Item) + AllPcdValueList.append(Item[1:-1]) + continue else: - ItemValue = ParseFieldValue(Item)[0] - for I in range(0, ItemSize): - ValueStr = '0x%02X' % (int(ItemValue) & 255) - ItemValue >>= 8 - AllPcdValueList.append(ValueStr) - Size += ItemSize - - if Size > 0: - PcdValue = '{' + ','.join(AllPcdValueList) + '}' - else: - raise BadExpression("Type: %s, Value: %s, %s"%(self.PcdType, PcdValue, Value)) + ValueType = "" + if Item.startswith(TAB_UINT8): + ItemSize = 1 + ValueType = TAB_UINT8 + elif Item.startswith(TAB_UINT16): + ItemSize = 2 + ValueType = TAB_UINT16 + elif Item.startswith(TAB_UINT32): + ItemSize = 4 + ValueType = TAB_UINT32 + elif Item.startswith(TAB_UINT64): + ItemSize = 8 + ValueType = TAB_UINT64 + else: + ItemSize = 0 + if ValueType: + TmpValue = ValueExpressionEx(Item, ValueType, self._Symb)(True) + else: + TmpValue = ValueExpressionEx(Item, self.PcdType, self._Symb)(True) + Item = '0x%x' % TmpValue if not isinstance(TmpValue, type('')) else TmpValue + if ItemSize == 0: + ItemValue, ItemSize = ParseFieldValue(Item) + 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] + for I in range(0, ItemSize): + ValueStr = '0x%02X' % (int(ItemValue) & 255) + ItemValue >>= 8 + AllPcdValueList.append(ValueStr) + Size += ItemSize + + if Size > 0: + PcdValue = '{' + ','.join(AllPcdValueList) + '}' + else: + raise BadExpression("Type: %s, Value: %s, %s"%(self.PcdType, PcdValue, Value)) - if PcdValue == 'True': - PcdValue = '1' - if PcdValue == 'False': - PcdValue = '0' + if PcdValue == 'True': + PcdValue = '1' + if PcdValue == 'False': + PcdValue = '0' if RealValue: return PcdValue @@ -1028,10 +1045,10 @@ if __name__ == '__main__': if input in 'qQ': break try: - print ValueExpression(input)(True) - print ValueExpression(input)(False) - except WrnExpression, Ex: - print Ex.result - print str(Ex) - except Exception, Ex: - print str(Ex) + print(ValueExpression(input)(True)) + print(ValueExpression(input)(False)) + except WrnExpression as Ex: + print(Ex.result) + print(str(Ex)) + except Exception as Ex: + print(str(Ex))