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