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