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

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

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