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