Ejemplo n.º 1
0
    def _GetSingleToken(self):
        self.__SkipWS()
        Expr = self._Expr[self._Idx:]
        if Expr.startswith('L"'):
            # Skip L
            self._Idx += 1
            UStr = self.__GetString()
            self._Token = 'L"' + UStr + '"'
            return self._Token
        elif Expr.startswith("L'"):
            # Skip L
            self._Idx += 1
            UStr = self.__GetString()
            self._Token = "L'" + UStr + "'"
            return self._Token
        elif Expr.startswith("'"):
            UStr = self.__GetString()
            self._Token = "'" + UStr + "'"
            return self._Token
        elif Expr.startswith('UINT'):
            Re = re.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)')
            try:
                RetValue = Re.search(Expr).group(1)
            except:
                 raise BadExpression('Invalid Expression %s' % Expr)
            Idx = self._Idx
            for Ch in Expr:
                self._Idx += 1
                if Ch == '(':
                    Prefix = self._Expr[Idx:self._Idx - 1]
                    Idx = self._Idx
                if Ch == ')':
                    TmpValue = self._Expr[Idx :self._Idx - 1]
                    TmpValue = ValueExpression(TmpValue)(True)
                    TmpValue = '0x%x' % int(TmpValue) if not isinstance(TmpValue, type('')) else TmpValue
                    break
            self._Token, Size = ParseFieldValue(Prefix + '(' + TmpValue + ')')
            return  self._Token

        self._Token = ''
        if Expr:
            Ch = Expr[0]
            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()
                self._Token = ValueExpression(GuidStringToGuidStructureString(Expr[0:Match.end()]))(True, self._Depth+1)
                return self._Token
            elif self.__IsIdChar(Ch):
                return self.__GetIdToken()
            elif Ch == '"':
                return self.__GetString()
            elif Ch == '{':
                return self.__GetArray()
            elif Ch == '(' or Ch == ')':
                self._Idx += 1
                self._Token = Ch
                return self._Token

        raise BadExpression(ERR_VALID_TOKEN % Expr)
Ejemplo n.º 2
0
    def __GetNList(self, InArray=False):
        self._GetSingleToken()
        if not self.__IsHexLiteral():
            if InArray:
                raise BadExpression(ERR_ARRAY_ELE % self._Token)
            return self._Token

        self.__SkipWS()
        Expr = self._Expr[self._Idx:]
        if not Expr.startswith(','):
            return self._Token

        NList = self._LiteralToken
        while Expr.startswith(','):
            NList += ','
            self._Idx += 1
            self.__SkipWS()
            self._GetSingleToken()
            if not self.__IsHexLiteral():
                raise BadExpression(ERR_ARRAY_ELE % self._Token)
            NList += self._LiteralToken
            self.__SkipWS()
            Expr = self._Expr[self._Idx:]
        self._Token = self._LiteralToken = NList
        return self._Token
Ejemplo n.º 3
0
    def __GetString(self):
        Idx = self._Idx

        # Skip left quote
        self._Idx += 1

        # Replace escape \\\", \"
        if self._Expr[Idx] == '"':
            Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace(
                '\\\"', '\\\'')
            for Ch in Expr:
                self._Idx += 1
                if Ch == '"':
                    break
            self._Token = self._LiteralToken = self._Expr[Idx:self._Idx]
            if not self._Token.endswith('"'):
                raise BadExpression(ERR_STRING_TOKEN % self._Token)
        #Replace escape \\\', \'
        elif self._Expr[Idx] == "'":
            Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace(
                "\\\'", "\\\"")
            for Ch in Expr:
                self._Idx += 1
                if Ch == "'":
                    break
            self._Token = self._LiteralToken = self._Expr[Idx:self._Idx]
            if not self._Token.endswith("'"):
                raise BadExpression(ERR_STRING_TOKEN % self._Token)
        self._Token = self._Token[1:-1]
        return self._Token
Ejemplo n.º 4
0
    def __GetArray(self):
        Token = '{'
        self._Idx += 1
        self.__GetNList(True)
        Token += self._LiteralToken
        if self._Idx >= self._Len or self._Expr[self._Idx] != '}':
            raise BadExpression(ERR_ARRAY_TOKEN % Token)
        Token += '}'

        # All whitespace and tabs in array are already stripped.
        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]
            HexList = Token.split(',')
            if HexList[3].startswith('{') and \
                not [Index for Index, Hex in enumerate(HexList) if len(Hex) > HexLen[Index]]:
                IsGuid = True
        if Token.lstrip('{').rstrip('}').find('{') == -1:
            if not [
                    Hex for Hex in Token.lstrip('{').rstrip('}').split(',')
                    if len(Hex) > 4
            ]:
                IsArray = True
        if not IsArray and not IsGuid:
            raise BadExpression(ERR_ARRAY_TOKEN % Token)
        self._Idx += 1
        self._Token = self._LiteralToken = Token
        return self._Token
 def _IdenExpr(self):
     Tk = self._GetToken()
     if Tk == '(':
         Val = self._OrExpr()
         try:
             # _GetToken may also raise BadExpression
             if self._GetToken() != ')':
                 raise BadExpression(ERR_MATCH)
         except BadExpression:
             raise BadExpression(ERR_MATCH)
         return Val
     return Tk
    def _GetOperator(self):
        self.__SkipWS()
        LegalOpLst = ['&&', '||', '!=', '==', '>=', '<='] + self.NonLetterOpLst

        self._Token = ''
        Expr = self._Expr[self._Idx:]

        # Reach end of expression
        if not Expr:
            return ''

        # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
        if Expr[0].isalpha():
            return self.__GetIdToken(True)

        # Start to get regular operator: +, -, <, > ...
        if Expr[0] not in self.NonLetterOpLst:
            return ''

        OpToken = ''
        for Ch in Expr:
            if Ch in self.NonLetterOpLst:
                if '!' == Ch and OpToken:
                    break
                self._Idx += 1
                OpToken += Ch
            else:
                break

        if OpToken not in LegalOpLst:
            raise BadExpression(ERR_OPERATOR_UNSUPPORT % OpToken)
        self._Token = OpToken
        return OpToken
    def __init__(self, Expression, PcdDataType, SymbolTable={}):
        super(RangeExpression, self).__init__(self, Expression, PcdDataType,
                                              SymbolTable)
        self._NoProcess = False
        if not isinstance(Expression, type('')):
            self._Expr = Expression
            self._NoProcess = True
            return

        self._Expr = Expression.strip()

        if not self._Expr.strip():
            raise BadExpression(ERR_EMPTY_EXPR)

        #
        # The symbol table including PCD and macro mapping
        #
        self._Symb = SymbolTable
        self._Symb.update(self.LogicalOperators)
        self._Idx = 0
        self._Len = len(self._Expr)
        self._Token = ''
        self._WarnExcept = None

        # Literal token without any conversion
        self._LiteralToken = ''

        # store the operand object
        self.operanddict = {}
        # The Pcd max value depends on PcdDataType
        self.PcdDataType = PcdDataType
Ejemplo n.º 8
0
def SplitString(String):
    # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
    RetList = []
    InSingleQuote = False
    InDoubleQuote = False
    Item = ''
    for i, ch in enumerate(String):
        if ch == '"' and not InSingleQuote:
            if String[i - 1] != '\\':
                InDoubleQuote = not InDoubleQuote
            if not InDoubleQuote:
                Item += String[i]
                RetList.append(Item)
                Item = ''
                continue
            if Item:
                RetList.append(Item)
                Item = ''
        elif ch == "'" and not InDoubleQuote:
            if String[i - 1] != '\\':
                InSingleQuote = not InSingleQuote
            if not InSingleQuote:
                Item += String[i]
                RetList.append(Item)
                Item = ''
                continue
            if Item:
                RetList.append(Item)
                Item = ''
        Item += String[i]
    if InSingleQuote or InDoubleQuote:
        raise BadExpression(ERR_STRING_TOKEN % Item)
    if Item:
        RetList.append(Item)
    return RetList
Ejemplo n.º 9
0
    def __init__(self, Expression, SymbolTable={}):
        super(ValueExpression, self).__init__(self, Expression, SymbolTable)
        self._NoProcess = False
        if not isinstance(Expression, type('')):
            self._Expr = Expression
            self._NoProcess = True
            return

        self._Expr = ReplaceExprMacro(Expression.strip(), SymbolTable,
                                      SupportedInMacroList)

        if not self._Expr.strip():
            raise BadExpression(ERR_EMPTY_EXPR)

        #
        # The symbol table including PCD and macro mapping
        #
        self._Symb = copy.deepcopy(SymbolTable)
        self._Symb.update(self.LogicalOperators)
        self._Idx = 0
        self._Len = len(self._Expr)
        self._Token = ''
        self._WarnExcept = None

        # Literal token without any conversion
        self._LiteralToken = ''
    def _GetSingleToken(self):
        self.__SkipWS()
        Expr = self._Expr[self._Idx:]
        if Expr.startswith('L"'):
            # Skip L
            self._Idx += 1
            UStr = self.__GetString()
            self._Token = 'L"' + UStr + '"'
            return self._Token

        self._Token = ''
        if Expr:
            Ch = Expr[0]
            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()
                self._Token = Expr[0:Match.end()]
                return self._Token
            elif self.__IsIdChar(Ch):
                return self.__GetIdToken()
            elif Ch == '(' or Ch == ')':
                self._Idx += 1
                self._Token = Ch
                return self._Token

        raise BadExpression(ERR_VALID_TOKEN % Expr)
Ejemplo n.º 11
0
def ReplaceExprMacro(String, Macros, ExceptionList=None):
    StrList = SplitString(String)
    for i, String in enumerate(StrList):
        InQuote = False
        if String.startswith('"'):
            InQuote = True
        MacroStartPos = String.find('$(')
        if MacroStartPos < 0:
            for Pcd in gPlatformPcds:
                if Pcd in String:
                    if Pcd not in gConditionalPcds:
                        gConditionalPcds.append(Pcd)
            continue
        RetStr = ''
        while MacroStartPos >= 0:
            RetStr = String[0:MacroStartPos]
            MacroEndPos = String.find(')', MacroStartPos)
            if MacroEndPos < 0:
                raise BadExpression(ERR_MACRO_TOKEN % String[MacroStartPos:])
            Macro = String[MacroStartPos + 2:MacroEndPos]
            if Macro not in Macros:
                # From C reference manual:
                # If an undefined macro name appears in the constant-expression of
                # !if or !elif, it is replaced by the integer constant 0.
                RetStr += '0'
            elif not InQuote:
                Tklst = RetStr.split()
                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
                # $(ARCH) is replaced with "IA32 X64"
                if ExceptionList and Macro in ExceptionList:
                    RetStr += '"' + Macros[Macro] + '"'
                elif Macros[Macro].strip():
                    RetStr += Macros[Macro]
                else:
                    RetStr += '""'
            else:
                RetStr += Macros[Macro]
            RetStr += String[MacroEndPos + 1:]
            String = RetStr
            MacroStartPos = String.find('$(')
        StrList[i] = RetStr
    return ''.join(StrList)
 def Calculate(self, Operand, DataType, SymbolTable):
     if isinstance(Operand, type('')) and not Operand.isalnum():
         Expr = "EQ ..."
         raise BadExpression(ERR_SNYTAX % Expr)
     rangeId1 = str(uuid.uuid1())
     rangeContainer = RangeContainer()
     rangeContainer.push(RangeObject(int(Operand), int(Operand)))
     SymbolTable[rangeId1] = rangeContainer
     return rangeId1
Ejemplo n.º 13
0
    def Eval(self, Operator, Oprand1, Oprand2 = None):

        if Operator in ["!", "NOT", "not"]:
            if not gGuidPattern.match(Oprand1.strip()):
                raise BadExpression(ERR_STRING_EXPR % Operator)
            return self.NegativeRange(Oprand1)
        else:
            if Operator in ["==", ">=", "<=", ">", "<", '^']:
                return self.EvalRange(Operator, Oprand1)
            elif Operator == 'and' :
                if not gGuidPatternEnd.match(Oprand1.strip()) or not gGuidPatternEnd.match(Oprand2.strip()):
                    raise BadExpression(ERR_STRING_EXPR % Operator)
                return self.Rangeintersection(Oprand1, Oprand2)
            elif Operator == 'or':
                if not gGuidPatternEnd.match(Oprand1.strip()) or not gGuidPatternEnd.match(Oprand2.strip()):
                    raise BadExpression(ERR_STRING_EXPR % Operator)
                return self.Rangecollections(Oprand1, Oprand2)
            else:
                raise BadExpression(ERR_STRING_EXPR % Operator)
Ejemplo n.º 14
0
    def __call__(self, RealValue=False, Depth=0):
        if self._NoProcess:
            return self._Expr

        self._Depth = Depth

        self._Expr = self._Expr.strip()
        if RealValue and Depth == 0:
            self._Token = self._Expr
            if self.__IsNumberToken():
                return self._Expr
            Token = ''
            try:
                Token = self._GetToken()
            except BadExpression:
                pass
            if isinstance(Token, type('')) and Token.startswith(
                    '{') and Token.endswith('}') and self._Idx >= self._Len:
                return self._Expr

            self._Idx = 0
            self._Token = ''

        Val = self._ConExpr()
        RealVal = Val
        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'") and not Val.startswith(
                        "'"):
                Val = True
                RealVal = '"' + RealVal + '"'

        # The expression has been parsed, but the end of expression is not reached
        # It means the rest does not comply EBNF of <Expression>
        if self._Idx != self._Len:
            raise BadExpression(ERR_SNYTAX % self._Expr[self._Idx:])

        if RealValue:
            RetVal = str(RealVal)
        elif Val:
            RetVal = True
        else:
            RetVal = False

        if self._WarnExcept:
            self._WarnExcept.result = RetVal
            raise self._WarnExcept
        else:
            return RetVal
Ejemplo n.º 15
0
def SplitPcdValueString(String):
    # There might be escaped comma in GUID() or DEVICE_PATH() or " "
    # or ' ' or L' ' or L" "
    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(String):
        if ch == '(':
            InParenthesis += 1
        elif ch == ')':
            if InParenthesis:
                InParenthesis -= 1
            else:
                raise BadExpression(ERR_STRING_TOKEN % Item)
        elif ch == '"' and not InSingleQuote:
            if String[i-1] != '\\':
                InDoubleQuote = not InDoubleQuote
        elif ch == "'" and not InDoubleQuote:
            if String[i-1] != '\\':
                InSingleQuote = not InSingleQuote
        elif ch == ',':
            if InParenthesis or InSingleQuote or InDoubleQuote:
                Item += String[i]
                continue
            elif Item:
                RetList.append(Item)
                Item = ''
            continue
        Item += String[i]
    if InSingleQuote or InDoubleQuote or InParenthesis:
        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
Ejemplo n.º 16
0
    def __ResolveToken(self):
        if not self._Token:
            raise BadExpression(ERR_EMPTY_TOKEN)

        # PCD 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 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"}:
            self._Token = False
        elif self._Token in {"TRUE", "true", "True"}:
            self._Token = True
        else:
            self.__IsNumberToken()
Ejemplo n.º 17
0
    def __ResolveToken(self):
        if not self._Token:
            raise BadExpression(ERR_EMPTY_TOKEN)

        # PCD 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 = RangeExpression(self._Symb[self._Token], self._Symb)(True, self._Depth + 1)
            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"]:
            self._Token = False
        elif self._Token in ["TRUE", "true", "True"]:
            self._Token = True
        else:
            self.__IsNumberToken()
 def _NeExpr(self):
     Val = self._RelExpr()
     while self._IsOperator({"!=", "NOT", "not"}):
         Op = self._Token
         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 as Warn:
             self._WarnExcept = Warn
             Val = Warn.result
     return Val
Ejemplo n.º 19
0
def SplitPcdValueString(String):
    # There might be escaped comma in GUID() or DEVICE_PATH() or " "
    # or ' ' or L' ' or L" "
    RetList = []
    InParenthesis = 0
    InSingleQuote = False
    InDoubleQuote = False
    Item = ''
    for i, ch in enumerate(String):
        if ch == '(':
            InParenthesis += 1
        elif ch == ')':
            if InParenthesis:
                InParenthesis -= 1
            else:
                raise BadExpression(ERR_STRING_TOKEN % Item)
        elif ch == '"' and not InSingleQuote:
            if String[i - 1] != '\\':
                InDoubleQuote = not InDoubleQuote
        elif ch == "'" and not InDoubleQuote:
            if String[i - 1] != '\\':
                InSingleQuote = not InSingleQuote
        elif ch == ',':
            if InParenthesis or InSingleQuote or InDoubleQuote:
                Item += String[i]
                continue
            elif Item:
                RetList.append(Item)
                Item = ''
            continue
        Item += String[i]
    if InSingleQuote or InDoubleQuote or InParenthesis:
        raise BadExpression(ERR_STRING_TOKEN % Item)
    if Item:
        RetList.append(Item)
    return RetList
def GetOperatorObject(Operator):
    if Operator == '>':
        return GTOperatorObject()
    elif Operator == '>=':
        return GEOperatorObject()
    elif Operator == '<':
        return LTOperatorObject()
    elif Operator == '<=':
        return LEOperatorObject()
    elif Operator == '==':
        return EQOperatorObject()
    elif Operator == '^':
        return XOROperatorObject()
    else:
        raise BadExpression("Bad Operator")
Ejemplo n.º 21
0
def SplitString(String):
    # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
    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(String):
        if ch == '"' and not InSingleQuote:
            if String[i - 1] != '\\':
                InDoubleQuote = not InDoubleQuote
            if not InDoubleQuote:
                Item += String[i]
                RetList.append(Item)
                Item = ''
                continue
            if Item:
                RetList.append(Item)
                Item = ''
        elif ch == "'" and not InDoubleQuote:
            if String[i - 1] != '\\':
                InSingleQuote = not InSingleQuote
            if not InSingleQuote:
                Item += String[i]
                RetList.append(Item)
                Item = ''
                continue
            if Item:
                RetList.append(Item)
                Item = ''
        Item += String[i]
    if InSingleQuote or InDoubleQuote:
        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
Ejemplo n.º 22
0
    def __call__(self, RealValue=False, Depth=0):
        PcdValue = self.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 isinstance(ItemValue, type('')):
                            ItemValue = int(ItemValue, 0)

                        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 = 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:
                    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
                            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:
                                try:
                                    ItemValue, ItemSize = ParseFieldValue(Item)
                                    LabelOffset = LabelOffset + ItemSize
                                except:
                                    LabelOffset = LabelOffset + 1

                        for Item in PcdValueList:
                            # for LABEL parse
                            Item = Item.strip()
                            try:
                                Item = _ReLabel.sub('', Item)
                            except:
                                pass
                            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:
                                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 RealValue:
            return PcdValue
Ejemplo n.º 23
0
    def Eval(Operator, Oprand1, Oprand2=None):
        WrnExp = None

        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 not isinstance(Oprand1, type('')):
                Oprand1 = IntToStr(Oprand1)
            if not isinstance(Oprand2, type('')):
                Oprand2 = IntToStr(Oprand2)
        TypeDict = {
            type(0): 0,
            # For python2 long type
            type(sys.maxsize + 1): 0,
            type(''): 1,
            type(True): 2
        }

        EvalStr = ''
        if Operator in {"!", "NOT", "not"}:
            if isinstance(Oprand1, type('')):
                raise BadExpression(ERR_STRING_EXPR % Operator)
            EvalStr = 'not Oprand1'
        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)
            }):
                # Boolean in '+'/'-' will be evaluated but raise warning
                WrnExp = WrnExpression(WRN_BOOL_EXPR)
            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)
                    WrnExp.result = False
                    raise WrnExp
                elif Operator == "!=":
                    WrnExp = WrnExpression(WRN_NECMP_STR_OTHERS)
                    WrnExp.result = True
                    raise WrnExp
                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)])):
                    # 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)])):
                    # bitwise and logical operation between number and boolean is allowed
                    pass
                else:
                    raise BadExpression(ERR_EXPR_TYPE)
            if isinstance(Oprand1, type('')) and isinstance(Oprand2, type('')):
                if (Oprand1.startswith('L"') and not Oprand2.startswith('L"')) or \
                    (not Oprand1.startswith('L"') and Oprand2.startswith('L"')):
                    raise BadExpression(ERR_STRING_CMP %
                                        (Oprand1, Operator, Oprand2))
            if 'in' in Operator and isinstance(Oprand2, type('')):
                Oprand2 = Oprand2.split()
            EvalStr = 'Oprand1 ' + Operator + ' Oprand2'

        # Local symbols used by built in eval function
        Dict = {'Oprand1': Oprand1, 'Oprand2': Oprand2}
        try:
            Val = eval(EvalStr, {}, Dict)
        except Exception as Excpt:
            raise BadExpression(str(Excpt))

        if Operator in {'and', 'or'}:
            if Val:
                Val = True
            else:
                Val = False

        if WrnExp:
            WrnExp.result = Val
            raise WrnExp
        return Val