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