-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
45 lines (34 loc) · 764 Bytes
/
lexer.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
import sly
class HFLexer(sly.Lexer):
tokens = {NUMBER,
PLUS, MINUS, TIMES, DIVIDE, EXPO,
STRING,
LIST_BEGIN, LIST_END, LIST_SEP,
LOWERCASE_ALPHABET, SEP}
ignore = "\t"
NUMBER = r"\d+"
PLUS = r"\+"
MINUS = r"-"
TIMES = r"\*"
EXPO = r"×"
DIVIDE = r"/"
LIST_BEGIN = r"\["
LIST_END = r"\]"
LIST_SEP = r","
SEP = r"[ _]"
# (ACTUAL) CONSTANTS
LOWERCASE_ALPHABET = r"a"
@_(r"\d+")
def NUMBER(self, token):
token.value = int(token.value)
return token
@_(r"\"[^\"]*\"")
def STRING(self, token):
token.value = token.value[1:-1]
return token
@_(r'\[.*?\]')
def LIST(self, token):
token.value = token.value[1:-1].split(',')
return token
lexer = HFLexer()
tokenize = lexer.tokenize