-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanner.py
149 lines (119 loc) · 4.87 KB
/
Scanner.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
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
from TokenType import getType
from Token import *
from Error import *
# this will construct the "sentence". It'll put all the lexemes together in an array list.
# I use string comparisons, but I should use enums. This will be slow in the future.
class Scanner:
def __init__(self, raw):
self.rawtext = raw
self.tokens = []
self.cursor = 0
self.line = 1
self.start = 0
self.length = len(self.rawtext)
self.error = Error()
self.scan()
# I feel like this should be recursive...
def scan(self):
self.tokens.append(Token("newline", "\n", False, self.line))
while(self.cursor < len(self.rawtext) - 1):
self.start = self.cursor
char = self.getNextChar()
isliteral = False
# ignores whitespace
if(char == " "):
continue
# Handles literal strings.
if(char == "\""):
# This clear the char so we can get rid of leading "
char = ""
while(self.peek() != "\""):
nextchar = self.getNextChar()
# TODO make this more like expected()
if(self.cursor == self.length):
self.line = self.line + 1
self.throw("open quotation", self.line)
if(char == "\n"):
self.line = self.line + 1
else:
char = char + nextchar
self.getNextChar()
tokentype = "string"
isliteral = True
# identifies keywords and idetifiers
elif(self.isAlpha(char)):
while(self.isAlpha(self.peek())):
nextchar = self.getNextChar()
char = char + nextchar
tokentype = getType(char)
if(tokentype == None):
tokentype = "identifier"
# handles literal ints and doubles. allows trailing and leading periods
elif(self.isNum(char) or char == "."):
while(self.isNum(self.peek()) or self.peek() == "."):
nextchar = self.getNextChar()
char = char + nextchar
if(char.__contains__(".")):
char = float(char)
tokentype = "double"
else:
char = int(char)
tokentype = "int"
isliteral = True
# handles comments
elif(char == "\\"):
while(self.peek() != "\\"):
char = self.getNextChar()
# TODO make this more like expected()
if(self.cursor == self.length):
self.line = self.line + 1
self.throw("unclosed comment", self.line)
if(char == "\n"):
self.line = self.line + 1
self.getNextChar()
continue
else:
# handles double lexemes.
if(char == "!" or char == "<" or char == ">" or char == "="):
if(self.expected("=")):
nextchar = self.getNextChar()
char = char + nextchar
# TODO This ignores the last character if it's an operator
elif(char == "\n"):
self.line + self.line + 1
tokentype = getType(char)
if(tokentype == None):
break
self.tokens.append(Token(tokentype, char, isliteral, self.line))
self.tokens.append(Token("EOF", "eof", False, self.line))
# Returns true if the next character is equal to the expected character c.
def expected(self, c):
if(self.peek() == c):
return True
else:
return False
# looks at next char without iterating the cursor
# TODO proper end of line return. throws an error if the input file doesn't end with a new line
def peek(self):
return self.rawtext[self.cursor]
# gets the next character while iterating the cursor.
def getNextChar(self):
self.cursor = self.cursor + 1
return self.rawtext[self.cursor - 1]
def isNum(self, value):
if(ord(value) >= 48 and ord(value) < 58):
return True
else:
return False
def isAlpha(self, value):
if((ord(value) >= 65 and ord(value) < 91) or (ord(value) >= 97 and ord(value) < 123)):
return True
else:
return False
def toString(self):
for i in self.tokens:
print(i.toString())
# helper method to deal with errors
def throw(self, s, l):
self.error.setError(s, l)
self.error.throwError()