2 # This file is used to parse and evaluate expression in directive or PCD value.
4 # Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 from Common
.GlobalData
import *
16 from CommonDataClass
.Exceptions
import BadExpression
17 from CommonDataClass
.Exceptions
import WrnExpression
18 from Misc
import GuidStringToGuidStructureString
20 ERR_STRING_EXPR
= 'This operator cannot be used in string expression: [%s].'
21 ERR_SNYTAX
= 'Syntax error, the rest of expression cannot be evaluated: [%s].'
22 ERR_MATCH
= 'No matching right parenthesis.'
23 ERR_STRING_TOKEN
= 'Bad string token: [%s].'
24 ERR_MACRO_TOKEN
= 'Bad macro token: [%s].'
25 ERR_EMPTY_TOKEN
= 'Empty token is not allowed.'
26 ERR_PCD_RESOLVE
= 'PCD token cannot be resolved: [%s].'
27 ERR_VALID_TOKEN
= 'No more valid token found from rest of string: [%s].'
28 ERR_EXPR_TYPE
= 'Different types found in expression.'
29 ERR_OPERATOR_UNSUPPORT
= 'Unsupported operator: [%s]'
30 ERR_REL_NOT_IN
= 'Expect "IN" after "not" operator.'
31 WRN_BOOL_EXPR
= 'Operand of boolean type cannot be used in arithmetic expression.'
32 WRN_EQCMP_STR_OTHERS
= '== Comparison between Operand of string type and Boolean/Number Type always return False.'
33 WRN_NECMP_STR_OTHERS
= '!= Comparison between Operand of string type and Boolean/Number Type always return True.'
34 ERR_RELCMP_STR_OTHERS
= 'Operator taking Operand of string type and Boolean/Number Type is not allowed: [%s].'
35 ERR_STRING_CMP
= 'Unicode string and general string cannot be compared: [%s %s %s]'
36 ERR_ARRAY_TOKEN
= 'Bad C array or C format GUID token: [%s].'
37 ERR_ARRAY_ELE
= 'This must be HEX value for NList or Array: [%s].'
38 ERR_EMPTY_EXPR
= 'Empty expression is not allowed.'
39 ERR_IN_OPERAND
= 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).'
42 # Split string to list according double quote
43 # For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']
45 def SplitString(String
):
46 # There might be escaped quote: "abc\"def\\\"ghi"
47 Str
= String
.replace('\\\\', '//').replace('\\\"', '\\\'')
51 for i
, ch
in enumerate(Str
):
64 raise BadExpression(ERR_STRING_TOKEN
% Item
)
71 def ReplaceExprMacro(String
, Macros
, ExceptionList
= None):
72 StrList
= SplitString(String
)
73 for i
, String
in enumerate(StrList
):
75 if String
.startswith('"'):
77 MacroStartPos
= String
.find('$(')
81 while MacroStartPos
>= 0:
82 RetStr
= String
[0:MacroStartPos
]
83 MacroEndPos
= String
.find(')', MacroStartPos
)
85 raise BadExpression(ERR_MACRO_TOKEN
% String
[MacroStartPos
:])
86 Macro
= String
[MacroStartPos
+2:MacroEndPos
]
87 if Macro
not in Macros
:
88 # From C reference manual:
89 # If an undefined macro name appears in the constant-expression of
90 # !if or !elif, it is replaced by the integer constant 0.
93 Tklst
= RetStr
.split()
94 if Tklst
and Tklst
[-1] in ['IN', 'in'] and ExceptionList
and Macro
not in ExceptionList
:
95 raise BadExpression(ERR_IN_OPERAND
)
96 # Make sure the macro in exception list is encapsulated by double quote
97 # For example: DEFINE ARCH = IA32 X64
98 # $(ARCH) is replaced with "IA32 X64"
99 if ExceptionList
and Macro
in ExceptionList
:
100 RetStr
+= '"' + Macros
[Macro
] + '"'
101 elif Macros
[Macro
].strip():
102 RetStr
+= Macros
[Macro
]
106 RetStr
+= Macros
[Macro
]
107 RetStr
+= String
[MacroEndPos
+1:]
109 MacroStartPos
= String
.find('$(')
111 return ''.join(StrList
)
113 SupportedInMacroList
= ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY']
115 class ValueExpression(object):
116 # Logical operator mapping
118 '&&' : 'and', '||' : 'or',
119 '!' : 'not', 'AND': 'and',
120 'OR' : 'or' , 'NOT': 'not',
121 'XOR': '^' , 'xor': '^',
122 'EQ' : '==' , 'NE' : '!=',
123 'GT' : '>' , 'LT' : '<',
124 'GE' : '>=' , 'LE' : '<=',
128 NonLetterOpLst
= ['+', '-', '&', '|', '^', '!', '=', '>', '<']
130 PcdPattern
= re
.compile(r
'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
131 HexPattern
= re
.compile(r
'0[xX][0-9a-fA-F]+$')
132 RegGuidPattern
= re
.compile(r
'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
134 SymbolPattern
= re
.compile("("
135 "\$\([A-Z][A-Z0-9_]*\)|\$\(\w+\.\w+\)|\w+\.\w+|"
137 "(?<=\W)AND(?=\W)|(?<=\W)OR(?=\W)|(?<=\W)NOT(?=\W)|(?<=\W)XOR(?=\W)|"
138 "(?<=\W)EQ(?=\W)|(?<=\W)NE(?=\W)|(?<=\W)GT(?=\W)|(?<=\W)LT(?=\W)|(?<=\W)GE(?=\W)|(?<=\W)LE(?=\W)"
142 def Eval(Operator
, Oprand1
, Oprand2
= None):
145 if Operator
not in ["==", "!=", ">=", "<=", ">", "<", "in", "not in"] and \
146 (type(Oprand1
) == type('') or type(Oprand2
) == type('')):
147 raise BadExpression(ERR_STRING_EXPR
% Operator
)
157 if Operator
in ["!", "NOT", "not"]:
158 if type(Oprand1
) == type(''):
159 raise BadExpression(ERR_STRING_EXPR
% Operator
)
160 EvalStr
= 'not Oprand1'
162 if Operator
in ["+", "-"] and (type(True) in [type(Oprand1
), type(Oprand2
)]):
163 # Boolean in '+'/'-' will be evaluated but raise warning
164 WrnExp
= WrnExpression(WRN_BOOL_EXPR
)
165 elif type('') in [type(Oprand1
), type(Oprand2
)] and type(Oprand1
)!= type(Oprand2
):
166 # == between string and number/boolean will always return False, != return True
168 WrnExp
= WrnExpression(WRN_EQCMP_STR_OTHERS
)
169 WrnExp
.result
= False
171 elif Operator
== "!=":
172 WrnExp
= WrnExpression(WRN_NECMP_STR_OTHERS
)
176 raise BadExpression(ERR_RELCMP_STR_OTHERS
% Operator
)
177 elif TypeDict
[type(Oprand1
)] != TypeDict
[type(Oprand2
)]:
178 if Operator
in ["==", "!=", ">=", "<=", ">", "<"] and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
179 # comparison between number and boolean is allowed
181 elif Operator
in ['&', '|', '^', "and", "or"] and set((TypeDict
[type(Oprand1
)], TypeDict
[type(Oprand2
)])) == set((TypeDict
[type(True)], TypeDict
[type(0)])):
182 # bitwise and logical operation between number and boolean is allowed
185 raise BadExpression(ERR_EXPR_TYPE
)
186 if type(Oprand1
) == type('') and type(Oprand2
) == type(''):
187 if (Oprand1
.startswith('L"') and not Oprand2
.startswith('L"')) or \
188 (not Oprand1
.startswith('L"') and Oprand2
.startswith('L"')):
189 raise BadExpression(ERR_STRING_CMP
% (Oprand1
, Operator
, Oprand2
))
190 if 'in' in Operator
and type(Oprand2
) == type(''):
191 Oprand2
= Oprand2
.split()
192 EvalStr
= 'Oprand1 ' + Operator
+ ' Oprand2'
194 # Local symbols used by built in eval function
200 Val
= eval(EvalStr
, {}, Dict
)
201 except Exception, Excpt
:
202 raise BadExpression(str(Excpt
))
204 if Operator
in ['and', 'or']:
215 def __init__(self
, Expression
, SymbolTable
={}):
216 self
._NoProcess
= False
217 if type(Expression
) != type(''):
218 self
._Expr
= Expression
219 self
._NoProcess
= True
222 self
._Expr
= ReplaceExprMacro(Expression
.strip(),
224 SupportedInMacroList
)
226 if not self
._Expr
.strip():
227 raise BadExpression(ERR_EMPTY_EXPR
)
230 # The symbol table including PCD and macro mapping
232 self
._Symb
= SymbolTable
233 self
._Symb
.update(self
.LogicalOperators
)
235 self
._Len
= len(self
._Expr
)
237 self
._WarnExcept
= None
239 # Literal token without any conversion
240 self
._LiteralToken
= ''
242 # Public entry for this class
243 # @param RealValue: False: only evaluate if the expression is true or false, used for conditional expression
244 # True : return the evaluated str(value), used for PCD value
246 # @return: True or False if RealValue is False
247 # Evaluated value of string format if RealValue is True
249 def __call__(self
, RealValue
=False):
253 self
._Expr
= self
._Expr
.strip()
255 self
._Token
= self
._Expr
256 if self
.__IsNumberToken
():
260 Token
= self
._GetToken
()
261 if type(Token
) == type('') and Token
.startswith('{') and Token
.endswith('}') and self
._Idx
>= self
._Len
:
263 except BadExpression
:
271 if type(Val
) == type(''):
277 elif not Val
.startswith('L"') and not Val
.startswith('{'):
279 RealVal
= '"' + RealVal
+ '"'
281 # The expression has been parsed, but the end of expression is not reached
282 # It means the rest does not comply EBNF of <Expression>
283 if self
._Idx
!= self
._Len
:
284 raise BadExpression(ERR_SNYTAX
% self
._Expr
[self
._Idx
:])
287 RetVal
= str(RealVal
)
294 self
._WarnExcept
.result
= RetVal
295 raise self
._WarnExcept
299 # Template function to parse binary operators which have same precedence
300 # Expr [Operator Expr]*
301 def _ExprFuncTemplate(self
, EvalFunc
, OpLst
):
303 while self
._IsOperator
(OpLst
):
306 Val
= self
.Eval(Op
, Val
, EvalFunc())
307 except WrnExpression
, Warn
:
308 self
._WarnExcept
= Warn
314 return self
._ExprFuncTemplate
(self
._AndExpr
, ["OR", "or", "||"])
318 return self
._ExprFuncTemplate
(self
._BitOr
, ["AND", "and", "&&"])
322 return self
._ExprFuncTemplate
(self
._BitXor
, ["|"])
326 return self
._ExprFuncTemplate
(self
._BitAnd
, ["XOR", "xor", "^"])
330 return self
._ExprFuncTemplate
(self
._EqExpr
, ["&"])
334 Val
= self
._RelExpr
()
335 while self
._IsOperator
(["==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"]):
337 if Op
in ["!", "NOT", "not"]:
338 if not self
._IsOperator
(["IN", "in"]):
339 raise BadExpression(ERR_REL_NOT_IN
)
340 Op
+= ' ' + self
._Token
342 Val
= self
.Eval(Op
, Val
, self
._RelExpr
())
343 except WrnExpression
, Warn
:
344 self
._WarnExcept
= Warn
350 return self
._ExprFuncTemplate
(self
._AddExpr
, ["<=", ">=", "<", ">", "LE", "GE", "LT", "GT"])
354 return self
._ExprFuncTemplate
(self
._UnaryExpr
, ["+", "-"])
357 def _UnaryExpr(self
):
358 if self
._IsOperator
(["!", "NOT", "not"]):
359 Val
= self
._UnaryExpr
()
361 return self
.Eval('not', Val
)
362 except WrnExpression
, Warn
:
363 self
._WarnExcept
= Warn
365 return self
._IdenExpr
()
367 # Parse identifier or encapsulated expression
369 Tk
= self
._GetToken
()
373 # _GetToken may also raise BadExpression
374 if self
._GetToken
() != ')':
375 raise BadExpression(ERR_MATCH
)
376 except BadExpression
:
377 raise BadExpression(ERR_MATCH
)
381 # Skip whitespace or tab
383 for Char
in self
._Expr
[self
._Idx
:]:
384 if Char
not in ' \t':
388 # Try to convert string to number
389 def __IsNumberToken(self
):
391 if self
._Token
.lower()[0:2] == '0x' and len(self
._Token
) > 2:
394 self
._Token
= int(self
._Token
, Radix
)
402 def __GetArray(self
):
405 self
.__GetNList
(True)
406 Token
+= self
._LiteralToken
407 if self
._Idx
>= self
._Len
or self
._Expr
[self
._Idx
] != '}':
408 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
411 # All whitespace and tabs in array are already stripped.
412 IsArray
= IsGuid
= False
413 if len(Token
.split(',')) == 11 and len(Token
.split(',{')) == 2 \
414 and len(Token
.split('},')) == 1:
415 HexLen
= [11,6,6,5,4,4,4,4,4,4,6]
416 HexList
= Token
.split(',')
417 if HexList
[3].startswith('{') and \
418 not [Index
for Index
, Hex
in enumerate(HexList
) if len(Hex
) > HexLen
[Index
]]:
420 if Token
.lstrip('{').rstrip('}').find('{') == -1:
421 if not [Hex
for Hex
in Token
.lstrip('{').rstrip('}').split(',') if len(Hex
) > 4]:
423 if not IsArray
and not IsGuid
:
424 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
426 self
._Token
= self
._LiteralToken
= Token
429 # Parse string, the format must be: "..."
430 def __GetString(self
):
436 # Replace escape \\\", \"
437 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace('\\\"', '\\\'')
442 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
443 if not self
._Token
.endswith('"'):
444 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
445 self
._Token
= self
._Token
[1:-1]
448 # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
449 # @param IsAlphaOp: Indicate if parsing general token or script operator(EQ, NE...)
450 def __GetIdToken(self
, IsAlphaOp
= False):
452 for Ch
in self
._Expr
[self
._Idx
:]:
453 if not self
.__IsIdChar
(Ch
):
458 self
._Token
= self
._LiteralToken
= IdToken
460 self
.__ResolveToken
()
463 # Try to resolve token
464 def __ResolveToken(self
):
466 raise BadExpression(ERR_EMPTY_TOKEN
)
469 if self
.PcdPattern
.match(self
._Token
):
470 if self
._Token
not in self
._Symb
:
471 Ex
= BadExpression(ERR_PCD_RESOLVE
% self
._Token
)
474 self
._Token
= ValueExpression(self
._Symb
[self
._Token
], self
._Symb
)(True)
475 if type(self
._Token
) != type(''):
476 self
._LiteralToken
= hex(self
._Token
)
479 if self
._Token
.startswith('"'):
480 self
._Token
= self
._Token
[1:-1]
481 elif self
._Token
in ["FALSE", "false", "False"]:
483 elif self
._Token
in ["TRUE", "true", "True"]:
486 self
.__IsNumberToken
()
488 def __GetNList(self
, InArray
=False):
489 self
._GetSingleToken
()
490 if not self
.__IsHexLiteral
():
492 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
496 Expr
= self
._Expr
[self
._Idx
:]
497 if not Expr
.startswith(','):
500 NList
= self
._LiteralToken
501 while Expr
.startswith(','):
505 self
._GetSingleToken
()
506 if not self
.__IsHexLiteral
():
507 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
508 NList
+= self
._LiteralToken
510 Expr
= self
._Expr
[self
._Idx
:]
511 self
._Token
= self
._LiteralToken
= NList
514 def __IsHexLiteral(self
):
515 if self
._LiteralToken
.startswith('{') and \
516 self
._LiteralToken
.endswith('}'):
519 if self
.HexPattern
.match(self
._LiteralToken
):
520 Token
= self
._LiteralToken
[2:]
521 Token
= Token
.lstrip('0')
523 self
._LiteralToken
= '0x0'
525 self
._LiteralToken
= '0x' + Token
.lower()
530 return self
.__GetNList
()
534 return Ch
in '._/:' or Ch
.isalnum()
537 def _GetSingleToken(self
):
539 Expr
= self
._Expr
[self
._Idx
:]
540 if Expr
.startswith('L"'):
543 UStr
= self
.__GetString
()
544 self
._Token
= 'L"' + UStr
+ '"'
550 Match
= self
.RegGuidPattern
.match(Expr
)
551 if Match
and not Expr
[Match
.end():Match
.end()+1].isalnum() \
552 and Expr
[Match
.end():Match
.end()+1] != '_':
553 self
._Idx
+= Match
.end()
554 self
._Token
= ValueExpression(GuidStringToGuidStructureString(Expr
[0:Match
.end()]))(True)
556 elif self
.__IsIdChar
(Ch
):
557 return self
.__GetIdToken
()
559 return self
.__GetString
()
561 return self
.__GetArray
()
562 elif Ch
== '(' or Ch
== ')':
567 raise BadExpression(ERR_VALID_TOKEN
% Expr
)
570 def _GetOperator(self
):
572 LegalOpLst
= ['&&', '||', '!=', '==', '>=', '<='] + self
.NonLetterOpLst
575 Expr
= self
._Expr
[self
._Idx
:]
577 # Reach end of expression
581 # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
582 if Expr
[0].isalpha():
583 return self
.__GetIdToken
(True)
585 # Start to get regular operator: +, -, <, > ...
586 if Expr
[0] not in self
.NonLetterOpLst
:
591 if Ch
in self
.NonLetterOpLst
:
592 if '!' == Ch
and OpToken
:
599 if OpToken
not in LegalOpLst
:
600 raise BadExpression(ERR_OPERATOR_UNSUPPORT
% OpToken
)
601 self
._Token
= OpToken
604 # Check if current token matches the operators given from OpList
605 def _IsOperator(self
, OpList
):
608 if self
._Token
in OpList
:
609 if self
._Token
in self
.LogicalOperators
:
610 self
._Token
= self
.LogicalOperators
[self
._Token
]
615 if __name__
== '__main__':
618 input = raw_input('Input expr: ')
622 print ValueExpression(input)(True)
623 print ValueExpression(input)(False)
624 except WrnExpression
, Ex
:
627 except Exception, Ex
: