540f95728072b8b8666e56e0eff757ae11753104
[mirror_edk2.git] / BaseTools / Source / Python / Common / RangeExpression.py
1 # # @file
2 # This file is used to parse and evaluate range expression in Pcd declaration.
3 #
4 # Copyright (c) 2015 - 2018, 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
9 #
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.
12
13 # # Import Modules
14 #
15 from Common.GlobalData import *
16 from CommonDataClass.Exceptions import BadExpression
17 from CommonDataClass.Exceptions import WrnExpression
18 import uuid
19
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).'
40
41 def MaxOfType(DataType):
42 if DataType == 'UINT8':
43 return int('0xFF', 16)
44 if DataType == 'UINT16':
45 return int('0xFFFF', 16)
46 if DataType == 'UINT32':
47 return int('0xFFFFFFFF', 16)
48 if DataType == 'UINT64':
49 return int('0xFFFFFFFFFFFFFFFF', 16)
50
51 class RangeObject(object):
52 def __init__(self, start, end, empty = False):
53
54 if int(start) < int(end):
55 self.start = int(start)
56 self.end = int(end)
57 else:
58 self.start = int(end)
59 self.end = int(start)
60 self.empty = empty
61
62 class RangeContainer(object):
63 def __init__(self):
64 self.rangelist = []
65
66 def push(self, RangeObject):
67 self.rangelist.append(RangeObject)
68 self.rangelist = sorted(self.rangelist, key = lambda rangeobj : rangeobj.start)
69 self.merge()
70
71 def pop(self):
72 for item in self.rangelist:
73 yield item
74
75 def __clean__(self):
76 newrangelist = []
77 for rangeobj in self.rangelist:
78 if rangeobj.empty == True:
79 continue
80 else:
81 newrangelist.append(rangeobj)
82 self.rangelist = newrangelist
83 def merge(self):
84 self.__clean__()
85 for i in range(0, len(self.rangelist) - 1):
86 if self.rangelist[i + 1].start > self.rangelist[i].end:
87 continue
88 else:
89 self.rangelist[i + 1].start = self.rangelist[i].start
90 self.rangelist[i + 1].end = self.rangelist[i + 1].end > self.rangelist[i].end and self.rangelist[i + 1].end or self.rangelist[i].end
91 self.rangelist[i].empty = True
92
93 self.__clean__()
94
95 def dump(self):
96 print "----------------------"
97 rangelist = ""
98 for object in self.rangelist:
99 rangelist = rangelist + "[%d , %d]" % (object.start, object.end)
100 print rangelist
101
102
103 class XOROperatorObject(object):
104 def __init__(self):
105 pass
106 def Calculate(self, Operand, DataType, SymbolTable):
107 if type(Operand) == type('') and not Operand.isalnum():
108 Expr = "XOR ..."
109 raise BadExpression(ERR_SNYTAX % Expr)
110 rangeId = str(uuid.uuid1())
111 rangeContainer = RangeContainer()
112 rangeContainer.push(RangeObject(0, int(Operand) - 1))
113 rangeContainer.push(RangeObject(int(Operand) + 1, MaxOfType(DataType)))
114 SymbolTable[rangeId] = rangeContainer
115 return rangeId
116
117 class LEOperatorObject(object):
118 def __init__(self):
119 pass
120 def Calculate(self, Operand, DataType, SymbolTable):
121 if type(Operand) == type('') and not Operand.isalnum():
122 Expr = "LE ..."
123 raise BadExpression(ERR_SNYTAX % Expr)
124 rangeId1 = str(uuid.uuid1())
125 rangeContainer = RangeContainer()
126 rangeContainer.push(RangeObject(0, int(Operand)))
127 SymbolTable[rangeId1] = rangeContainer
128 return rangeId1
129 class LTOperatorObject(object):
130 def __init__(self):
131 pass
132 def Calculate(self, Operand, DataType, SymbolTable):
133 if type(Operand) == type('') and not Operand.isalnum():
134 Expr = "LT ..."
135 raise BadExpression(ERR_SNYTAX % Expr)
136 rangeId1 = str(uuid.uuid1())
137 rangeContainer = RangeContainer()
138 rangeContainer.push(RangeObject(0, int(Operand) - 1))
139 SymbolTable[rangeId1] = rangeContainer
140 return rangeId1
141
142 class GEOperatorObject(object):
143 def __init__(self):
144 pass
145 def Calculate(self, Operand, DataType, SymbolTable):
146 if type(Operand) == type('') and not Operand.isalnum():
147 Expr = "GE ..."
148 raise BadExpression(ERR_SNYTAX % Expr)
149 rangeId1 = str(uuid.uuid1())
150 rangeContainer = RangeContainer()
151 rangeContainer.push(RangeObject(int(Operand), MaxOfType(DataType)))
152 SymbolTable[rangeId1] = rangeContainer
153 return rangeId1
154
155 class GTOperatorObject(object):
156 def __init__(self):
157 pass
158 def Calculate(self, Operand, DataType, SymbolTable):
159 if type(Operand) == type('') and not Operand.isalnum():
160 Expr = "GT ..."
161 raise BadExpression(ERR_SNYTAX % Expr)
162 rangeId1 = str(uuid.uuid1())
163 rangeContainer = RangeContainer()
164 rangeContainer.push(RangeObject(int(Operand) + 1, MaxOfType(DataType)))
165 SymbolTable[rangeId1] = rangeContainer
166 return rangeId1
167
168 class EQOperatorObject(object):
169 def __init__(self):
170 pass
171 def Calculate(self, Operand, DataType, SymbolTable):
172 if type(Operand) == type('') and not Operand.isalnum():
173 Expr = "EQ ..."
174 raise BadExpression(ERR_SNYTAX % Expr)
175 rangeId1 = str(uuid.uuid1())
176 rangeContainer = RangeContainer()
177 rangeContainer.push(RangeObject(int(Operand) , int(Operand)))
178 SymbolTable[rangeId1] = rangeContainer
179 return rangeId1
180
181 def GetOperatorObject(Operator):
182 if Operator == '>':
183 return GTOperatorObject()
184 elif Operator == '>=':
185 return GEOperatorObject()
186 elif Operator == '<':
187 return LTOperatorObject()
188 elif Operator == '<=':
189 return LEOperatorObject()
190 elif Operator == '==':
191 return EQOperatorObject()
192 elif Operator == '^':
193 return XOROperatorObject()
194 else:
195 raise BadExpression("Bad Operator")
196
197 class RangeExpression(object):
198 # Logical operator mapping
199 LogicalOperators = {
200 '&&' : 'and', '||' : 'or',
201 '!' : 'not', 'AND': 'and',
202 'OR' : 'or' , 'NOT': 'not',
203 'XOR': '^' , 'xor': '^',
204 'EQ' : '==' , 'NE' : '!=',
205 'GT' : '>' , 'LT' : '<',
206 'GE' : '>=' , 'LE' : '<=',
207 'IN' : 'in'
208 }
209
210 NonLetterOpLst = ['+', '-', '&', '|', '^', '!', '=', '>', '<']
211
212 PcdPattern = re.compile(r'[_a-zA-Z][0-9A-Za-z_]*\.[_a-zA-Z][0-9A-Za-z_]*$')
213 HexPattern = re.compile(r'0[xX][0-9a-fA-F]+')
214
215 RangePattern = re.compile(r'[0-9]+ - [0-9]+')
216
217 def preProcessRangeExpr(self, expr):
218 # convert hex to int
219 # convert interval to object index. ex. 1 - 10 to a GUID
220 expr = expr.strip()
221 NumberDict = {}
222 for HexNumber in self.HexPattern.findall(expr):
223 Number = str(int(HexNumber, 16))
224 NumberDict[HexNumber] = Number
225 for HexNum in NumberDict:
226 expr = expr.replace(HexNum, NumberDict[HexNum])
227
228 rangedict = {}
229 for validrange in self.RangePattern.findall(expr):
230 start, end = validrange.split(" - ")
231 start = start.strip()
232 end = end.strip()
233 rangeid = str(uuid.uuid1())
234 rangeContainer = RangeContainer()
235 rangeContainer.push(RangeObject(start, end))
236 self.operanddict[str(rangeid)] = rangeContainer
237 rangedict[validrange] = str(rangeid)
238
239 for validrange in rangedict:
240 expr = expr.replace(validrange, rangedict[validrange])
241
242 self._Expr = expr
243 return expr
244
245
246 def EvalRange(self, Operator, Oprand):
247
248 operatorobj = GetOperatorObject(Operator)
249 return operatorobj.Calculate(Oprand, self.PcdDataType, self.operanddict)
250
251 def Rangeintersection(self, Oprand1, Oprand2):
252 rangeContainer1 = self.operanddict[Oprand1]
253 rangeContainer2 = self.operanddict[Oprand2]
254 rangeContainer = RangeContainer()
255 for range1 in rangeContainer1.pop():
256 for range2 in rangeContainer2.pop():
257 start1 = range1.start
258 end1 = range1.end
259 start2 = range2.start
260 end2 = range2.end
261 if start1 >= start2:
262 start1, start2 = start2, start1
263 end1, end2 = end2, end1
264 if range1.empty:
265 rangeid = str(uuid.uuid1())
266 rangeContainer.push(RangeObject(0, 0, True))
267 if end1 < start2:
268 rangeid = str(uuid.uuid1())
269 rangeContainer.push(RangeObject(0, 0, True))
270 elif end1 == start2:
271 rangeid = str(uuid.uuid1())
272 rangeContainer.push(RangeObject(end1, end1))
273 elif end1 <= end2 and end1 > start2:
274 rangeid = str(uuid.uuid1())
275 rangeContainer.push(RangeObject(start2, end1))
276 elif end1 >= end2:
277 rangeid = str(uuid.uuid1())
278 rangeContainer.push(RangeObject(start2, end2))
279
280 self.operanddict[rangeid] = rangeContainer
281 # rangeContainer.dump()
282 return rangeid
283
284 def Rangecollections(self, Oprand1, Oprand2):
285
286 rangeContainer1 = self.operanddict[Oprand1]
287 rangeContainer2 = self.operanddict[Oprand2]
288 rangeContainer = RangeContainer()
289
290 for rangeobj in rangeContainer2.pop():
291 rangeContainer.push(rangeobj)
292 for rangeobj in rangeContainer1.pop():
293 rangeContainer.push(rangeobj)
294
295 rangeid = str(uuid.uuid1())
296 self.operanddict[rangeid] = rangeContainer
297
298 # rangeContainer.dump()
299 return rangeid
300
301
302 def NegtiveRange(self, Oprand1):
303 rangeContainer1 = self.operanddict[Oprand1]
304
305
306 rangeids = []
307
308 for rangeobj in rangeContainer1.pop():
309 rangeContainer = RangeContainer()
310 rangeid = str(uuid.uuid1())
311 if rangeobj.empty:
312 rangeContainer.push(RangeObject(0, MaxOfType(self.PcdDataType)))
313 else:
314 if rangeobj.start > 0:
315 rangeContainer.push(RangeObject(0, rangeobj.start - 1))
316 if rangeobj.end < MaxOfType(self.PcdDataType):
317 rangeContainer.push(RangeObject(rangeobj.end + 1, MaxOfType(self.PcdDataType)))
318 self.operanddict[rangeid] = rangeContainer
319 rangeids.append(rangeid)
320
321 if len(rangeids) == 0:
322 rangeContainer = RangeContainer()
323 rangeContainer.push(RangeObject(0, MaxOfType(self.PcdDataType)))
324 rangeid = str(uuid.uuid1())
325 self.operanddict[rangeid] = rangeContainer
326 return rangeid
327
328 if len(rangeids) == 1:
329 return rangeids[0]
330
331 re = self.Rangeintersection(rangeids[0], rangeids[1])
332 for i in range(2, len(rangeids)):
333 re = self.Rangeintersection(re, rangeids[i])
334
335 rangeid2 = str(uuid.uuid1())
336 self.operanddict[rangeid2] = self.operanddict[re]
337 return rangeid2
338
339 def Eval(self, Operator, Oprand1, Oprand2 = None):
340
341 if Operator in ["!", "NOT", "not"]:
342 if not gGuidPattern.match(Oprand1.strip()):
343 raise BadExpression(ERR_STRING_EXPR % Operator)
344 return self.NegtiveRange(Oprand1)
345 else:
346 if Operator in ["==", ">=", "<=", ">", "<", '^']:
347 return self.EvalRange(Operator, Oprand1)
348 elif Operator == 'and' :
349 if not gGuidPatternEnd.match(Oprand1.strip()) or not gGuidPatternEnd.match(Oprand2.strip()):
350 raise BadExpression(ERR_STRING_EXPR % Operator)
351 return self.Rangeintersection(Oprand1, Oprand2)
352 elif Operator == 'or':
353 if not gGuidPatternEnd.match(Oprand1.strip()) or not gGuidPatternEnd.match(Oprand2.strip()):
354 raise BadExpression(ERR_STRING_EXPR % Operator)
355 return self.Rangecollections(Oprand1, Oprand2)
356 else:
357 raise BadExpression(ERR_STRING_EXPR % Operator)
358
359
360 def __init__(self, Expression, PcdDataType, SymbolTable = {}):
361 self._NoProcess = False
362 if type(Expression) != type(''):
363 self._Expr = Expression
364 self._NoProcess = True
365 return
366
367 self._Expr = Expression.strip()
368
369 if not self._Expr.strip():
370 raise BadExpression(ERR_EMPTY_EXPR)
371
372 #
373 # The symbol table including PCD and macro mapping
374 #
375 self._Symb = SymbolTable
376 self._Symb.update(self.LogicalOperators)
377 self._Idx = 0
378 self._Len = len(self._Expr)
379 self._Token = ''
380 self._WarnExcept = None
381
382
383 # Literal token without any conversion
384 self._LiteralToken = ''
385
386 # store the operand object
387 self.operanddict = {}
388 # The Pcd max value depends on PcdDataType
389 self.PcdDataType = PcdDataType
390
391 # Public entry for this class
392 # @param RealValue: False: only evaluate if the expression is true or false, used for conditional expression
393 # True : return the evaluated str(value), used for PCD value
394 #
395 # @return: True or False if RealValue is False
396 # Evaluated value of string format if RealValue is True
397 #
398 def __call__(self, RealValue = False, Depth = 0):
399 if self._NoProcess:
400 return self._Expr
401
402 self._Depth = Depth
403
404 self._Expr = self._Expr.strip()
405
406 self.preProcessRangeExpr(self._Expr)
407
408 # check if the expression does not need to evaluate
409 if RealValue and Depth == 0:
410 self._Token = self._Expr
411 if gGuidPatternEnd.match(self._Expr):
412 return [self.operanddict[self._Expr] ]
413
414 self._Idx = 0
415 self._Token = ''
416
417 Val = self._OrExpr()
418 RealVal = Val
419
420 RangeIdList = RealVal.split("or")
421 RangeList = []
422 for rangeid in RangeIdList:
423 RangeList.append(self.operanddict[rangeid.strip()])
424
425 return RangeList
426
427 # Template function to parse binary operators which have same precedence
428 # Expr [Operator Expr]*
429 def _ExprFuncTemplate(self, EvalFunc, OpLst):
430 Val = EvalFunc()
431 while self._IsOperator(OpLst):
432 Op = self._Token
433 try:
434 Val = self.Eval(Op, Val, EvalFunc())
435 except WrnExpression, Warn:
436 self._WarnExcept = Warn
437 Val = Warn.result
438 return Val
439
440 # A [|| B]*
441 def _OrExpr(self):
442 return self._ExprFuncTemplate(self._AndExpr, ["OR", "or"])
443
444 # A [&& B]*
445 def _AndExpr(self):
446 return self._ExprFuncTemplate(self._NeExpr, ["AND", "and"])
447
448 def _NeExpr(self):
449 Val = self._RelExpr()
450 while self._IsOperator([ "!=", "NOT", "not"]):
451 Op = self._Token
452 if Op in ["!", "NOT", "not"]:
453 if not self._IsOperator(["IN", "in"]):
454 raise BadExpression(ERR_REL_NOT_IN)
455 Op += ' ' + self._Token
456 try:
457 Val = self.Eval(Op, Val, self._RelExpr())
458 except WrnExpression, Warn:
459 self._WarnExcept = Warn
460 Val = Warn.result
461 return Val
462
463 # [!]*A
464 def _RelExpr(self):
465 if self._IsOperator(["NOT" , "LE", "GE", "LT", "GT", "EQ", "XOR"]):
466 Token = self._Token
467 Val = self._NeExpr()
468 try:
469 return self.Eval(Token, Val)
470 except WrnExpression, Warn:
471 self._WarnExcept = Warn
472 return Warn.result
473 return self._IdenExpr()
474
475 # Parse identifier or encapsulated expression
476 def _IdenExpr(self):
477 Tk = self._GetToken()
478 if Tk == '(':
479 Val = self._OrExpr()
480 try:
481 # _GetToken may also raise BadExpression
482 if self._GetToken() != ')':
483 raise BadExpression(ERR_MATCH)
484 except BadExpression:
485 raise BadExpression(ERR_MATCH)
486 return Val
487 return Tk
488
489 # Skip whitespace or tab
490 def __SkipWS(self):
491 for Char in self._Expr[self._Idx:]:
492 if Char not in ' \t':
493 break
494 self._Idx += 1
495
496 # Try to convert string to number
497 def __IsNumberToken(self):
498 Radix = 10
499 if self._Token.lower()[0:2] == '0x' and len(self._Token) > 2:
500 Radix = 16
501 try:
502 self._Token = int(self._Token, Radix)
503 return True
504 except ValueError:
505 return False
506 except TypeError:
507 return False
508
509 # Parse array: {...}
510 def __GetArray(self):
511 Token = '{'
512 self._Idx += 1
513 self.__GetNList(True)
514 Token += self._LiteralToken
515 if self._Idx >= self._Len or self._Expr[self._Idx] != '}':
516 raise BadExpression(ERR_ARRAY_TOKEN % Token)
517 Token += '}'
518
519 # All whitespace and tabs in array are already stripped.
520 IsArray = IsGuid = False
521 if len(Token.split(',')) == 11 and len(Token.split(',{')) == 2 \
522 and len(Token.split('},')) == 1:
523 HexLen = [11, 6, 6, 5, 4, 4, 4, 4, 4, 4, 6]
524 HexList = Token.split(',')
525 if HexList[3].startswith('{') and \
526 not [Index for Index, Hex in enumerate(HexList) if len(Hex) > HexLen[Index]]:
527 IsGuid = True
528 if Token.lstrip('{').rstrip('}').find('{') == -1:
529 if not [Hex for Hex in Token.lstrip('{').rstrip('}').split(',') if len(Hex) > 4]:
530 IsArray = True
531 if not IsArray and not IsGuid:
532 raise BadExpression(ERR_ARRAY_TOKEN % Token)
533 self._Idx += 1
534 self._Token = self._LiteralToken = Token
535 return self._Token
536
537 # Parse string, the format must be: "..."
538 def __GetString(self):
539 Idx = self._Idx
540
541 # Skip left quote
542 self._Idx += 1
543
544 # Replace escape \\\", \"
545 Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace('\\\"', '\\\'')
546 for Ch in Expr:
547 self._Idx += 1
548 if Ch == '"':
549 break
550 self._Token = self._LiteralToken = self._Expr[Idx:self._Idx]
551 if not self._Token.endswith('"'):
552 raise BadExpression(ERR_STRING_TOKEN % self._Token)
553 self._Token = self._Token[1:-1]
554 return self._Token
555
556 # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
557 # @param IsAlphaOp: Indicate if parsing general token or script operator(EQ, NE...)
558 def __GetIdToken(self, IsAlphaOp = False):
559 IdToken = ''
560 for Ch in self._Expr[self._Idx:]:
561 if not self.__IsIdChar(Ch):
562 break
563 self._Idx += 1
564 IdToken += Ch
565
566 self._Token = self._LiteralToken = IdToken
567 if not IsAlphaOp:
568 self.__ResolveToken()
569 return self._Token
570
571 # Try to resolve token
572 def __ResolveToken(self):
573 if not self._Token:
574 raise BadExpression(ERR_EMPTY_TOKEN)
575
576 # PCD token
577 if self.PcdPattern.match(self._Token):
578 if self._Token not in self._Symb:
579 Ex = BadExpression(ERR_PCD_RESOLVE % self._Token)
580 Ex.Pcd = self._Token
581 raise Ex
582 self._Token = RangeExpression(self._Symb[self._Token], self._Symb)(True, self._Depth + 1)
583 if type(self._Token) != type(''):
584 self._LiteralToken = hex(self._Token)
585 return
586
587 if self._Token.startswith('"'):
588 self._Token = self._Token[1:-1]
589 elif self._Token in ["FALSE", "false", "False"]:
590 self._Token = False
591 elif self._Token in ["TRUE", "true", "True"]:
592 self._Token = True
593 else:
594 self.__IsNumberToken()
595
596 def __GetNList(self, InArray = False):
597 self._GetSingleToken()
598 if not self.__IsHexLiteral():
599 if InArray:
600 raise BadExpression(ERR_ARRAY_ELE % self._Token)
601 return self._Token
602
603 self.__SkipWS()
604 Expr = self._Expr[self._Idx:]
605 if not Expr.startswith(','):
606 return self._Token
607
608 NList = self._LiteralToken
609 while Expr.startswith(','):
610 NList += ','
611 self._Idx += 1
612 self.__SkipWS()
613 self._GetSingleToken()
614 if not self.__IsHexLiteral():
615 raise BadExpression(ERR_ARRAY_ELE % self._Token)
616 NList += self._LiteralToken
617 self.__SkipWS()
618 Expr = self._Expr[self._Idx:]
619 self._Token = self._LiteralToken = NList
620 return self._Token
621
622 def __IsHexLiteral(self):
623 if self._LiteralToken.startswith('{') and \
624 self._LiteralToken.endswith('}'):
625 return True
626
627 if self.HexPattern.match(self._LiteralToken):
628 Token = self._LiteralToken[2:]
629 Token = Token.lstrip('0')
630 if not Token:
631 self._LiteralToken = '0x0'
632 else:
633 self._LiteralToken = '0x' + Token.lower()
634 return True
635 return False
636
637 def _GetToken(self):
638 return self.__GetNList()
639
640 @staticmethod
641 def __IsIdChar(Ch):
642 return Ch in '._/:' or Ch.isalnum()
643
644 # Parse operand
645 def _GetSingleToken(self):
646 self.__SkipWS()
647 Expr = self._Expr[self._Idx:]
648 if Expr.startswith('L"'):
649 # Skip L
650 self._Idx += 1
651 UStr = self.__GetString()
652 self._Token = 'L"' + UStr + '"'
653 return self._Token
654
655 self._Token = ''
656 if Expr:
657 Ch = Expr[0]
658 Match = gGuidPattern.match(Expr)
659 if Match and not Expr[Match.end():Match.end() + 1].isalnum() \
660 and Expr[Match.end():Match.end() + 1] != '_':
661 self._Idx += Match.end()
662 self._Token = Expr[0:Match.end()]
663 return self._Token
664 elif self.__IsIdChar(Ch):
665 return self.__GetIdToken()
666 elif Ch == '(' or Ch == ')':
667 self._Idx += 1
668 self._Token = Ch
669 return self._Token
670
671 raise BadExpression(ERR_VALID_TOKEN % Expr)
672
673 # Parse operator
674 def _GetOperator(self):
675 self.__SkipWS()
676 LegalOpLst = ['&&', '||', '!=', '==', '>=', '<='] + self.NonLetterOpLst
677
678 self._Token = ''
679 Expr = self._Expr[self._Idx:]
680
681 # Reach end of expression
682 if not Expr:
683 return ''
684
685 # Script operator: LT, GT, LE, GE, EQ, NE, and, or, xor, not
686 if Expr[0].isalpha():
687 return self.__GetIdToken(True)
688
689 # Start to get regular operator: +, -, <, > ...
690 if Expr[0] not in self.NonLetterOpLst:
691 return ''
692
693 OpToken = ''
694 for Ch in Expr:
695 if Ch in self.NonLetterOpLst:
696 if '!' == Ch and OpToken:
697 break
698 self._Idx += 1
699 OpToken += Ch
700 else:
701 break
702
703 if OpToken not in LegalOpLst:
704 raise BadExpression(ERR_OPERATOR_UNSUPPORT % OpToken)
705 self._Token = OpToken
706 return OpToken
707
708 # Check if current token matches the operators given from OpList
709 def _IsOperator(self, OpList):
710 Idx = self._Idx
711 self._GetOperator()
712 if self._Token in OpList:
713 if self._Token in self.LogicalOperators:
714 self._Token = self.LogicalOperators[self._Token]
715 return True
716 self._Idx = Idx
717 return False
718
719
720
721
722
723
724
725
726
727
728 # UTRangeList()