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
21 from Common
.DataType
import *
24 ERR_STRING_EXPR
= 'This operator cannot be used in string expression: [%s].'
25 ERR_SNYTAX
= 'Syntax error, the rest of expression cannot be evaluated: [%s].'
26 ERR_MATCH
= 'No matching right parenthesis.'
27 ERR_STRING_TOKEN
= 'Bad string token: [%s].'
28 ERR_MACRO_TOKEN
= 'Bad macro token: [%s].'
29 ERR_EMPTY_TOKEN
= 'Empty token is not allowed.'
30 ERR_PCD_RESOLVE
= 'The PCD should be FeatureFlag type or FixedAtBuild type: [%s].'
31 ERR_VALID_TOKEN
= 'No more valid token found from rest of string: [%s].'
32 ERR_EXPR_TYPE
= 'Different types found in expression.'
33 ERR_OPERATOR_UNSUPPORT
= 'Unsupported operator: [%s]'
34 ERR_REL_NOT_IN
= 'Expect "IN" after "not" operator.'
35 WRN_BOOL_EXPR
= 'Operand of boolean type cannot be used in arithmetic expression.'
36 WRN_EQCMP_STR_OTHERS
= '== Comparison between Operand of string type and Boolean/Number Type always return False.'
37 WRN_NECMP_STR_OTHERS
= '!= Comparison between Operand of string type and Boolean/Number Type always return True.'
38 ERR_RELCMP_STR_OTHERS
= 'Operator taking Operand of string type and Boolean/Number Type is not allowed: [%s].'
39 ERR_STRING_CMP
= 'Unicode string and general string cannot be compared: [%s %s %s]'
40 ERR_ARRAY_TOKEN
= 'Bad C array or C format GUID token: [%s].'
41 ERR_ARRAY_ELE
= 'This must be HEX value for NList or Array: [%s].'
42 ERR_EMPTY_EXPR
= 'Empty expression is not allowed.'
43 ERR_IN_OPERAND
= 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).'
45 __ValidString
= re
.compile(r
'[_a-zA-Z][_0-9a-zA-Z]*$')
46 _ReLabel
= re
.compile('LABEL\((\w+)\)')
47 _ReOffset
= re
.compile('OFFSET_OF\((\w+)\)')
48 PcdPattern
= re
.compile(r
'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
51 # Split string to list according double quote
52 # For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']
54 def SplitString(String
):
55 # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
60 for i
, ch
in enumerate(String
):
61 if ch
== '"' and not InSingleQuote
:
62 if String
[i
- 1] != '\\':
63 InDoubleQuote
= not InDoubleQuote
72 elif ch
== "'" and not InDoubleQuote
:
73 if String
[i
- 1] != '\\':
74 InSingleQuote
= not InSingleQuote
84 if InSingleQuote
or InDoubleQuote
:
85 raise BadExpression(ERR_STRING_TOKEN
% Item
)
90 def SplitPcdValueString(String
):
91 # There might be escaped comma in GUID() or DEVICE_PATH() or " "
92 # or ' ' or L' ' or L" "
98 for i
, ch
in enumerate(String
):
105 raise BadExpression(ERR_STRING_TOKEN
% Item
)
106 elif ch
== '"' and not InSingleQuote
:
107 if String
[i
-1] != '\\':
108 InDoubleQuote
= not InDoubleQuote
109 elif ch
== "'" and not InDoubleQuote
:
110 if String
[i
-1] != '\\':
111 InSingleQuote
= not InSingleQuote
113 if InParenthesis
or InSingleQuote
or InDoubleQuote
:
121 if InSingleQuote
or InDoubleQuote
or InParenthesis
:
122 raise BadExpression(ERR_STRING_TOKEN
% Item
)
127 def IsValidCName(Str
):
128 return True if __ValidString
.match(Str
) else False
130 def BuildOptionValue(PcdValue
, GuidDict
):
131 if PcdValue
.startswith('H'):
132 InputValue
= PcdValue
[1:]
133 elif PcdValue
.startswith("L'") or PcdValue
.startswith("'"):
134 InputValue
= PcdValue
135 elif PcdValue
.startswith('L'):
136 InputValue
= 'L"' + PcdValue
[1:] + '"'
138 InputValue
= PcdValue
140 PcdValue
= ValueExpressionEx(InputValue
, TAB_VOID
, GuidDict
)(True)
148 def ReplaceExprMacro(String
, Macros
, ExceptionList
= None):
149 StrList
= SplitString(String
)
150 for i
, String
in enumerate(StrList
):
152 if String
.startswith('"'):
154 MacroStartPos
= String
.find('$(')
155 if MacroStartPos
< 0:
156 for Pcd
in gPlatformPcds
:
158 if Pcd
not in gConditionalPcds
:
159 gConditionalPcds
.append(Pcd
)
162 while MacroStartPos
>= 0:
163 RetStr
= String
[0:MacroStartPos
]
164 MacroEndPos
= String
.find(')', MacroStartPos
)
166 raise BadExpression(ERR_MACRO_TOKEN
% String
[MacroStartPos
:])
167 Macro
= String
[MacroStartPos
+2:MacroEndPos
]
168 if Macro
not in Macros
:
169 # From C reference manual:
170 # If an undefined macro name appears in the constant-expression of
171 # !if or !elif, it is replaced by the integer constant 0.
174 Tklst
= RetStr
.split()
175 if Tklst
and Tklst
[-1] in {'IN', 'in'} and ExceptionList
and Macro
not in ExceptionList
:
176 raise BadExpression(ERR_IN_OPERAND
)
177 # Make sure the macro in exception list is encapsulated by double quote
178 # For example: DEFINE ARCH = IA32 X64
179 # $(ARCH) is replaced with "IA32 X64"
180 if ExceptionList
and Macro
in ExceptionList
:
181 RetStr
+= '"' + Macros
[Macro
] + '"'
182 elif Macros
[Macro
].strip():
183 RetStr
+= Macros
[Macro
]
187 RetStr
+= Macros
[Macro
]
188 RetStr
+= String
[MacroEndPos
+1:]
190 MacroStartPos
= String
.find('$(')
192 return ''.join(StrList
)
194 # transfer int to string for in/not in expression
198 StrList
.append(chr(Value
& 0xff))
200 Value
= '"' + ''.join(StrList
) + '"'
203 SupportedInMacroList
= ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY']
205 class BaseExpression(object):
206 def __init__(self
, *args
, **kwargs
):
209 # Check if current token matches the operators given from parameter
210 def _IsOperator(self
, OpSet
):
213 if self
._Token
in OpSet
:
214 if self
._Token
in self
.LogicalOperators
:
215 self
._Token
= self
.LogicalOperators
[self
._Token
]
220 class ValueExpression(BaseExpression
):
221 # Logical operator mapping
223 '&&' : 'and', '||' : 'or',
224 '!' : 'not', 'AND': 'and',
225 'OR' : 'or' , 'NOT': 'not',
226 'XOR': '^' , 'xor': '^',
227 'EQ' : '==' , 'NE' : '!=',
228 'GT' : '>' , 'LT' : '<',
229 'GE' : '>=' , 'LE' : '<=',
233 NonLetterOpLst
= ['+', '-', '*', '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':']
236 SymbolPattern
= re
.compile("("
237 "\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|"
239 "(?<=\W)AND(?=\W)|(?<=\W)OR(?=\W)|(?<=\W)NOT(?=\W)|(?<=\W)XOR(?=\W)|"
240 "(?<=\W)EQ(?=\W)|(?<=\W)NE(?=\W)|(?<=\W)GT(?=\W)|(?<=\W)LT(?=\W)|(?<=\W)GE(?=\W)|(?<=\W)LE(?=\W)"
244 def Eval(Operator
, Oprand1
, Oprand2
= None):
247 if Operator
not in {"==", "!=", ">=", "<=", ">", "<", "in", "not in"} and \
248 (isinstance(Oprand1
, type('')) or isinstance(Oprand2
, type(''))):
249 raise BadExpression(ERR_STRING_EXPR
% Operator
)
250 if Operator
in {'in', 'not in'}:
251 if not isinstance(Oprand1
, type('')):
252 Oprand1
= IntToStr(Oprand1
)
253 if not isinstance(Oprand2
, type('')):
254 Oprand2
= IntToStr(Oprand2
)
257 # For python2 long type
258 type(sys
.maxsize
+ 1) : 0,
264 if Operator
in {"!", "NOT", "not"}:
265 if isinstance(Oprand1
, type('')):
266 raise BadExpression(ERR_STRING_EXPR
% Operator
)
267 EvalStr
= 'not Oprand1'
268 elif Operator
in {"~"}:
269 if isinstance(Oprand1
, type('')):
270 raise BadExpression(ERR_STRING_EXPR
% Operator
)
271 EvalStr
= '~ Oprand1'
273 if Operator
in {"+", "-"} and (type(True) in {type(Oprand1
), type(Oprand2
)}):
274 # Boolean in '+'/'-' will be evaluated but raise warning
275 WrnExp
= WrnExpression(WRN_BOOL_EXPR
)
276 elif type('') in {type(Oprand1
), type(Oprand2
)} and not isinstance(Oprand1
, type(Oprand2
)):
277 # == between string and number/boolean will always return False, != return True
279 WrnExp
= WrnExpression(WRN_EQCMP_STR_OTHERS
)
280 WrnExp
.result
= False
282 elif Operator
== "!=":
283 WrnExp
= WrnExpression(WRN_NECMP_STR_OTHERS
)
287 raise BadExpression(ERR_RELCMP_STR_OTHERS
% Operator
)
288 elif TypeDict
[type(Oprand1
)] != TypeDict
[type(Oprand2
)]:
289 if Operator
in {"==", "!=", ">=", "<=", ">", "<"} and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
290 # comparison between number and boolean is allowed
292 elif Operator
in {'&', '|', '^', "and", "or"} and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
293 # bitwise and logical operation between number and boolean is allowed
296 raise BadExpression(ERR_EXPR_TYPE
)
297 if isinstance(Oprand1
, type('')) and isinstance(Oprand2
, type('')):
298 if (Oprand1
.startswith('L"') and not Oprand2
.startswith('L"')) or \
299 (not Oprand1
.startswith('L"') and Oprand2
.startswith('L"')):
300 raise BadExpression(ERR_STRING_CMP
% (Oprand1
, Operator
, Oprand2
))
301 if 'in' in Operator
and isinstance(Oprand2
, type('')):
302 Oprand2
= Oprand2
.split()
303 EvalStr
= 'Oprand1 ' + Operator
+ ' Oprand2'
305 # Local symbols used by built in eval function
311 Val
= eval(EvalStr
, {}, Dict
)
312 except Exception as Excpt
:
313 raise BadExpression(str(Excpt
))
315 if Operator
in {'and', 'or'}:
326 def __init__(self
, Expression
, SymbolTable
={}):
327 super().__init
__(self
, Expression
, SymbolTable
)
328 self
._NoProcess
= False
329 if not isinstance(Expression
, type('')):
330 self
._Expr
= Expression
331 self
._NoProcess
= True
334 self
._Expr
= ReplaceExprMacro(Expression
.strip(),
336 SupportedInMacroList
)
338 if not self
._Expr
.strip():
339 raise BadExpression(ERR_EMPTY_EXPR
)
342 # The symbol table including PCD and macro mapping
344 self
._Symb
= copy
.deepcopy(SymbolTable
)
345 self
._Symb
.update(self
.LogicalOperators
)
347 self
._Len
= len(self
._Expr
)
349 self
._WarnExcept
= None
351 # Literal token without any conversion
352 self
._LiteralToken
= ''
354 # Public entry for this class
355 # @param RealValue: False: only evaluate if the expression is true or false, used for conditional expression
356 # True : return the evaluated str(value), used for PCD value
358 # @return: True or False if RealValue is False
359 # Evaluated value of string format if RealValue is True
361 def __call__(self
, RealValue
=False, Depth
=0):
367 self
._Expr
= self
._Expr
.strip()
368 if RealValue
and Depth
== 0:
369 self
._Token
= self
._Expr
370 if self
.__IsNumberToken
():
374 Token
= self
._GetToken
()
375 except BadExpression
:
377 if isinstance(Token
, type('')) and Token
.startswith('{') and Token
.endswith('}') and self
._Idx
>= self
._Len
:
383 Val
= self
._ConExpr
()
385 if isinstance(Val
, type('')):
391 elif not Val
.startswith('L"') and not Val
.startswith('{') and not Val
.startswith("L'"):
393 RealVal
= '"' + RealVal
+ '"'
395 # The expression has been parsed, but the end of expression is not reached
396 # It means the rest does not comply EBNF of <Expression>
397 if self
._Idx
!= self
._Len
:
398 raise BadExpression(ERR_SNYTAX
% self
._Expr
[self
._Idx
:])
401 RetVal
= str(RealVal
)
408 self
._WarnExcept
.result
= RetVal
409 raise self
._WarnExcept
413 # Template function to parse binary operators which have same precedence
414 # Expr [Operator Expr]*
415 def _ExprFuncTemplate(self
, EvalFunc
, OpSet
):
417 while self
._IsOperator
(OpSet
):
421 if self
._IsOperator
({':'}):
429 # PEP 238 -- Changing the Division Operator
430 # x/y to return a reasonable approximation of the mathematical result of the division ("true division")
431 # x//y to return the floor ("floor division")
436 Val
= self
.Eval(Op
, Val
, EvalFunc())
437 except WrnExpression
as Warn
:
438 self
._WarnExcept
= Warn
443 return self
._ExprFuncTemplate
(self
._OrExpr
, {'?', ':'})
447 return self
._ExprFuncTemplate
(self
._AndExpr
, {"OR", "or", "||"})
451 return self
._ExprFuncTemplate
(self
._BitOr
, {"AND", "and", "&&"})
455 return self
._ExprFuncTemplate
(self
._BitXor
, {"|"})
459 return self
._ExprFuncTemplate
(self
._BitAnd
, {"XOR", "xor", "^"})
463 return self
._ExprFuncTemplate
(self
._EqExpr
, {"&"})
467 Val
= self
._RelExpr
()
468 while self
._IsOperator
({"==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"}):
470 if Op
in {"!", "NOT", "not"}:
471 if not self
._IsOperator
({"IN", "in"}):
472 raise BadExpression(ERR_REL_NOT_IN
)
473 Op
+= ' ' + self
._Token
475 Val
= self
.Eval(Op
, Val
, self
._RelExpr
())
476 except WrnExpression
as Warn
:
477 self
._WarnExcept
= Warn
483 return self
._ExprFuncTemplate
(self
._ShiftExpr
, {"<=", ">=", "<", ">", "LE", "GE", "LT", "GT"})
485 def _ShiftExpr(self
):
486 return self
._ExprFuncTemplate
(self
._AddExpr
, {"<<", ">>"})
490 return self
._ExprFuncTemplate
(self
._MulExpr
, {"+", "-"})
494 return self
._ExprFuncTemplate
(self
._UnaryExpr
, {"*", "/", "%"})
497 def _UnaryExpr(self
):
498 if self
._IsOperator
({"!", "NOT", "not"}):
499 Val
= self
._UnaryExpr
()
501 return self
.Eval('not', Val
)
502 except WrnExpression
as Warn
:
503 self
._WarnExcept
= Warn
505 if self
._IsOperator
({"~"}):
506 Val
= self
._UnaryExpr
()
508 return self
.Eval('~', Val
)
509 except WrnExpression
as Warn
:
510 self
._WarnExcept
= Warn
512 return self
._IdenExpr
()
514 # Parse identifier or encapsulated expression
516 Tk
= self
._GetToken
()
518 Val
= self
._ConExpr
()
520 # _GetToken may also raise BadExpression
521 if self
._GetToken
() != ')':
522 raise BadExpression(ERR_MATCH
)
523 except BadExpression
:
524 raise BadExpression(ERR_MATCH
)
528 # Skip whitespace or tab
530 for Char
in self
._Expr
[self
._Idx
:]:
531 if Char
not in ' \t':
535 # Try to convert string to number
536 def __IsNumberToken(self
):
538 if self
._Token
.lower()[0:2] == '0x' and len(self
._Token
) > 2:
540 if self
._Token
.startswith('"') or self
._Token
.startswith('L"'):
542 for Index
in range(len(self
._Token
)):
543 if self
._Token
[Index
] in {'"'}:
544 if self
._Token
[Index
- 1] == '\\':
547 if Flag
== 2 and self
._Token
.endswith('"'):
549 if self
._Token
.startswith("'") or self
._Token
.startswith("L'"):
551 for Index
in range(len(self
._Token
)):
552 if self
._Token
[Index
] in {"'"}:
553 if self
._Token
[Index
- 1] == '\\':
556 if Flag
== 2 and self
._Token
.endswith("'"):
559 self
._Token
= int(self
._Token
, Radix
)
567 def __GetArray(self
):
570 self
.__GetNList
(True)
571 Token
+= self
._LiteralToken
572 if self
._Idx
>= self
._Len
or self
._Expr
[self
._Idx
] != '}':
573 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
576 # All whitespace and tabs in array are already stripped.
577 IsArray
= IsGuid
= False
578 if len(Token
.split(',')) == 11 and len(Token
.split(',{')) == 2 \
579 and len(Token
.split('},')) == 1:
580 HexLen
= [11, 6, 6, 5, 4, 4, 4, 4, 4, 4, 6]
581 HexList
= Token
.split(',')
582 if HexList
[3].startswith('{') and \
583 not [Index
for Index
, Hex
in enumerate(HexList
) if len(Hex
) > HexLen
[Index
]]:
585 if Token
.lstrip('{').rstrip('}').find('{') == -1:
586 if not [Hex
for Hex
in Token
.lstrip('{').rstrip('}').split(',') if len(Hex
) > 4]:
588 if not IsArray
and not IsGuid
:
589 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
591 self
._Token
= self
._LiteralToken
= Token
594 # Parse string, the format must be: "..."
595 def __GetString(self
):
601 # Replace escape \\\", \"
602 if self
._Expr
[Idx
] == '"':
603 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace('\\\"', '\\\'')
608 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
609 if not self
._Token
.endswith('"'):
610 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
611 #Replace escape \\\', \'
612 elif self
._Expr
[Idx
] == "'":
613 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace("\\\'", "\\\"")
618 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
619 if not self
._Token
.endswith("'"):
620 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
621 self
._Token
= self
._Token
[1:-1]
624 # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
625 # @param IsAlphaOp: Indicate if parsing general token or script operator(EQ, NE...)
626 def __GetIdToken(self
, IsAlphaOp
= False):
628 for Ch
in self
._Expr
[self
._Idx
:]:
629 if not self
.__IsIdChar
(Ch
) or ('?' in self
._Expr
and Ch
== ':'):
634 self
._Token
= self
._LiteralToken
= IdToken
636 self
.__ResolveToken
()
639 # Try to resolve token
640 def __ResolveToken(self
):
642 raise BadExpression(ERR_EMPTY_TOKEN
)
645 if PcdPattern
.match(self
._Token
):
646 if self
._Token
not in self
._Symb
:
647 Ex
= BadExpression(ERR_PCD_RESOLVE
% self
._Token
)
650 self
._Token
= ValueExpression(self
._Symb
[self
._Token
], self
._Symb
)(True, self
._Depth
+1)
651 if not isinstance(self
._Token
, type('')):
652 self
._LiteralToken
= hex(self
._Token
)
655 if self
._Token
.startswith('"'):
656 self
._Token
= self
._Token
[1:-1]
657 elif self
._Token
in {"FALSE", "false", "False"}:
659 elif self
._Token
in {"TRUE", "true", "True"}:
662 self
.__IsNumberToken
()
664 def __GetNList(self
, InArray
=False):
665 self
._GetSingleToken
()
666 if not self
.__IsHexLiteral
():
668 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
672 Expr
= self
._Expr
[self
._Idx
:]
673 if not Expr
.startswith(','):
676 NList
= self
._LiteralToken
677 while Expr
.startswith(','):
681 self
._GetSingleToken
()
682 if not self
.__IsHexLiteral
():
683 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
684 NList
+= self
._LiteralToken
686 Expr
= self
._Expr
[self
._Idx
:]
687 self
._Token
= self
._LiteralToken
= NList
690 def __IsHexLiteral(self
):
691 if self
._LiteralToken
.startswith('{') and \
692 self
._LiteralToken
.endswith('}'):
695 if gHexPattern
.match(self
._LiteralToken
):
696 Token
= self
._LiteralToken
[2:]
698 self
._LiteralToken
= '0x0'
700 self
._LiteralToken
= '0x' + Token
705 return self
.__GetNList
()
709 return Ch
in '._:' or Ch
.isalnum()
712 def _GetSingleToken(self
):
714 Expr
= self
._Expr
[self
._Idx
:]
715 if Expr
.startswith('L"'):
718 UStr
= self
.__GetString
()
719 self
._Token
= 'L"' + UStr
+ '"'
721 elif Expr
.startswith("L'"):
724 UStr
= self
.__GetString
()
725 self
._Token
= "L'" + UStr
+ "'"
727 elif Expr
.startswith("'"):
728 UStr
= self
.__GetString
()
729 self
._Token
= "'" + UStr
+ "'"
731 elif Expr
.startswith('UINT'):
732 Re
= re
.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)')
734 RetValue
= Re
.search(Expr
).group(1)
736 raise BadExpression('Invalid Expression %s' % Expr
)
741 Prefix
= self
._Expr
[Idx
:self
._Idx
- 1]
744 TmpValue
= self
._Expr
[Idx
:self
._Idx
- 1]
745 TmpValue
= ValueExpression(TmpValue
)(True)
746 TmpValue
= '0x%x' % int(TmpValue
) if not isinstance(TmpValue
, type('')) else TmpValue
748 self
._Token
, Size
= ParseFieldValue(Prefix
+ '(' + TmpValue
+ ')')
754 Match
= gGuidPattern
.match(Expr
)
755 if Match
and not Expr
[Match
.end():Match
.end()+1].isalnum() \
756 and Expr
[Match
.end():Match
.end()+1] != '_':
757 self
._Idx
+= Match
.end()
758 self
._Token
= ValueExpression(GuidStringToGuidStructureString(Expr
[0:Match
.end()]))(True, self
._Depth
+1)
760 elif self
.__IsIdChar
(Ch
):
761 return self
.__GetIdToken
()
763 return self
.__GetString
()
765 return self
.__GetArray
()
766 elif Ch
== '(' or Ch
== ')':
771 raise BadExpression(ERR_VALID_TOKEN
% Expr
)
774 def _GetOperator(self
):
776 LegalOpLst
= ['&&', '||', '!=', '==', '>=', '<='] + self
.NonLetterOpLst
+ ['?', ':']
779 Expr
= self
._Expr
[self
._Idx
:]
781 # Reach end of expression
785 # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
786 if Expr
[0].isalpha():
787 return self
.__GetIdToken
(True)
789 # Start to get regular operator: +, -, <, > ...
790 if Expr
[0] not in self
.NonLetterOpLst
:
795 if Ch
in self
.NonLetterOpLst
:
796 if Ch
in ['!', '~'] and OpToken
:
803 if OpToken
not in LegalOpLst
:
804 raise BadExpression(ERR_OPERATOR_UNSUPPORT
% OpToken
)
805 self
._Token
= OpToken
808 class ValueExpressionEx(ValueExpression
):
809 def __init__(self
, PcdValue
, PcdType
, SymbolTable
={}):
810 ValueExpression
.__init
__(self
, PcdValue
, SymbolTable
)
811 self
.PcdValue
= PcdValue
812 self
.PcdType
= PcdType
814 def __call__(self
, RealValue
=False, Depth
=0):
815 PcdValue
= self
.PcdValue
817 PcdValue
= ValueExpression
.__call
__(self
, RealValue
, Depth
)
818 if self
.PcdType
== TAB_VOID
and (PcdValue
.startswith("'") or PcdValue
.startswith("L'")):
819 PcdValue
, Size
= ParseFieldValue(PcdValue
)
821 for I
in range(Size
):
822 PcdValueList
.append('0x%02X'%(PcdValue
& 0xff))
823 PcdValue
= PcdValue
>> 8
824 PcdValue
= '{' + ','.join(PcdValueList
) + '}'
825 elif self
.PcdType
in TAB_PCD_NUMERIC_TYPES
and (PcdValue
.startswith("'") or \
826 PcdValue
.startswith('"') or PcdValue
.startswith("L'") or PcdValue
.startswith('L"') or PcdValue
.startswith('{')):
828 except WrnExpression
as Value
:
829 PcdValue
= Value
.result
830 except BadExpression
as Value
:
831 if self
.PcdType
in TAB_PCD_NUMERIC_TYPES
:
832 PcdValue
= PcdValue
.strip()
833 if PcdValue
.startswith('{') and PcdValue
.endswith('}'):
834 PcdValue
= SplitPcdValueString(PcdValue
[1:-1])
835 if isinstance(PcdValue
, type([])):
839 for Item
in PcdValue
:
841 if Item
.startswith(TAB_UINT8
):
843 ValueType
= TAB_UINT8
844 elif Item
.startswith(TAB_UINT16
):
846 ValueType
= TAB_UINT16
847 elif Item
.startswith(TAB_UINT32
):
849 ValueType
= TAB_UINT32
850 elif Item
.startswith(TAB_UINT64
):
852 ValueType
= TAB_UINT64
853 elif Item
[0] in {'"', "'", 'L'}:
858 ValueType
= TAB_UINT8
859 Item
= ValueExpressionEx(Item
, ValueType
, self
._Symb
)(True)
863 tmpValue
= int(Item
, 0)
865 raise BadExpression("Byte array number %s should less than 0xFF." % Item
)
866 except BadExpression
as Value
:
867 raise BadExpression(Value
)
870 ItemValue
, ItemSize
= ParseFieldValue(Item
)
872 ItemValue
= ParseFieldValue(Item
)[0]
874 if isinstance(ItemValue
, type('')):
875 ItemValue
= int(ItemValue
, 0)
877 TmpValue
= (ItemValue
<< (Size
* 8)) | TmpValue
878 Size
= Size
+ ItemSize
881 TmpValue
, Size
= ParseFieldValue(PcdValue
)
882 except BadExpression
as Value
:
883 raise BadExpression("Type: %s, Value: %s, %s" % (self
.PcdType
, PcdValue
, Value
))
884 if isinstance(TmpValue
, type('')):
886 TmpValue
= int(TmpValue
)
888 raise BadExpression(Value
)
890 PcdValue
= '0x%0{}X'.format(Size
) % (TmpValue
)
892 raise BadExpression('Type %s PCD Value is negative' % self
.PcdType
)
893 if self
.PcdType
== TAB_UINT8
and Size
> 1:
894 raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self
.PcdType
)
895 if self
.PcdType
== TAB_UINT16
and Size
> 2:
896 raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self
.PcdType
)
897 if self
.PcdType
== TAB_UINT32
and Size
> 4:
898 raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self
.PcdType
)
899 if self
.PcdType
== TAB_UINT64
and Size
> 8:
900 raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self
.PcdType
)
903 TmpValue
= int(PcdValue
)
905 if TmpValue
.bit_length() == 0:
908 for I
in range((TmpValue
.bit_length() + 7) // 8):
909 TmpList
.append('0x%02x' % ((TmpValue
>> I
* 8) & 0xff))
910 PcdValue
= '{' + ', '.join(TmpList
) + '}'
912 if PcdValue
.strip().startswith('{'):
913 PcdValueList
= SplitPcdValueString(PcdValue
.strip()[1:-1])
917 for Item
in PcdValueList
:
918 # compute byte offset of every LABEL
919 LabelList
= _ReLabel
.findall(Item
)
920 Item
= _ReLabel
.sub('', Item
)
923 for Label
in LabelList
:
924 if not IsValidCName(Label
):
925 raise BadExpression('%s is not a valid c variable name' % Label
)
926 if Label
not in LabelDict
:
927 LabelDict
[Label
] = str(LabelOffset
)
928 if Item
.startswith(TAB_UINT8
):
929 LabelOffset
= LabelOffset
+ 1
930 elif Item
.startswith(TAB_UINT16
):
931 LabelOffset
= LabelOffset
+ 2
932 elif Item
.startswith(TAB_UINT32
):
933 LabelOffset
= LabelOffset
+ 4
934 elif Item
.startswith(TAB_UINT64
):
935 LabelOffset
= LabelOffset
+ 8
938 ItemValue
, ItemSize
= ParseFieldValue(Item
)
939 LabelOffset
= LabelOffset
+ ItemSize
941 LabelOffset
= LabelOffset
+ 1
943 for Item
in PcdValueList
:
947 Item
= _ReLabel
.sub('', Item
)
951 OffsetList
= _ReOffset
.findall(Item
)
954 # replace each offset, except errors
955 for Offset
in OffsetList
:
957 Item
= Item
.replace('OFFSET_OF({})'.format(Offset
), LabelDict
[Offset
])
959 raise BadExpression('%s not defined' % Offset
)
961 NewPcdValueList
.append(Item
)
964 for Item
in NewPcdValueList
:
967 TokenSpaceGuidName
= ''
968 if Item
.startswith(TAB_GUID
) and Item
.endswith(')'):
970 TokenSpaceGuidName
= re
.search('GUID\((\w+)\)', Item
).group(1)
973 if TokenSpaceGuidName
and TokenSpaceGuidName
in self
._Symb
:
974 Item
= 'GUID(' + self
._Symb
[TokenSpaceGuidName
] + ')'
975 elif TokenSpaceGuidName
:
976 raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName
)
977 Item
, Size
= ParseFieldValue(Item
)
978 for Index
in range(0, Size
):
979 ValueStr
= '0x%02X' % (int(Item
) & 255)
981 AllPcdValueList
.append(ValueStr
)
983 elif Item
.startswith('DEVICE_PATH') and Item
.endswith(')'):
984 Item
, Size
= ParseFieldValue(Item
)
985 AllPcdValueList
.append(Item
[1:-1])
989 if Item
.startswith(TAB_UINT8
):
991 ValueType
= TAB_UINT8
992 elif Item
.startswith(TAB_UINT16
):
994 ValueType
= TAB_UINT16
995 elif Item
.startswith(TAB_UINT32
):
997 ValueType
= TAB_UINT32
998 elif Item
.startswith(TAB_UINT64
):
1000 ValueType
= TAB_UINT64
1004 TmpValue
= ValueExpressionEx(Item
, ValueType
, self
._Symb
)(True)
1006 TmpValue
= ValueExpressionEx(Item
, self
.PcdType
, self
._Symb
)(True)
1007 Item
= '0x%x' % TmpValue
if not isinstance(TmpValue
, type('')) else TmpValue
1009 ItemValue
, ItemSize
= ParseFieldValue(Item
)
1010 if Item
[0] not in {'"', 'L', '{'} and ItemSize
> 1:
1011 raise BadExpression("Byte array number %s should less than 0xFF." % Item
)
1013 ItemValue
= ParseFieldValue(Item
)[0]
1014 for I
in range(0, ItemSize
):
1015 ValueStr
= '0x%02X' % (int(ItemValue
) & 255)
1017 AllPcdValueList
.append(ValueStr
)
1021 PcdValue
= '{' + ','.join(AllPcdValueList
) + '}'
1023 raise BadExpression("Type: %s, Value: %s, %s"%(self
.PcdType
, PcdValue
, Value
))
1025 if PcdValue
== 'True':
1027 if PcdValue
== 'False':
1033 if __name__
== '__main__':
1036 input = input('Input expr: ')
1040 print(ValueExpression(input)(True))
1041 print(ValueExpression(input)(False))
1042 except WrnExpression
as Ex
:
1045 except Exception as Ex
: