From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id E0F9A21DFA7B1 for ; Fri, 31 Mar 2017 07:21:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intel.com; i=@intel.com; q=dns/txt; s=intel; t=1490970102; x=1522506102; h=from:to:cc:subject:date:message-id; bh=wG88ETBHGuxedRGg+6X4XGeIeKZq9UcUZLbe4cmcfQE=; b=jIKtA8fJnpRFqVSqfdbBanWBS08ecn4+LXWyAfAIpBD6glsjZdX6Z/OW jVbBPDYWo3HJKH8Xq3FeEwuEI5q2Mg==; Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 Mar 2017 07:21:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.36,252,1486454400"; d="scan'208";a="82730048" Received: from shwdeopenpsi168.ccr.corp.intel.com ([10.239.158.121]) by fmsmga005.fm.intel.com with ESMTP; 31 Mar 2017 07:21:41 -0700 From: Yonghong Zhu To: edk2-devel@lists.01.org Cc: Liming Gao Date: Fri, 31 Mar 2017 22:21:38 +0800 Message-Id: <1490970098-52308-1-git-send-email-yonghong.zhu@intel.com> X-Mailer: git-send-email 2.6.1.windows.1 Subject: [Patch] BaseTools: Enhance expression to support some more operation X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 31 Mar 2017 14:21:43 -0000 Enhance expression to support some more operation that allowed in the spec, eg: *, /, %, <<, >>, ~. Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Yonghong Zhu --- BaseTools/Source/Python/Common/Expression.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py index 7b3030c..6d002f5 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -1,9 +1,9 @@ ## @file # This file is used to parse and evaluate expression in directive or PCD value. # -# Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.
+# Copyright (c) 2011 - 2017, 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 # @@ -127,11 +127,11 @@ class ValueExpression(object): 'GT' : '>' , 'LT' : '<', 'GE' : '>=' , 'LE' : '<=', 'IN' : 'in' } - NonLetterOpLst = ['+', '-', '&', '|', '^', '!', '=', '>', '<'] + 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}') @@ -160,10 +160,14 @@ class ValueExpression(object): EvalStr = '' if Operator in ["!", "NOT", "not"]: if type(Oprand1) == type(''): raise BadExpression(ERR_STRING_EXPR % Operator) EvalStr = 'not Oprand1' + elif Operator in ["~"]: + if type(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 type(Oprand1)!= type(Oprand2): @@ -351,25 +355,39 @@ class ValueExpression(object): Val = Warn.result return Val # A [ > B]* def _RelExpr(self): - return self._ExprFuncTemplate(self._AddExpr, ["<=", ">=", "<", ">", "LE", "GE", "LT", "GT"]) + return self._ExprFuncTemplate(self._ShiftExpr, ["<=", ">=", "<", ">", "LE", "GE", "LT", "GT"]) + + def _ShiftExpr(self): + return self._ExprFuncTemplate(self._AddExpr, ["<<", ">>"]) # A [ + B]* def _AddExpr(self): - return self._ExprFuncTemplate(self._UnaryExpr, ["+", "-"]) + return self._ExprFuncTemplate(self._MulExpr, ["+", "-"]) + + # A [ * B]* + def _MulExpr(self): + return self._ExprFuncTemplate(self._UnaryExpr, ["*", "/", "%"]) # [!]*A def _UnaryExpr(self): if self._IsOperator(["!", "NOT", "not"]): Val = self._UnaryExpr() try: return self.Eval('not', Val) except WrnExpression, Warn: self._WarnExcept = Warn return Warn.result + if self._IsOperator(["~"]): + Val = self._UnaryExpr() + try: + return self.Eval('~', Val) + except WrnExpression, Warn: + self._WarnExcept = Warn + return Warn.result return self._IdenExpr() # Parse identifier or encapsulated expression def _IdenExpr(self): Tk = self._GetToken() @@ -535,11 +553,11 @@ class ValueExpression(object): def _GetToken(self): return self.__GetNList() @staticmethod def __IsIdChar(Ch): - return Ch in '._/:' or Ch.isalnum() + return Ch in '._:' or Ch.isalnum() # Parse operand def _GetSingleToken(self): self.__SkipWS() Expr = self._Expr[self._Idx:] -- 2.6.1.windows.1