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