[edk2] [Patch] BaseTools: Enhance expression to support some more operation

Yonghong Zhu posted 1 patch 7 years, 7 months ago
Failed in applying to current master (apply log)
BaseTools/Source/Python/Common/Expression.py | 28 +++++++++++++++++++++++-----
1 file changed, 23 insertions(+), 5 deletions(-)
[edk2] [Patch] BaseTools: Enhance expression to support some more operation
Posted by Yonghong Zhu 7 years, 7 months ago
Enhance expression to support some more operation that allowed in the
spec, eg: *, /, %, <<, >>, ~.

Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Yonghong Zhu <yonghong.zhu@intel.com>
---
 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.<BR>
+# Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
 # 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

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [Patch] BaseTools: Enhance expression to support some more operation
Posted by Gao, Liming 7 years, 7 months ago
Reviewed-by: Liming Gao <liming.gao@intel.com>

>-----Original Message-----
>From: Zhu, Yonghong
>Sent: Friday, March 31, 2017 10:22 PM
>To: edk2-devel@lists.01.org
>Cc: Gao, Liming <liming.gao@intel.com>
>Subject: [Patch] BaseTools: Enhance expression to support some more
>operation
>
>Enhance expression to support some more operation that allowed in the
>spec, eg: *, /, %, <<, >>, ~.
>
>Cc: Liming Gao <liming.gao@intel.com>
>Contributed-under: TianoCore Contribution Agreement 1.0
>Signed-off-by: Yonghong Zhu <yonghong.zhu@intel.com>
>---
> 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.<BR>
>+# Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
> # 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

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel