2 # This file is used to parse and evaluate expression in directive or PCD value.
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 from Common
.GlobalData
import *
16 from CommonDataClass
.Exceptions
import BadExpression
17 from CommonDataClass
.Exceptions
import WrnExpression
18 from Misc
import GuidStringToGuidStructureString
, ParseFieldValue
, IsFieldValueAnArray
19 import Common
.EdkLogger
as EdkLogger
21 from Common
.DataType
import *
23 ERR_STRING_EXPR
= 'This operator cannot be used in string expression: [%s].'
24 ERR_SNYTAX
= 'Syntax error, the rest of expression cannot be evaluated: [%s].'
25 ERR_MATCH
= 'No matching right parenthesis.'
26 ERR_STRING_TOKEN
= 'Bad string token: [%s].'
27 ERR_MACRO_TOKEN
= 'Bad macro token: [%s].'
28 ERR_EMPTY_TOKEN
= 'Empty token is not allowed.'
29 ERR_PCD_RESOLVE
= 'PCD token cannot be resolved: [%s].'
30 ERR_VALID_TOKEN
= 'No more valid token found from rest of string: [%s].'
31 ERR_EXPR_TYPE
= 'Different types found in expression.'
32 ERR_OPERATOR_UNSUPPORT
= 'Unsupported operator: [%s]'
33 ERR_REL_NOT_IN
= 'Expect "IN" after "not" operator.'
34 WRN_BOOL_EXPR
= 'Operand of boolean type cannot be used in arithmetic expression.'
35 WRN_EQCMP_STR_OTHERS
= '== Comparison between Operand of string type and Boolean/Number Type always return False.'
36 WRN_NECMP_STR_OTHERS
= '!= Comparison between Operand of string type and Boolean/Number Type always return True.'
37 ERR_RELCMP_STR_OTHERS
= 'Operator taking Operand of string type and Boolean/Number Type is not allowed: [%s].'
38 ERR_STRING_CMP
= 'Unicode string and general string cannot be compared: [%s %s %s]'
39 ERR_ARRAY_TOKEN
= 'Bad C array or C format GUID token: [%s].'
40 ERR_ARRAY_ELE
= 'This must be HEX value for NList or Array: [%s].'
41 ERR_EMPTY_EXPR
= 'Empty expression is not allowed.'
42 ERR_IN_OPERAND
= 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).'
44 __ValidString
= re
.compile(r
'[_a-zA-Z][_0-9a-zA-Z]*$')
45 _ReLabel
= re
.compile('LABEL\((\w+)\)')
46 _ReOffset
= re
.compile('OFFSET_OF\((\w+)\)')
47 PcdPattern
= re
.compile(r
'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
50 # Split string to list according double quote
51 # For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']
53 def SplitString(String
):
54 # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
59 for i
, ch
in enumerate(String
):
60 if ch
== '"' and not InSingleQuote
:
61 if String
[i
- 1] != '\\':
62 InDoubleQuote
= not InDoubleQuote
71 elif ch
== "'" and not InDoubleQuote
:
72 if String
[i
- 1] != '\\':
73 InSingleQuote
= not InSingleQuote
83 if InSingleQuote
or InDoubleQuote
:
84 raise BadExpression(ERR_STRING_TOKEN
% Item
)
89 def SplitPcdValueString(String
):
90 # There might be escaped comma in GUID() or DEVICE_PATH() or " "
91 # or ' ' or L' ' or L" "
97 for i
, ch
in enumerate(String
):
104 raise BadExpression(ERR_STRING_TOKEN
% Item
)
105 elif ch
== '"' and not InSingleQuote
:
106 if String
[i
-1] != '\\':
107 InDoubleQuote
= not InDoubleQuote
108 elif ch
== "'" and not InDoubleQuote
:
109 if String
[i
-1] != '\\':
110 InSingleQuote
= not InSingleQuote
112 if InParenthesis
or InSingleQuote
or InDoubleQuote
:
120 if InSingleQuote
or InDoubleQuote
or InParenthesis
:
121 raise BadExpression(ERR_STRING_TOKEN
% Item
)
126 def IsValidCName(Str
):
127 return True if __ValidString
.match(Str
) else False
129 def BuildOptionValue(PcdValue
, GuidDict
):
130 if PcdValue
.startswith('H'):
131 InputValue
= PcdValue
[1:]
132 elif PcdValue
.startswith("L'") or PcdValue
.startswith("'"):
133 InputValue
= PcdValue
134 elif PcdValue
.startswith('L'):
135 InputValue
= 'L"' + PcdValue
[1:] + '"'
137 InputValue
= PcdValue
138 if IsFieldValueAnArray(InputValue
):
140 PcdValue
= ValueExpressionEx(InputValue
, TAB_VOID
, GuidDict
)(True)
147 def ReplaceExprMacro(String
, Macros
, ExceptionList
= None):
148 StrList
= SplitString(String
)
149 for i
, String
in enumerate(StrList
):
151 if String
.startswith('"'):
153 MacroStartPos
= String
.find('$(')
154 if MacroStartPos
< 0:
155 for Pcd
in gPlatformPcds
:
157 if Pcd
not in gConditionalPcds
:
158 gConditionalPcds
.append(Pcd
)
161 while MacroStartPos
>= 0:
162 RetStr
= String
[0:MacroStartPos
]
163 MacroEndPos
= String
.find(')', MacroStartPos
)
165 raise BadExpression(ERR_MACRO_TOKEN
% String
[MacroStartPos
:])
166 Macro
= String
[MacroStartPos
+2:MacroEndPos
]
167 if Macro
not in Macros
:
168 # From C reference manual:
169 # If an undefined macro name appears in the constant-expression of
170 # !if or !elif, it is replaced by the integer constant 0.
173 Tklst
= RetStr
.split()
174 if Tklst
and Tklst
[-1] in ['IN', 'in'] and ExceptionList
and Macro
not in ExceptionList
:
175 raise BadExpression(ERR_IN_OPERAND
)
176 # Make sure the macro in exception list is encapsulated by double quote
177 # For example: DEFINE ARCH = IA32 X64
178 # $(ARCH) is replaced with "IA32 X64"
179 if ExceptionList
and Macro
in ExceptionList
:
180 RetStr
+= '"' + Macros
[Macro
] + '"'
181 elif Macros
[Macro
].strip():
182 RetStr
+= Macros
[Macro
]
186 RetStr
+= Macros
[Macro
]
187 RetStr
+= String
[MacroEndPos
+1:]
189 MacroStartPos
= String
.find('$(')
191 return ''.join(StrList
)
193 # transfer int to string for in/not in expression
197 StrList
.append(chr(Value
& 0xff))
199 Value
= '"' + ''.join(StrList
) + '"'
202 SupportedInMacroList
= ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY']
204 class ValueExpression(object):
205 # Logical operator mapping
207 '&&' : 'and', '||' : 'or',
208 '!' : 'not', 'AND': 'and',
209 'OR' : 'or' , 'NOT': 'not',
210 'XOR': '^' , 'xor': '^',
211 'EQ' : '==' , 'NE' : '!=',
212 'GT' : '>' , 'LT' : '<',
213 'GE' : '>=' , 'LE' : '<=',
217 NonLetterOpLst
= ['+', '-', '*', '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':']
220 SymbolPattern
= re
.compile("("
221 "\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|"
223 "(?<=\W)AND(?=\W)|(?<=\W)OR(?=\W)|(?<=\W)NOT(?=\W)|(?<=\W)XOR(?=\W)|"
224 "(?<=\W)EQ(?=\W)|(?<=\W)NE(?=\W)|(?<=\W)GT(?=\W)|(?<=\W)LT(?=\W)|(?<=\W)GE(?=\W)|(?<=\W)LE(?=\W)"
228 def Eval(Operator
, Oprand1
, Oprand2
= None):
231 if Operator
not in ["==", "!=", ">=", "<=", ">", "<", "in", "not in"] and \
232 (type(Oprand1
) == type('') or type(Oprand2
) == type('')):
233 raise BadExpression(ERR_STRING_EXPR
% Operator
)
234 if Operator
in ['in', 'not in']:
235 if type(Oprand1
) != type(''):
236 Oprand1
= IntToStr(Oprand1
)
237 if type(Oprand2
) != type(''):
238 Oprand2
= IntToStr(Oprand2
)
247 if Operator
in ["!", "NOT", "not"]:
248 if type(Oprand1
) == type(''):
249 raise BadExpression(ERR_STRING_EXPR
% Operator
)
250 EvalStr
= 'not Oprand1'
251 elif Operator
in ["~"]:
252 if type(Oprand1
) == type(''):
253 raise BadExpression(ERR_STRING_EXPR
% Operator
)
254 EvalStr
= '~ Oprand1'
256 if Operator
in ["+", "-"] and (type(True) in [type(Oprand1
), type(Oprand2
)]):
257 # Boolean in '+'/'-' will be evaluated but raise warning
258 WrnExp
= WrnExpression(WRN_BOOL_EXPR
)
259 elif type('') in [type(Oprand1
), type(Oprand2
)] and type(Oprand1
)!= type(Oprand2
):
260 # == between string and number/boolean will always return False, != return True
262 WrnExp
= WrnExpression(WRN_EQCMP_STR_OTHERS
)
263 WrnExp
.result
= False
265 elif Operator
== "!=":
266 WrnExp
= WrnExpression(WRN_NECMP_STR_OTHERS
)
270 raise BadExpression(ERR_RELCMP_STR_OTHERS
% Operator
)
271 elif TypeDict
[type(Oprand1
)] != TypeDict
[type(Oprand2
)]:
272 if Operator
in ["==", "!=", ">=", "<=", ">", "<"] and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
273 # comparison between number and boolean is allowed
275 elif Operator
in ['&', '|', '^', "and", "or"] and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
276 # bitwise and logical operation between number and boolean is allowed
279 raise BadExpression(ERR_EXPR_TYPE
)
280 if type(Oprand1
) == type('') and type(Oprand2
) == type(''):
281 if (Oprand1
.startswith('L"') and not Oprand2
.startswith('L"')) or \
282 (not Oprand1
.startswith('L"') and Oprand2
.startswith('L"')):
283 raise BadExpression(ERR_STRING_CMP
% (Oprand1
, Operator
, Oprand2
))
284 if 'in' in Operator
and type(Oprand2
) == type(''):
285 Oprand2
= Oprand2
.split()
286 EvalStr
= 'Oprand1 ' + Operator
+ ' Oprand2'
288 # Local symbols used by built in eval function
294 Val
= eval(EvalStr
, {}, Dict
)
295 except Exception, Excpt
:
296 raise BadExpression(str(Excpt
))
298 if Operator
in ['and', 'or']:
309 def __init__(self
, Expression
, SymbolTable
={}):
310 self
._NoProcess
= False
311 if type(Expression
) != type(''):
312 self
._Expr
= Expression
313 self
._NoProcess
= True
316 self
._Expr
= ReplaceExprMacro(Expression
.strip(),
318 SupportedInMacroList
)
320 if not self
._Expr
.strip():
321 raise BadExpression(ERR_EMPTY_EXPR
)
324 # The symbol table including PCD and macro mapping
326 self
._Symb
= copy
.deepcopy(SymbolTable
)
327 self
._Symb
.update(self
.LogicalOperators
)
329 self
._Len
= len(self
._Expr
)
331 self
._WarnExcept
= None
333 # Literal token without any conversion
334 self
._LiteralToken
= ''
336 # Public entry for this class
337 # @param RealValue: False: only evaluate if the expression is true or false, used for conditional expression
338 # True : return the evaluated str(value), used for PCD value
340 # @return: True or False if RealValue is False
341 # Evaluated value of string format if RealValue is True
343 def __call__(self
, RealValue
=False, Depth
=0):
349 self
._Expr
= self
._Expr
.strip()
350 if RealValue
and Depth
== 0:
351 self
._Token
= self
._Expr
352 if self
.__IsNumberToken
():
356 Token
= self
._GetToken
()
357 except BadExpression
:
359 if type(Token
) == type('') and Token
.startswith('{') and Token
.endswith('}') and self
._Idx
>= self
._Len
:
365 Val
= self
._ConExpr
()
367 if type(Val
) == type(''):
373 elif not Val
.startswith('L"') and not Val
.startswith('{') and not Val
.startswith("L'"):
375 RealVal
= '"' + RealVal
+ '"'
377 # The expression has been parsed, but the end of expression is not reached
378 # It means the rest does not comply EBNF of <Expression>
379 if self
._Idx
!= self
._Len
:
380 raise BadExpression(ERR_SNYTAX
% self
._Expr
[self
._Idx
:])
383 RetVal
= str(RealVal
)
390 self
._WarnExcept
.result
= RetVal
391 raise self
._WarnExcept
395 # Template function to parse binary operators which have same precedence
396 # Expr [Operator Expr]*
397 def _ExprFuncTemplate(self
, EvalFunc
, OpLst
):
399 while self
._IsOperator
(OpLst
):
403 if self
._IsOperator
(':'):
411 Val
= self
.Eval(Op
, Val
, EvalFunc())
412 except WrnExpression
, Warn
:
413 self
._WarnExcept
= Warn
418 return self
._ExprFuncTemplate
(self
._OrExpr
, ['?', ':'])
422 return self
._ExprFuncTemplate
(self
._AndExpr
, ["OR", "or", "||"])
426 return self
._ExprFuncTemplate
(self
._BitOr
, ["AND", "and", "&&"])
430 return self
._ExprFuncTemplate
(self
._BitXor
, ["|"])
434 return self
._ExprFuncTemplate
(self
._BitAnd
, ["XOR", "xor", "^"])
438 return self
._ExprFuncTemplate
(self
._EqExpr
, ["&"])
442 Val
= self
._RelExpr
()
443 while self
._IsOperator
(["==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"]):
445 if Op
in ["!", "NOT", "not"]:
446 if not self
._IsOperator
(["IN", "in"]):
447 raise BadExpression(ERR_REL_NOT_IN
)
448 Op
+= ' ' + self
._Token
450 Val
= self
.Eval(Op
, Val
, self
._RelExpr
())
451 except WrnExpression
, Warn
:
452 self
._WarnExcept
= Warn
458 return self
._ExprFuncTemplate
(self
._ShiftExpr
, ["<=", ">=", "<", ">", "LE", "GE", "LT", "GT"])
460 def _ShiftExpr(self
):
461 return self
._ExprFuncTemplate
(self
._AddExpr
, ["<<", ">>"])
465 return self
._ExprFuncTemplate
(self
._MulExpr
, ["+", "-"])
469 return self
._ExprFuncTemplate
(self
._UnaryExpr
, ["*", "/", "%"])
472 def _UnaryExpr(self
):
473 if self
._IsOperator
(["!", "NOT", "not"]):
474 Val
= self
._UnaryExpr
()
476 return self
.Eval('not', Val
)
477 except WrnExpression
, Warn
:
478 self
._WarnExcept
= Warn
480 if self
._IsOperator
(["~"]):
481 Val
= self
._UnaryExpr
()
483 return self
.Eval('~', Val
)
484 except WrnExpression
, Warn
:
485 self
._WarnExcept
= Warn
487 return self
._IdenExpr
()
489 # Parse identifier or encapsulated expression
491 Tk
= self
._GetToken
()
493 Val
= self
._ConExpr
()
495 # _GetToken may also raise BadExpression
496 if self
._GetToken
() != ')':
497 raise BadExpression(ERR_MATCH
)
498 except BadExpression
:
499 raise BadExpression(ERR_MATCH
)
503 # Skip whitespace or tab
505 for Char
in self
._Expr
[self
._Idx
:]:
506 if Char
not in ' \t':
510 # Try to convert string to number
511 def __IsNumberToken(self
):
513 if self
._Token
.lower()[0:2] == '0x' and len(self
._Token
) > 2:
515 if self
._Token
.startswith('"') or self
._Token
.startswith('L"'):
517 for Index
in range(len(self
._Token
)):
518 if self
._Token
[Index
] in ['"']:
519 if self
._Token
[Index
- 1] == '\\':
522 if Flag
== 2 and self
._Token
.endswith('"'):
524 if self
._Token
.startswith("'") or self
._Token
.startswith("L'"):
526 for Index
in range(len(self
._Token
)):
527 if self
._Token
[Index
] in ["'"]:
528 if self
._Token
[Index
- 1] == '\\':
531 if Flag
== 2 and self
._Token
.endswith("'"):
534 self
._Token
= int(self
._Token
, Radix
)
542 def __GetArray(self
):
545 self
.__GetNList
(True)
546 Token
+= self
._LiteralToken
547 if self
._Idx
>= self
._Len
or self
._Expr
[self
._Idx
] != '}':
548 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
551 # All whitespace and tabs in array are already stripped.
552 IsArray
= IsGuid
= False
553 if len(Token
.split(',')) == 11 and len(Token
.split(',{')) == 2 \
554 and len(Token
.split('},')) == 1:
555 HexLen
= [11,6,6,5,4,4,4,4,4,4,6]
556 HexList
= Token
.split(',')
557 if HexList
[3].startswith('{') and \
558 not [Index
for Index
, Hex
in enumerate(HexList
) if len(Hex
) > HexLen
[Index
]]:
560 if Token
.lstrip('{').rstrip('}').find('{') == -1:
561 if not [Hex
for Hex
in Token
.lstrip('{').rstrip('}').split(',') if len(Hex
) > 4]:
563 if not IsArray
and not IsGuid
:
564 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
566 self
._Token
= self
._LiteralToken
= Token
569 # Parse string, the format must be: "..."
570 def __GetString(self
):
576 # Replace escape \\\", \"
577 if self
._Expr
[Idx
] == '"':
578 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace('\\\"', '\\\'')
583 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
584 if not self
._Token
.endswith('"'):
585 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
586 #Replace escape \\\', \'
587 elif self
._Expr
[Idx
] == "'":
588 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace("\\\'", "\\\"")
593 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
594 if not self
._Token
.endswith("'"):
595 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
596 self
._Token
= self
._Token
[1:-1]
599 # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
600 # @param IsAlphaOp: Indicate if parsing general token or script operator(EQ, NE...)
601 def __GetIdToken(self
, IsAlphaOp
= False):
603 for Ch
in self
._Expr
[self
._Idx
:]:
604 if not self
.__IsIdChar
(Ch
) or ('?' in self
._Expr
and Ch
== ':'):
609 self
._Token
= self
._LiteralToken
= IdToken
611 self
.__ResolveToken
()
614 # Try to resolve token
615 def __ResolveToken(self
):
617 raise BadExpression(ERR_EMPTY_TOKEN
)
620 if PcdPattern
.match(self
._Token
):
621 if self
._Token
not in self
._Symb
:
622 Ex
= BadExpression(ERR_PCD_RESOLVE
% self
._Token
)
625 self
._Token
= ValueExpression(self
._Symb
[self
._Token
], self
._Symb
)(True, self
._Depth
+1)
626 if type(self
._Token
) != type(''):
627 self
._LiteralToken
= hex(self
._Token
)
630 if self
._Token
.startswith('"'):
631 self
._Token
= self
._Token
[1:-1]
632 elif self
._Token
in ["FALSE", "false", "False"]:
634 elif self
._Token
in ["TRUE", "true", "True"]:
637 self
.__IsNumberToken
()
639 def __GetNList(self
, InArray
=False):
640 self
._GetSingleToken
()
641 if not self
.__IsHexLiteral
():
643 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
647 Expr
= self
._Expr
[self
._Idx
:]
648 if not Expr
.startswith(','):
651 NList
= self
._LiteralToken
652 while Expr
.startswith(','):
656 self
._GetSingleToken
()
657 if not self
.__IsHexLiteral
():
658 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
659 NList
+= self
._LiteralToken
661 Expr
= self
._Expr
[self
._Idx
:]
662 self
._Token
= self
._LiteralToken
= NList
665 def __IsHexLiteral(self
):
666 if self
._LiteralToken
.startswith('{') and \
667 self
._LiteralToken
.endswith('}'):
670 if gHexPattern
.match(self
._LiteralToken
):
671 Token
= self
._LiteralToken
[2:]
673 self
._LiteralToken
= '0x0'
675 self
._LiteralToken
= '0x' + Token
680 return self
.__GetNList
()
684 return Ch
in '._:' or Ch
.isalnum()
687 def _GetSingleToken(self
):
689 Expr
= self
._Expr
[self
._Idx
:]
690 if Expr
.startswith('L"'):
693 UStr
= self
.__GetString
()
694 self
._Token
= 'L"' + UStr
+ '"'
696 elif Expr
.startswith("L'"):
699 UStr
= self
.__GetString
()
700 self
._Token
= "L'" + UStr
+ "'"
702 elif Expr
.startswith("'"):
703 UStr
= self
.__GetString
()
704 self
._Token
= "'" + UStr
+ "'"
706 elif Expr
.startswith('UINT'):
707 Re
= re
.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)')
709 RetValue
= Re
.search(Expr
).group(1)
711 raise BadExpression('Invalid Expression %s' % Expr
)
716 Prefix
= self
._Expr
[Idx
:self
._Idx
- 1]
719 TmpValue
= self
._Expr
[Idx
:self
._Idx
- 1]
720 TmpValue
= ValueExpression(TmpValue
)(True)
721 TmpValue
= '0x%x' % int(TmpValue
) if type(TmpValue
) != type('') else TmpValue
723 self
._Token
, Size
= ParseFieldValue(Prefix
+ '(' + TmpValue
+ ')')
729 Match
= gGuidPattern
.match(Expr
)
730 if Match
and not Expr
[Match
.end():Match
.end()+1].isalnum() \
731 and Expr
[Match
.end():Match
.end()+1] != '_':
732 self
._Idx
+= Match
.end()
733 self
._Token
= ValueExpression(GuidStringToGuidStructureString(Expr
[0:Match
.end()]))(True, self
._Depth
+1)
735 elif self
.__IsIdChar
(Ch
):
736 return self
.__GetIdToken
()
738 return self
.__GetString
()
740 return self
.__GetArray
()
741 elif Ch
== '(' or Ch
== ')':
746 raise BadExpression(ERR_VALID_TOKEN
% Expr
)
749 def _GetOperator(self
):
751 LegalOpLst
= ['&&', '||', '!=', '==', '>=', '<='] + self
.NonLetterOpLst
+ ['?',':']
754 Expr
= self
._Expr
[self
._Idx
:]
756 # Reach end of expression
760 # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
761 if Expr
[0].isalpha():
762 return self
.__GetIdToken
(True)
764 # Start to get regular operator: +, -, <, > ...
765 if Expr
[0] not in self
.NonLetterOpLst
:
770 if Ch
in self
.NonLetterOpLst
:
771 if '!' == Ch
and OpToken
:
778 if OpToken
not in LegalOpLst
:
779 raise BadExpression(ERR_OPERATOR_UNSUPPORT
% OpToken
)
780 self
._Token
= OpToken
783 # Check if current token matches the operators given from OpList
784 def _IsOperator(self
, OpList
):
787 if self
._Token
in OpList
:
788 if self
._Token
in self
.LogicalOperators
:
789 self
._Token
= self
.LogicalOperators
[self
._Token
]
794 class ValueExpressionEx(ValueExpression
):
795 def __init__(self
, PcdValue
, PcdType
, SymbolTable
={}):
796 ValueExpression
.__init
__(self
, PcdValue
, SymbolTable
)
797 self
.PcdValue
= PcdValue
798 self
.PcdType
= PcdType
800 def __call__(self
, RealValue
=False, Depth
=0):
801 PcdValue
= self
.PcdValue
803 PcdValue
= ValueExpression
.__call
__(self
, RealValue
, Depth
)
804 if self
.PcdType
== TAB_VOID
and (PcdValue
.startswith("'") or PcdValue
.startswith("L'")):
805 PcdValue
, Size
= ParseFieldValue(PcdValue
)
807 for I
in range(Size
):
808 PcdValueList
.append('0x%02X'%(PcdValue
& 0xff))
809 PcdValue
= PcdValue
>> 8
810 PcdValue
= '{' + ','.join(PcdValueList
) + '}'
811 elif self
.PcdType
in TAB_PCD_NUMERIC_TYPES
and (PcdValue
.startswith("'") or \
812 PcdValue
.startswith('"') or PcdValue
.startswith("L'") or PcdValue
.startswith('L"') or PcdValue
.startswith('{')):
814 except WrnExpression
, Value
:
815 PcdValue
= Value
.result
816 except BadExpression
, Value
:
817 if self
.PcdType
in TAB_PCD_NUMERIC_TYPES
:
818 PcdValue
= PcdValue
.strip()
819 if PcdValue
.startswith('{') and PcdValue
.endswith('}'):
820 PcdValue
= SplitPcdValueString(PcdValue
[1:-1])
821 if type(PcdValue
) == type([]):
825 for Item
in PcdValue
:
827 if Item
.startswith(TAB_UINT8
):
829 ValueType
= TAB_UINT8
830 elif Item
.startswith(TAB_UINT16
):
832 ValueType
= TAB_UINT16
833 elif Item
.startswith(TAB_UINT32
):
835 ValueType
= TAB_UINT32
836 elif Item
.startswith(TAB_UINT64
):
838 ValueType
= TAB_UINT64
839 elif Item
[0] in ['"',"'",'L']:
844 ValueType
= TAB_UINT8
845 Item
= ValueExpressionEx(Item
, ValueType
, self
._Symb
)(True)
849 tmpValue
= int(Item
, 0)
851 raise BadExpression("Byte array number %s should less than 0xFF." % Item
)
852 except BadExpression
, Value
:
853 raise BadExpression(Value
)
856 ItemValue
, ItemSize
= ParseFieldValue(Item
)
858 ItemValue
= ParseFieldValue(Item
)[0]
860 if type(ItemValue
) == type(''):
861 ItemValue
= int(ItemValue
, 0)
863 TmpValue
= (ItemValue
<< (Size
* 8)) | TmpValue
864 Size
= Size
+ ItemSize
867 TmpValue
, Size
= ParseFieldValue(PcdValue
)
868 except BadExpression
, Value
:
869 raise BadExpression("Type: %s, Value: %s, %s" % (self
.PcdType
, PcdValue
, Value
))
870 if type(TmpValue
) == type(''):
872 TmpValue
= int(TmpValue
)
874 raise BadExpression(Value
)
876 PcdValue
= '0x%0{}X'.format(Size
) % (TmpValue
)
878 raise BadExpression('Type %s PCD Value is negative' % self
.PcdType
)
879 if self
.PcdType
== TAB_UINT8
and Size
> 1:
880 raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self
.PcdType
)
881 if self
.PcdType
== TAB_UINT16
and Size
> 2:
882 raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self
.PcdType
)
883 if self
.PcdType
== TAB_UINT32
and Size
> 4:
884 raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self
.PcdType
)
885 if self
.PcdType
== TAB_UINT64
and Size
> 8:
886 raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self
.PcdType
)
889 TmpValue
= long(PcdValue
)
891 if TmpValue
.bit_length() == 0:
894 for I
in range((TmpValue
.bit_length() + 7) / 8):
895 TmpList
.append('0x%02x' % ((TmpValue
>> I
* 8) & 0xff))
896 PcdValue
= '{' + ', '.join(TmpList
) + '}'
898 if PcdValue
.strip().startswith('{'):
899 PcdValueList
= SplitPcdValueString(PcdValue
.strip()[1:-1])
903 for Item
in PcdValueList
:
904 # compute byte offset of every LABEL
905 LabelList
= _ReLabel
.findall(Item
)
906 Item
= _ReLabel
.sub('', Item
)
909 for Label
in LabelList
:
910 if not IsValidCName(Label
):
911 raise BadExpression('%s is not a valid c variable name' % Label
)
912 if Label
not in LabelDict
:
913 LabelDict
[Label
] = str(LabelOffset
)
914 if Item
.startswith(TAB_UINT8
):
915 LabelOffset
= LabelOffset
+ 1
916 elif Item
.startswith(TAB_UINT16
):
917 LabelOffset
= LabelOffset
+ 2
918 elif Item
.startswith(TAB_UINT32
):
919 LabelOffset
= LabelOffset
+ 4
920 elif Item
.startswith(TAB_UINT64
):
921 LabelOffset
= LabelOffset
+ 8
924 ItemValue
, ItemSize
= ParseFieldValue(Item
)
925 LabelOffset
= LabelOffset
+ ItemSize
927 LabelOffset
= LabelOffset
+ 1
929 for Item
in PcdValueList
:
933 Item
= _ReLabel
.sub('', Item
)
937 OffsetList
= _ReOffset
.findall(Item
)
940 # replace each offset, except errors
941 for Offset
in OffsetList
:
943 Item
= Item
.replace('OFFSET_OF({})'.format(Offset
),LabelDict
[Offset
])
945 raise BadExpression('%s not defined' % Offset
)
947 NewPcdValueList
.append(Item
)
950 for Item
in NewPcdValueList
:
953 TokenSpaceGuidName
= ''
954 if Item
.startswith('GUID') and Item
.endswith(')'):
956 TokenSpaceGuidName
= re
.search('GUID\((\w+)\)', Item
).group(1)
959 if TokenSpaceGuidName
and TokenSpaceGuidName
in self
._Symb
:
960 Item
= 'GUID(' + self
._Symb
[TokenSpaceGuidName
] + ')'
961 elif TokenSpaceGuidName
:
962 raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName
)
963 Item
, Size
= ParseFieldValue(Item
)
964 for Index
in range(0, Size
):
965 ValueStr
= '0x%02X' % (int(Item
) & 255)
967 AllPcdValueList
.append(ValueStr
)
969 elif Item
.startswith('DEVICE_PATH') and Item
.endswith(')'):
970 Item
, Size
= ParseFieldValue(Item
)
971 AllPcdValueList
.append(Item
[1:-1])
975 if Item
.startswith(TAB_UINT8
):
977 ValueType
= TAB_UINT8
978 elif Item
.startswith(TAB_UINT16
):
980 ValueType
= TAB_UINT16
981 elif Item
.startswith(TAB_UINT32
):
983 ValueType
= TAB_UINT32
984 elif Item
.startswith(TAB_UINT64
):
986 ValueType
= TAB_UINT64
990 TmpValue
= ValueExpressionEx(Item
, ValueType
, self
._Symb
)(True)
992 TmpValue
= ValueExpressionEx(Item
, self
.PcdType
, self
._Symb
)(True)
993 Item
= '0x%x' % TmpValue
if type(TmpValue
) != type('') else TmpValue
995 ItemValue
, ItemSize
= ParseFieldValue(Item
)
996 if Item
[0] not in ['"','L','{'] and ItemSize
> 1:
997 raise BadExpression("Byte array number %s should less than 0xFF." % Item
)
999 ItemValue
= ParseFieldValue(Item
)[0]
1000 for I
in range(0, ItemSize
):
1001 ValueStr
= '0x%02X' % (int(ItemValue
) & 255)
1003 AllPcdValueList
.append(ValueStr
)
1007 PcdValue
= '{' + ','.join(AllPcdValueList
) + '}'
1009 raise BadExpression("Type: %s, Value: %s, %s"%(self
.PcdType
, PcdValue
, Value
))
1011 if PcdValue
== 'True':
1013 if PcdValue
== 'False':
1019 if __name__
== '__main__':
1022 input = raw_input('Input expr: ')
1026 print ValueExpression(input)(True)
1027 print ValueExpression(input)(False)
1028 except WrnExpression
, Ex
:
1031 except Exception, Ex
: