-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.py
55 lines (49 loc) · 1.49 KB
/
token.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Here we define the language tokens"""
from enum import Enum
class TokenType(Enum):
# single-character token types
PLUS = '+'
MINUS = '-'
MUL = '*'
FLOAT_DIV = '/'
LPAREN = '('
RPAREN = ')'
SEMI = ';'
DOT = '.'
COLON = ':'
COMMA = ','
# block of reserved words
PROGRAM = 'PROGRAM' # marks the beginning of the block
INTEGER = 'INTEGER'
REAL = 'REAL'
INTEGER_DIV = 'DIV'
VAR = 'VAR'
PROCEDURE = 'PROCEDURE'
BEGIN = 'BEGIN'
END = 'END' # marks the end of the block
# misc
ID = 'ID'
INTEGER_CONST = 'INTEGER_CONST'
REAL_CONST = 'REAL_CONST'
ASSIGN = ':='
EOF = 'EOF'
class Token:
def __init__(self, type, value, lineno=None, column=None):
self.type = type
self.value = value
self.lineno = lineno
self.column = column
def __str__(self):
"""String representation of the class instance.
Example:
>>> Token(TokenType.INTEGER, 7, lineno=5, column=10)
Token(TokenType.INTEGER, 7, position=5:10)
"""
return 'Token({type}, {value}, position={lineno}:{column})'.format(
type=self.type,
value=repr(self.value),
lineno=self.lineno,
column=self.column,
)
def __repr__(self):
return self.__str__()