symbol.py 1.5 KB
Newer Older
1 2 3 4
# Symbol is a 'lightweight' enum, in Python 3.4, we could use Enum as superclass
class Symbol(object):
    NONE             = -1
    Integer          =  0
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    Double           =  1
    Not              =  2
    And              =  3
    Or               =  4
    Star             =  5
    Div              =  6
    Mod              =  7
    Plus             =  8
    Minus            =  9
    Equal            = 10
    More             = 11
    Less             = 12
    Comma            = 13
    At               = 14
    Per              = 15
    NewBlock         = 16
    EndBlock         = 17
    Colon            = 18
    Period           = 19
    Exit             = 20
    Assign           = 21
    NewTerm          = 22
    EndTerm          = 23
    Pound            = 24
    Primitive        = 25
    Separator        = 26
    STString         = 27
    Identifier       = 28
    Keyword          = 29
    KeywordSequence  = 30
    OperatorSequence = 31

37 38 39

def _sorted_symbols(cls):
    "NOT_RPYTHON"
40 41 42
    """This function is only called a single time, at load time of this module.
       For RPypthon, this means, during translation of the module.
    """
Stefan Marr's avatar
Stefan Marr committed
43 44 45
    return [key for value, key in
            sorted([(value, key) for key, value in cls.__dict__.items()])
            if isinstance(value, int)]
46
_symbols = _sorted_symbols(Symbol)
47

Stefan Marr's avatar
Stefan Marr committed
48

49
def symbol_as_str(symbol):
50 51 52
    index = symbol + 1
    if index > len(_symbols):
        raise ValueError('No Symbol defined for the value %d.' % symbol)
53
    else:
54
        return _symbols[index]