-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.py
324 lines (277 loc) · 10.1 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""Read the circuit definition file and translate the characters into symbols.
Used in the Logic Simulator project to read the characters in the definition
file and translate them into symbols that are usable by the parser.
Classes
-------
Scanner - reads definition file and translates characters into symbols.
Symbol - encapsulates a symbol and stores its properties.
"""
from inspect import FrameInfo, currentframe, getframeinfo
from pathlib import Path
import sys
import os
class Symbol:
"""Encapsulate a symbol and store its properties.
Parameters
----------
No parameters.
Public methods
--------------
No public methods.
"""
def __init__(self):
"""Initialise symbol properties."""
self.type = None
self.id = None
class Scanner:
"""Read circuit definition file and translate the characters into symbols.
Once supplied with the path to a valid definition file, the scanner
translates the sequence of characters in the definition file into symbols
that the parser can use. It also skips over comments and irrelevant
formatting characters, such as spaces and line breaks.
Parameters
----------
path: path to the circuit definition file.
names: instance of the names.Names() class.
Public methods
--------------
get_symbol(self): Translates the next sequence of characters into a symbol
and returns the symbol.
error_found(self):Outputs the current line and a ^ symbol on the next line
to highlight the location of the error.
Non-public methods
------------------
_advance(self): Read the next character.
_skip_spaces(self): Skip all whitespace character.
_get_number(self): Seek and return the next number.
_get_name(self): Seek and return the next name.
"""
def __init__(self, path, names):
"""Open specified file and initialise reserved words and IDs."""
self.char_counter = 0
self.line_counter = 1
if Path(str(path)).is_file():
if Path(str(path)).suffix == ".txt":
self.file = open(Path(str(path)), "r")
self.file.seek(0, 0)
self.current_character = self.file.read(1)
self.names = names
self.afterdot = False
self.char_in_line = []
self.symbol_type_list = [
self.NONETYPE,
# fixes a spurious error with scanner seeing a semicolon
# as the 0th element
self.DOT,
self.COMMA,
self.SEMICOLON,
self.EQUALS,
self.ARROW,
self.OPENBRACKET,
self.CLOSEDBRACKET,
self.KEYWORD,
self.DEVICE_ARG,
self.DEVICE,
self.DTYPE_IP,
self.DTYPE_OP,
self.NUMBER,
self.NAME,
self.EOF,
] = range(16)
self.keywords_list = [
"DEVICES",
"CONNECT",
"MONITOR",
"END",
"I",
]
self.device_arg_list = [
"CLOCK",
"AND",
"NAND",
"OR",
"NOR",
"SWITCH",
]
self.device_list = ["DTYPE", "XOR", "NOT"]
self.dtype_ip_list = ["SET", "CLEAR", "DATA", "CLK"]
self.dtype_op_list = ["Q", "QBAR"]
[
self.DEVICES_ID,
self.CONNECT_ID,
self.MONITOR_ID,
self.END_ID,
self.I_ID,
] = self.names.lookup(self.keywords_list)
[
self.CLOCK_ID,
self.AND_ID,
self.NAND_ID,
self.OR_ID,
self.NOR_ID,
self.SWITCH_ID,
] = self.names.lookup(self.device_arg_list)
[self.DTYPE_ID, self.XOR_ID, self.NOT_ID] = self.names.lookup(
self.device_list
)
[
self.SET_ID,
self.CLEAR_ID,
self.DATA_ID,
self.CLK_ID,
] = self.names.lookup(self.dtype_ip_list)
[self.Q_ID, self.QBAR_ID] = self.names.lookup(
self.dtype_op_list
)
else:
print(_("\nError: incorrect file type\n"))
else:
print(_("\nError invalid path\n"))
def _advance(self):
"""Read the next character."""
# add 1 to the character counter to track location in line
self.current_character = self.file.read(1)
self.char_counter += 1
if self.current_character == "\n":
self.char_in_line.append(self.char_counter - 1)
self.line_counter += 1
self.char_counter = 0
def _skip_spaces(self):
"""Skip all whitespace character."""
while self.current_character.isspace():
self._advance()
def _get_number(self):
"""Seek and return the next number."""
num = ""
num += self.current_character
self._advance()
while self.current_character.isdigit():
num += self.current_character
self._advance()
return num
def _get_name(self):
"""Seek and return the next name."""
name = self.current_character
self._advance()
while self.current_character.isalnum():
name += self.current_character
self._advance()
return name
def error_found(self):
"""Scanner error output.
Outputs the current line and a ^ symbol on the next line to
highlight the location of the error.
"""
error_location = self.file.tell()
self.file.seek(0, 0)
if self.char_counter == 0:
if self.line_counter != 1:
self.line_counter -= 1
line_text = self.file.read().split("\n")[self.line_counter - 1]
self.char_counter = self.char_in_line[-1]
else: # if the file is an empty file
line_text = ""
else:
line_text = self.file.read().split("\n")[self.line_counter - 1]
output = (
_("Error on line ")
+ str(self.line_counter)
+ "\n"
+ line_text
+ "\n"
+ " " * (self.char_counter - 1)
+ "^"
)
self.file.seek(0, 0)
self.line_counter = 1
self.char_counter = 0
for i in range(error_location):
self._advance()
return output
def get_symbol(self):
"""Translate the next sequence of characters into a symbol."""
symbol = Symbol()
self._skip_spaces()
# if symbol is a name
if self.current_character.isalpha():
if (
self.afterdot is True
and self.current_character in self.keywords_list
):
self.afterdot = False
symbol.type = self.KEYWORD
symbol.id = self.I_ID
self._advance()
else:
name_string = self._get_name()
self.afterdot = False
if name_string in self.keywords_list:
symbol.type = self.KEYWORD
elif name_string in self.device_arg_list:
symbol.type = self.DEVICE_ARG
elif name_string in self.device_list:
symbol.type = self.DEVICE
elif name_string in self.dtype_ip_list:
symbol.type = self.DTYPE_IP
elif name_string in self.dtype_op_list:
symbol.type = self.DTYPE_OP
else:
symbol.type = self.NAME
[symbol.id] = self.names.lookup([name_string])
# if symbol is a number
elif self.current_character.isdigit():
self.afterdot = False
symbol.id = self._get_number()
symbol.type = self.NUMBER
# if symbol is a dot
elif self.current_character == ".":
self.afterdot = True
symbol.type = self.DOT
self._advance()
# if symbol is a comma
elif self.current_character == ",":
self.afterdot = False
symbol.type = self.COMMA
self._advance()
# if symbol is a semicolon
elif self.current_character == ";":
self.afterdot = False
symbol.type = self.SEMICOLON
self._advance()
# if symbol is a equals
elif self.current_character == "=":
self.afterdot = False
symbol.type = self.EQUALS
self._advance()
# if symbol is an arrow
elif self.current_character == ">":
self.afterdot = False
symbol.type = self.ARROW
self._advance()
# if symbol is an openbracket
elif self.current_character == "(":
self.afterdot = False
symbol.type = self.OPENBRACKET
self._advance()
# if symbol is an closedbracket
elif self.current_character == ")":
self.afterdot = False
symbol.type = self.CLOSEDBRACKET
self._advance()
# if symbol is the end of file
elif self.current_character == "":
symbol.type = self.EOF
# if symbol is a hashtag - comment
elif self.current_character == "#":
while True:
self._advance()
if (
self.current_character == "\n"
or self.current_character == ""
):
break
return self.get_symbol()
# if symbol is an invalid character
else:
self._advance()
return symbol