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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
from tokens import *
# consts
DOUBLE_QUOTE = '"'
BACKSLASH = "\\"
OPEN_PAREN = "("
CLOSE_PAREN = ")"
DIGITS = "0123456789"
LETTERS = "abcdefghijklmnopqrstuvwxyz"
PUNCTUATION = "-_!*$@%^&=+/?<>~"
SYMBOL_VALS = list(LETTERS + LETTERS.upper() + DIGITS + PUNCTUATION)
def lex_string(inp):
token = ""
esc = False
for idx, c in enumerate(inp):
# if we're escaping a quote, don't add the \
if esc:
if c == DOUBLE_QUOTE:
token += DOUBLE_QUOTE
elif c == BACKSLASH:
token += BACKSLASH
else:
token += f"{BACKSLASH}{c}"
# if it's an ecsape char, set esc and continue
elif c == BACKSLASH:
esc = True
continue
elif c == DOUBLE_QUOTE:
#return token, inp[idx + 1:]
return NebLiteral(NebType.STRING, token), inp[idx + 1:]
else:
token += c
esc = False
raise Exception("improperly ended string!")
def lex_bool(inp):
if inp[0:4] == "true":
token = True
elif inp[0:5] == "false":
token = False
else:
raise Exception("invalid boolean")
if peek(inp[len(str(token)):]) not in (None, " ", CLOSE_PAREN):
raise Exception("invalid boolean")
#return token, inp[len(str(token)):]
return NebLiteral(NebType.BOOL, token), inp[len(str(token)):]
def lex_number(inp):
token = ""
for idx, c in enumerate(inp):
if c in (" ", CLOSE_PAREN):
if "." in token:
#return float(token), inp[idx:]
return NebLiteral(NebType.FLOAT, float(token)), inp[idx:]
else:
#return int(token), inp[idx:]
return NebLiteral(NebType.INT, int(token)), inp[idx:]
if c in list(DIGITS): # or c in ("-", "."):
token += c
elif c == "+":
if idx == 0:
continue
else:
raise Exception("improper sign placement!")
elif c == "-":
if idx == 0:
token += c
else:
raise Exception("improper sign placement!")
elif c == ".":
if c not in token:
token += c
else:
raise Exception("too many decimal points")
else:
raise Exception("improper numeric!")
if "." in token:
#return float(token), ""
return NebLiteral(NebType.FLOAT, float(token)), ""
else:
#return int(token), ""
return NebLiteral(NebType.INT, int(token)), ""
def lex_symbol(inp):
token = ""
for idx, c in enumerate(inp):
if c in (CLOSE_PAREN, " "):
return NebSymbol(token), inp[idx:]
elif c in SYMBOL_VALS:
token += c
else:
raise Exception("improper symbol")
return NebSymbol(token), ""
def peek(inp):
if len(inp) == 0:
return None
return inp[0]
def lex(inp, tokens):
inp = inp.strip() # white space doesn't matter at this point
nxt = peek(inp)
if nxt is None:
#print(f"returning [{tokens}]")
return tokens
# parens
if nxt == OPEN_PAREN:
tokens.append(NebOpen())
return lex(inp[1:], tokens)
elif nxt == CLOSE_PAREN:
tokens.append(NebClose())
return lex(inp[1:], tokens)
# numbers
elif nxt in list(DIGITS) or nxt in ("+", "-", "."):
# + and - are symbols, too
if nxt in ("+", "-"):
after = peek(inp[1:])
if after not in DIGITS: # parse a symbol
token, remainder = lex_symbol(inp)
if peek(remainder) not in (None, CLOSE_PAREN, " "):
raise Exception("spaces required between tokens")
tokens.append(token)
return lex(remainder, tokens)
token, remainder = lex_number(inp)
tokens.append(token)
return lex(remainder, tokens)
# strings
elif nxt == DOUBLE_QUOTE:
token, remainder = lex_string(inp[1:])
#print(f"received [{token}] [{remainder}]")
if peek(remainder) not in (None, CLOSE_PAREN, " "):
raise Exception("spaces required between tokens")
tokens.append(token)
return lex(remainder, tokens)
# bool
elif nxt == "#":
token, remainder = lex_bool(inp[1:])
if peek(remainder) not in (None, CLOSE_PAREN, " "):
raise Exception("spaces required between tokens")
tokens.append(token)
return lex(remainder, tokens)
# symbols
elif nxt in SYMBOL_VALS:
token, remainder = lex_symbol(inp)
if peek(remainder) not in (None, CLOSE_PAREN, " "):
raise Exception("spaces required between tokens")
tokens.append(token)
return lex(remainder, tokens)
else:
raise Exception("unable to lex")
|