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
19 import Common
.EdkLogger
as EdkLogger
22 ERR_STRING_EXPR
= 'This operator cannot be used in string expression: [%s].'
23 ERR_SNYTAX
= 'Syntax error, the rest of expression cannot be evaluated: [%s].'
24 ERR_MATCH
= 'No matching right parenthesis.'
25 ERR_STRING_TOKEN
= 'Bad string token: [%s].'
26 ERR_MACRO_TOKEN
= 'Bad macro token: [%s].'
27 ERR_EMPTY_TOKEN
= 'Empty token is not allowed.'
28 ERR_PCD_RESOLVE
= 'PCD token cannot be resolved: [%s].'
29 ERR_VALID_TOKEN
= 'No more valid token found from rest of string: [%s].'
30 ERR_EXPR_TYPE
= 'Different types found in expression.'
31 ERR_OPERATOR_UNSUPPORT
= 'Unsupported operator: [%s]'
32 ERR_REL_NOT_IN
= 'Expect "IN" after "not" operator.'
33 WRN_BOOL_EXPR
= 'Operand of boolean type cannot be used in arithmetic expression.'
34 WRN_EQCMP_STR_OTHERS
= '== Comparison between Operand of string type and Boolean/Number Type always return False.'
35 WRN_NECMP_STR_OTHERS
= '!= Comparison between Operand of string type and Boolean/Number Type always return True.'
36 ERR_RELCMP_STR_OTHERS
= 'Operator taking Operand of string type and Boolean/Number Type is not allowed: [%s].'
37 ERR_STRING_CMP
= 'Unicode string and general string cannot be compared: [%s %s %s]'
38 ERR_ARRAY_TOKEN
= 'Bad C array or C format GUID token: [%s].'
39 ERR_ARRAY_ELE
= 'This must be HEX value for NList or Array: [%s].'
40 ERR_EMPTY_EXPR
= 'Empty expression is not allowed.'
41 ERR_IN_OPERAND
= 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).'
44 # Split string to list according double quote
45 # For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']
47 def SplitString(String
):
48 # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
54 for i
, ch
in enumerate(Str
):
55 if ch
== '"' and not InSingleQuote
:
56 if Str
[i
- 1] != '\\':
57 InDoubleQuote
= not InDoubleQuote
66 elif ch
== "'" and not InDoubleQuote
:
67 if Str
[i
- 1] != '\\':
68 InSingleQuote
= not InSingleQuote
78 if InSingleQuote
or InDoubleQuote
:
79 raise BadExpression(ERR_STRING_TOKEN
% Item
)
84 def SplitPcdValueString(String
):
85 # There might be escaped comma in GUID() or DEVICE_PATH() or " "
86 # or ' ' or L' ' or L" "
93 for i
, ch
in enumerate(Str
):
100 raise BadExpression(ERR_STRING_TOKEN
% Item
)
101 if ch
== '"' and not InSingleQuote
:
102 if String
[i
-1] != '\\':
103 InDoubleQuote
= not InDoubleQuote
104 if ch
== "'" and not InDoubleQuote
:
105 if String
[i
-1] != '\\':
106 InSingleQuote
= not InSingleQuote
108 if InParenthesis
or InSingleQuote
or InDoubleQuote
:
116 if InSingleQuote
or InDoubleQuote
or InParenthesis
:
117 raise BadExpression(ERR_STRING_TOKEN
% Item
)
122 def IsValidCString(Str
):
123 ValidString
= re
.compile(r
'[_a-zA-Z][_0-9a-zA-Z]*$')
124 if not ValidString
.match(Str
):
130 def ReplaceExprMacro(String
, Macros
, ExceptionList
= None):
131 StrList
= SplitString(String
)
132 for i
, String
in enumerate(StrList
):
134 if String
.startswith('"'):
136 MacroStartPos
= String
.find('$(')
137 if MacroStartPos
< 0:
138 for Pcd
in gPlatformPcds
.keys():
140 if Pcd
not in gConditionalPcds
:
141 gConditionalPcds
.append(Pcd
)
144 while MacroStartPos
>= 0:
145 RetStr
= String
[0:MacroStartPos
]
146 MacroEndPos
= String
.find(')', MacroStartPos
)
148 raise BadExpression(ERR_MACRO_TOKEN
% String
[MacroStartPos
:])
149 Macro
= String
[MacroStartPos
+2:MacroEndPos
]
150 if Macro
not in Macros
:
151 # From C reference manual:
152 # If an undefined macro name appears in the constant-expression of
153 # !if or !elif, it is replaced by the integer constant 0.
156 Tklst
= RetStr
.split()
157 if Tklst
and Tklst
[-1] in ['IN', 'in'] and ExceptionList
and Macro
not in ExceptionList
:
158 raise BadExpression(ERR_IN_OPERAND
)
159 # Make sure the macro in exception list is encapsulated by double quote
160 # For example: DEFINE ARCH = IA32 X64
161 # $(ARCH) is replaced with "IA32 X64"
162 if ExceptionList
and Macro
in ExceptionList
:
163 RetStr
+= '"' + Macros
[Macro
] + '"'
164 elif Macros
[Macro
].strip():
165 RetStr
+= Macros
[Macro
]
169 RetStr
+= Macros
[Macro
]
170 RetStr
+= String
[MacroEndPos
+1:]
172 MacroStartPos
= String
.find('$(')
174 return ''.join(StrList
)
176 # transfer int to string for in/not in expression
180 StrList
.append(chr(Value
& 0xff))
182 Value
= '"' + ''.join(StrList
) + '"'
185 SupportedInMacroList
= ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY']
187 class ValueExpression(object):
188 # Logical operator mapping
190 '&&' : 'and', '||' : 'or',
191 '!' : 'not', 'AND': 'and',
192 'OR' : 'or' , 'NOT': 'not',
193 'XOR': '^' , 'xor': '^',
194 'EQ' : '==' , 'NE' : '!=',
195 'GT' : '>' , 'LT' : '<',
196 'GE' : '>=' , 'LE' : '<=',
200 NonLetterOpLst
= ['+', '-', '*', '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':']
202 PcdPattern
= re
.compile(r
'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
203 HexPattern
= re
.compile(r
'0[xX][0-9a-fA-F]+$')
204 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}')
206 SymbolPattern
= re
.compile("("
207 "\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|"
209 "(?<=\W)AND(?=\W)|(?<=\W)OR(?=\W)|(?<=\W)NOT(?=\W)|(?<=\W)XOR(?=\W)|"
210 "(?<=\W)EQ(?=\W)|(?<=\W)NE(?=\W)|(?<=\W)GT(?=\W)|(?<=\W)LT(?=\W)|(?<=\W)GE(?=\W)|(?<=\W)LE(?=\W)"
214 def Eval(Operator
, Oprand1
, Oprand2
= None):
217 if Operator
not in ["==", "!=", ">=", "<=", ">", "<", "in", "not in"] and \
218 (type(Oprand1
) == type('') or type(Oprand2
) == type('')):
219 raise BadExpression(ERR_STRING_EXPR
% Operator
)
220 if Operator
in ['in', 'not in']:
221 if type(Oprand1
) != type(''):
222 Oprand1
= IntToStr(Oprand1
)
223 if type(Oprand2
) != type(''):
224 Oprand2
= IntToStr(Oprand2
)
233 if Operator
in ["!", "NOT", "not"]:
234 if type(Oprand1
) == type(''):
235 raise BadExpression(ERR_STRING_EXPR
% Operator
)
236 EvalStr
= 'not Oprand1'
237 elif Operator
in ["~"]:
238 if type(Oprand1
) == type(''):
239 raise BadExpression(ERR_STRING_EXPR
% Operator
)
240 EvalStr
= '~ Oprand1'
242 if Operator
in ["+", "-"] and (type(True) in [type(Oprand1
), type(Oprand2
)]):
243 # Boolean in '+'/'-' will be evaluated but raise warning
244 WrnExp
= WrnExpression(WRN_BOOL_EXPR
)
245 elif type('') in [type(Oprand1
), type(Oprand2
)] and type(Oprand1
)!= type(Oprand2
):
246 # == between string and number/boolean will always return False, != return True
248 WrnExp
= WrnExpression(WRN_EQCMP_STR_OTHERS
)
249 WrnExp
.result
= False
251 elif Operator
== "!=":
252 WrnExp
= WrnExpression(WRN_NECMP_STR_OTHERS
)
256 raise BadExpression(ERR_RELCMP_STR_OTHERS
% Operator
)
257 elif TypeDict
[type(Oprand1
)] != TypeDict
[type(Oprand2
)]:
258 if Operator
in ["==", "!=", ">=", "<=", ">", "<"] and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
259 # comparison between number and boolean is allowed
261 elif Operator
in ['&', '|', '^', "and", "or"] and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
262 # bitwise and logical operation between number and boolean is allowed
265 raise BadExpression(ERR_EXPR_TYPE
)
266 if type(Oprand1
) == type('') and type(Oprand2
) == type(''):
267 if (Oprand1
.startswith('L"') and not Oprand2
.startswith('L"')) or \
268 (not Oprand1
.startswith('L"') and Oprand2
.startswith('L"')):
269 raise BadExpression(ERR_STRING_CMP
% (Oprand1
, Operator
, Oprand2
))
270 if 'in' in Operator
and type(Oprand2
) == type(''):
271 Oprand2
= Oprand2
.split()
272 EvalStr
= 'Oprand1 ' + Operator
+ ' Oprand2'
274 # Local symbols used by built in eval function
280 Val
= eval(EvalStr
, {}, Dict
)
281 except Exception, Excpt
:
282 raise BadExpression(str(Excpt
))
284 if Operator
in ['and', 'or']:
295 def __init__(self
, Expression
, SymbolTable
={}):
296 self
._NoProcess
= False
297 if type(Expression
) != type(''):
298 self
._Expr
= Expression
299 self
._NoProcess
= True
302 self
._Expr
= ReplaceExprMacro(Expression
.strip(),
304 SupportedInMacroList
)
306 if not self
._Expr
.strip():
307 raise BadExpression(ERR_EMPTY_EXPR
)
310 # The symbol table including PCD and macro mapping
312 self
._Symb
= copy
.deepcopy(SymbolTable
)
313 self
._Symb
.update(self
.LogicalOperators
)
315 self
._Len
= len(self
._Expr
)
317 self
._WarnExcept
= None
319 # Literal token without any conversion
320 self
._LiteralToken
= ''
322 # Public entry for this class
323 # @param RealValue: False: only evaluate if the expression is true or false, used for conditional expression
324 # True : return the evaluated str(value), used for PCD value
326 # @return: True or False if RealValue is False
327 # Evaluated value of string format if RealValue is True
329 def __call__(self
, RealValue
=False, Depth
=0):
335 self
._Expr
= self
._Expr
.strip()
336 if RealValue
and Depth
== 0:
337 self
._Token
= self
._Expr
338 if self
.__IsNumberToken
():
342 Token
= self
._GetToken
()
343 except BadExpression
:
345 if type(Token
) == type('') and Token
.startswith('{') and Token
.endswith('}') and self
._Idx
>= self
._Len
:
351 Val
= self
._ConExpr
()
353 if type(Val
) == type(''):
359 elif not Val
.startswith('L"') and not Val
.startswith('{') and not Val
.startswith("L'"):
361 RealVal
= '"' + RealVal
+ '"'
363 # The expression has been parsed, but the end of expression is not reached
364 # It means the rest does not comply EBNF of <Expression>
365 if self
._Idx
!= self
._Len
:
366 raise BadExpression(ERR_SNYTAX
% self
._Expr
[self
._Idx
:])
369 RetVal
= str(RealVal
)
376 self
._WarnExcept
.result
= RetVal
377 raise self
._WarnExcept
381 # Template function to parse binary operators which have same precedence
382 # Expr [Operator Expr]*
383 def _ExprFuncTemplate(self
, EvalFunc
, OpLst
):
385 while self
._IsOperator
(OpLst
):
389 if self
._IsOperator
(':'):
397 Val
= self
.Eval(Op
, Val
, EvalFunc())
398 except WrnExpression
, Warn
:
399 self
._WarnExcept
= Warn
404 return self
._ExprFuncTemplate
(self
._OrExpr
, ['?', ':'])
408 return self
._ExprFuncTemplate
(self
._AndExpr
, ["OR", "or", "||"])
412 return self
._ExprFuncTemplate
(self
._BitOr
, ["AND", "and", "&&"])
416 return self
._ExprFuncTemplate
(self
._BitXor
, ["|"])
420 return self
._ExprFuncTemplate
(self
._BitAnd
, ["XOR", "xor", "^"])
424 return self
._ExprFuncTemplate
(self
._EqExpr
, ["&"])
428 Val
= self
._RelExpr
()
429 while self
._IsOperator
(["==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"]):
431 if Op
in ["!", "NOT", "not"]:
432 if not self
._IsOperator
(["IN", "in"]):
433 raise BadExpression(ERR_REL_NOT_IN
)
434 Op
+= ' ' + self
._Token
436 Val
= self
.Eval(Op
, Val
, self
._RelExpr
())
437 except WrnExpression
, Warn
:
438 self
._WarnExcept
= Warn
444 return self
._ExprFuncTemplate
(self
._ShiftExpr
, ["<=", ">=", "<", ">", "LE", "GE", "LT", "GT"])
446 def _ShiftExpr(self
):
447 return self
._ExprFuncTemplate
(self
._AddExpr
, ["<<", ">>"])
451 return self
._ExprFuncTemplate
(self
._MulExpr
, ["+", "-"])
455 return self
._ExprFuncTemplate
(self
._UnaryExpr
, ["*", "/", "%"])
458 def _UnaryExpr(self
):
459 if self
._IsOperator
(["!", "NOT", "not"]):
460 Val
= self
._UnaryExpr
()
462 return self
.Eval('not', Val
)
463 except WrnExpression
, Warn
:
464 self
._WarnExcept
= Warn
466 if self
._IsOperator
(["~"]):
467 Val
= self
._UnaryExpr
()
469 return self
.Eval('~', Val
)
470 except WrnExpression
, Warn
:
471 self
._WarnExcept
= Warn
473 return self
._IdenExpr
()
475 # Parse identifier or encapsulated expression
477 Tk
= self
._GetToken
()
479 Val
= self
._ConExpr
()
481 # _GetToken may also raise BadExpression
482 if self
._GetToken
() != ')':
483 raise BadExpression(ERR_MATCH
)
484 except BadExpression
:
485 raise BadExpression(ERR_MATCH
)
489 # Skip whitespace or tab
491 for Char
in self
._Expr
[self
._Idx
:]:
492 if Char
not in ' \t':
496 # Try to convert string to number
497 def __IsNumberToken(self
):
499 if self
._Token
.lower()[0:2] == '0x' and len(self
._Token
) > 2:
501 if self
._Token
.startswith('"') or self
._Token
.startswith('L"'):
503 for Index
in range(len(self
._Token
)):
504 if self
._Token
[Index
] in ['"']:
505 if self
._Token
[Index
- 1] == '\\':
508 if Flag
== 2 and self
._Token
.endswith('"'):
510 if self
._Token
.startswith("'") or self
._Token
.startswith("L'"):
512 for Index
in range(len(self
._Token
)):
513 if self
._Token
[Index
] in ["'"]:
514 if self
._Token
[Index
- 1] == '\\':
517 if Flag
== 2 and self
._Token
.endswith("'"):
520 self
._Token
= int(self
._Token
, Radix
)
528 def __GetArray(self
):
531 self
.__GetNList
(True)
532 Token
+= self
._LiteralToken
533 if self
._Idx
>= self
._Len
or self
._Expr
[self
._Idx
] != '}':
534 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
537 # All whitespace and tabs in array are already stripped.
538 IsArray
= IsGuid
= False
539 if len(Token
.split(',')) == 11 and len(Token
.split(',{')) == 2 \
540 and len(Token
.split('},')) == 1:
541 HexLen
= [11,6,6,5,4,4,4,4,4,4,6]
542 HexList
= Token
.split(',')
543 if HexList
[3].startswith('{') and \
544 not [Index
for Index
, Hex
in enumerate(HexList
) if len(Hex
) > HexLen
[Index
]]:
546 if Token
.lstrip('{').rstrip('}').find('{') == -1:
547 if not [Hex
for Hex
in Token
.lstrip('{').rstrip('}').split(',') if len(Hex
) > 4]:
549 if not IsArray
and not IsGuid
:
550 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
552 self
._Token
= self
._LiteralToken
= Token
555 # Parse string, the format must be: "..."
556 def __GetString(self
):
562 # Replace escape \\\", \"
563 if self
._Expr
[Idx
] == '"':
564 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace('\\\"', '\\\'')
569 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
570 if not self
._Token
.endswith('"'):
571 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
572 #Replace escape \\\', \'
573 elif self
._Expr
[Idx
] == "'":
574 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace("\\\'", "\\\"")
579 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
580 if not self
._Token
.endswith("'"):
581 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
582 self
._Token
= self
._Token
[1:-1]
585 # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
586 # @param IsAlphaOp: Indicate if parsing general token or script operator(EQ, NE...)
587 def __GetIdToken(self
, IsAlphaOp
= False):
589 for Ch
in self
._Expr
[self
._Idx
:]:
590 if not self
.__IsIdChar
(Ch
) or ('?' in self
._Expr
and Ch
== ':'):
595 self
._Token
= self
._LiteralToken
= IdToken
597 self
.__ResolveToken
()
600 # Try to resolve token
601 def __ResolveToken(self
):
603 raise BadExpression(ERR_EMPTY_TOKEN
)
606 if self
.PcdPattern
.match(self
._Token
):
607 if self
._Token
not in self
._Symb
:
608 Ex
= BadExpression(ERR_PCD_RESOLVE
% self
._Token
)
611 self
._Token
= ValueExpression(self
._Symb
[self
._Token
], self
._Symb
)(True, self
._Depth
+1)
612 if type(self
._Token
) != type(''):
613 self
._LiteralToken
= hex(self
._Token
)
616 if self
._Token
.startswith('"'):
617 self
._Token
= self
._Token
[1:-1]
618 elif self
._Token
in ["FALSE", "false", "False"]:
620 elif self
._Token
in ["TRUE", "true", "True"]:
623 self
.__IsNumberToken
()
625 def __GetNList(self
, InArray
=False):
626 self
._GetSingleToken
()
627 if not self
.__IsHexLiteral
():
629 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
633 Expr
= self
._Expr
[self
._Idx
:]
634 if not Expr
.startswith(','):
637 NList
= self
._LiteralToken
638 while Expr
.startswith(','):
642 self
._GetSingleToken
()
643 if not self
.__IsHexLiteral
():
644 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
645 NList
+= self
._LiteralToken
647 Expr
= self
._Expr
[self
._Idx
:]
648 self
._Token
= self
._LiteralToken
= NList
651 def __IsHexLiteral(self
):
652 if self
._LiteralToken
.startswith('{') and \
653 self
._LiteralToken
.endswith('}'):
656 if self
.HexPattern
.match(self
._LiteralToken
):
657 Token
= self
._LiteralToken
[2:]
659 self
._LiteralToken
= '0x0'
661 self
._LiteralToken
= '0x' + Token
666 return self
.__GetNList
()
670 return Ch
in '._:' or Ch
.isalnum()
673 def _GetSingleToken(self
):
675 Expr
= self
._Expr
[self
._Idx
:]
676 if Expr
.startswith('L"'):
679 UStr
= self
.__GetString
()
680 self
._Token
= 'L"' + UStr
+ '"'
682 elif Expr
.startswith("L'"):
685 UStr
= self
.__GetString
()
686 self
._Token
= "L'" + UStr
+ "'"
688 elif Expr
.startswith("'"):
689 UStr
= self
.__GetString
()
690 self
._Token
= "'" + UStr
+ "'"
692 elif Expr
.startswith('UINT'):
693 Re
= re
.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)')
695 RetValue
= Re
.search(Expr
).group(1)
697 raise BadExpression('Invalid Expression %s' % Expr
)
702 Prefix
= self
._Expr
[Idx
:self
._Idx
- 1]
705 TmpValue
= self
._Expr
[Idx
:self
._Idx
- 1]
706 TmpValue
= ValueExpression(TmpValue
)(True)
707 TmpValue
= '0x%x' % int(TmpValue
) if type(TmpValue
) != type('') else TmpValue
709 self
._Token
, Size
= ParseFieldValue(Prefix
+ '(' + TmpValue
+ ')')
715 Match
= self
.RegGuidPattern
.match(Expr
)
716 if Match
and not Expr
[Match
.end():Match
.end()+1].isalnum() \
717 and Expr
[Match
.end():Match
.end()+1] != '_':
718 self
._Idx
+= Match
.end()
719 self
._Token
= ValueExpression(GuidStringToGuidStructureString(Expr
[0:Match
.end()]))(True, self
._Depth
+1)
721 elif self
.__IsIdChar
(Ch
):
722 return self
.__GetIdToken
()
724 return self
.__GetString
()
726 return self
.__GetArray
()
727 elif Ch
== '(' or Ch
== ')':
732 raise BadExpression(ERR_VALID_TOKEN
% Expr
)
735 def _GetOperator(self
):
737 LegalOpLst
= ['&&', '||', '!=', '==', '>=', '<='] + self
.NonLetterOpLst
+ ['?',':']
740 Expr
= self
._Expr
[self
._Idx
:]
742 # Reach end of expression
746 # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
747 if Expr
[0].isalpha():
748 return self
.__GetIdToken
(True)
750 # Start to get regular operator: +, -, <, > ...
751 if Expr
[0] not in self
.NonLetterOpLst
:
756 if Ch
in self
.NonLetterOpLst
:
757 if '!' == Ch
and OpToken
:
764 if OpToken
not in LegalOpLst
:
765 raise BadExpression(ERR_OPERATOR_UNSUPPORT
% OpToken
)
766 self
._Token
= OpToken
769 # Check if current token matches the operators given from OpList
770 def _IsOperator(self
, OpList
):
773 if self
._Token
in OpList
:
774 if self
._Token
in self
.LogicalOperators
:
775 self
._Token
= self
.LogicalOperators
[self
._Token
]
780 class ValueExpressionEx(ValueExpression
):
781 def __init__(self
, PcdValue
, PcdType
, SymbolTable
={}):
782 ValueExpression
.__init
__(self
, PcdValue
, SymbolTable
)
783 self
.PcdValue
= PcdValue
784 self
.PcdType
= PcdType
786 def __call__(self
, RealValue
=False, Depth
=0):
787 PcdValue
= self
.PcdValue
789 PcdValue
= ValueExpression
.__call
__(self
, RealValue
, Depth
)
790 if self
.PcdType
== 'VOID*' and (PcdValue
.startswith("'") or PcdValue
.startswith("L'")):
791 PcdValue
, Size
= ParseFieldValue(PcdValue
)
793 for I
in range(Size
):
794 PcdValueList
.append('0x%02X'%(PcdValue
& 0xff))
795 PcdValue
= PcdValue
>> 8
796 PcdValue
= '{' + ','.join(PcdValueList
) + '}'
797 elif self
.PcdType
in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN'] and (PcdValue
.startswith("'") or \
798 PcdValue
.startswith('"') or PcdValue
.startswith("L'") or PcdValue
.startswith('L"') or PcdValue
.startswith('{')):
800 except WrnExpression
, Value
:
801 PcdValue
= Value
.result
802 except BadExpression
, Value
:
803 if self
.PcdType
in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN']:
804 PcdValue
= PcdValue
.strip()
805 if type(PcdValue
) == type('') and PcdValue
.startswith('{') and PcdValue
.endswith('}'):
806 PcdValue
= SplitPcdValueString(PcdValue
[1:-1])
807 if type(PcdValue
) == type([]):
811 for Item
in PcdValue
:
813 if Item
.startswith('UINT8'):
816 elif Item
.startswith('UINT16'):
819 elif Item
.startswith('UINT32'):
822 elif Item
.startswith('UINT64'):
825 elif Item
.startswith('"') or Item
.startswith("'") or Item
.startswith('L'):
831 Item
= ValueExpressionEx(Item
, ValueType
, self
._Symb
)(True)
835 tmpValue
= int(Item
, 16) if Item
.upper().startswith('0X') else int(Item
, 0)
837 raise BadExpression("Byte array number %s should less than 0xFF." % Item
)
838 except BadExpression
, Value
:
839 raise BadExpression(Value
)
842 ItemValue
, ItemSize
= ParseFieldValue(Item
)
844 ItemValue
= ParseFieldValue(Item
)[0]
846 if type(ItemValue
) == type(''):
847 ItemValue
= int(ItemValue
, 16) if ItemValue
.startswith('0x') else int(ItemValue
)
849 TmpValue
= (ItemValue
<< (Size
* 8)) | TmpValue
850 Size
= Size
+ ItemSize
853 TmpValue
, Size
= ParseFieldValue(PcdValue
)
854 except BadExpression
, Value
:
855 raise BadExpression("Type: %s, Value: %s, %s" % (self
.PcdType
, PcdValue
, Value
))
856 if type(TmpValue
) == type(''):
858 TmpValue
= int(TmpValue
)
860 raise BadExpression(Value
)
862 PcdValue
= '0x%0{}X'.format(Size
) % (TmpValue
)
864 raise BadExpression('Type %s PCD Value is negative' % self
.PcdType
)
865 if self
.PcdType
== 'UINT8' and Size
> 1:
866 raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self
.PcdType
)
867 if self
.PcdType
== 'UINT16' and Size
> 2:
868 raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self
.PcdType
)
869 if self
.PcdType
== 'UINT32' and Size
> 4:
870 raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self
.PcdType
)
871 if self
.PcdType
== 'UINT64' and Size
> 8:
872 raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self
.PcdType
)
875 TmpValue
= long(PcdValue
)
877 if TmpValue
.bit_length() == 0:
880 for I
in range((TmpValue
.bit_length() + 7) / 8):
881 TmpList
.append('0x%02x' % ((TmpValue
>> I
* 8) & 0xff))
882 PcdValue
= '{' + ', '.join(TmpList
) + '}'
884 if PcdValue
.strip().startswith('{'):
885 PcdValueList
= SplitPcdValueString(PcdValue
.strip()[1:-1])
888 ReLabel
= re
.compile('LABEL\((\w+)\)')
889 ReOffset
= re
.compile('OFFSET_OF\((\w+)\)')
891 for Index
, Item
in enumerate(PcdValueList
):
892 # compute byte offset of every LABEL
894 LabelList
= ReLabel
.findall(Item
)
896 for Label
in LabelList
:
897 if not IsValidCString(Label
):
898 raise BadExpression('%s is not a valid c variable name' % Label
)
899 if Label
not in LabelDict
.keys():
900 LabelDict
[Label
] = str(LabelOffset
)
901 if Item
.startswith('UINT8'):
902 LabelOffset
= LabelOffset
+ 1
903 elif Item
.startswith('UINT16'):
904 LabelOffset
= LabelOffset
+ 2
905 elif Item
.startswith('UINT32'):
906 LabelOffset
= LabelOffset
+ 4
907 elif Item
.startswith('UINT64'):
908 LabelOffset
= LabelOffset
+ 8
911 ItemValue
, ItemSize
= ParseFieldValue(Item
)
912 LabelOffset
= LabelOffset
+ ItemSize
914 LabelOffset
= LabelOffset
+ 1
916 for Index
, Item
in enumerate(PcdValueList
):
920 Item
= ReLabel
.sub('', Item
)
924 OffsetList
= ReOffset
.findall(Item
)
927 for Offset
in OffsetList
:
928 if Offset
in LabelDict
.keys():
929 Re
= re
.compile('OFFSET_OF\(%s\)' % Offset
)
930 Item
= Re
.sub(LabelDict
[Offset
], Item
)
932 raise BadExpression('%s not defined' % Offset
)
933 NewPcdValueList
.append(Item
)
936 for Item
in NewPcdValueList
:
939 TokenSpaceGuidName
= ''
940 if Item
.startswith('GUID') and Item
.endswith(')'):
942 TokenSpaceGuidName
= re
.search('GUID\((\w+)\)', Item
).group(1)
945 if TokenSpaceGuidName
and TokenSpaceGuidName
in self
._Symb
:
946 Item
= 'GUID(' + self
._Symb
[TokenSpaceGuidName
] + ')'
947 elif TokenSpaceGuidName
:
948 raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName
)
949 Item
, Size
= ParseFieldValue(Item
)
950 for Index
in range(0, Size
):
951 ValueStr
= '0x%02X' % (int(Item
) & 255)
953 AllPcdValueList
.append(ValueStr
)
955 elif Item
.startswith('DEVICE_PATH') and Item
.endswith(')'):
956 Item
, Size
= ParseFieldValue(Item
)
957 AllPcdValueList
.append(Item
[1:-1])
961 if Item
.startswith('UINT8'):
964 elif Item
.startswith('UINT16'):
967 elif Item
.startswith('UINT32'):
970 elif Item
.startswith('UINT64'):
976 TmpValue
= ValueExpressionEx(Item
, ValueType
, self
._Symb
)(True)
978 TmpValue
= ValueExpressionEx(Item
, self
.PcdType
, self
._Symb
)(True)
979 Item
= '0x%x' % TmpValue
if type(TmpValue
) != type('') else TmpValue
981 ItemValue
, ItemSize
= ParseFieldValue(Item
)
982 if not (Item
.startswith('"') or Item
.startswith('L') or Item
.startswith('{')) and ItemSize
> 1:
983 raise BadExpression("Byte array number %s should less than 0xFF." % Item
)
985 ItemValue
= ParseFieldValue(Item
)[0]
986 for I
in range(0, ItemSize
):
987 ValueStr
= '0x%02X' % (int(ItemValue
) & 255)
989 AllPcdValueList
.append(ValueStr
)
993 PcdValue
= '{' + ','.join(AllPcdValueList
) + '}'
995 raise BadExpression("Type: %s, Value: %s, %s"%(self
.PcdType
, PcdValue
, Value
))
997 if PcdValue
== 'True':
999 if PcdValue
== 'False':
1005 if __name__
== '__main__':
1008 input = raw_input('Input expr: ')
1012 print ValueExpression(input)(True)
1013 print ValueExpression(input)(False)
1014 except WrnExpression
, Ex
:
1017 except Exception, Ex
: