-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
745 lines (693 loc) · 30.7 KB
/
parse.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
"""Parse the definition file and build the logic network.
Used in the Logic Simulator project to analyse the syntactic and semantic
correctness of the symbols received from the scanner and then builds the
logic network.
Classes
-------
Parser - parses the definition file and builds the logic network.
"""
import builtins
import wx
builtins._ = wx.GetTranslation
class Parser:
r"""Parse the definition file and build the logic network.
The parser deals with error handling. It analyses the syntactic and
semantic correctness of the symbols it receives from the scanner, and
then builds the logic network. If there are errors in the definition file,
the parser detects this and tries to recover from it, giving helpful
error messages.
Parameters
----------
names: instance of the names.Names() class.
devices: instance of the devices.Devices() class.
network: instance of the network.Network() class.
monitors: instance of the monitors.Monitors() class.
scanner: instance of the scanner.Scanner() class.
Public methods
--------------
parse_network(self): Parses the circuit definition file.
Non-public methods
------------------
_parser_output(self, text, end="\n"): Print text to the correct output
terminal.
_error(self, category, type=None, sym=None, keyword=None): Print error and
skip parsing
until stopping
symbol.
_name(self): Parse a name and return the name ID if it is a valid name.
_argument(self): Parse an argument and return the number.
_signal_name(self): Parse an input or output name and return device_id
and port_id.
_device(self): Parse a device name and return device_kind and
device_property.
_assignment(self): Parse an assignment statement and make device.
_connection(self): Parse a connection statement in _section_connect and
connect the devices.
_monitor(self): Parse a monitor statement in _section_monitor and make
monitor point.
_section_devices(self): Parse a devices section.
_section_connect(self): Parse a connect section.
_section_monitor(self): Parse a monitor section.
_section(self,type): Call _section_### function if a keyword is present.
If keyword is not present call the intended function.
Return section type.
_program(self): Parse the entire program.
"""
def __init__(
self,
names,
devices,
network,
monitors,
scanner,
mode="terminal",
output_cmd=None,
):
"""Initialise constants."""
self.mode = mode
self.output_cmd = output_cmd
self.name_error = False
# get instances of other modules from arguments
self.names = names
self.devices = devices
self.network = network
self.monitors = monitors
self.scanner = scanner
# initialise error counter, error categories, and stopping symbols
self.error_count = 0
self.error_categories = [self.SYNTAX, self.SEMANTIC] = range(2)
self.stopping_symbols = [
self.scanner.SEMICOLON,
self.scanner.EOF,
] = range(2)
# boolean variable indicating whether signal_name is input or output
self.isoutput = True
self.section_type = [self.dev, self.con, self.mon] = range(3)
[
self.invalid_device_name,
self.device_as_name,
self.keyword_as_name,
self.dtype_as_name,
self.invalid_arg_type,
self.port_error,
self.missing_symbol,
self.missing_argument,
self.unrecognised_device_type,
self.missing_keyword,
self.invalid_arg,
self.duplicate_name,
self.duplicate_monitor,
self.monitor_is_input,
self.input_to_input,
self.output_to_output,
self.port_absent,
self.input_connected,
self.unconnected_inputs,
self.section_order_error,
] = self.names.unique_error_codes(20)
def _parser_output(self, text, end="\n"):
"""Print text to the correct output terminal."""
if self.mode == "terminal":
print(text, end=end)
elif self.mode == "gui":
self.output_cmd(text)
def _error(self, category, type=None, sym=None, keyword=None, skip=True):
"""Print an error.
Print error category (syntax or semantic), error type,
and error location. Error location is specified using error_found
function from scanner.py.
If an error is found, skip parsing until stopping symbol.
"""
if self.name_error is False:
self._parser_output(self.scanner.error_found())
self.error_count += 1
if type == self.invalid_device_name:
self._parser_output(_("invalid device name\n"))
if type == self.device_as_name:
self._parser_output(
_("device type '{}' cannot be" " device name\n").format(
keyword
)
)
if type == self.dtype_as_name:
self._parser_output(
_(
"dtype input/output '{}' cannot" " be device name\n"
).format(keyword)
)
if type == self.keyword_as_name:
self._parser_output(
_("keyword '{}' cannot be device name\n").format(keyword)
)
if type == self.invalid_arg_type:
self._parser_output(_("invalid argument type\n"))
if type == self.port_error:
self._parser_output(_("invalid port identifier\n"))
if type == self.missing_symbol:
self._parser_output(_("missing symbol: {}\n").format(sym))
if type == self.missing_argument:
self._parser_output(
_("missing argument for", " decive type '{}'\n").format(
keyword
)
)
if type == self.unrecognised_device_type:
self._parser_output(_("unrecognised device type\n"))
if type == self.missing_keyword:
self._parser_output(
_("missing keyword '{}'\n").format(keyword)
)
if type == self.invalid_arg:
self._parser_output(
_("argument '{}' outside of" " accepted range\n").format(
keyword
)
)
if type == self.duplicate_name:
self._parser_output(
_(
"name '{}' already used in previous device"
" assignment\n"
).format(keyword)
)
if type == self.duplicate_monitor:
self._parser_output(
_("monitor point '{}' already declared\n").format(keyword)
)
if type == self.monitor_is_input:
self._parser_output(
_("monitor point is an input, only outputs are allowed\n")
)
if type == self.input_to_input:
self._parser_output(_("input connected to input\n"))
if type == self.output_to_output:
self._parser_output(_("output connected to output\n"))
if type == self.port_absent:
self._parser_output(_("port is absent\n"))
if type == self.input_connected:
self._parser_output(_("input is already connected\n"))
if type == self.unconnected_inputs:
self._parser_output(
_("incomplete network, not all inputs are connected\n")
)
if type == self.section_order_error:
self._parser_output(_("incorrect ordering of sections\n"))
if skip is True:
while self.symbol.type not in self.stopping_symbols:
self.symbol = self.scanner.get_symbol()
elif skip is False:
self.symbol = self.scanner.get_symbol()
def _name(self):
"""Parse a name and return the name ID if it is a valid name.
Else return None.
"""
if self.symbol.type == self.scanner.NAME:
device_id = self.symbol.id
self.symbol = self.scanner.get_symbol()
return device_id
# if device type is device name
elif (
self.symbol.type == self.scanner.DEVICE
or self.symbol.type == self.scanner.DEVICE_ARG
):
device_type = self.names.get_name_string(self.symbol.id)
self._error(self.SYNTAX, self.device_as_name, keyword=device_type)
self.name_error = True
return None
# if dtype input output is device name
elif (
self.symbol.type == self.scanner.DTYPE_IP
or self.symbol.type == self.scanner.DTYPE_OP
):
dtype = self.names.get_name_string(self.symbol.id)
self._error(self.SYNTAX, self.dtype_as_name, keyword=dtype)
self.name_error = True
return None
# if keyword is device name
elif self.symbol.type == self.scanner.KEYWORD:
keyword = self.names.get_name_string(self.symbol.id)
self._error(self.SYNTAX, self.keyword_as_name, keyword=keyword)
self.name_error = True
return None
else:
# invalid device name
self._error(self.SYNTAX, self.invalid_device_name)
self.name_error = True
return None
def _argument(self):
"""Parse an argument and return the number.
Else return None (if it's not a number).
"""
if self.symbol.type == self.scanner.NUMBER:
argument = int(self.symbol.id)
self.symbol = self.scanner.get_symbol()
return argument
else:
# invalid argument type
self._error(self.SYNTAX, self.invalid_arg_type)
return None
def _signal_name(self):
"""Parse an input or output name in the formats.
input - [name].I[port_id]
or [name].[dtype_ip]
output -[name]
or [name].[dtype_op].
Returns device_id and port_id.
"""
device_id = self._name()
if device_id is not None: # if the device name is valid
if self.symbol.type == self.scanner.DOT: # seen a "."
self.symbol = self.scanner.get_symbol()
if (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.I_ID
): # see "I"
self.isoutput = False
self.symbol = self.scanner.get_symbol()
arg = self._argument()
if arg is not None: # if the argument is valid
port_id = self.names.lookup(f"I{arg}")
else: # if the argument is invalid
device_id = None # <- error
port_id = None
# see dtype input
elif self.symbol.type == self.scanner.DTYPE_IP:
self.isoutput = False
if self.symbol.id == self.devices.SET_ID:
port_id = self.devices.SET_ID
elif self.symbol.id == self.devices.CLEAR_ID:
port_id = self.devices.CLEAR_ID
elif self.symbol.id == self.devices.DATA_ID:
port_id = self.devices.DATA_ID
elif self.symbol.id == self.devices.CLK_ID:
port_id = self.devices.CLK_ID
self.symbol = self.scanner.get_symbol()
# see dtype output
elif self.symbol.type == self.scanner.DTYPE_OP:
self.isoutput = True
if self.symbol.id == self.devices.Q_ID:
port_id = self.devices.Q_ID
elif self.symbol.id == self.devices.QBAR_ID:
port_id = self.devices.QBAR_ID
self.symbol = self.scanner.get_symbol()
else: # unrecognised port
# unrecognised port
self._error(self.SYNTAX, self.port_error)
device_id = None # <- error
port_id = None
else: # there was no "." -> output
self.isoutput = True
port_id = None
return (device_id, port_id)
else: # if the device name is invalid
return (None, None)
def _device(self):
"""Parse a device name.
Returns:
device_kind - type of device
device_property - number of inputs to the device (None if device
without arguments).
"""
# if the symbol is a device without arguments
if self.symbol.type == self.scanner.DEVICE:
if self.symbol.id == self.scanner.DTYPE_ID: # it's a DTYPE
device_kind = self.devices.D_TYPE
elif self.symbol.id == self.scanner.XOR_ID: # it's a XOR
device_kind = self.devices.XOR
elif self.symbol.id == self.scanner.NOT_ID: # it's a NOT
device_kind = self.devices.NOT
self.symbol = self.scanner.get_symbol()
return device_kind, None
# if the symbol is a device with arguments
elif self.symbol.type == self.scanner.DEVICE_ARG:
device_id = self.names.get_name_string(self.symbol.id)
if self.symbol.id == self.scanner.CLOCK_ID: # it's a CLOCK
device_kind = self.devices.CLOCK
if self.symbol.id == self.scanner.AND_ID: # it's a AND
device_kind = self.devices.AND
if self.symbol.id == self.scanner.OR_ID: # it's a OR
device_kind = self.devices.OR
if self.symbol.id == self.scanner.NAND_ID: # it's a NAND
device_kind = self.devices.NAND
if self.symbol.id == self.scanner.NOR_ID: # it's a NOR
device_kind = self.devices.NOR
if self.symbol.id == self.scanner.SWITCH_ID: # it's a SWITCH
device_kind = self.devices.SWITCH
self.symbol = self.scanner.get_symbol()
# device with arguments have to be followed by an openbracket
if self.symbol.type == self.scanner.OPENBRACKET:
self.symbol = self.scanner.get_symbol()
device_property = self._argument()
if device_property is not None: # the argument was valid
# we remembered to close the bracket
if self.symbol.type == self.scanner.CLOSEDBRACKET:
self.symbol = self.scanner.get_symbol()
return device_kind, device_property
else: # we forgot to close the bracket
# missing ")"
self._error(self.SYNTAX, self.missing_symbol, sym=")")
return None, None
else: # the argument was not valid
return None, None
else: # we forgot an "("
# missing argument for device
self._error(
self.SYNTAX, self.missing_argument, keyword=device_id
)
return None, None
else: # device is not recoginised
self._error(self.SYNTAX, self.unrecognised_device_type)
return None, None
def _assignment(self):
"""Parse an assigment line.
This could be a list of names or a single name.
Make all the devices, as required.
"""
name_list = [] # to store any names, single or multiple
device_id = self._name()
if device_id is not None: # if device name is valid
name_list.append(device_id) # store valid name in list
# see a "," or the next symbol is not stopping symbol
while self.symbol.type == self.scanner.COMMA:
self.symbol = self.scanner.get_symbol()
device_id = self._name()
if device_id is not None: # valid name
name_list.append(device_id) # store valid name in list
else: # it's not a valid name
break
if self.symbol.type == self.scanner.EOF:
break
# we encountered another name without a ","
if self.name_error is False:
if self.symbol.type == self.scanner.NAME:
self._error(
self.SYNTAX, self.missing_symbol, sym=","
) # missing symbol ","
# finished reading in names
elif self.symbol.type == self.scanner.EQUALS:
self.symbol = self.scanner.get_symbol()
device_kind, device_property = self._device()
if device_kind is not None: # it's a valid device
error_list = [] # create list of errors to catch duplicate
# names and arguments outside range
for i in range(len(name_list)):
error_type = self.devices.make_device(
name_list[i], device_kind, device_property
)
error_list.append(error_type)
# argument for device outside of range
if self.devices.INVALID_QUALIFIER in error_list:
# invalid argument
self._error(
self.SEMANTIC,
self.invalid_arg,
keyword=device_property,
)
# used name in device declaration (duplicate name used)
if self.devices.DEVICE_PRESENT in error_list or len(
name_list
) != len(set(name_list)):
device_name = self.names.get_name_string(device_id)
# used name in device declaration
self._error(
self.SEMANTIC,
self.duplicate_name,
keyword=device_name,
)
else: # it's not a valid device
pass # device outputs error for us here
else: # not another name or "=", therefore missing equals symbol
self._error(
self.SYNTAX, self.missing_symbol, sym="="
) # missing "="
if self.symbol.type == self.scanner.SEMICOLON: # see ";"
pass
else:
self._error(self.SYNTAX, self.missing_symbol, sym=";")
def _connection(self):
"""Parse a connection statement.
Make all the connections, as required.
"""
# call signal_name and save device_id and port_id
first_device_id, first_port_id = self._signal_name()
second_device_ids = [] # list to store device ids of inputs
second_port_ids = [] # list to store port ids of inputs
error_list = [] # list to store errors returned from network
if first_device_id is not None: # it's a valid signal name
if self.symbol.type == self.scanner.ARROW: # see a ">"
self.symbol = self.scanner.get_symbol()
second_device_id, second_port_id = self._signal_name()
if second_device_id is not None: # it's a valid signal name
# add device_id to list of device ids
second_device_ids.append(second_device_id)
# add port_id to list of port ids
second_port_ids.append(second_port_id)
while self.symbol.type == self.scanner.COMMA: # see a ","
self.symbol = self.scanner.get_symbol()
second_device_id, second_port_id = self._signal_name()
# it's a valid signal name
if second_device_id is not None:
# add device_id to list of device ids
second_device_ids.append(second_device_id)
# add port_id to list of port ids
second_port_ids.append(second_port_id)
else: # signal_name raises an error
break
if self.symbol.type == self.scanner.EOF:
break
# iterate over devices in connection output
for i in range(len(second_device_ids)):
# see if an error is returned by network.make_connection
error = self.network.make_connection(
first_device_id,
first_port_id,
second_device_ids[i],
second_port_ids[i],
)
# add the error to a list of errors
error_list.append(error)
# input is already in a connection
if self.network.INPUT_CONNECTED in error_list:
self._error(self.SEMANTIC, self.input_connected)
# both ports are inputs
if self.network.INPUT_TO_INPUT in error_list:
self._error(self.SYNTAX, self.input_to_input)
# both ports are outputs
if self.network.OUTPUT_TO_OUTPUT in error_list:
self._error(self.SYNTAX, self.output_to_output)
if self.network.PORT_ABSENT in error_list: # invalid port
self._error(self.SEMANTIC, self.port_absent)
else: # you don't see an arrow
self._error(self.SYNTAX, self.missing_symbol, sym=">")
if self.symbol.type == self.scanner.SEMICOLON:
pass
else: # you don't see a ";"
self._error(self.SYNTAX, self.missing_symbol, sym=";")
def _monitor(self):
"""Parse a monitor statement.
Make all monitor points, as required.
"""
device_id, output_id = self._signal_name()
if self.isoutput is False: # monitor point is an input, invalid
self._error(self.SYNTAX, self.monitor_is_input)
else: # monitor point is an output, valid
# see if an error is returned by monitors.make_monitor
error = self.monitors.make_monitor(device_id, output_id)
if error == self.monitors.MONITOR_PRESENT:
monitor_name = self.names.get_name_string(device_id)
if output_id is not None:
monitor_name += "."
monitor_name += self.names.get_name_string(output_id)
self._error(
self.SEMANTIC, self.duplicate_monitor, keyword=monitor_name
)
if self.symbol.type == self.scanner.SEMICOLON:
pass
else:
self._error(self.SYNTAX, self.missing_symbol, sym=";")
def _section_devices(self):
"""Parse a section of assignments."""
# cheking for keyword "DEVICES" to start the devices section
if (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.DEVICES_ID
):
self.symbol = self.scanner.get_symbol()
else: # missing the keyword "DEVICES"
# missing keyword "DEVICES
self._error(
self.SYNTAX,
self.missing_keyword,
keyword="DEVICES",
skip=False,
)
self._assignment()
# _assignment function ends with a semicolon
while self.symbol.type == self.scanner.SEMICOLON:
self.symbol = self.scanner.get_symbol()
# stop loop when you see an "END"
if (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.END_ID
):
break
# stop loop when you see "CONNECT" or "MONITOR" and raise error
elif self.symbol.type == self.scanner.KEYWORD and (
self.symbol.id == self.scanner.CONNECT_ID
or self.symbol.id == self.scanner.MONITOR_ID
):
self._error(self.SYNTAX, self.missing_keyword, keyword="END")
break
# prevent infinite loop
elif self.symbol.type == self.scanner.EOF:
self._error(self.SYNTAX, self.missing_keyword, keyword="END")
break
else:
self._assignment()
if self.symbol.type != self.scanner.EOF:
self.symbol = self.scanner.get_symbol()
def _section_connect(self):
"""Parse a section of connections."""
# cheking for keyword "CONNECT" to start the devices section
if (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.CONNECT_ID
):
self.symbol = self.scanner.get_symbol()
else: # missing the keyword "CONNECT"
# missing keyword "CONNECT
self._error(
self.SYNTAX,
self.missing_keyword,
keyword="CONNECT",
skip=False,
)
self._connection()
# _connection function ends with a semicolon
while self.symbol.type == self.scanner.SEMICOLON:
self.symbol = self.scanner.get_symbol()
# stop loop when you see an "END"
if (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.END_ID
):
break
# stop loop when you see "DEVICES" or "MONITOR" and raise error
elif self.symbol.type == self.scanner.KEYWORD and (
self.symbol.id == self.scanner.DEVICES_ID
or self.symbol.id == self.scanner.MONITOR_ID
):
self._error(self.SYNTAX, self.missing_keyword, keyword="END")
break
# prevent infinite loop
elif self.symbol.type == self.scanner.EOF:
self._error(self.SYNTAX, self.missing_keyword, keyword="END")
break
else:
self._connection()
if self.symbol.type != self.scanner.EOF:
self.symbol = self.scanner.get_symbol()
def _section_monitor(self):
"""Parse a section of monitors."""
# checking for keyword "MONITOR" to start the connect section
if (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.MONITOR_ID
):
self.symbol = self.scanner.get_symbol()
else: # missing the keyword "MONITOR"
# missing keyword "CONNECT"
self._error(
self.SYNTAX,
self.missing_keyword,
keyword="MONITOR",
skip=False,
)
self._monitor()
# _monitor function ends with a semicolon
while self.symbol.type == self.scanner.SEMICOLON:
self.symbol = self.scanner.get_symbol()
# stop loop when you see an "END"
if (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.END_ID
):
break
# stop loop when you see "DEVICES" or "CONNECT" and raise error
elif self.symbol.type == self.scanner.KEYWORD and (
self.symbol.id == self.scanner.DEVICES_ID
or self.symbol.id == self.scanner.CONNECT_ID
):
self._error(self.SYNTAX, self.missing_keyword, keyword="END")
break
# prevent infinite loop
elif self.symbol.type == self.scanner.EOF:
self._error(self.SYNTAX, self.missing_keyword, keyword="END")
break
else:
self._monitor()
if self.symbol.type != self.scanner.EOF:
self.symbol = self.scanner.get_symbol()
def _section(self, type):
"""Call _section_### function if a keyword is present.
If keyword is not present call the intended function.
Return section type.
"""
if (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.DEVICES_ID
):
self._section_devices()
return self.dev
elif (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.CONNECT_ID
):
self._section_connect()
return self.con
elif (
self.symbol.type == self.scanner.KEYWORD
and self.symbol.id == self.scanner.MONITOR_ID
):
self._section_monitor()
return self.mon
else: # if keyword is not present
if type == self.dev:
self._section_devices()
elif type == self.con:
self._section_connect()
elif type == self.mon:
self._section_monitor()
return type
def _program(self):
"""Parse the entire program."""
self.symbol = self.scanner.get_symbol()
section_dev = self._section(self.dev)
# if the first section is not a devices section
if section_dev != self.dev:
self._error(self.SYNTAX, self.section_order_error, skip=False)
section_con = self._section(self.con)
# if the second section is not a connect section
if section_con != self.con:
self._error(self.SYNTAX, self.section_order_error, skip=False)
section_mon = self._section(self.mon)
# if the third section is not a monitor section
if section_mon != self.mon:
self._error(self.SYNTAX, self.section_order_error, skip=False)
if self.error_count == 0:
# if all inputs are connected
if self.network.check_network() is True:
pass
# if not all inputs are connected
elif self.network.check_network() is False:
self._error(self.SEMANTIC, self.unconnected_inputs)
else:
pass
def parse_network(self):
"""Parse the circuit definition file."""
self._program()
if self.error_count == 0:
return True
else:
self._parser_output(_("Error Count: {}").format(self.error_count))
return False