-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
185 lines (158 loc) · 5.38 KB
/
scanner.py
File metadata and controls
185 lines (158 loc) · 5.38 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from token_ import Token
class Scanner:
reserved_words = {'and' : 'AND',
'class':'CLASS',
'else':'ELSE',
'false':'FALSE',
'for':'FOR',
'fun':'FUN',
'if':'IF',
'nil':'NIL',
'or':'OR',
'print':'PRINT',
'return':'RETURN',
'super':'SUPER',
'this':'THIS',
'true':'TRUE',
'var':'VAR',
'while':'WHILE'
}
def __init__(self, source):
self.source = source
self.tokens = []
self.start = 0
self.current = 0
self.line = 1
self.errors = []
def is_at_end(self):
return self.current >= len(self.source)
def scan_tokens(self):
while not self.is_at_end():
self.start = self.current
self.scan_token()
self.tokens.append(Token("EOF", "", None, self.line))
return self.tokens, self.errors
def scan_token(self):
c = self.advance()
match c:
case "(":
self.add_token("LEFT_PAREN")
case ")":
self.add_token("RIGHT_PAREN")
case "{":
self.add_token("LEFT_BRACE")
case "}":
self.add_token("RIGHT_BRACE")
case "*":
self.add_token("STAR")
case ".":
self.add_token("DOT")
case ",":
self.add_token("COMMA")
case "+":
self.add_token("PLUS")
case "-":
self.add_token("MINUS")
case ";":
self.add_token("SEMICOLON")
case "=":
if self.match("="):
self.add_token("EQUAL_EQUAL")
else:
self.add_token("EQUAL")
case "!":
if self.match("="):
self.add_token("BANG_EQUAL")
else:
self.add_token("BANG")
case ">":
if self.match("="):
self.add_token("GREATER_EQUAL")
else:
self.add_token("GREATER")
case "<":
if self.match("="):
self.add_token("LESS_EQUAL")
else:
self.add_token("LESS")
case "/":
if self.match("/"):
while self.peek() != '\n' and not self.is_at_end():
self.advance()
else:
self.add_token("SLASH")
case ' ' | '\r' | '\t':
next
case '\n':
self.line += 1
case '"':
self.string()
case _:
if self.is_digit(c):
self.number()
elif self.is_alpha(c):
self.identifier()
else:
self.error(f"Unexpected character: {c}")
def is_alpha(self,c):
if (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') or c == '_':
return True
return False
def identifier(self):
while self.is_alpha_numeric(self.peek()):
self.advance()
text = self.source[self.start : self.current]
type = self.reserved_words.get(text)
if type == None:
type = "IDENTIFIER"
self.add_token(type)
def is_alpha_numeric(self,c):
if self.is_digit(c) or self.is_alpha(c):
return True
return False
def is_digit(self,c):
if c >= '0' and c <= '9':
return True
return False
def number(self):
while self.is_digit(self.peek()):
self.advance()
if self.peek() == '.' and self.is_digit(self.peekNext()):
self.advance()
while self.is_digit(self.peek()):
self.advance()
self.add_token('NUMBER', float(self.source[self.start : self.current]))
def peekNext(self):
if (self.current + 1) >= len(self.source):
return '\0'
return self.source[self.current + 1]
def string(self):
while self.peek() != '"' and not self.is_at_end():
if self.peek() == '\n':
self.line += 1
self.advance()
if self.is_at_end():
self.error("Unterminated string.")
return
self.advance()
value = self.source[self.start+1 : self.current-1]
self.add_token('STRING', value)
def match(self, expected):
if self.is_at_end():
return False
if self.source[self.current] != expected:
return False
self.current += 1
return True
def peek(self):
if self.is_at_end():
return '\0'
return self.source[self.current]
def add_token(self, type, literal=None):
token = self.source[self.start : self.current]
self.tokens.append(Token(type, token, literal, self.line))
def error(self, message):
self.errors.append(f"[line {self.line}] Error: {message}")
def advance(self):
self.current += 1
return self.source[self.current - 1]