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, Depth
=0):
255 self
._Expr
= self
._Expr
.strip()
256 if RealValue
and Depth
== 0:
257 self
._Token
= self
._Expr
258 if self
.__IsNumberToken
():
262 Token
= self
._GetToken
()
263 if type(Token
) == type('') and Token
.startswith('{') and Token
.endswith('}') and self
._Idx
>= self
._Len
:
265 except BadExpression
:
273 if type(Val
) == type(''):
279 elif not Val
.startswith('L"') and not Val
.startswith('{'):
281 RealVal
= '"' + RealVal
+ '"'
283 # The expression has been parsed, but the end of expression is not reached
284 # It means the rest does not comply EBNF of <Expression>
285 if self
._Idx
!= self
._Len
:
286 raise BadExpression(ERR_SNYTAX
% self
._Expr
[self
._Idx
:])
289 RetVal
= str(RealVal
)
296 self
._WarnExcept
.result
= RetVal
297 raise self
._WarnExcept
301 # Template function to parse binary operators which have same precedence
302 # Expr [Operator Expr]*
303 def _ExprFuncTemplate(self
, EvalFunc
, OpLst
):
305 while self
._IsOperator
(OpLst
):
308 Val
= self
.Eval(Op
, Val
, EvalFunc())
309 except WrnExpression
, Warn
:
310 self
._WarnExcept
= Warn
316 return self
._ExprFuncTemplate
(self
._AndExpr
, ["OR", "or", "||"])
320 return self
._ExprFuncTemplate
(self
._BitOr
, ["AND", "and", "&&"])
324 return self
._ExprFuncTemplate
(self
._BitXor
, ["|"])
328 return self
._ExprFuncTemplate
(self
._BitAnd
, ["XOR", "xor", "^"])
332 return self
._ExprFuncTemplate
(self
._EqExpr
, ["&"])
336 Val
= self
._RelExpr
()
337 while self
._IsOperator
(["==", "!=", "EQ", "NE", "IN", "in", "!", "NOT", "not"]):
339 if Op
in ["!", "NOT", "not"]:
340 if not self
._IsOperator
(["IN", "in"]):
341 raise BadExpression(ERR_REL_NOT_IN
)
342 Op
+= ' ' + self
._Token
344 Val
= self
.Eval(Op
, Val
, self
._RelExpr
())
345 except WrnExpression
, Warn
:
346 self
._WarnExcept
= Warn
352 return self
._ExprFuncTemplate
(self
._AddExpr
, ["<=", ">=", "<", ">", "LE", "GE", "LT", "GT"])
356 return self
._ExprFuncTemplate
(self
._UnaryExpr
, ["+", "-"])
359 def _UnaryExpr(self
):
360 if self
._IsOperator
(["!", "NOT", "not"]):
361 Val
= self
._UnaryExpr
()
363 return self
.Eval('not', Val
)
364 except WrnExpression
, Warn
:
365 self
._WarnExcept
= Warn
367 return self
._IdenExpr
()
369 # Parse identifier or encapsulated expression
371 Tk
= self
._GetToken
()
375 # _GetToken may also raise BadExpression
376 if self
._GetToken
() != ')':
377 raise BadExpression(ERR_MATCH
)
378 except BadExpression
:
379 raise BadExpression(ERR_MATCH
)
383 # Skip whitespace or tab
385 for Char
in self
._Expr
[self
._Idx
:]:
386 if Char
not in ' \t':
390 # Try to convert string to number
391 def __IsNumberToken(self
):
393 if self
._Token
.lower()[0:2] == '0x' and len(self
._Token
) > 2:
396 self
._Token
= int(self
._Token
, Radix
)
404 def __GetArray(self
):
407 self
.__GetNList
(True)
408 Token
+= self
._LiteralToken
409 if self
._Idx
>= self
._Len
or self
._Expr
[self
._Idx
] != '}':
410 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
413 # All whitespace and tabs in array are already stripped.
414 IsArray
= IsGuid
= False
415 if len(Token
.split(',')) == 11 and len(Token
.split(',{')) == 2 \
416 and len(Token
.split('},')) == 1:
417 HexLen
= [11,6,6,5,4,4,4,4,4,4,6]
418 HexList
= Token
.split(',')
419 if HexList
[3].startswith('{') and \
420 not [Index
for Index
, Hex
in enumerate(HexList
) if len(Hex
) > HexLen
[Index
]]:
422 if Token
.lstrip('{').rstrip('}').find('{') == -1:
423 if not [Hex
for Hex
in Token
.lstrip('{').rstrip('}').split(',') if len(Hex
) > 4]:
425 if not IsArray
and not IsGuid
:
426 raise BadExpression(ERR_ARRAY_TOKEN
% Token
)
428 self
._Token
= self
._LiteralToken
= Token
431 # Parse string, the format must be: "..."
432 def __GetString(self
):
438 # Replace escape \\\", \"
439 Expr
= self
._Expr
[self
._Idx
:].replace('\\\\', '//').replace('\\\"', '\\\'')
444 self
._Token
= self
._LiteralToken
= self
._Expr
[Idx
:self
._Idx
]
445 if not self
._Token
.endswith('"'):
446 raise BadExpression(ERR_STRING_TOKEN
% self
._Token
)
447 self
._Token
= self
._Token
[1:-1]
450 # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
451 # @param IsAlphaOp: Indicate if parsing general token or script operator(EQ, NE...)
452 def __GetIdToken(self
, IsAlphaOp
= False):
454 for Ch
in self
._Expr
[self
._Idx
:]:
455 if not self
.__IsIdChar
(Ch
):
460 self
._Token
= self
._LiteralToken
= IdToken
462 self
.__ResolveToken
()
465 # Try to resolve token
466 def __ResolveToken(self
):
468 raise BadExpression(ERR_EMPTY_TOKEN
)
471 if self
.PcdPattern
.match(self
._Token
):
472 if self
._Token
not in self
._Symb
:
473 Ex
= BadExpression(ERR_PCD_RESOLVE
% self
._Token
)
476 self
._Token
= ValueExpression(self
._Symb
[self
._Token
], self
._Symb
)(True, self
._Depth
+1)
477 if type(self
._Token
) != type(''):
478 self
._LiteralToken
= hex(self
._Token
)
481 if self
._Token
.startswith('"'):
482 self
._Token
= self
._Token
[1:-1]
483 elif self
._Token
in ["FALSE", "false", "False"]:
485 elif self
._Token
in ["TRUE", "true", "True"]:
488 self
.__IsNumberToken
()
490 def __GetNList(self
, InArray
=False):
491 self
._GetSingleToken
()
492 if not self
.__IsHexLiteral
():
494 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
498 Expr
= self
._Expr
[self
._Idx
:]
499 if not Expr
.startswith(','):
502 NList
= self
._LiteralToken
503 while Expr
.startswith(','):
507 self
._GetSingleToken
()
508 if not self
.__IsHexLiteral
():
509 raise BadExpression(ERR_ARRAY_ELE
% self
._Token
)
510 NList
+= self
._LiteralToken
512 Expr
= self
._Expr
[self
._Idx
:]
513 self
._Token
= self
._LiteralToken
= NList
516 def __IsHexLiteral(self
):
517 if self
._LiteralToken
.startswith('{') and \
518 self
._LiteralToken
.endswith('}'):
521 if self
.HexPattern
.match(self
._LiteralToken
):
522 Token
= self
._LiteralToken
[2:]
523 Token
= Token
.lstrip('0')
525 self
._LiteralToken
= '0x0'
527 self
._LiteralToken
= '0x' + Token
.lower()
532 return self
.__GetNList
()
536 return Ch
in '._/:' or Ch
.isalnum()
539 def _GetSingleToken(self
):
541 Expr
= self
._Expr
[self
._Idx
:]
542 if Expr
.startswith('L"'):
545 UStr
= self
.__GetString
()
546 self
._Token
= 'L"' + UStr
+ '"'
552 Match
= self
.RegGuidPattern
.match(Expr
)
553 if Match
and not Expr
[Match
.end():Match
.end()+1].isalnum() \
554 and Expr
[Match
.end():Match
.end()+1] != '_':
555 self
._Idx
+= Match
.end()
556 self
._Token
= ValueExpression(GuidStringToGuidStructureString(Expr
[0:Match
.end()]))(True, self
._Depth
+1)
558 elif self
.__IsIdChar
(Ch
):
559 return self
.__GetIdToken
()
561 return self
.__GetString
()
563 return self
.__GetArray
()
564 elif Ch
== '(' or Ch
== ')':
569 raise BadExpression(ERR_VALID_TOKEN
% Expr
)
572 def _GetOperator(self
):
574 LegalOpLst
= ['&&', '||', '!=', '==', '>=', '<='] + self
.NonLetterOpLst
577 Expr
= self
._Expr
[self
._Idx
:]
579 # Reach end of expression
583 # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
584 if Expr
[0].isalpha():
585 return self
.__GetIdToken
(True)
587 # Start to get regular operator: +, -, <, > ...
588 if Expr
[0] not in self
.NonLetterOpLst
:
593 if Ch
in self
.NonLetterOpLst
:
594 if '!' == Ch
and OpToken
:
601 if OpToken
not in LegalOpLst
:
602 raise BadExpression(ERR_OPERATOR_UNSUPPORT
% OpToken
)
603 self
._Token
= OpToken
606 # Check if current token matches the operators given from OpList
607 def _IsOperator(self
, OpList
):
610 if self
._Token
in OpList
:
611 if self
._Token
in self
.LogicalOperators
:
612 self
._Token
= self
.LogicalOperators
[self
._Token
]
617 if __name__
== '__main__':
620 input = raw_input('Input expr: ')
624 print ValueExpression(input)(True)
625 print ValueExpression(input)(False)
626 except WrnExpression
, Ex
:
629 except Exception, Ex
: