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 # SPDX-License-Identifier: BSD-2-Clause-Patent
9 from __future__
import print_function
10 from __future__
import absolute_import
11 from Common
.GlobalData
import *
12 from CommonDataClass
.Exceptions
import BadExpression
13 from CommonDataClass
.Exceptions
import WrnExpression
14 from .Misc
import GuidStringToGuidStructureString
, ParseFieldValue
,CopyDict
15 import Common
.EdkLogger
as EdkLogger
17 from Common
.DataType
import *
19 from random
import sample
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
= 'The PCD should be FeatureFlag type or FixedAtBuild type: [%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).'
43 __ValidString
= re
.compile(r
'[_a-zA-Z][_0-9a-zA-Z]*$')
44 _ReLabel
= re
.compile('LABEL\((\w+)\)')
45 _ReOffset
= re
.compile('OFFSET_OF\((\w+)\)')
46 PcdPattern
= re
.compile(r
'^[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
49 # Split string to list according double quote
50 # For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']
52 def SplitString(String
):
53 # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
54 RanStr
= ''.join(sample(string
.ascii_letters
+ string
.digits
, 8))
55 String
= String
.replace('\\\\', RanStr
).strip()
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
)
88 for i
, ch
in enumerate(RetList
):
90 RetList
[i
] = ch
.replace(RanStr
,'\\\\')
93 def SplitPcdValueString(String
):
94 # There might be escaped comma in GUID() or DEVICE_PATH() or " "
95 # or ' ' or L' ' or L" "
96 RanStr
= ''.join(sample(string
.ascii_letters
+ string
.digits
, 8))
97 String
= String
.replace('\\\\', RanStr
).strip()
100 InSingleQuote
= False
101 InDoubleQuote
= False
103 for i
, ch
in enumerate(String
):
110 raise BadExpression(ERR_STRING_TOKEN
% Item
)
111 elif ch
== '"' and not InSingleQuote
:
112 if String
[i
-1] != '\\':
113 InDoubleQuote
= not InDoubleQuote
114 elif ch
== "'" and not InDoubleQuote
:
115 if String
[i
-1] != '\\':
116 InSingleQuote
= not InSingleQuote
118 if InParenthesis
or InSingleQuote
or InDoubleQuote
:
126 if InSingleQuote
or InDoubleQuote
or InParenthesis
:
127 raise BadExpression(ERR_STRING_TOKEN
% Item
)
130 for i
, ch
in enumerate(RetList
):
132 RetList
[i
] = ch
.replace(RanStr
,'\\\\')
135 def IsValidCName(Str
):
136 return True if __ValidString
.match(Str
) else False
138 def BuildOptionValue(PcdValue
, GuidDict
):
139 if PcdValue
.startswith('H'):
140 InputValue
= PcdValue
[1:]
141 elif PcdValue
.startswith("L'") or PcdValue
.startswith("'"):
142 InputValue
= PcdValue
143 elif PcdValue
.startswith('L'):
144 InputValue
= 'L"' + PcdValue
[1:] + '"'
146 InputValue
= PcdValue
148 PcdValue
= ValueExpressionEx(InputValue
, TAB_VOID
, GuidDict
)(True)
156 def ReplaceExprMacro(String
, Macros
, ExceptionList
= None):
157 StrList
= SplitString(String
)
158 for i
, String
in enumerate(StrList
):
160 if String
.startswith('"'):
162 MacroStartPos
= String
.find('$(')
163 if MacroStartPos
< 0:
164 for Pcd
in gPlatformPcds
:
166 if Pcd
not in gConditionalPcds
:
167 gConditionalPcds
.append(Pcd
)
170 while MacroStartPos
>= 0:
171 RetStr
= String
[0:MacroStartPos
]
172 MacroEndPos
= String
.find(')', MacroStartPos
)
174 raise BadExpression(ERR_MACRO_TOKEN
% String
[MacroStartPos
:])
175 Macro
= String
[MacroStartPos
+2:MacroEndPos
]
176 if Macro
not in Macros
:
177 # From C reference manual:
178 # If an undefined macro name appears in the constant-expression of
179 # !if or !elif, it is replaced by the integer constant 0.
182 Tklst
= RetStr
.split()
183 if Tklst
and Tklst
[-1] in {'IN', 'in'} and ExceptionList
and Macro
not in ExceptionList
:
184 raise BadExpression(ERR_IN_OPERAND
)
185 # Make sure the macro in exception list is encapsulated by double quote
186 # For example: DEFINE ARCH = IA32 X64
187 # $(ARCH) is replaced with "IA32 X64"
188 if ExceptionList
and Macro
in ExceptionList
:
189 RetStr
+= '"' + Macros
[Macro
] + '"'
190 elif Macros
[Macro
].strip():
191 RetStr
+= Macros
[Macro
]
195 RetStr
+= Macros
[Macro
]
196 RetStr
+= String
[MacroEndPos
+1:]
198 MacroStartPos
= String
.find('$(')
200 return ''.join(StrList
)
202 # transfer int to string for in/not in expression
206 StrList
.append(chr(Value
& 0xff))
208 Value
= '"' + ''.join(StrList
) + '"'
211 SupportedInMacroList
= ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY']
213 class BaseExpression(object):
214 def __init__(self
, *args
, **kwargs
):
215 super(BaseExpression
, self
).__init
__()
217 # Check if current token matches the operators given from parameter
218 def _IsOperator(self
, OpSet
):
221 if self
._Token
in OpSet
:
222 if self
._Token
in self
.LogicalOperators
:
223 self
._Token
= self
.LogicalOperators
[self
._Token
]
228 class ValueExpression(BaseExpression
):
229 # Logical operator mapping
231 '&&' : 'and', '||' : 'or',
232 '!' : 'not', 'AND': 'and',
233 'OR' : 'or' , 'NOT': 'not',
234 'XOR': '^' , 'xor': '^',
235 'EQ' : '==' , 'NE' : '!=',
236 'GT' : '>' , 'LT' : '<',
237 'GE' : '>=' , 'LE' : '<=',
241 NonLetterOpLst
= ['+', '-', TAB_STAR
, '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':']
244 SymbolPattern
= re
.compile("("
245 "\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|"
247 "(?<=\W)AND(?=\W)|(?<=\W)OR(?=\W)|(?<=\W)NOT(?=\W)|(?<=\W)XOR(?=\W)|"
248 "(?<=\W)EQ(?=\W)|(?<=\W)NE(?=\W)|(?<=\W)GT(?=\W)|(?<=\W)LT(?=\W)|(?<=\W)GE(?=\W)|(?<=\W)LE(?=\W)"
252 def Eval(Operator
, Oprand1
, Oprand2
= None):
255 if Operator
not in {"==", "!=", ">=", "<=", ">", "<", "in", "not in"} and \
256 (isinstance(Oprand1
, type('')) or isinstance(Oprand2
, type(''))):
257 raise BadExpression(ERR_STRING_EXPR
% Operator
)
258 if Operator
in {'in', 'not in'}:
259 if not isinstance(Oprand1
, type('')):
260 Oprand1
= IntToStr(Oprand1
)
261 if not isinstance(Oprand2
, type('')):
262 Oprand2
= IntToStr(Oprand2
)
265 # For python2 long type
266 type(sys
.maxsize
+ 1) : 0,
272 if Operator
in {"!", "NOT", "not"}:
273 if isinstance(Oprand1
, type('')):
274 raise BadExpression(ERR_STRING_EXPR
% Operator
)
275 EvalStr
= 'not Oprand1'
276 elif Operator
in {"~"}:
277 if isinstance(Oprand1
, type('')):
278 raise BadExpression(ERR_STRING_EXPR
% Operator
)
279 EvalStr
= '~ Oprand1'
281 if Operator
in {"+", "-"} and (type(True) in {type(Oprand1
), type(Oprand2
)}):
282 # Boolean in '+'/'-' will be evaluated but raise warning
283 WrnExp
= WrnExpression(WRN_BOOL_EXPR
)
284 elif type('') in {type(Oprand1
), type(Oprand2
)} and not isinstance(Oprand1
, type(Oprand2
)):
285 # == between string and number/boolean will always return False, != return True
287 WrnExp
= WrnExpression(WRN_EQCMP_STR_OTHERS
)
288 WrnExp
.result
= False
290 elif Operator
== "!=":
291 WrnExp
= WrnExpression(WRN_NECMP_STR_OTHERS
)
295 raise BadExpression(ERR_RELCMP_STR_OTHERS
% Operator
)
296 elif TypeDict
[type(Oprand1
)] != TypeDict
[type(Oprand2
)]:
297 if Operator
in {"==", "!=", ">=", "<=", ">", "<"} and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
298 # comparison between number and boolean is allowed
300 elif Operator
in {'&', '|', '^', "and", "or"} and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
301 # bitwise and logical operation between number and boolean is allowed
304 raise BadExpression(ERR_EXPR_TYPE
)
305 if isinstance(Oprand1
, type('')) and isinstance(Oprand2
, type('')):
306 if ((Oprand1
.startswith('L"') or Oprand1
.startswith("L'")) and (not Oprand2
.startswith('L"')) and (not Oprand2
.startswith("L'"))) or \
307 (((not Oprand1
.startswith('L"')) and (not Oprand1
.startswith("L'"))) and (Oprand2
.startswith('L"') or Oprand2
.startswith("L'"))):
308 raise BadExpression(ERR_STRING_CMP
% (Oprand1
, Operator
, Oprand2
))
309 if 'in' in Operator
and isinstance(Oprand2
, type('')):
310 Oprand2
= Oprand2
.split()
311 EvalStr
= 'Oprand1 ' + Operator
+ ' Oprand2'
313 # Local symbols used by built in eval function
319 Val
= eval(EvalStr
, {}, Dict
)
320 except Exception as Excpt
:
321 raise BadExpression(str(Excpt
))
323 if Operator
in {'and', 'or'}:
334 def __init__(self
, Expression
, SymbolTable
={}):
335 super(ValueExpression
, self
).__init
__(self
, Expression
, SymbolTable
)
336 self
._NoProcess
= False
337 if not isinstance(Expression
, type('')):
338 self
._Expr
= Expression
339 self
._NoProcess
= True
342 self
._Expr
= ReplaceExprMacro(Expression
.strip(),
344 SupportedInMacroList
)
346 if not self
._Expr
.strip():
347 raise BadExpression(ERR_EMPTY_EXPR
)
350 # The symbol table including PCD and macro mapping
352 self
._Symb
= CopyDict(SymbolTable
)
353 self
._Symb
.update(self
.LogicalOperators
)
355 self
._Len
= len(self
._Expr
)
357 self
._WarnExcept
= None
359 # Literal token without any conversion
360 self
._LiteralToken
= ''
362 # Public entry for this class
363 # @param RealValue: False: only evaluate if the expression is true or false, used for conditional expression
364 # True : return the evaluated str(value), used for PCD value
366 # @return: True or False if RealValue is False
367 # Evaluated value of string format if RealValue is True
369 def __call__(self
, RealValue
=False, Depth
=0):
375 self
._Expr
= self
._Expr
.strip()
376 if RealValue
and Depth
== 0:
377 self
._Token
= self
._Expr
378 if self
.__IsNumberToken
():
382 Token
= self
._GetToken
()
383 except BadExpression
:
385 if isinstance(Token
, type('')) and Token
.startswith('{') and Token
.endswith('}') and self
._Idx
>= self
._Len
:
391 Val
= self
._ConExpr
()
393 if isinstance(Val
, type('')):
399 elif not Val
.startswith('L"') and not Val
.startswith('{') and not Val
.startswith("L'") and not Val
.startswith("'"):
401 RealVal
= '"' + RealVal
+ '"'
403 # The expression has been parsed, but the end of expression is not reached
404 # It means the rest does not comply EBNF of <Expression>
405 if self
._Idx
!= self
._Len
:
406 raise BadExpression(ERR_SNYTAX
% self
._Expr
[self
._Idx
:])
409 RetVal
= str(RealVal
)
416 self
._WarnExcept
.result
= RetVal
417 raise self
._WarnExcept
421 # Template function to parse binary operators which have same precedence
422 # Expr [Operator Expr]*
423 def _ExprFuncTemplate(self
, EvalFunc
, OpSet
):
425 while self
._IsOperator
(OpSet
):
429 if self
._IsOperator
({':'}):
437 # PEP 238 -- Changing the Division Operator
438 # x/y to return a reasonable approximation of the mathematical result of the division ("true division")
439 # x//y to return the floor ("floor division")
444 Val
= self
.Eval(Op
, Val
, EvalFunc())
445 except WrnExpression
as Warn
:
446 self
._WarnExcept
= Warn
451 return self
._ExprFuncTemplate
(self
._OrExpr
, {'?', ':'})
455 return self
._ExprFuncTemplate
(self
._AndExpr
, {"OR", "or", "||"})
459 return self
._ExprFuncTemplate
(self
._BitOr
, {"AND", "and", "&&"})
463 return self
._ExprFuncTemplate
(self
._BitXor
, {"|"})
467 return self
._ExprFuncTemplate
(self
._BitAnd
, {"XOR", "xor", "^"})
471 return self
._ExprFuncTemplate
(self
._EqExpr
, {"&"})
475 Val
= self
._RelExpr
()
476 while self
._IsOperator
({"==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"}):
478 if Op
in {"!", "NOT", "not"}:
479 if not self
._IsOperator
({"IN", "in"}):
480 raise BadExpression(ERR_REL_NOT_IN
)
481 Op
+= ' ' + self
._Token
483 Val
= self
.Eval(Op
, Val
, self
._RelExpr
())
484 except WrnExpression
as Warn
:
485 self
._WarnExcept
= Warn
491 return self
._ExprFuncTemplate
(self
._ShiftExpr
, {"<=", ">=", "<", ">", "LE", "GE", "LT", "GT"})
493 def _ShiftExpr(self
):
494 return self
._ExprFuncTemplate
(self
._AddExpr
, {"<<", ">>"})
498 return self
._ExprFuncTemplate
(self
._MulExpr
, {"+", "-"})
502 return self
._ExprFuncTemplate
(self
._UnaryExpr
, {TAB_STAR
, "/", "%"})
505 def _UnaryExpr(self
):
506 if self
._IsOperator
({"!", "NOT", "not"}):
507 Val
= self
._UnaryExpr
()
509 return self
.Eval('not', Val
)
510 except WrnExpression
as Warn
:
511 self
._WarnExcept
= Warn
513 if self
._IsOperator
({"~"}):
514 Val
= self
._UnaryExpr
()
516 return self
.Eval('~', Val
)
517 except WrnExpression
as Warn
:
518 self
._WarnExcept
= Warn
520 return self
._IdenExpr
()
522 # Parse identifier or encapsulated expression
524 Tk
= self
._GetToken
()
526 Val
= self
._ConExpr
()
528 # _GetToken may also raise BadExpression
529 if self
._GetToken
() != ')':
530 raise BadExpression(ERR_MATCH
)
531 except BadExpression
:
532 raise BadExpression(ERR_MATCH
)
536 # Skip whitespace or tab
538 for Char
in self
._Expr
[self
._Idx
:]:
539 if Char
not in ' \t':
543 # Try to convert string to number
544 def __IsNumberToken(self
):
546 if self
._Token
.lower()[0:2] == '0x' and len(self
._Token
) > 2:
548 if self
._Token
.startswith('"') or self
._Token
.startswith('L"'):
550 for Index
in range(len(self
._Token
)):
551 if self
._Token
[Index
] in {'"'}:
552 if self
._Token
[Index
- 1] == '\\':
555 if Flag
== 2 and self
._Token
.endswith('"'):
557 if self
._Token
.startswith("'") or self
._Token
.startswith("L'"):
559 for Index
in range(len(self
._Token
)):
560 if self
._Token
[Index
] in {"'"}:
561 if self
._Token
[Index
- 1] == '\\':
564 if Flag
== 2 and self
._Token
.endswith("'"):
567 self
._Token
= int(self
._Token
, Radix
)
575 def __GetArray(self
):
578 self
.__GetNList
(True)
579 Token
+= self
._LiteralToken
580 if self
._Idx
>= self
._Len
or self
._Expr
[self
._Idx
] != '}':
581 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
584 # All whitespace and tabs in array are already stripped.
585 IsArray
= IsGuid
= False
586 if len(Token
.split(',')) == 11 and len(Token
.split(',{')) == 2 \
587 and len(Token
.split('},')) == 1:
588 HexLen
= [11, 6, 6, 5, 4, 4, 4, 4, 4, 4, 6]
589 HexList
= Token
.split(',')
590 if HexList
[3].startswith('{') and \
591 not [Index
for Index
, Hex
in enumerate(HexList
) if len(Hex
) > HexLen
[Index
]]:
593 if Token
.lstrip('{').rstrip('}').find('{') == -1:
594 if not [Hex
for Hex
in Token
.lstrip('{').rstrip('}').split(',') if len(Hex
) > 4]:
596 if not IsArray
and not IsGuid
:
597 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
599 self
._Token
= self
._LiteralToken
= Token
602 # Parse string, the format must be: "..."
603 def __GetString(self
):
609 # Replace escape \\\", \"
610 if self
._Expr
[Idx
] == '"':
611 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace('\\\"', '\\\'')
616 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
617 if not self
._Token
.endswith('"'):
618 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
619 #Replace escape \\\', \'
620 elif self
._Expr
[Idx
] == "'":
621 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace("\\\'", "\\\"")
626 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
627 if not self
._Token
.endswith("'"):
628 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
629 self
._Token
= self
._Token
[1:-1]
632 # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
633 # @param IsAlphaOp: Indicate if parsing general token or script operator(EQ, NE...)
634 def __GetIdToken(self
, IsAlphaOp
= False):
636 for Ch
in self
._Expr
[self
._Idx
:]:
637 if not self
.__IsIdChar
(Ch
) or ('?' in self
._Expr
and Ch
== ':'):
642 self
._Token
= self
._LiteralToken
= IdToken
644 self
.__ResolveToken
()
647 # Try to resolve token
648 def __ResolveToken(self
):
650 raise BadExpression(ERR_EMPTY_TOKEN
)
653 if PcdPattern
.match(self
._Token
):
654 if self
._Token
not in self
._Symb
:
655 Ex
= BadExpression(ERR_PCD_RESOLVE
% self
._Token
)
658 self
._Token
= ValueExpression(self
._Symb
[self
._Token
], self
._Symb
)(True, self
._Depth
+1)
659 if not isinstance(self
._Token
, type('')):
660 self
._LiteralToken
= hex(self
._Token
)
663 if self
._Token
.startswith('"'):
664 self
._Token
= self
._Token
[1:-1]
665 elif self
._Token
in {"FALSE", "false", "False"}:
667 elif self
._Token
in {"TRUE", "true", "True"}:
670 self
.__IsNumberToken
()
672 def __GetNList(self
, InArray
=False):
673 self
._GetSingleToken
()
674 if not self
.__IsHexLiteral
():
676 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
680 Expr
= self
._Expr
[self
._Idx
:]
681 if not Expr
.startswith(','):
684 NList
= self
._LiteralToken
685 while Expr
.startswith(','):
689 self
._GetSingleToken
()
690 if not self
.__IsHexLiteral
():
691 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
692 NList
+= self
._LiteralToken
694 Expr
= self
._Expr
[self
._Idx
:]
695 self
._Token
= self
._LiteralToken
= NList
698 def __IsHexLiteral(self
):
699 if self
._LiteralToken
.startswith('{') and \
700 self
._LiteralToken
.endswith('}'):
703 if gHexPattern
.match(self
._LiteralToken
):
704 Token
= self
._LiteralToken
[2:]
706 self
._LiteralToken
= '0x0'
708 self
._LiteralToken
= '0x' + Token
713 return self
.__GetNList
()
717 return Ch
in '._:' or Ch
.isalnum()
720 def _GetSingleToken(self
):
722 Expr
= self
._Expr
[self
._Idx
:]
723 if Expr
.startswith('L"'):
726 UStr
= self
.__GetString
()
727 self
._Token
= 'L"' + UStr
+ '"'
729 elif Expr
.startswith("L'"):
732 UStr
= self
.__GetString
()
733 self
._Token
= "L'" + UStr
+ "'"
735 elif Expr
.startswith("'"):
736 UStr
= self
.__GetString
()
737 self
._Token
= "'" + UStr
+ "'"
739 elif Expr
.startswith('UINT'):
740 Re
= re
.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)')
742 RetValue
= Re
.search(Expr
).group(1)
744 raise BadExpression('Invalid Expression %s' % Expr
)
749 Prefix
= self
._Expr
[Idx
:self
._Idx
- 1]
752 TmpValue
= self
._Expr
[Idx
:self
._Idx
- 1]
753 TmpValue
= ValueExpression(TmpValue
)(True)
754 TmpValue
= '0x%x' % int(TmpValue
) if not isinstance(TmpValue
, type('')) else TmpValue
756 self
._Token
, Size
= ParseFieldValue(Prefix
+ '(' + TmpValue
+ ')')
762 Match
= gGuidPattern
.match(Expr
)
763 if Match
and not Expr
[Match
.end():Match
.end()+1].isalnum() \
764 and Expr
[Match
.end():Match
.end()+1] != '_':
765 self
._Idx
+= Match
.end()
766 self
._Token
= ValueExpression(GuidStringToGuidStructureString(Expr
[0:Match
.end()]))(True, self
._Depth
+1)
768 elif self
.__IsIdChar
(Ch
):
769 return self
.__GetIdToken
()
771 return self
.__GetString
()
773 return self
.__GetArray
()
774 elif Ch
== '(' or Ch
== ')':
779 raise BadExpression(ERR_VALID_TOKEN
% Expr
)
782 def _GetOperator(self
):
784 LegalOpLst
= ['&&', '||', '!=', '==', '>=', '<='] + self
.NonLetterOpLst
+ ['?', ':']
787 Expr
= self
._Expr
[self
._Idx
:]
789 # Reach end of expression
793 # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
794 if Expr
[0].isalpha():
795 return self
.__GetIdToken
(True)
797 # Start to get regular operator: +, -, <, > ...
798 if Expr
[0] not in self
.NonLetterOpLst
:
803 if Ch
in self
.NonLetterOpLst
:
804 if Ch
in ['!', '~'] and OpToken
:
811 if OpToken
not in LegalOpLst
:
812 raise BadExpression(ERR_OPERATOR_UNSUPPORT
% OpToken
)
813 self
._Token
= OpToken
816 class ValueExpressionEx(ValueExpression
):
817 def __init__(self
, PcdValue
, PcdType
, SymbolTable
={}):
818 ValueExpression
.__init
__(self
, PcdValue
, SymbolTable
)
819 self
.PcdValue
= PcdValue
820 self
.PcdType
= PcdType
822 def __call__(self
, RealValue
=False, Depth
=0):
823 PcdValue
= self
.PcdValue
824 if "{CODE(" not in PcdValue
:
826 PcdValue
= ValueExpression
.__call
__(self
, RealValue
, Depth
)
827 if self
.PcdType
== TAB_VOID
and (PcdValue
.startswith("'") or PcdValue
.startswith("L'")):
828 PcdValue
, Size
= ParseFieldValue(PcdValue
)
830 for I
in range(Size
):
831 PcdValueList
.append('0x%02X'%(PcdValue
& 0xff))
832 PcdValue
= PcdValue
>> 8
833 PcdValue
= '{' + ','.join(PcdValueList
) + '}'
834 elif self
.PcdType
in TAB_PCD_NUMERIC_TYPES
and (PcdValue
.startswith("'") or \
835 PcdValue
.startswith('"') or PcdValue
.startswith("L'") or PcdValue
.startswith('L"') or PcdValue
.startswith('{')):
837 except WrnExpression
as Value
:
838 PcdValue
= Value
.result
839 except BadExpression
as Value
:
840 if self
.PcdType
in TAB_PCD_NUMERIC_TYPES
:
841 PcdValue
= PcdValue
.strip()
842 if PcdValue
.startswith('{') and PcdValue
.endswith('}'):
843 PcdValue
= SplitPcdValueString(PcdValue
[1:-1])
844 if isinstance(PcdValue
, type([])):
848 for Item
in PcdValue
:
850 if Item
.startswith(TAB_UINT8
):
852 ValueType
= TAB_UINT8
853 elif Item
.startswith(TAB_UINT16
):
855 ValueType
= TAB_UINT16
856 elif Item
.startswith(TAB_UINT32
):
858 ValueType
= TAB_UINT32
859 elif Item
.startswith(TAB_UINT64
):
861 ValueType
= TAB_UINT64
862 elif Item
[0] in {'"', "'", 'L'}:
867 ValueType
= TAB_UINT8
868 Item
= ValueExpressionEx(Item
, ValueType
, self
._Symb
)(True)
871 tmpValue
= int(Item
, 0)
873 raise BadExpression("Byte array number %s should less than 0xFF." % Item
)
874 except BadExpression
as Value
:
875 raise BadExpression(Value
)
878 ItemValue
, ItemSize
= ParseFieldValue(Item
)
880 ItemValue
= ParseFieldValue(Item
)[0]
882 if isinstance(ItemValue
, type('')):
883 ItemValue
= int(ItemValue
, 0)
885 TmpValue
= (ItemValue
<< (Size
* 8)) | TmpValue
886 Size
= Size
+ ItemSize
889 TmpValue
, Size
= ParseFieldValue(PcdValue
)
890 except BadExpression
as Value
:
891 raise BadExpression("Type: %s, Value: %s, %s" % (self
.PcdType
, PcdValue
, Value
))
892 if isinstance(TmpValue
, type('')):
894 TmpValue
= int(TmpValue
)
896 raise BadExpression(Value
)
898 PcdValue
= '0x%0{}X'.format(Size
) % (TmpValue
)
900 raise BadExpression('Type %s PCD Value is negative' % self
.PcdType
)
901 if self
.PcdType
== TAB_UINT8
and Size
> 1:
902 raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self
.PcdType
)
903 if self
.PcdType
== TAB_UINT16
and Size
> 2:
904 raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self
.PcdType
)
905 if self
.PcdType
== TAB_UINT32
and Size
> 4:
906 raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self
.PcdType
)
907 if self
.PcdType
== TAB_UINT64
and Size
> 8:
908 raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self
.PcdType
)
911 TmpValue
= int(PcdValue
)
913 if TmpValue
.bit_length() == 0:
916 for I
in range((TmpValue
.bit_length() + 7) // 8):
917 TmpList
.append('0x%02x' % ((TmpValue
>> I
* 8) & 0xff))
918 PcdValue
= '{' + ', '.join(TmpList
) + '}'
920 if PcdValue
.strip().startswith('{'):
921 PcdValueList
= SplitPcdValueString(PcdValue
.strip()[1:-1])
925 for Item
in PcdValueList
:
926 # compute byte offset of every LABEL
927 LabelList
= _ReLabel
.findall(Item
)
928 Item
= _ReLabel
.sub('', Item
)
931 for Label
in LabelList
:
932 if not IsValidCName(Label
):
933 raise BadExpression('%s is not a valid c variable name' % Label
)
934 if Label
not in LabelDict
:
935 LabelDict
[Label
] = str(LabelOffset
)
936 if Item
.startswith(TAB_UINT8
):
937 LabelOffset
= LabelOffset
+ 1
938 elif Item
.startswith(TAB_UINT16
):
939 LabelOffset
= LabelOffset
+ 2
940 elif Item
.startswith(TAB_UINT32
):
941 LabelOffset
= LabelOffset
+ 4
942 elif Item
.startswith(TAB_UINT64
):
943 LabelOffset
= LabelOffset
+ 8
946 ItemValue
, ItemSize
= ParseFieldValue(Item
)
947 LabelOffset
= LabelOffset
+ ItemSize
949 LabelOffset
= LabelOffset
+ 1
951 for Item
in PcdValueList
:
955 Item
= _ReLabel
.sub('', Item
)
959 OffsetList
= _ReOffset
.findall(Item
)
962 # replace each offset, except errors
963 for Offset
in OffsetList
:
965 Item
= Item
.replace('OFFSET_OF({})'.format(Offset
), LabelDict
[Offset
])
967 raise BadExpression('%s not defined' % Offset
)
969 NewPcdValueList
.append(Item
)
972 for Item
in NewPcdValueList
:
975 TokenSpaceGuidName
= ''
976 if Item
.startswith(TAB_GUID
) and Item
.endswith(')'):
978 TokenSpaceGuidName
= re
.search('GUID\((\w+)\)', Item
).group(1)
981 if TokenSpaceGuidName
and TokenSpaceGuidName
in self
._Symb
:
982 Item
= 'GUID(' + self
._Symb
[TokenSpaceGuidName
] + ')'
983 elif TokenSpaceGuidName
:
984 raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName
)
985 Item
, Size
= ParseFieldValue(Item
)
986 for Index
in range(0, Size
):
987 ValueStr
= '0x%02X' % (int(Item
) & 255)
989 AllPcdValueList
.append(ValueStr
)
991 elif Item
.startswith('DEVICE_PATH') and Item
.endswith(')'):
992 Item
, Size
= ParseFieldValue(Item
)
993 AllPcdValueList
.append(Item
[1:-1])
997 if Item
.startswith(TAB_UINT8
):
999 ValueType
= TAB_UINT8
1000 elif Item
.startswith(TAB_UINT16
):
1002 ValueType
= TAB_UINT16
1003 elif Item
.startswith(TAB_UINT32
):
1005 ValueType
= TAB_UINT32
1006 elif Item
.startswith(TAB_UINT64
):
1008 ValueType
= TAB_UINT64
1012 TmpValue
= ValueExpressionEx(Item
, ValueType
, self
._Symb
)(True)
1014 TmpValue
= ValueExpressionEx(Item
, self
.PcdType
, self
._Symb
)(True)
1015 Item
= '0x%x' % TmpValue
if not isinstance(TmpValue
, type('')) else TmpValue
1017 ItemValue
, ItemSize
= ParseFieldValue(Item
)
1018 if Item
[0] not in {'"', 'L', '{'} and ItemSize
> 1:
1019 raise BadExpression("Byte array number %s should less than 0xFF." % Item
)
1021 ItemValue
= ParseFieldValue(Item
)[0]
1022 for I
in range(0, ItemSize
):
1023 ValueStr
= '0x%02X' % (int(ItemValue
) & 255)
1025 AllPcdValueList
.append(ValueStr
)
1029 PcdValue
= '{' + ', '.join(AllPcdValueList
) + '}'
1031 raise BadExpression("Type: %s, Value: %s, %s"%(self
.PcdType
, PcdValue
, Value
))
1033 if PcdValue
== 'True':
1035 if PcdValue
== 'False':
1041 if __name__
== '__main__':
1044 input = raw_input('Input expr: ')
1048 print(ValueExpression(input)(True))
1049 print(ValueExpression(input)(False))
1050 except WrnExpression
as Ex
:
1053 except Exception as Ex
: