30711cedd784f83922430c39b95a7343f09a0473
[mirror_edk2.git] / BaseTools / Source / Python / Common / Expression.py
1 ## @file
2 # This file is used to parse and evaluate expression in directive or PCD value.
3 #
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
9 #
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.
12
13 ## Import Modules
14 #
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
20 import copy
21
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).'
42
43 __ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')
44
45 ## SplitString
46 # Split string to list according double quote
47 # For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']
48 #
49 def SplitString(String):
50 # There might be escaped quote: "abc\"def\\\"ghi", 'abc\'def\\\'ghi'
51 RetList = []
52 InSingleQuote = False
53 InDoubleQuote = False
54 Item = ''
55 for i, ch in enumerate(String):
56 if ch == '"' and not InSingleQuote:
57 if String[i - 1] != '\\':
58 InDoubleQuote = not InDoubleQuote
59 if not InDoubleQuote:
60 Item += String[i]
61 RetList.append(Item)
62 Item = ''
63 continue
64 if Item:
65 RetList.append(Item)
66 Item = ''
67 elif ch == "'" and not InDoubleQuote:
68 if String[i - 1] != '\\':
69 InSingleQuote = not InSingleQuote
70 if not InSingleQuote:
71 Item += String[i]
72 RetList.append(Item)
73 Item = ''
74 continue
75 if Item:
76 RetList.append(Item)
77 Item = ''
78 Item += String[i]
79 if InSingleQuote or InDoubleQuote:
80 raise BadExpression(ERR_STRING_TOKEN % Item)
81 if Item:
82 RetList.append(Item)
83 return RetList
84
85 def SplitPcdValueString(String):
86 # There might be escaped comma in GUID() or DEVICE_PATH() or " "
87 # or ' ' or L' ' or L" "
88 RetList = []
89 InParenthesis = 0
90 InSingleQuote = False
91 InDoubleQuote = False
92 Item = ''
93 for i, ch in enumerate(String):
94 if ch == '(':
95 InParenthesis += 1
96 elif ch == ')':
97 if InParenthesis:
98 InParenthesis -= 1
99 else:
100 raise BadExpression(ERR_STRING_TOKEN % Item)
101 elif ch == '"' and not InSingleQuote:
102 if String[i-1] != '\\':
103 InDoubleQuote = not InDoubleQuote
104 elif ch == "'" and not InDoubleQuote:
105 if String[i-1] != '\\':
106 InSingleQuote = not InSingleQuote
107 elif ch == ',':
108 if InParenthesis or InSingleQuote or InDoubleQuote:
109 Item += String[i]
110 continue
111 elif Item:
112 RetList.append(Item)
113 Item = ''
114 continue
115 Item += String[i]
116 if InSingleQuote or InDoubleQuote or InParenthesis:
117 raise BadExpression(ERR_STRING_TOKEN % Item)
118 if Item:
119 RetList.append(Item)
120 return RetList
121
122 def IsValidCName(Str):
123 return True if __ValidString.match(Str) else False
124
125 def BuildOptionValue(PcdValue, GuidDict):
126 if PcdValue.startswith('H'):
127 InputValue = PcdValue[1:]
128 elif PcdValue.startswith("L'") or PcdValue.startswith("'"):
129 InputValue = PcdValue
130 elif PcdValue.startswith('L'):
131 InputValue = 'L"' + PcdValue[1:] + '"'
132 else:
133 InputValue = PcdValue
134 if IsFieldValueAnArray(InputValue):
135 try:
136 PcdValue = ValueExpressionEx(InputValue, 'VOID*', GuidDict)(True)
137 except:
138 pass
139 return PcdValue
140
141 ## ReplaceExprMacro
142 #
143 def ReplaceExprMacro(String, Macros, ExceptionList = None):
144 StrList = SplitString(String)
145 for i, String in enumerate(StrList):
146 InQuote = False
147 if String.startswith('"'):
148 InQuote = True
149 MacroStartPos = String.find('$(')
150 if MacroStartPos < 0:
151 for Pcd in gPlatformPcds.keys():
152 if Pcd in String:
153 if Pcd not in gConditionalPcds:
154 gConditionalPcds.append(Pcd)
155 continue
156 RetStr = ''
157 while MacroStartPos >= 0:
158 RetStr = String[0:MacroStartPos]
159 MacroEndPos = String.find(')', MacroStartPos)
160 if MacroEndPos < 0:
161 raise BadExpression(ERR_MACRO_TOKEN % String[MacroStartPos:])
162 Macro = String[MacroStartPos+2:MacroEndPos]
163 if Macro not in Macros:
164 # From C reference manual:
165 # If an undefined macro name appears in the constant-expression of
166 # !if or !elif, it is replaced by the integer constant 0.
167 RetStr += '0'
168 elif not InQuote:
169 Tklst = RetStr.split()
170 if Tklst and Tklst[-1] in ['IN', 'in'] and ExceptionList and Macro not in ExceptionList:
171 raise BadExpression(ERR_IN_OPERAND)
172 # Make sure the macro in exception list is encapsulated by double quote
173 # For example: DEFINE ARCH = IA32 X64
174 # $(ARCH) is replaced with "IA32 X64"
175 if ExceptionList and Macro in ExceptionList:
176 RetStr += '"' + Macros[Macro] + '"'
177 elif Macros[Macro].strip():
178 RetStr += Macros[Macro]
179 else:
180 RetStr += '""'
181 else:
182 RetStr += Macros[Macro]
183 RetStr += String[MacroEndPos+1:]
184 String = RetStr
185 MacroStartPos = String.find('$(')
186 StrList[i] = RetStr
187 return ''.join(StrList)
188
189 # transfer int to string for in/not in expression
190 def IntToStr(Value):
191 StrList = []
192 while Value > 0:
193 StrList.append(chr(Value & 0xff))
194 Value = Value >> 8
195 Value = '"' + ''.join(StrList) + '"'
196 return Value
197
198 SupportedInMacroList = ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY']
199
200 class ValueExpression(object):
201 # Logical operator mapping
202 LogicalOperators = {
203 '&&' : 'and', '||' : 'or',
204 '!' : 'not', 'AND': 'and',
205 'OR' : 'or' , 'NOT': 'not',
206 'XOR': '^' , 'xor': '^',
207 'EQ' : '==' , 'NE' : '!=',
208 'GT' : '>' , 'LT' : '<',
209 'GE' : '>=' , 'LE' : '<=',
210 'IN' : 'in'
211 }
212
213 NonLetterOpLst = ['+', '-', '*', '/', '%', '&', '|', '^', '~', '<<', '>>', '!', '=', '>', '<', '?', ':']
214
215 PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
216
217 SymbolPattern = re.compile("("
218 "\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|"
219 "&&|\|\||!(?!=)|"
220 "(?<=\W)AND(?=\W)|(?<=\W)OR(?=\W)|(?<=\W)NOT(?=\W)|(?<=\W)XOR(?=\W)|"
221 "(?<=\W)EQ(?=\W)|(?<=\W)NE(?=\W)|(?<=\W)GT(?=\W)|(?<=\W)LT(?=\W)|(?<=\W)GE(?=\W)|(?<=\W)LE(?=\W)"
222 ")")
223
224 @staticmethod
225 def Eval(Operator, Oprand1, Oprand2 = None):
226 WrnExp = None
227
228 if Operator not in ["==", "!=", ">=", "<=", ">", "<", "in", "not in"] and \
229 (type(Oprand1) == type('') or type(Oprand2) == type('')):
230 raise BadExpression(ERR_STRING_EXPR % Operator)
231 if Operator in ['in', 'not in']:
232 if type(Oprand1) != type(''):
233 Oprand1 = IntToStr(Oprand1)
234 if type(Oprand2) != type(''):
235 Oprand2 = IntToStr(Oprand2)
236 TypeDict = {
237 type(0) : 0,
238 type(0L) : 0,
239 type('') : 1,
240 type(True) : 2
241 }
242
243 EvalStr = ''
244 if Operator in ["!", "NOT", "not"]:
245 if type(Oprand1) == type(''):
246 raise BadExpression(ERR_STRING_EXPR % Operator)
247 EvalStr = 'not Oprand1'
248 elif Operator in ["~"]:
249 if type(Oprand1) == type(''):
250 raise BadExpression(ERR_STRING_EXPR % Operator)
251 EvalStr = '~ Oprand1'
252 else:
253 if Operator in ["+", "-"] and (type(True) in [type(Oprand1), type(Oprand2)]):
254 # Boolean in '+'/'-' will be evaluated but raise warning
255 WrnExp = WrnExpression(WRN_BOOL_EXPR)
256 elif type('') in [type(Oprand1), type(Oprand2)] and type(Oprand1)!= type(Oprand2):
257 # == between string and number/boolean will always return False, != return True
258 if Operator == "==":
259 WrnExp = WrnExpression(WRN_EQCMP_STR_OTHERS)
260 WrnExp.result = False
261 raise WrnExp
262 elif Operator == "!=":
263 WrnExp = WrnExpression(WRN_NECMP_STR_OTHERS)
264 WrnExp.result = True
265 raise WrnExp
266 else:
267 raise BadExpression(ERR_RELCMP_STR_OTHERS % Operator)
268 elif TypeDict[type(Oprand1)] != TypeDict[type(Oprand2)]:
269 if Operator in ["==", "!=", ">=", "<=", ">", "<"] and set((TypeDict[type(Oprand1)], TypeDict[type(Oprand2)])) == set((TypeDict[type(True)], TypeDict[type(0)])):
270 # comparison between number and boolean is allowed
271 pass
272 elif Operator in ['&', '|', '^', "and", "or"] and set((TypeDict[type(Oprand1)], TypeDict[type(Oprand2)])) == set((TypeDict[type(True)], TypeDict[type(0)])):
273 # bitwise and logical operation between number and boolean is allowed
274 pass
275 else:
276 raise BadExpression(ERR_EXPR_TYPE)
277 if type(Oprand1) == type('') and type(Oprand2) == type(''):
278 if (Oprand1.startswith('L"') and not Oprand2.startswith('L"')) or \
279 (not Oprand1.startswith('L"') and Oprand2.startswith('L"')):
280 raise BadExpression(ERR_STRING_CMP % (Oprand1, Operator, Oprand2))
281 if 'in' in Operator and type(Oprand2) == type(''):
282 Oprand2 = Oprand2.split()
283 EvalStr = 'Oprand1 ' + Operator + ' Oprand2'
284
285 # Local symbols used by built in eval function
286 Dict = {
287 'Oprand1' : Oprand1,
288 'Oprand2' : Oprand2
289 }
290 try:
291 Val = eval(EvalStr, {}, Dict)
292 except Exception, Excpt:
293 raise BadExpression(str(Excpt))
294
295 if Operator in ['and', 'or']:
296 if Val:
297 Val = True
298 else:
299 Val = False
300
301 if WrnExp:
302 WrnExp.result = Val
303 raise WrnExp
304 return Val
305
306 def __init__(self, Expression, SymbolTable={}):
307 self._NoProcess = False
308 if type(Expression) != type(''):
309 self._Expr = Expression
310 self._NoProcess = True
311 return
312
313 self._Expr = ReplaceExprMacro(Expression.strip(),
314 SymbolTable,
315 SupportedInMacroList)
316
317 if not self._Expr.strip():
318 raise BadExpression(ERR_EMPTY_EXPR)
319
320 #
321 # The symbol table including PCD and macro mapping
322 #
323 self._Symb = copy.deepcopy(SymbolTable)
324 self._Symb.update(self.LogicalOperators)
325 self._Idx = 0
326 self._Len = len(self._Expr)
327 self._Token = ''
328 self._WarnExcept = None
329
330 # Literal token without any conversion
331 self._LiteralToken = ''
332
333 # Public entry for this class
334 # @param RealValue: False: only evaluate if the expression is true or false, used for conditional expression
335 # True : return the evaluated str(value), used for PCD value
336 #
337 # @return: True or False if RealValue is False
338 # Evaluated value of string format if RealValue is True
339 #
340 def __call__(self, RealValue=False, Depth=0):
341 if self._NoProcess:
342 return self._Expr
343
344 self._Depth = Depth
345
346 self._Expr = self._Expr.strip()
347 if RealValue and Depth == 0:
348 self._Token = self._Expr
349 if self.__IsNumberToken():
350 return self._Expr
351 Token = ''
352 try:
353 Token = self._GetToken()
354 except BadExpression:
355 pass
356 if type(Token) == type('') and Token.startswith('{') and Token.endswith('}') and self._Idx >= self._Len:
357 return self._Expr
358
359 self._Idx = 0
360 self._Token = ''
361
362 Val = self._ConExpr()
363 RealVal = Val
364 if type(Val) == type(''):
365 if Val == 'L""':
366 Val = False
367 elif not Val:
368 Val = False
369 RealVal = '""'
370 elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'"):
371 Val = True
372 RealVal = '"' + RealVal + '"'
373
374 # The expression has been parsed, but the end of expression is not reached
375 # It means the rest does not comply EBNF of <Expression>
376 if self._Idx != self._Len:
377 raise BadExpression(ERR_SNYTAX % self._Expr[self._Idx:])
378
379 if RealValue:
380 RetVal = str(RealVal)
381 elif Val:
382 RetVal = True
383 else:
384 RetVal = False
385
386 if self._WarnExcept:
387 self._WarnExcept.result = RetVal
388 raise self._WarnExcept
389 else:
390 return RetVal
391
392 # Template function to parse binary operators which have same precedence
393 # Expr [Operator Expr]*
394 def _ExprFuncTemplate(self, EvalFunc, OpLst):
395 Val = EvalFunc()
396 while self._IsOperator(OpLst):
397 Op = self._Token
398 if Op == '?':
399 Val2 = EvalFunc()
400 if self._IsOperator(':'):
401 Val3 = EvalFunc()
402 if Val:
403 Val = Val2
404 else:
405 Val = Val3
406 continue
407 try:
408 Val = self.Eval(Op, Val, EvalFunc())
409 except WrnExpression, Warn:
410 self._WarnExcept = Warn
411 Val = Warn.result
412 return Val
413 # A [? B]*
414 def _ConExpr(self):
415 return self._ExprFuncTemplate(self._OrExpr, ['?', ':'])
416
417 # A [|| B]*
418 def _OrExpr(self):
419 return self._ExprFuncTemplate(self._AndExpr, ["OR", "or", "||"])
420
421 # A [&& B]*
422 def _AndExpr(self):
423 return self._ExprFuncTemplate(self._BitOr, ["AND", "and", "&&"])
424
425 # A [ | B]*
426 def _BitOr(self):
427 return self._ExprFuncTemplate(self._BitXor, ["|"])
428
429 # A [ ^ B]*
430 def _BitXor(self):
431 return self._ExprFuncTemplate(self._BitAnd, ["XOR", "xor", "^"])
432
433 # A [ & B]*
434 def _BitAnd(self):
435 return self._ExprFuncTemplate(self._EqExpr, ["&"])
436
437 # A [ == B]*
438 def _EqExpr(self):
439 Val = self._RelExpr()
440 while self._IsOperator(["==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"]):
441 Op = self._Token
442 if Op in ["!", "NOT", "not"]:
443 if not self._IsOperator(["IN", "in"]):
444 raise BadExpression(ERR_REL_NOT_IN)
445 Op += ' ' + self._Token
446 try:
447 Val = self.Eval(Op, Val, self._RelExpr())
448 except WrnExpression, Warn:
449 self._WarnExcept = Warn
450 Val = Warn.result
451 return Val
452
453 # A [ > B]*
454 def _RelExpr(self):
455 return self._ExprFuncTemplate(self._ShiftExpr, ["<=", ">=", "<", ">", "LE", "GE", "LT", "GT"])
456
457 def _ShiftExpr(self):
458 return self._ExprFuncTemplate(self._AddExpr, ["<<", ">>"])
459
460 # A [ + B]*
461 def _AddExpr(self):
462 return self._ExprFuncTemplate(self._MulExpr, ["+", "-"])
463
464 # A [ * B]*
465 def _MulExpr(self):
466 return self._ExprFuncTemplate(self._UnaryExpr, ["*", "/", "%"])
467
468 # [!]*A
469 def _UnaryExpr(self):
470 if self._IsOperator(["!", "NOT", "not"]):
471 Val = self._UnaryExpr()
472 try:
473 return self.Eval('not', Val)
474 except WrnExpression, Warn:
475 self._WarnExcept = Warn
476 return Warn.result
477 if self._IsOperator(["~"]):
478 Val = self._UnaryExpr()
479 try:
480 return self.Eval('~', Val)
481 except WrnExpression, Warn:
482 self._WarnExcept = Warn
483 return Warn.result
484 return self._IdenExpr()
485
486 # Parse identifier or encapsulated expression
487 def _IdenExpr(self):
488 Tk = self._GetToken()
489 if Tk == '(':
490 Val = self._ConExpr()
491 try:
492 # _GetToken may also raise BadExpression
493 if self._GetToken() != ')':
494 raise BadExpression(ERR_MATCH)
495 except BadExpression:
496 raise BadExpression(ERR_MATCH)
497 return Val
498 return Tk
499
500 # Skip whitespace or tab
501 def __SkipWS(self):
502 for Char in self._Expr[self._Idx:]:
503 if Char not in ' \t':
504 break
505 self._Idx += 1
506
507 # Try to convert string to number
508 def __IsNumberToken(self):
509 Radix = 10
510 if self._Token.lower()[0:2] == '0x' and len(self._Token) > 2:
511 Radix = 16
512 if self._Token.startswith('"') or self._Token.startswith('L"'):
513 Flag = 0
514 for Index in range(len(self._Token)):
515 if self._Token[Index] in ['"']:
516 if self._Token[Index - 1] == '\\':
517 continue
518 Flag += 1
519 if Flag == 2 and self._Token.endswith('"'):
520 return True
521 if self._Token.startswith("'") or self._Token.startswith("L'"):
522 Flag = 0
523 for Index in range(len(self._Token)):
524 if self._Token[Index] in ["'"]:
525 if self._Token[Index - 1] == '\\':
526 continue
527 Flag += 1
528 if Flag == 2 and self._Token.endswith("'"):
529 return True
530 try:
531 self._Token = int(self._Token, Radix)
532 return True
533 except ValueError:
534 return False
535 except TypeError:
536 return False
537
538 # Parse array: {...}
539 def __GetArray(self):
540 Token = '{'
541 self._Idx += 1
542 self.__GetNList(True)
543 Token += self._LiteralToken
544 if self._Idx >= self._Len or self._Expr[self._Idx] != '}':
545 raise BadExpression(ERR_ARRAY_TOKEN % Token)
546 Token += '}'
547
548 # All whitespace and tabs in array are already stripped.
549 IsArray = IsGuid = False
550 if len(Token.split(',')) == 11 and len(Token.split(',{')) == 2 \
551 and len(Token.split('},')) == 1:
552 HexLen = [11,6,6,5,4,4,4,4,4,4,6]
553 HexList= Token.split(',')
554 if HexList[3].startswith('{') and \
555 not [Index for Index, Hex in enumerate(HexList) if len(Hex) > HexLen[Index]]:
556 IsGuid = True
557 if Token.lstrip('{').rstrip('}').find('{') == -1:
558 if not [Hex for Hex in Token.lstrip('{').rstrip('}').split(',') if len(Hex) > 4]:
559 IsArray = True
560 if not IsArray and not IsGuid:
561 raise BadExpression(ERR_ARRAY_TOKEN % Token)
562 self._Idx += 1
563 self._Token = self._LiteralToken = Token
564 return self._Token
565
566 # Parse string, the format must be: "..."
567 def __GetString(self):
568 Idx = self._Idx
569
570 # Skip left quote
571 self._Idx += 1
572
573 # Replace escape \\\", \"
574 if self._Expr[Idx] == '"':
575 Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace('\\\"', '\\\'')
576 for Ch in Expr:
577 self._Idx += 1
578 if Ch == '"':
579 break
580 self._Token = self._LiteralToken = self._Expr[Idx:self._Idx]
581 if not self._Token.endswith('"'):
582 raise BadExpression(ERR_STRING_TOKEN % self._Token)
583 #Replace escape \\\', \'
584 elif self._Expr[Idx] == "'":
585 Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace("\\\'", "\\\"")
586 for Ch in Expr:
587 self._Idx += 1
588 if Ch == "'":
589 break
590 self._Token = self._LiteralToken = self._Expr[Idx:self._Idx]
591 if not self._Token.endswith("'"):
592 raise BadExpression(ERR_STRING_TOKEN % self._Token)
593 self._Token = self._Token[1:-1]
594 return self._Token
595
596 # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
597 # @param IsAlphaOp: Indicate if parsing general token or script operator(EQ, NE...)
598 def __GetIdToken(self, IsAlphaOp = False):
599 IdToken = ''
600 for Ch in self._Expr[self._Idx:]:
601 if not self.__IsIdChar(Ch) or ('?' in self._Expr and Ch == ':'):
602 break
603 self._Idx += 1
604 IdToken += Ch
605
606 self._Token = self._LiteralToken = IdToken
607 if not IsAlphaOp:
608 self.__ResolveToken()
609 return self._Token
610
611 # Try to resolve token
612 def __ResolveToken(self):
613 if not self._Token:
614 raise BadExpression(ERR_EMPTY_TOKEN)
615
616 # PCD token
617 if self.PcdPattern.match(self._Token):
618 if self._Token not in self._Symb:
619 Ex = BadExpression(ERR_PCD_RESOLVE % self._Token)
620 Ex.Pcd = self._Token
621 raise Ex
622 self._Token = ValueExpression(self._Symb[self._Token], self._Symb)(True, self._Depth+1)
623 if type(self._Token) != type(''):
624 self._LiteralToken = hex(self._Token)
625 return
626
627 if self._Token.startswith('"'):
628 self._Token = self._Token[1:-1]
629 elif self._Token in ["FALSE", "false", "False"]:
630 self._Token = False
631 elif self._Token in ["TRUE", "true", "True"]:
632 self._Token = True
633 else:
634 self.__IsNumberToken()
635
636 def __GetNList(self, InArray=False):
637 self._GetSingleToken()
638 if not self.__IsHexLiteral():
639 if InArray:
640 raise BadExpression(ERR_ARRAY_ELE % self._Token)
641 return self._Token
642
643 self.__SkipWS()
644 Expr = self._Expr[self._Idx:]
645 if not Expr.startswith(','):
646 return self._Token
647
648 NList = self._LiteralToken
649 while Expr.startswith(','):
650 NList += ','
651 self._Idx += 1
652 self.__SkipWS()
653 self._GetSingleToken()
654 if not self.__IsHexLiteral():
655 raise BadExpression(ERR_ARRAY_ELE % self._Token)
656 NList += self._LiteralToken
657 self.__SkipWS()
658 Expr = self._Expr[self._Idx:]
659 self._Token = self._LiteralToken = NList
660 return self._Token
661
662 def __IsHexLiteral(self):
663 if self._LiteralToken.startswith('{') and \
664 self._LiteralToken.endswith('}'):
665 return True
666
667 if gHexPattern.match(self._LiteralToken):
668 Token = self._LiteralToken[2:]
669 if not Token:
670 self._LiteralToken = '0x0'
671 else:
672 self._LiteralToken = '0x' + Token
673 return True
674 return False
675
676 def _GetToken(self):
677 return self.__GetNList()
678
679 @staticmethod
680 def __IsIdChar(Ch):
681 return Ch in '._:' or Ch.isalnum()
682
683 # Parse operand
684 def _GetSingleToken(self):
685 self.__SkipWS()
686 Expr = self._Expr[self._Idx:]
687 if Expr.startswith('L"'):
688 # Skip L
689 self._Idx += 1
690 UStr = self.__GetString()
691 self._Token = 'L"' + UStr + '"'
692 return self._Token
693 elif Expr.startswith("L'"):
694 # Skip L
695 self._Idx += 1
696 UStr = self.__GetString()
697 self._Token = "L'" + UStr + "'"
698 return self._Token
699 elif Expr.startswith("'"):
700 UStr = self.__GetString()
701 self._Token = "'" + UStr + "'"
702 return self._Token
703 elif Expr.startswith('UINT'):
704 Re = re.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)')
705 try:
706 RetValue = Re.search(Expr).group(1)
707 except:
708 raise BadExpression('Invalid Expression %s' % Expr)
709 Idx = self._Idx
710 for Ch in Expr:
711 self._Idx += 1
712 if Ch == '(':
713 Prefix = self._Expr[Idx:self._Idx - 1]
714 Idx = self._Idx
715 if Ch == ')':
716 TmpValue = self._Expr[Idx :self._Idx - 1]
717 TmpValue = ValueExpression(TmpValue)(True)
718 TmpValue = '0x%x' % int(TmpValue) if type(TmpValue) != type('') else TmpValue
719 break
720 self._Token, Size = ParseFieldValue(Prefix + '(' + TmpValue + ')')
721 return self._Token
722
723 self._Token = ''
724 if Expr:
725 Ch = Expr[0]
726 Match = gGuidPattern.match(Expr)
727 if Match and not Expr[Match.end():Match.end()+1].isalnum() \
728 and Expr[Match.end():Match.end()+1] != '_':
729 self._Idx += Match.end()
730 self._Token = ValueExpression(GuidStringToGuidStructureString(Expr[0:Match.end()]))(True, self._Depth+1)
731 return self._Token
732 elif self.__IsIdChar(Ch):
733 return self.__GetIdToken()
734 elif Ch == '"':
735 return self.__GetString()
736 elif Ch == '{':
737 return self.__GetArray()
738 elif Ch == '(' or Ch == ')':
739 self._Idx += 1
740 self._Token = Ch
741 return self._Token
742
743 raise BadExpression(ERR_VALID_TOKEN % Expr)
744
745 # Parse operator
746 def _GetOperator(self):
747 self.__SkipWS()
748 LegalOpLst = ['&&', '||', '!=', '==', '>=', '<='] + self.NonLetterOpLst + ['?',':']
749
750 self._Token = ''
751 Expr = self._Expr[self._Idx:]
752
753 # Reach end of expression
754 if not Expr:
755 return ''
756
757 # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
758 if Expr[0].isalpha():
759 return self.__GetIdToken(True)
760
761 # Start to get regular operator: +, -, <, > ...
762 if Expr[0] not in self.NonLetterOpLst:
763 return ''
764
765 OpToken = ''
766 for Ch in Expr:
767 if Ch in self.NonLetterOpLst:
768 if '!' == Ch and OpToken:
769 break
770 self._Idx += 1
771 OpToken += Ch
772 else:
773 break
774
775 if OpToken not in LegalOpLst:
776 raise BadExpression(ERR_OPERATOR_UNSUPPORT % OpToken)
777 self._Token = OpToken
778 return OpToken
779
780 # Check if current token matches the operators given from OpList
781 def _IsOperator(self, OpList):
782 Idx = self._Idx
783 self._GetOperator()
784 if self._Token in OpList:
785 if self._Token in self.LogicalOperators:
786 self._Token = self.LogicalOperators[self._Token]
787 return True
788 self._Idx = Idx
789 return False
790
791 class ValueExpressionEx(ValueExpression):
792 def __init__(self, PcdValue, PcdType, SymbolTable={}):
793 ValueExpression.__init__(self, PcdValue, SymbolTable)
794 self.PcdValue = PcdValue
795 self.PcdType = PcdType
796
797 def __call__(self, RealValue=False, Depth=0):
798 PcdValue = self.PcdValue
799 try:
800 PcdValue = ValueExpression.__call__(self, RealValue, Depth)
801 if self.PcdType == 'VOID*' and (PcdValue.startswith("'") or PcdValue.startswith("L'")):
802 PcdValue, Size = ParseFieldValue(PcdValue)
803 PcdValueList = []
804 for I in range(Size):
805 PcdValueList.append('0x%02X'%(PcdValue & 0xff))
806 PcdValue = PcdValue >> 8
807 PcdValue = '{' + ','.join(PcdValueList) + '}'
808 elif self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN'] and (PcdValue.startswith("'") or \
809 PcdValue.startswith('"') or PcdValue.startswith("L'") or PcdValue.startswith('L"') or PcdValue.startswith('{')):
810 raise BadExpression
811 except WrnExpression, Value:
812 PcdValue = Value.result
813 except BadExpression, Value:
814 if self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN']:
815 PcdValue = PcdValue.strip()
816 if type(PcdValue) == type('') and PcdValue.startswith('{') and PcdValue.endswith('}'):
817 PcdValue = SplitPcdValueString(PcdValue[1:-1])
818 if type(PcdValue) == type([]):
819 TmpValue = 0
820 Size = 0
821 ValueType = ''
822 for Item in PcdValue:
823 Item = Item.strip()
824 if Item.startswith('UINT8'):
825 ItemSize = 1
826 ValueType = 'UINT8'
827 elif Item.startswith('UINT16'):
828 ItemSize = 2
829 ValueType = 'UINT16'
830 elif Item.startswith('UINT32'):
831 ItemSize = 4
832 ValueType = 'UINT32'
833 elif Item.startswith('UINT64'):
834 ItemSize = 8
835 ValueType = 'UINT64'
836 elif Item.startswith('"') or Item.startswith("'") or Item.startswith('L'):
837 ItemSize = 0
838 ValueType = 'VOID*'
839 else:
840 ItemSize = 0
841 ValueType = 'UINT8'
842 Item = ValueExpressionEx(Item, ValueType, self._Symb)(True)
843
844 if ItemSize == 0:
845 try:
846 tmpValue = int(Item, 16) if Item.upper().startswith('0X') else int(Item, 0)
847 if tmpValue > 255:
848 raise BadExpression("Byte array number %s should less than 0xFF." % Item)
849 except BadExpression, Value:
850 raise BadExpression(Value)
851 except ValueError:
852 pass
853 ItemValue, ItemSize = ParseFieldValue(Item)
854 else:
855 ItemValue = ParseFieldValue(Item)[0]
856
857 if type(ItemValue) == type(''):
858 ItemValue = int(ItemValue, 16) if ItemValue.startswith('0x') else int(ItemValue)
859
860 TmpValue = (ItemValue << (Size * 8)) | TmpValue
861 Size = Size + ItemSize
862 else:
863 try:
864 TmpValue, Size = ParseFieldValue(PcdValue)
865 except BadExpression, Value:
866 raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value))
867 if type(TmpValue) == type(''):
868 try:
869 TmpValue = int(TmpValue)
870 except:
871 raise BadExpression(Value)
872 else:
873 PcdValue = '0x%0{}X'.format(Size) % (TmpValue)
874 if TmpValue < 0:
875 raise BadExpression('Type %s PCD Value is negative' % self.PcdType)
876 if self.PcdType == 'UINT8' and Size > 1:
877 raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self.PcdType)
878 if self.PcdType == 'UINT16' and Size > 2:
879 raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self.PcdType)
880 if self.PcdType == 'UINT32' and Size > 4:
881 raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self.PcdType)
882 if self.PcdType == 'UINT64' and Size > 8:
883 raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType)
884 else:
885 try:
886 TmpValue = long(PcdValue)
887 TmpList = []
888 if TmpValue.bit_length() == 0:
889 PcdValue = '{0x00}'
890 else:
891 for I in range((TmpValue.bit_length() + 7) / 8):
892 TmpList.append('0x%02x' % ((TmpValue >> I * 8) & 0xff))
893 PcdValue = '{' + ', '.join(TmpList) + '}'
894 except:
895 if PcdValue.strip().startswith('{'):
896 PcdValueList = SplitPcdValueString(PcdValue.strip()[1:-1])
897 LabelDict = {}
898 NewPcdValueList = []
899 ReLabel = re.compile('LABEL\((\w+)\)')
900 ReOffset = re.compile('OFFSET_OF\((\w+)\)')
901 LabelOffset = 0
902 for Index, Item in enumerate(PcdValueList):
903 # compute byte offset of every LABEL
904 LabelList = ReLabel.findall(Item)
905 Item = ReLabel.sub('', Item)
906 Item = Item.strip()
907 if LabelList:
908 for Label in LabelList:
909 if not IsValidCName(Label):
910 raise BadExpression('%s is not a valid c variable name' % Label)
911 if Label not in LabelDict.keys():
912 LabelDict[Label] = str(LabelOffset)
913 if Item.startswith('UINT8'):
914 LabelOffset = LabelOffset + 1
915 elif Item.startswith('UINT16'):
916 LabelOffset = LabelOffset + 2
917 elif Item.startswith('UINT32'):
918 LabelOffset = LabelOffset + 4
919 elif Item.startswith('UINT64'):
920 LabelOffset = LabelOffset + 8
921 else:
922 try:
923 ItemValue, ItemSize = ParseFieldValue(Item)
924 LabelOffset = LabelOffset + ItemSize
925 except:
926 LabelOffset = LabelOffset + 1
927
928 for Index, Item in enumerate(PcdValueList):
929 # for LABEL parse
930 Item = Item.strip()
931 try:
932 Item = ReLabel.sub('', Item)
933 except:
934 pass
935 try:
936 OffsetList = ReOffset.findall(Item)
937 except:
938 pass
939 for Offset in OffsetList:
940 if Offset in LabelDict.keys():
941 Re = re.compile('OFFSET_OF\(%s\)' % Offset)
942 Item = Re.sub(LabelDict[Offset], Item)
943 else:
944 raise BadExpression('%s not defined' % Offset)
945 NewPcdValueList.append(Item)
946
947 AllPcdValueList = []
948 for Item in NewPcdValueList:
949 Size = 0
950 ValueStr = ''
951 TokenSpaceGuidName = ''
952 if Item.startswith('GUID') and Item.endswith(')'):
953 try:
954 TokenSpaceGuidName = re.search('GUID\((\w+)\)', Item).group(1)
955 except:
956 pass
957 if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb:
958 Item = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')'
959 elif TokenSpaceGuidName:
960 raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName)
961 Item, Size = ParseFieldValue(Item)
962 for Index in range(0, Size):
963 ValueStr = '0x%02X' % (int(Item) & 255)
964 Item >>= 8
965 AllPcdValueList.append(ValueStr)
966 continue
967 elif Item.startswith('DEVICE_PATH') and Item.endswith(')'):
968 Item, Size = ParseFieldValue(Item)
969 AllPcdValueList.append(Item[1:-1])
970 continue
971 else:
972 ValueType = ""
973 if Item.startswith('UINT8'):
974 ItemSize = 1
975 ValueType = "UINT8"
976 elif Item.startswith('UINT16'):
977 ItemSize = 2
978 ValueType = "UINT16"
979 elif Item.startswith('UINT32'):
980 ItemSize = 4
981 ValueType = "UINT32"
982 elif Item.startswith('UINT64'):
983 ItemSize = 8
984 ValueType = "UINT64"
985 else:
986 ItemSize = 0
987 if ValueType:
988 TmpValue = ValueExpressionEx(Item, ValueType, self._Symb)(True)
989 else:
990 TmpValue = ValueExpressionEx(Item, self.PcdType, self._Symb)(True)
991 Item = '0x%x' % TmpValue if type(TmpValue) != type('') else TmpValue
992 if ItemSize == 0:
993 ItemValue, ItemSize = ParseFieldValue(Item)
994 if Item[0] not in ['"','L','{'] and ItemSize > 1:
995 raise BadExpression("Byte array number %s should less than 0xFF." % Item)
996 else:
997 ItemValue = ParseFieldValue(Item)[0]
998 for I in range(0, ItemSize):
999 ValueStr = '0x%02X' % (int(ItemValue) & 255)
1000 ItemValue >>= 8
1001 AllPcdValueList.append(ValueStr)
1002 Size += ItemSize
1003
1004 if Size > 0:
1005 PcdValue = '{' + ','.join(AllPcdValueList) + '}'
1006 else:
1007 raise BadExpression("Type: %s, Value: %s, %s"%(self.PcdType, PcdValue, Value))
1008
1009 if PcdValue == 'True':
1010 PcdValue = '1'
1011 if PcdValue == 'False':
1012 PcdValue = '0'
1013
1014 if RealValue:
1015 return PcdValue
1016
1017 if __name__ == '__main__':
1018 pass
1019 while True:
1020 input = raw_input('Input expr: ')
1021 if input in 'qQ':
1022 break
1023 try:
1024 print ValueExpression(input)(True)
1025 print ValueExpression(input)(False)
1026 except WrnExpression, Ex:
1027 print Ex.result
1028 print str(Ex)
1029 except Exception, Ex:
1030 print str(Ex)