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