NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

simpleEval.py (1732B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\script\util\simpleEval.py
      2 from string import digits
      3 from cStringIO import StringIO
      4 from tokenize import generate_tokens, NL
      5 _consts = {'None': None,
      6  'False': False,
      7  'True': True}
      8 
      9 def SimpleEval(source):
     10     itertokens = generate_tokens(StringIO(source).readline)
     11     next = (token[1] for token in itertokens if token[0] is not NL).next
     12     res = atom(next, next())
     13     if next():
     14         raise SyntaxError('bogus data after expression')
     15     return res
     16 
     17 
     18 def atom(next, token):
     19 
     20     def _iter_sequence(end):
     21         token = next()
     22         while token != end:
     23             yield atom(next, token)
     24             token = next()
     25             if token == ',':
     26                 token = next()
     27 
     28     firstchar = token[0]
     29     if token in _consts:
     30         return _consts[token]
     31     if token[-1] == 'L':
     32         return long(token)
     33     if firstchar in digits:
     34         if '.' in token:
     35             return float(token)
     36         return int(token)
     37     if firstchar in '"\'':
     38         return token[1:-1].decode('string-escape')
     39     if firstchar == 'u':
     40         return token[2:-1].decode('unicode-escape')
     41     if token == '-':
     42         return -atom(next, next())
     43     if token == '(':
     44         return tuple(_iter_sequence(')'))
     45     if token == '[':
     46         return list(_iter_sequence(']'))
     47     if token == '{':
     48         out = {}
     49         token = next()
     50         while token != '}':
     51             key = atom(next, token)
     52             next()
     53             token = next()
     54             out[key] = atom(next, token)
     55             token = next()
     56             if token == ',':
     57                 token = next()
     58 
     59         return out
     60     raise SyntaxError('malformed expression (%r)' % token)