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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
|
#!/usr/bin/python
##
## license:BSD-3-Clause
## copyright-holders:Vas Crabb
import argparse
import glob
import io
import os.path
import sys
import xml.sax
def path_components(path):
result = [ ]
while True:
path, basename = os.path.split(path)
if basename:
result.append(basename)
else:
if path:
result.append(path)
return tuple(reversed(result))
class ParserBase:
def process_lines(self, inputfile):
self.input_line = 1
for line in inputfile:
start = 0
if line.endswith('\n'):
line = line[:-1]
used = 0
while used is not None:
start += used
used = self.processors[self.parse_state](line[start:])
self.input_line += 1
class CppParser(ParserBase):
TOKEN_LEAD = frozenset(
[chr(x) for x in range(ord('A'), ord('Z') + 1)] +
[chr(x) for x in range(ord('a'), ord('z') + 1)] +
['_'])
TOKEN_CONTINUATION = frozenset(
[chr(x) for x in range(ord('0'), ord('9') + 1)] +
[chr(x) for x in range(ord('A'), ord('Z') + 1)] +
[chr(x) for x in range(ord('a'), ord('z') + 1)] +
['_'])
HEXADECIMAL_DIGIT = frozenset(
[chr(x) for x in range(ord('0'), ord('9') + 1)] +
[chr(x) for x in range(ord('A'), ord('F') + 1)] +
[chr(x) for x in range(ord('a'), ord('f') + 1)])
class Handler:
def line(self, text):
pass
def comment(self, text):
pass
def line_comment(self, text):
pass
class ParseState:
DEFAULT = 0
COMMENT = 1
LINE_COMMENT = 2
TOKEN = 3
STRING_CONSTANT = 4
CHARACTER_CONSTANT = 5
NUMERIC_CONSTANT = 6
def __init__(self, handler, **kwargs):
super().__init__(**kwargs)
self.handler = handler
self.processors = {
self.ParseState.DEFAULT: self.process_default,
self.ParseState.COMMENT: self.process_comment,
self.ParseState.LINE_COMMENT: self.process_line_comment,
self.ParseState.TOKEN: self.process_token,
self.ParseState.STRING_CONSTANT: self.process_text,
self.ParseState.CHARACTER_CONSTANT: self.process_text,
self.ParseState.NUMERIC_CONSTANT: self.process_numeric }
def parse(self, inputfile):
self.parse_state = self.ParseState.DEFAULT
self.comment_line = None
self.lead_digit = None
self.radix = None
self.line_buffer = ''
self.comment_buffer = ''
self.process_lines(inputfile)
if self.parse_state == self.ParseState.COMMENT:
raise Exception('unterminated multi-line comment beginning on line %d' % (self.comment_line, ))
elif self.parse_state == self.ParseState.CHARACTER_CONSTANT:
raise Exception('unterminated character literal on line %d' % (self.input_line, ))
elif self.parse_state == self.ParseState.STRING_CONSTANT:
raise Exception('unterminated string literal on line %d' % (self.input_line, ))
def process_default(self, line):
escape = False
pos = 0
length = len(line)
while pos < length:
ch = line[pos]
if (ch == '"') or (ch == "'"):
self.parse_state = self.ParseState.STRING_CONSTANT if ch == '"' else self.ParseState.CHARACTER_CONSTANT
self.line_buffer += line[:pos + 1]
return pos + 1
elif ch == '*':
if escape:
self.parse_state = self.ParseState.COMMENT
self.comment_line = self.input_line
self.line_buffer += line[:pos - 1] + ' '
return pos + 1
elif ch == '/':
if escape:
self.parse_state = self.ParseState.LINE_COMMENT
self.handler.line(self.line_buffer + line[:pos - 1] + ' ')
self.line_buffer = ''
return pos + 1
elif ch in self.TOKEN_LEAD:
self.parse_state = self.ParseState.TOKEN
self.line_buffer += line[:pos]
return pos
elif (ch >= '0') and (ch <= '9'):
self.parse_state = self.ParseState.NUMERIC_CONSTANT
self.line_buffer += line[:pos]
return pos
escape = ch == '/'
pos += 1
if line.endswith('\\'):
self.line_buffer += line[:-1]
else:
self.handler.line(self.line_buffer + line)
self.line_buffer = ''
def process_comment(self, line):
escape = False
pos = 0
length = len(line)
while pos < length:
ch = line[pos]
if escape and (ch == '/'):
self.parse_state = self.ParseState.DEFAULT
self.comment_line = None
self.handler.comment(self.comment_buffer + line[:pos - 1])
self.comment_buffer = ''
return pos + 1
escape = ch == '*'
pos += 1
if line.endswith('\\'):
self.comment_buffer += line[:-1]
else:
self.comment_buffer += line + '\n'
def process_line_comment(self, line):
self.parse_state = self.ParseState.DEFAULT
self.handler.line_comment(self.comment_buffer + line)
self.comment_buffer = ''
def process_token(self, line):
pos = 0
length = len(line)
while pos < length:
ch = line[pos]
if ch not in self.TOKEN_CONTINUATION:
self.parse_state = self.ParseState.DEFAULT
self.line_buffer += line[:pos]
return pos
pos += 1
self.parse_state = self.ParseState.DEFAULT
self.handler.line(self.line_buffer + line)
self.line_buffer = ''
def process_text(self, line):
quote = '"' if self.parse_state == self.ParseState.STRING_CONSTANT else "'"
escape = False
pos = 0
length = len(line)
while pos < length:
ch = line[pos]
if (ch == quote) and not escape:
self.parse_state = self.ParseState.DEFAULT
self.line_buffer += line[:pos + 1]
return pos + 1
escape = (ch == '\\') and not escape
pos += 1
if line.endswith('\\'):
self.line_buffer += line[:-1]
else:
t = 'string' if self.ParseState == self.ParseState.STRING_CONSTANT else 'character'
raise Exception('unterminated %s literal on line %d' % (t, self.input_line))
def process_numeric(self, line):
escape = False
pos = 0
length = len(line)
while pos < length:
ch = line[pos]
if self.lead_digit is None:
self.lead_digit = ch
if ch != '0':
self.radix = 10
elif self.radix is None:
if ch == "'":
if escape:
raise Exception('adjacent digit separators on line %d' % (self.input_line, ))
else:
escape = True
elif (ch == 'B') or (ch == 'b'):
self.radix = 2
elif (ch == 'X') or (ch == 'x'):
self.radix = 16
elif (ch >= '0') and (ch <= '7'):
self.radix = 8
else:
self.parse_state = self.ParseState.DEFAULT # probably an argument to a token-pasting or stringifying macro
else:
if ch == "'":
if escape:
raise Exception('adjacent digit separators on line %d' % (self.input_line, ))
else:
escape = True
else:
escape = False
if self.radix == 2:
if (ch < '0') or (ch > '1'):
self.parse_state = self.ParseState.DEFAULT
elif self.radix == 8:
if (ch < '0') or (ch > '7'):
self.parse_state = self.ParseState.DEFAULT
elif self.radix == 10:
if (ch < '0') or (ch > '9'):
self.parse_state = self.ParseState.DEFAULT
elif self.radix == 16:
if ch not in self.HEXADECIMAL_DIGIT:
self.parse_state = self.ParseState.DEFAULT
if self.parse_state == self.ParseState.DEFAULT:
self.lead_digit = None
self.radix = None
self.line_buffer += line[:pos]
return pos
pos += 1
self.parse_state = self.ParseState.DEFAULT
self.lead_digit = None
self.radix = None
self.handler.line(self.line_buffer + line)
self.line_buffer = ''
class LuaParser(ParserBase):
class Handler:
def short_comment(self, text):
pass
def long_comment_start(self, level):
pass
def long_comment_line(self, text):
pass
def long_comment_end(self):
pass
class ParseState:
DEFAULT = 0
SHORT_COMMENT = 1
LONG_COMMENT = 2
STRING_CONSTANT = 3
LONG_STRING_CONSTANT = 4
def __init__(self, handler, **kwargs):
super().__init__(**kwargs)
self.handler = handler
self.processors = {
self.ParseState.DEFAULT: self.process_default,
self.ParseState.SHORT_COMMENT: self.process_short_comment,
self.ParseState.LONG_COMMENT: self.process_long_comment,
self.ParseState.STRING_CONSTANT: self.process_string_constant,
self.ParseState.LONG_STRING_CONSTANT: self.process_long_string_constant }
def parse(self, inputfile):
self.parse_state = self.ParseState.DEFAULT
self.long_bracket_level = None
self.escape = False
self.block_line = None
self.block_level = None
self.string_quote = None
self.process_lines(inputfile)
if self.parse_state == self.ParseState.LONG_COMMENT:
raise Exception('unterminated long comment beginning on line %d' % (self.block_line, ))
if self.parse_state == self.ParseState.STRING_CONSTANT:
raise Exception('unterminated string literal on line %d' % (self.input_line, ))
if self.parse_state == self.ParseState.LONG_STRING_CONSTANT:
raise Exception('unterminated long string literal beginning on line %d' % (self.block_line, ))
def process_default(self, line):
pos = 0
length = len(line)
while pos < length:
ch = line[pos]
if (ch == '"') or (ch == "'"):
self.string_quote = ch
self.parse_state = self.ParseState.STRING_CONSTANT
self.long_bracket_level = None
self.escape = False
return pos + 1
elif (ch == '-') and self.escape:
self.parse_state = self.ParseState.SHORT_COMMENT
self.long_bracket_level = None
self.escape = False
return pos + 1
elif self.long_bracket_level is not None:
if ch == '=':
self.long_bracket_level += 1
elif ch == '[':
self.block_line = self.input_line
self.block_level = self.long_bracket_level
self.parse_state = self.ParseState.LONG_STRING_CONSTANT
self.long_bracket_level = None
self.escape = False
return pos + 1
else:
self.long_bracket_level = None
elif ch == '[':
self.long_bracket_level = 0
self.escape = ch == '-'
pos += 1
self.escape = False
def process_short_comment(self, line):
pos = 0
length = len(line)
while pos < length:
ch = line[pos]
if self.long_bracket_level is not None:
if ch == '=':
self.long_bracket_level += 1
elif ch == '[':
self.block_line = self.input_line
self.block_level = self.long_bracket_level
self.parse_state = self.ParseState.LONG_COMMENT
self.long_bracket_level = None
self.handler.long_comment_start(self.block_level)
return pos + 1
else:
self.long_bracket_level = None
elif ch == '[':
self.long_bracket_level = 0
if self.long_bracket_level is None:
self.handler.short_comment(line[pos:])
self.parse_state = self.ParseState.DEFAULT
return None
pos += 1
self.handler.short_comment(line)
self.parse_state = self.ParseState.DEFAULT
def process_long_comment(self, line):
pos = 0
length = len(line)
while pos < length:
ch = line[pos]
if self.long_bracket_level is not None:
if ch == '=':
self.long_bracket_level += 1
elif ch == ']':
if self.long_bracket_level == self.block_level:
if self.parse_state == self.ParseState.LONG_COMMENT:
self.handler.long_comment_line(line[:endpos])
self.handler.long_comment_end()
self.parse_state = self.ParseState.DEFAULT
return pos + 1
else:
self.long_bracket_level = 0
else:
self.long_bracket_level = None
elif ch == ']':
endpos = pos
self.long_bracket_level = 0
pos += 1
self.long_bracket_level = None
self.handler.long_comment_line(line)
def process_string_constant(self, line):
pos = 0
length = len(line)
while pos < length:
ch = line[pos]
if (ch == self.string_quote) and not self.escape:
self.parse_state = self.ParseState.DEFAULT
return pos + 1
self.escape = (ch == '\\') and not self.escape
pos += 1
if not self.escape:
raise Exception('unterminated string literal on line %d' % (self.input_line, ))
def process_long_string_constant(self, line):
self.process_long_comment(line) # this works because they're both closed by a matching long bracket
class DriverFilter:
DRIVER_CHARS = frozenset(
[chr(x) for x in range(ord('0'), ord('9') + 1)] +
[chr(x) for x in range(ord('a'), ord('z') + 1)] +
['_'])
def parse_filter(self, root, path, sourcefile, inclusion, exclusion):
def line_hook(text):
text = text.strip()
if text.startswith('#'):
do_parse(os.path.join(os.path.dirname(n), text[1:].lstrip()))
elif text.startswith('+'):
text = text[1:].lstrip()
if not text:
sys.stderr.write('%s:%s: Empty driver name\n' % (path, parser.input_line))
sys.exit(1)
elif not all(x in self.DRIVER_CHARS for x in text):
sys.stderr.write('%s:%s: Invalid character in driver name "%s"\n' % (path, parser.input_line, text))
sys.exit(1)
inclusion(text)
elif text.startswith('-'):
text = text[1:].lstrip()
if not text:
sys.stderr.write('%s:%s: Empty driver name\n' % (path, parser.input_line))
sys.exit(1)
elif not all(x in self.DRIVER_CHARS for x in text):
sys.stderr.write('%s:%s: Invalid character in driver name "%s"\n' % (path, parser.input_line, text))
sys.exit(1)
exclusion(text)
elif text:
if (len(text) >= 2) and ((text[0] == '"') or (text[0] == "'")) and (text[0] == text[-1]):
text = text[1:-1]
paths = glob.glob(os.path.join(basepath, *text.split('/')))
if not paths:
sys.stderr.write('%s:%s: Pattern "%s" did not match any source files\n' % (path, parser.input_line, text))
sys.exit(1)
for source in paths:
sourcefile('/'.join(path_components(os.path.relpath(source, basepath))))
try:
filterfile = io.open(path, 'r', encoding='utf-8')
except IOError:
sys.stderr.write('Unable to open filter file "%s"\n' % (path, ))
sys.exit(1)
with filterfile:
basepath = os.path.join(root, 'src', 'mame')
handler = CppParser.Handler()
handler.line = line_hook
parser = CppParser(handler)
try:
parser.parse(filterfile)
except IOError:
sys.stderr.write('Error reading filter file "%s"\n' % (path, ))
sys.exit(1)
except Exception as e:
sys.stderr.write('Error parsing filter file "%s": %s\n' % (path, e))
sys.exit(1)
def parse_list(self, path, sourcefile, driver):
def line_hook(text):
text = text.strip()
if text.startswith('#'):
do_parse(os.path.join(os.path.dirname(n), text[1:].lstrip()))
elif text.startswith('@'):
parts = text[1:].lstrip().split(':', 1)
parts[0] = parts[0].strip()
if (parts[0] == 'source') and (len(parts) == 2):
parts[1] = parts[1].strip()
if not parts[1]:
sys.stderr.write('%s:%s: Empty source file name "%s"\n' % (path, parser.input_line, text))
sys.exit(1)
else:
sourcefile(parts[1])
else:
sys.stderr.write('%s:%s: Unsupported directive "%s"\n' % (path, parser.input_line, text))
sys.exit(1)
elif text:
if not all(x in self.DRIVER_CHARS for x in text):
sys.stderr.write('%s:%s: Invalid character in driver name "%s"\n' % (path, parser.input_line, text))
sys.exit(1)
else:
driver(text)
try:
listfile = io.open(path, 'r', encoding='utf-8')
except IOError:
sys.stderr.write('Unable to open list file "%s"\n' % (path, ))
sys.exit(1)
with listfile:
handler = CppParser.Handler()
handler.line = line_hook
parser = CppParser(handler)
try:
parser.parse(listfile)
except IOError:
sys.stderr.write('Error reading list file "%s"\n' % (path, ))
sys.exit(1)
except Exception as e:
sys.stderr.write('Error parsing list file "%s": %s\n' % (path, e))
sys.exit(1)
class DriverLister(DriverFilter):
def __init__(self, options, **kwargs):
super().__init__(**kwargs)
def includesource(filename):
sources.add(filename)
def includedriver(shortname):
includes.add(shortname)
excludes.discard(shortname)
def excludedriver(shortname):
includes.discard(shortname)
excludes.add(shortname)
def sourcefile(filename):
if self.sources:
state['includesrc'] = filename in self.sources
def driver(shortname):
if state['includesrc'] and (shortname not in self.excludes):
drivers.add(shortname)
sources = set()
includes = set()
excludes = set()
if options.filter is not None:
self.parse_filter(options.root, options.filter, includesource, includedriver, excludedriver)
sys.stderr.write('%d source file(s) found\n' % (len(sources), ))
self.sources = frozenset(sources)
self.includes = frozenset(includes)
self.excludes = frozenset(excludes)
drivers = set()
state = { 'includesrc': True }
self.parse_list(options.list, sourcefile, driver)
for driver in self.includes:
drivers.add(driver)
sys.stderr.write('%d driver(s) found\n' % (len(drivers), ))
drivers.add('___empty')
self.drivers = sorted(drivers)
def write_source(self, f):
f.write(
'#include "emu.h"\n' \
'\n' \
'#include "drivenum.h"\n' \
'\n')
for driver in self.drivers:
f.write('GAME_EXTERN(%s);\n' % driver)
f.write(
'\n' \
'game_driver const *const driver_list::s_drivers_sorted[%d] =\n' \
'{\n' % (len(self.drivers), ))
for driver in self.drivers:
f.write('\t&GAME_NAME(%s),\n' % driver)
f.write(
'};\n' \
'\n' \
'std::size_t const driver_list::s_driver_count = %d;\n' % (len(self.drivers), ))
class DriverCollector(DriverFilter):
def __init__(self, options, **kwargs):
super().__init__(**kwargs)
def includesource(filename):
sources.add(filename)
def includedriver(shortname):
includes.add(shortname)
def excludedriver(shortname):
includes.discard(shortname)
def sourcefile(filename):
state['prevsource'] = filename
def driver(shortname):
if shortname in includes:
sources.add(state['prevsource'])
sources = set()
includes = set()
state = { 'prevsource': None }
self.parse_filter(options.root, options.filter, includesource, includedriver, excludedriver)
self.parse_list(options.list, sourcefile, driver)
sys.stderr.write('%d source file(s) found\n' % (len(sources), ))
self.sources = sorted(sources)
class DriverReconciler(DriverFilter):
class InfoHandler:
def __init__(self, drivers, **kwargs):
super().__init__(**kwargs)
self.drivers = drivers
self.bad = False
self.locator = None
self.ignored_depth = 0
self.in_document = False
self.in_mame = False
def startElement(self, name, attrs):
if not self.in_document:
raise xml.sax.SAXParseException('Unexpected start of element "%s"' % (name, ), None, self.locator)
elif self.ignored_depth > 0:
self.ignored_depth += 1
elif not self.in_mame:
if name != 'mame':
raise xml.sax.SAXParseException('Unexpected start of element "%s"' % (name, ), None, self.locator)
self.in_mame = True
elif name != 'machine':
raise xml.sax.SAXParseException('Unexpected start of element "%s"' % (name, ), None, self.locator)
else:
runnable = attrs.get('runnable', 'yes')
if runnable == 'yes':
shortname = attrs['name']
source = attrs['sourcefile'].replace('\\', '/')
declared = self.drivers.get(shortname)
if declared is None:
sys.stderr.write('Driver "%s" not declared in list file\n' % (shortname, ))
self.bad = True
elif declared != source:
sys.stderr.write('Driver "%s" found for source file "%s" but defined in source file "%s"\n' % (shortname, declared, source))
self.bad = True
self.ignored_depth = 1
def endElement(self, name):
if self.ignored_depth > 0:
self.ignored_depth -= 1
elif self.in_mame:
if name != 'mame':
raise xml.sax.SAXParseException('Unexpected end of element "%s"' % (name, ), None, self.locator)
self.in_mame = False
else:
raise xml.sax.SAXParseException('Unexpected end of element "%s"' % (name, ), None, self.locator)
def startDocument(self):
if self.in_document:
raise xml.sax.SAXParseException('Unexpected start of document', None, self.locator)
self.in_document = True
def endDocument(self):
if not self.in_document:
raise xml.sax.SAXParseException('Unexpected end of document', None, self.locator)
self.in_document = False
def setDocumentLocator(self, locator):
self.locator = locator
def startPrefixMapping(self, prefix, uri):
pass
def endPrefixMapping(self, prefix):
pass
def characters(self, content):
pass
def ignorableWhitespace(self, whitespace):
pass
def processingInstruction(self, target, data):
pass
def __init__(self, options, **kwargs):
super().__init__(**kwargs)
def sourcefile(filename):
if (state['prevsource'] is not None) and (not state['prevdrivers']):
sys.stderr.write('No drivers for source file "%s"\n' % (state['prevsource'], ))
self.bad = True
if filename in self.sources:
sys.stderr.write('Duplicate source file "%s"\n' % (filename, ))
state['prevdrivers'] = self.sources[filename]
self.bad = True
else:
drivers = set()
state['prevdrivers'] = drivers
self.sources[filename] = drivers
state['prevsource'] = filename
def driver(shortname):
if shortname in self.drivers:
sys.stderr.write('Duplicate driver "%s" for source file "%s" (previously seen for source file "%s")\n' % (shortname, state['prevsource'], self.drivers[shortname]))
self.bad = True
else:
self.drivers[shortname] = state['prevsource']
drivers = state['prevdrivers']
if drivers is None:
sys.stderr.write('Driver "%s" found outside source file section\n' % (shortname, ))
self.bad = True
else:
drivers.add(shortname)
state = { 'prevsource': None, 'prevdrivers': None }
self.bad = False
self.sources = { }
self.drivers = { }
self.parse_list(options.list, sourcefile, driver)
def reconcile_xml(self, xmlfile):
handler = self.InfoHandler(self.drivers)
try:
xml.sax.parse(xmlfile, handler=handler)
if handler.bad:
self.bad = True
except xml.sax.SAXException as err:
sys.stderr.write('Error parsing system information file: %s\n' % (err, ))
self.bad = True
def split_path(path):
path = os.path.normpath(path)
result = [ ]
while True:
dirname, basename = os.path.split(path)
if dirname == path:
result.insert(0, dirname)
return result
elif basename == path:
result.insert(0, basename)
return result
else:
result.insert(0, basename)
path = dirname
def parse_command_line():
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--root', metavar='<srcroot>', default='.', help='path to emulator source root (defaults to working directory)')
subparsers = parser.add_subparsers(title='commands', dest='command', metavar='<command>')
subparser = subparsers.add_parser('sourcesproject', help='generate project directives for source files')
subparser.add_argument('-t', '--target', metavar='<target>', required=True, help='generated emulator target name')
subparser.add_argument('-l', '--list', metavar='<lstfile>', required=True, help='master driver list file')
subparser.add_argument('sources', metavar='<srcfile>', nargs='+', help='source files to include')
subparser = subparsers.add_parser('filterproject', help='generate project directives using filter file')
subparser.add_argument('-t', '--target', metavar='<target>', required=True, help='generated emulator target name')
subparser.add_argument('-f', '--filter', metavar='<fltfile>', required=True, help='input filter file')
subparser.add_argument('list', metavar='<lstfile>', help='input list file')
subparser = subparsers.add_parser('sourcesfilter', help='generate driver filter for source files')
subparser.add_argument('-l', '--list', metavar='<lstfile>', required=True, help='master driver list file')
subparser.add_argument('sources', metavar='<srcfile>', nargs='+', help='source files to include')
subparser = subparsers.add_parser('driverlist', help='generate driver list source')
subparser.add_argument('-f', '--filter', metavar='<fltfile>', help='input filter file')
subparser.add_argument('list', metavar='<lstfile>', help='input list file')
subparser = subparsers.add_parser('reconcilelist', help='reconcile driver list')
subparser.add_argument('-l', '--list', metavar='<lstfile>', required=True, help='master driver list file')
subparser.add_argument('infoxml', metavar='<xmlfile>', nargs='?', help='XML system information file')
return parser.parse_args()
def collect_lua_directives(options):
def short_comment_hook(text):
if text.startswith('@'):
name, action = text[1:].rstrip().rsplit(',', 1)
if name not in result:
result[name] = [ ]
result[name].append(action)
base = os.path.join(options.root, 'scripts', 'src')
result = { }
handler = LuaParser.Handler()
handler.short_comment = short_comment_hook
parser = LuaParser(handler)
for name in ('bus', 'cpu', 'machine', 'sound', 'video', 'formats'):
path = os.path.join(base, name + '.lua')
try:
f = io.open(path, 'r', encoding='utf-8')
except IOError:
sys.stderr.write('Unable to open source file "%s"\n' % (path, ))
sys.exit(1)
try:
with f:
parser.parse(f)
except IOError:
sys.stderr.write('Error reading source file "%s"\n' % (path, ))
sys.exit(1)
except Exception as e:
sys.stderr.write('Error parsing source file "%s": %s\n' % (path, e))
sys.exit(1)
return result
def scan_source_dependencies(root, sources):
def locate_include(path):
split = [ ]
forward = 0
reverse = 0
for part in path.split('/'):
if part and (part != '.'):
if part != '..':
forward += 1
split.append(part)
elif forward:
split.pop()
forward -= 1
else:
split.append(part)
reverse += 1
split = tuple(split)
for incdir, depth in roots:
if (not depth) or (not reverse):
components = incdir + split
depth = depth + forward - 1
elif depth >= reverse:
components = incdir[:-reverse] + split[reverse:]
depth = depth + forward - reverse - 1
else:
components = incdir[:-depth] + split[depth:]
depth = forward - 1
if os.path.isfile(os.path.join(root, *components)):
return components, depth
return None, 0
def test_siblings(relative, basename, depth):
pathbase = '/'.join(relative) + '/'
dirname = os.path.join(root, *relative)
for ext in ('.cpp', '.ipp', '.hxx'):
path = pathbase + basename + ext
if (path not in seen) and os.path.isfile(os.path.join(dirname, basename + ext)):
remaining.append((path, depth))
seen.add(path)
def line_hook(text):
text = text.lstrip()
if text.startswith('#'):
text = text[1:].lstrip()
if text.startswith('include'):
text = text[7:]
if text[:1].isspace():
text = text.strip()
if (len(text) > 2) and (text[0] == '"') and (text[-1] == '"'):
components, depth = locate_include(text[1:-1])
if components:
path = '/'.join(components)
if path not in seen:
remaining.append((path, depth))
seen.add(path)
base, ext = os.path.splitext(components[-1])
if ext.lower().startswith('.h'):
components = components[:-1]
test_siblings(components, base, depth)
if components[:2] == ('src', 'mame'):
for aspect in ('_a', '_v', '_m'):
test_siblings(components, base + aspect, depth)
handler = CppParser.Handler()
handler.line = line_hook
parser = CppParser(handler)
seen = set('/'.join(x for x in split_path(source) if x) for source in sources)
remaining = list([(x, 0) for x in seen])
default_roots = ((('src', 'devices'), 0), (('src', 'mame', 'shared'), 0), (('src', 'lib'), 0))
while remaining:
source, depth = remaining.pop()
components = tuple(source.split('/'))
roots = ((components[:-1], depth), ) + default_roots
try:
f = io.open(os.path.join(root, *components), 'r', encoding='utf-8')
except IOError:
sys.stderr.write('Unable to open source file "%s"\n' % (source, ))
sys.exit(1)
try:
with f:
parser.parse(f)
except IOError:
sys.stderr.write('Error reading source file "%s"\n' % (source, ))
sys.exit(1)
except Exception as e:
sys.stderr.write('Error parsing source file "%s": %s\n' % (source, e))
sys.exit(1)
return seen
def write_project(options, projectfile, mappings, sources, single):
if single:
targetsrc = ''
for source in sorted(sources):
action = mappings.get(source)
if action:
for line in action:
projectfile.write(line + '\n')
if source.startswith('src/mame/'):
targetsrc += ' MAME_DIR .. "%s",\n' % (source, )
projectfile.write(
'\n' \
'function createProjects_mame_%s(_target, _subtarget)\n' \
' project ("mame_%s")\n' \
' targetsubdir(_target .."_" .. _subtarget)\n' \
' kind (LIBTYPE)\n' \
' uuid (os.uuid("drv-mame-%s"))\n' \
' addprojectflags()\n' \
' \n' \
' includedirs {\n' \
' MAME_DIR .. "src/osd",\n' \
' MAME_DIR .. "src/emu",\n' \
' MAME_DIR .. "src/devices",\n' \
' MAME_DIR .. "src/mame/shared",\n' \
' MAME_DIR .. "src/lib",\n' \
' MAME_DIR .. "src/lib/util",\n' \
' MAME_DIR .. "src/lib/netlist",\n' \
' MAME_DIR .. "3rdparty",\n' \
' GEN_DIR .. "mame/layout",\n' \
' ext_includedir("asio"),\n' \
' ext_includedir("flac"),\n' \
' ext_includedir("glm"),\n' \
' ext_includedir("jpeg"),\n' \
' ext_includedir("rapidjson"),\n' \
' ext_includedir("zlib"),\n' \
' }\n' \
'\n' \
' files{\n%s' \
' }\n' \
'end\n' \
'\n' \
'function linkProjects_mame_%s(_target, _subtarget)\n' \
' links {\n' \
' "mame_%s",\n' \
' }\n' \
'end\n' % (options.target, options.target, options.target, targetsrc, options.target, options.target))
else:
libraries = { }
for source in sorted(sources):
components = source.split('/')
if (len(components) > 3) and (components[:2] == ['src', 'mame']):
line = ' MAME_DIR .. "%s",\n' % (source, )
liblines = libraries.get(components[2])
if liblines is not None:
liblines.append(line)
else:
libraries[components[2]] = [line]
action = mappings.get(source)
if action:
for line in action:
projectfile.write(line + '\n')
libnames = sorted(libraries.keys())
projectfile.write(
'\n' \
'function createMAMEProjects(_target, _subtarget, _name)\n' \
' project (_name)\n' \
' targetsubdir(_target .."_" .. _subtarget)\n' \
' kind (LIBTYPE)\n' \
' uuid (os.uuid("drv-" .. _target .. "_" .. _subtarget .. "-" .. _name))\n' \
' addprojectflags()\n' \
' \n' \
' includedirs {\n' \
' MAME_DIR .. "src/osd",\n' \
' MAME_DIR .. "src/emu",\n' \
' MAME_DIR .. "src/devices",\n' \
' MAME_DIR .. "src/mame/shared",\n' \
' MAME_DIR .. "src/lib",\n' \
' MAME_DIR .. "src/lib/util",\n' \
' MAME_DIR .. "src/lib/netlist",\n' \
' MAME_DIR .. "3rdparty",\n' \
' GEN_DIR .. "mame/layout",\n' \
' ext_includedir("asio"),\n' \
' ext_includedir("flac"),\n' \
' ext_includedir("glm"),\n' \
' ext_includedir("jpeg"),\n' \
' ext_includedir("rapidjson"),\n' \
' ext_includedir("zlib"),\n' \
' }\n' \
'end\n' \
'\n' \
'function linkProjects_mame_%s(_target, _subtarget)\n' \
' links {\n' % (options.target, ))
for lib in libnames:
if lib != 'shared':
projectfile.write(' "%s",\n' % (lib, ))
if 'shared' in libraries:
projectfile.write(' "shared",\n')
projectfile.write(
' }\n' \
'end\n' \
'\n' \
'function createProjects_mame_%s(_target, _subtarget)\n' \
'\n' % (options.target, ))
for lib in libnames:
projectfile.write(
'createMAMEProjects(_target, _subtarget, "%s")\n' \
'files {\n' % (lib, ))
for line in libraries[lib]:
projectfile.write(line)
projectfile.write('}\n\n')
projectfile.write('end\n')
def collect_sources(root, sources):
result = [ ]
for source in sources:
fullpath = os.path.join(root, source)
if os.path.isdir(fullpath):
for subdir, dirs, files in os.walk(fullpath):
for candidate in files:
if os.path.splitext(candidate)[1] == '.cpp':
if subdir != fullpath:
result.append(os.path.join(source, os.path.relpath(subdir, fullpath), candidate))
else:
result.append(os.path.join(source, candidate))
else:
result.append(source)
return result
def write_sources_project(options, projectfile):
def sourcefile(filename):
if tuple(filename.split('/')) in splitsources:
state['havedrivers'] = True
def driver(shortname):
pass
header_to_optional = collect_lua_directives(options)
sources = collect_sources(options.root, options.sources)
splitsources = frozenset(s[2:] for s in (path_components(s) for s in sources) if s[:2] == ('src', 'mame'))
state = { 'havedrivers': False }
DriverFilter().parse_list(options.list, sourcefile, driver)
if not state['havedrivers']:
sys.stderr.write('None of the specified source files contain system drivers\n')
sys.exit(1)
source_dependencies = scan_source_dependencies(options.root, sources)
write_project(options, projectfile, header_to_optional, source_dependencies, True)
def write_filter_project(options, projectfile):
header_to_optional = collect_lua_directives(options)
sources = DriverCollector(options).sources
source_dependencies = scan_source_dependencies(options.root, (os.path.join('src', 'mame', *n.split('/')) for n in sources))
write_project(options, projectfile, header_to_optional, source_dependencies, False)
def write_sources_filter(options, filterfile):
sources = set()
DriverFilter().parse_list(options.list, lambda n: sources.add(n), lambda n: None)
drivers = set()
for source in collect_sources(options.root, options.sources):
components = tuple(x for x in split_path(source) if x)
if (len(components) > 3) and (components[:2] == ('src', 'mame')):
ext = os.path.splitext(components[-1])[1].lower()
if ext.startswith('.c'):
if '/'.join(components[2:]) in sources:
drivers.add('/'.join(components[2:]))
for driver in sorted(drivers):
filterfile.write(driver + '\n')
if __name__ == '__main__':
options = parse_command_line()
if options.command == 'sourcesproject':
write_sources_project(options, sys.stdout)
elif options.command == 'filterproject':
write_filter_project(options, sys.stdout)
elif options.command == 'sourcesfilter':
write_sources_filter(options, sys.stdout)
elif options.command == 'driverlist':
DriverLister(options).write_source(sys.stdout)
elif options.command == 'reconcilelist':
reconciler = DriverReconciler(options)
if options.infoxml == '-':
reconciler.reconcile_xml(sys.stdin)
elif options.infoxml is not None:
try:
xmlfile = io.open(options.infoxml, 'rb')
with xmlfile:
reconciler.reconcile_xml(xmlfile)
except IOError:
sys.stderr.write('Unable to open system information file "%s"\n' % (options.infoxml, ))
sys.exit(1)
if reconciler.bad:
sys.exit(1)
|