-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocparse.py
873 lines (710 loc) · 25.8 KB
/
ocparse.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
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
"""Module for parsing and analyzing machine code instructions
The module provides a class **Parser** for a machine code parsers and a
corresponding class **Opcode** for opcode-patterns that defines bitpatterns
and/or bitfields of which the parser can extract the values.
It also has an analyzer class **Analyzer** with its own opocde class
**AnalyzerOpcode** that can be used for analysis of
instruction sets and their decoding.
The module is written with fixed-width instructions in mind.
"""
from __future__ import annotations
import math
def strip_sep(s: str, seps=(' ', '_', '|')) -> str:
"""remove separator characters from string
"""
return ''.join(c for c in s if c not in seps)
def nibble_sep(s: str, sep='_', seps=(' ', '_', '|')) -> str:
""" remove all separator characters and insert new separator for
each nibble
"""
s = strip_sep(s, seps=seps)
sn = ''
n = 0
m = 0
ls = len(s)
for c in s[::-1]:
sn = c + sn
n += 1
m += 1
if n == 4 and m != ls:
n = 0
sn = sep + sn
return sn
def unzip_sep(s: str, seps=(' ', '_', '|')) -> tuple[str, list[str, ...]]:
"""split into string without separators and a list of separators
Returns tuple of stripped string and list of separators.
There is one separator per character of the stripped string and
one at the end. A separator string in the list is '' if
there is no separator.
"""
sbare = ''
sepl = []
cs = ''
for c in s[::-1]:
if c in seps:
cs += c
else:
sbare += c
sepl.append(cs[::-1])
cs = ''
sepl.append(cs)
return (sbare[::-1], sepl)
def zip_sep(s: str, sepl: list[str, ...]) -> str:
"""combine string and list of separators into string with separators
"""
s = s[::-1]
sout = ''
for n in range(len(s)):
sout += sepl[n] + s[n]
sout += sepl[-1]
return sout
class Opcode():
"""Opcode pattern for use by the opcode parser
:param name: Name of opcode
:param pattern: Pattern of opcode.
'0' means bit must be zero, '1' means bit must be one,
and '*' means bit can take any value. If the field is a letter,
e.g. 'A', the bit belongs to parameter A and can take any value.
' ', '|' and '_' are separators and are stripped away before pattern
is used.
:param param_filter: Logical function with dictionary as parameter.
Dictionary holds values opcode fields.
Returns True if values are valid, otherwise False.
"""
def __init__(self, name: str, pattern_str: str,
param_filter=lambda x: True):
"""Constructor method
"""
self.name = name
pattern_str = strip_sep(pattern_str)
self.pattern_str = pattern_str
self._len = len(pattern_str)
self.pattern = 0
self.mask = 0
self.params = {}
self.param_filter = param_filter
cp = None
cmask = 0
rshift = 0
d = 1
n = 0
for c in pattern_str[::-1]:
if c == '1':
self.pattern += d
self.mask += d
elif c == '0':
self.mask += d
if c in '10*' and cp is not None:
if cp in self.params:
raise Exception(
"parameter \"{}\" already defined".format(c))
self.params[cp] = (cmask, rshift)
cp = None
elif c not in '10*' and cp is not None and c != cp:
if cp in self.params:
raise Exception(
"parameter \"{}\" already defined".format(cp))
self.params[cp] = (cmask, rshift)
cp = c
rshift = n
cmask = d
elif c not in '10*' and cp is None:
cp = c
rshift = n
cmask = d
elif c == cp:
cmask += d
n += 1
d *= 2
if cp is not None:
if cp in self.params:
raise Exception(
"parameter \"{}\" already defined".format(c))
self.params[cp] = (cmask, rshift)
def __repr__(self):
fmt = "{{:0{}b}}\n".format(self._len)
str = self.name + '\n'
str += fmt.format(self.pattern)
str += fmt.format(self.mask)
for k in self.params:
str += k + '\n'
str += fmt.format(self.params[k][0])
return str
def __len__(self):
return self._len
"""Return number of bits
"""
def rename_field(self, *pairs):
"""Rename parameter field(s)
rename_field(self, kold1, knew1, kold2, knew2, ...)
kold1 old field name (default is character used in pattern)
knew1 new field name, string without whitespace
"""
for p in zip(pairs[0::2], pairs[1::2]):
self.params[p[1]] = self.params[p[0]]
self.params.pop(p[0])
def decode(self, code: int) -> None | dict:
""" Return dictionary of parameters if code matches opcode
"""
if code >= 2**self._len:
return None
if 0 == (code ^ self.pattern) & self.mask:
d = {}
d['name'] = self.name
for k in self.params:
d[k] = (code & self.params[k][0]) >> self.params[k][1]
if self.param_filter(d):
return d
else:
return None
def intersect(self, oc: Opcode) -> int:
"""Check if opcode patterns can overlap
"""
if 0 == (oc.pattern ^ self.pattern) & self.mask & oc.mask:
return 1
else:
return 0
class Parser():
"""Opcode parser
"""
def __init__(self):
"""Constructor method
"""
self.opcodes = []
def __repr__(self):
str = ''
for o in self.opcodes:
ocp = o[0]
str += "{}, {}: ".format(ocp.name, o[1])
str += ', '.join(s for s in ocp.params)
str += '\n'
return str
def add(self, opc: Opcode):
"""Add opcode to parser
If name of opc is unique, the opcode is added to the parser.
If the name is already in use, an exception is raised.
"""
names = [o[0].name for o in self.opcodes]
if opc.name in names:
raise Exception("opcode name already exists")
else:
self.opcodes.append([opc, 0])
def set_priority(self, name: str, pri: int):
"""Set priority of named opcode
"""
for le in self.opcodes:
if le[0].name == name:
le[1] = pri
break
def get_priority(self, name: str) -> int | None:
""" Return priority of named opcode
"""
for o in self.opcodes:
if o[0].name == name:
return o[1]
return None
def parse(self, code: int) -> list[dict, ...]:
"""Parse opcode
Returns list of dictionaries each of which describes the
an interpretation of the opcode
"""
ocd = []
pri = max(o[1] for o in self.opcodes)+1
for o in self.opcodes:
if (len(ocd) > 0 and o[1] <= pri) or len(ocd) == 0:
d = o[0].decode(code)
if d is not None and o[1] < pri:
ocd = [d]
pri = o[1]
elif d is not None:
ocd.append(d)
return ocd
def ambiguity_matrix(self) -> list[list[int, ...], ...]:
"""Return list of lists whose [i][j]-element is nonzero if
opcode i and j can not be distinguished
"""
return [[o1[0].intersect(o2[0]) for o2 in self.opcodes]
for o1 in self.opcodes]
def ambiguity_list(self) -> list[tuple[str, str], ...]:
"""Return list of pairs of names of ambigous opcodes
"""
n = len(self.opcodes)
o = self.opcodes
return [(o[i1][0].name, o[i2][0].name) for i1 in range(n)
for i2 in range(i1+1, n) if o[i1][0].intersect(o[i2][0])]
def __str__(self):
s = ''
for o in self.opcodes:
s += nibble_sep(o[0].pattern_str) + ' ' + o[0].name + '\n'
return s
class AnalyzerOpcode():
"""Opcode pattern for Analyzer
:param opcode_pattern: String of bitpatterns with separator characters
:param description: Description of opcode, e.g. name or mnemonic
"""
def __init__(self, opcode_pattern: str, description: str):
"""Constructor method
"""
p, self.seps = unzip_sep(opcode_pattern)
# store lsb first
self._pattern = p[::-1]
self.desc = description.strip()
def __len__(self):
return len(self._pattern)
def copy(self) -> 'AnalyzerOpcode':
"""Make a copy of opcode
"""
oc = AnalyzerOpcode(self.pattern(), self.desc)
oc.seps = self.seps.copy()
return oc
def pattern(self) -> str:
"""Make string of bitpattern without separators
"""
return self._pattern[::-1]
def lstr(self) -> str:
"""Make string of bitpattern with separators
"""
nb = len(self._pattern)
ncs = ''
for ii in range(nb):
ncs += self.seps[ii][::-1]
ncs += self._pattern[ii]
ncs += self.seps[nb][::-1]
return ncs[::-1]
def lstrlen(self) -> int:
"""Length of bitpattern with separators
"""
return len(self._pattern) + sum([len(s) for s in self.seps])
def setsep(self, sepind: list[int, ...], sep='|'):
"""Set separator at specified bit positions
"""
for jj in sepind:
self.seps[jj] = sep
def delsep(self, seps: list[int, ...]):
"""Remove separators at specified bit positions
"""
rmseps = [jj for jj in seps if jj < len(self.seps)]
for jj in rmseps:
self.seps[jj] = ''
def rmbits(self, bl: list[int, ...]) -> 'AnalyzerOpcode':
"""Make opcode with specified bits removed
"""
nbits = len(self._pattern)
sn = ''.join(self._pattern[ii] for ii in range(nbits) if ii not in bl)
newseps = []
rem = ''
for ii in range(nbits):
if ii in bl:
rem += self.seps[ii]
else:
newseps.append(self.seps[ii] + rem)
rem = ''
newseps.append(self.seps[nbits]+rem)
oc = AnalyzerOpcode(sn[::-1], self.desc)
oc.seps = newseps
return oc
def combine(self, oc: 'AnalyzerOpcode') -> None | 'AnalyzerOpcode':
"""Combine opcode patterns if possible
:param oc: other Analyzeropcode
:returns: None if combinations is not possible.
AnalyzerOpcode with combined pattern
if the the two patterns differ in only one bit.
"""
if len(self) != len(oc):
return None
s1 = ''.join([c if c in '01' else '*' for c in self.pattern()])
s2 = ''.join([c if c in '01' else '*' for c in oc.pattern()])
count = 0
s = ''
for t in zip(s1, s2):
if t[0] == t[1]:
s += t[0]
else:
s += '*'
count += 1
if count == 1:
return AnalyzerOpcode(s, self.desc + ' ' + oc.desc)
else:
return None
def intersect(self, oc: 'AnalyzerOpcode') -> bool:
"""Check if opcodes can describe same bitpattern
"""
p1 = self._pattern
p2 = oc.pattern()[::-1]
l1 = len(p1)
l2 = len(p2)
d = l1 - l2
if d > 0:
p1 = p1[:l2]
elif d < 0:
p2 = p2[:l1]
x = False
for ii in range(len(p1)):
c1 = p1[ii]
c2 = p2[ii]
m = (c1 in {'0', '1'}) and (c2 in {'0', '1'})
x = x or (m and c1 != c2)
return not x
def replace_field(self, field: str, val: str):
"""Replace a field by a string
:param field: field character or string
:param val: string to replace field
"""
self._pattern = self._pattern.replace(field, val)
def expand_field(self, field: str, excpt: list[str, ...] = [],
tag=None) -> list['AnalyzerOpcode', ...]:
"""Make list of opcodes with given field expanded
Expands to all possible bitpatterns except those given
in list of exceptions.
:param field: The opcode field to be expanded
:param excpt: Bitpatterns to be ignored
:param tag: Function tag(desc0, field, p) that returns new description
field. It takes as the first parameter a string desc0 which is a
description to be modified/substituted, the single character string
field holding the name of the bitfield and the string p holding
the actual pattern of the field.
"""
if field in ['1', '0', '|', '_', '*', ' ']:
return [self]
iis = [ii for ii in range(len(self._pattern))
if field == self._pattern[ii]]
iis.reverse()
if iis:
nb = len(iis)
fmt = '{{:0{}b}}'.format(nb)
patt = [fmt.format(ii) for ii in range(2**nb)]
patt = [p for p in patt if p not in excpt]
new = []
for p in patt:
s = self._pattern
ls = len(s)
for jj in range(len(iis)):
ii = iis[jj]
s = s[0:ii] + p[jj] + s[ii+1:ls]
desc = tag(self.desc, field, p)
oc = AnalyzerOpcode(s[::-1], desc)
oc.seps = self.seps
new.append(oc)
return new
else:
return [self]
class Analyzer():
"""Class for analyzing instruction set
:param opcode_patterns: Instruction set as list of AnalyzerOpcode objects
"""
def __init__(self, opcode_patterns: list[AnalyzerOpcode, ...]):
"""Constructor method
"""
self.codes = [[AnalyzerOpcode(*p) for p in opcode_patterns]]
self.cp = 0
def __repr__(self):
s = 'Analyzer(['
codes = self.codes[self.cp]
if len(codes) == 0:
return 'Analyzer([])'
for c in codes:
s += '(\'{}\', \'{}\'), '.format(c.pattern(), c.desc)
s = s[:-2] + '])'
return s
def __str__(self):
codes = self.codes[self.cp]
s = ''
if len(codes) == 0:
return s
for c in codes:
s += '{} {}\n'.format(c.pattern(), c.desc)
s = s[:-1]
return s
def __len__(self):
"""Number of opcode patterns defined
"""
return len(self.codes[self.cp])
def __add__(self, other):
a = self.copy_current()
b = other.copy_current()
a.codes[0] += b.codes[0]
return a
def copy_current(self, codenos: list[int, ...] = None) -> 'Analyzer':
"""Make a deep copy of current analyzer state
:param codenos: list of opcode pattern numbers
"""
an = Analyzer([])
if codenos:
codes = [self.codes[self.cp][ii] for ii in codenos]
else:
codes = self.codes[self.cp]
an.codes = [[c.copy() for c in codes]]
return an
def ls(self):
"""List opcode patterns
"""
codes = self.codes[self.cp]
nc = len(codes)
if not nc:
print('-- no opcodes defined --')
return
w = math.ceil(math.log(nc)/math.log(10.0))
cl = max([c.lstrlen() for c in codes])
fmt = "{{:>{}}} {{:>{}}} {{}}".format(w, cl)
for n in range(nc):
code = codes[n]
if len(code):
s = fmt.format(n, code.lstr(), code.desc)
else:
s = fmt.format(n, code.seps[0]+code.desc, '')
print(s)
def newsep(self, seps: list[int, ...], ins: None | list[int, ...] = None,
sep: str = '|'):
"""Introduce separators in listing of opcodes
Only affects current opcode set
:param seps: position of separators (0-#bits)
:param ins: index to opcodes which are affected
:param sep: separator character
"""
codes = self.codes[self.cp]
if ins is None:
ins = list(range(len(codes)))
for ii in ins:
codes[ii].setsep(seps, sep)
def delsep(self, seps: list[int, ...], ins: None | list[int, ...] = None):
"""Delete separators
Only affects current opcode set
:param seps: position of separators (0-#bits)
:param ins: index to opcodes which are affected
"""
codes = self.codes[self.cp]
if ins is None:
ins = list(range(len(codes)))
for ii in ins:
codes[ii].delsep(seps)
def rmbits(self, bl: list[int, ...]):
"""Remove bits from opcodes
:param bl: list of bits to remove
"""
codes = self.codes[self.cp]
self.cp += 1
self.codes = self.codes[:self.cp]
self.codes.append([c.rmbits(bl) for c in codes])
def lsambig(self):
"""List ambiguities between opcode patterns
Checks all pairs of opcode patterns to see if there
exists a bit pattern that matches both opcode patterns.
Lists all such ambiguities that are found.
"""
amb = self.ambiguities()
for a in amb:
print("{}: {}, {}: {}".format(a[0][0], a[0][1], a[1][0], a[1][1]))
print('Number of ambiguities: {}\n'.format(len(amb)))
def ambiguities(self):
"""Checks opcode patterns for possible ambiguities
Checks all pairs of opcode patterns to see if there
exists a bit pattern that matches both opcode patterns.
:returns: list of tuples of mutually ambiguous opcode patterns
each specified as a tuple of opcode index and description
"""
codes = [c for c in self.codes[self.cp] if len(c)]
nc = len(codes)
count = 0
amb = []
for ii in range(nc):
c1 = codes[ii]
for jj in range(ii+1, nc):
if c1.intersect(codes[jj]):
amb.append(((ii, c1.desc), (jj, codes[jj].desc)))
return amb
def bitworth(self) -> list[int, ...]:
"""Calculate each bit's worth
The worth is calculated as the increase in the number of ambiguities
in the instruction set if the bit is removed.
:returns: List of each bit's worth
"""
nbits = max(len(c) for c in self.codes[self.cp])
worth = []
an = self.copy_current()
a0 = len(an.ambiguities())
for n in range(nbits):
an.rmbits([n])
worth.append(len(an.ambiguities())-a0)
an.undo()
return worth
def undo(self):
"""Sets the previous opcode-pattern set to current set
"""
if self.cp:
self.cp -= 1
else:
print("nothing to undo!")
def redo(self):
"""Set the next opcode-pattern set, if any, to current set
"""
if self.cp == len(self.codes) - 1:
print("nothing to redo!")
else:
self.cp += 1
def when(self):
"""Print where current opcode-pattern set is in history
"""
print("At {} in history of 0-{}".format(self.cp, len(self.codes)-1))
def rmfuture(self):
"""Delete opcode-pattern sets newer than the current
"""
self.codes = self.codes[:self.cp + 1]
def rmpast(self):
"""Delete opcode-pattern sets older than the current
"""
self.codes = self.codes[self.cp:]
self.cp = 0
def replace_field(self, field: str, val: str, cno: list[int, ...] = None):
"""Replace a field by a string
:param field: field character or string
:param val: string to replace field
"""
codes = [c.copy() for c in self.codes[self.cp]]
nc = len(codes)
if cno is None:
cno = list(range(nc))
for ii in cno:
codes[ii].replace_field(field, val)
self.cp += 1
self.codes = self.codes[:self.cp]
self.codes.append(codes)
def expand_field(self, field: str, cno: None | list[int, ...] = None,
excpt: list[int, ...] = [],
tag=lambda d, f, v: d + '_' + f + v):
"""Expand an opcode field
Replaces opocode pattern by opcode patterns where a named field
has bin substituted for all its possible bit patterns except
those given list of exceptions. The operation can be carried out on
all opcode patters or on patterns given in a list.
:param field: Character naming the field to expand to binary.
:param cno: List of numbers of the codes this should be done for.
An argument of None results in all codes containing
the field being expanded.
:param excpt: List of values that should not be included.
:param tag: Function tag(desc0, field, p) to generate new
description string. The first parameter is a string
desc0 with the description to be modified/substituted.
The second, field, is a single character string holding
the name of the bitfield. The third string parameter p
holds the actual pattern of the field.
"""
if field in ['1', '0', '|', '_', '*', ' ']:
return
codes = self.codes[self.cp]
nc = len(codes)
if cno is None:
cno = list(range(nc))
expanded = []
for ii in range(nc):
if ii in cno:
expanded += codes[ii].expand_field(field, excpt, tag)
else:
expanded.append(codes[ii])
self.cp += 1
self.codes = self.codes[:self.cp]
self.codes.append(expanded)
def delcodes(self, cl: list[int, ...]):
"""Delete opocde patterns
Takes a list of opcode-pattern number and deletes patterns with the
given number.
"""
cl.sort(reverse=True)
nc = self.codes[self.cp]
for ii in cl:
nc.pop(ii)
self.cp += 1
self.codes = self.codes[:self.cp]
self.codes.append(nc)
def combinecodes(self):
"""Combine as many code patterns as possible
"""
self.dup()
codes = self.get_codes()
combined = 1
while combined:
combined = 0
nc = len(codes)
pp = [(ii, jj) for ii in range(nc) for jj in range(ii+1, nc)]
deletes = []
while len(pp):
p = pp.pop()
c1 = codes[p[0]]
c2 = codes[p[1]]
c = c1.combine(c2)
if c is not None:
pp = [p0 for p0 in pp if not (p0[0] in p or p0[1] in p)]
codes[p[0]] = c
deletes.append(p[1])
combined += 1
deletes.sort(reverse=True)
for ii in deletes:
del codes[ii]
def newcode(self, pattern: str, description: str, pos: None | int = None):
"""Add a new opcode pattern to the analyzer
Add the pattern AnalyzerOpcode(pattern, description) in
opcode position pos. If pos is not given, the code is added
to the end.
"""
nc = self.codes[self.cp]
if pos is None:
pos = len(nc)
nc.insert(pos, AnalyzerOpcode(pattern, description))
self.cp += 1
self.codes = self.codes[:self.cp]
self.codes.append(nc)
def title(self, pos, text):
"""Add a title
Equivalent to adding a new, empty opcode pattern with the title
text as description.
"""
self.newcode('', text, pos)
def rmtitles(self):
codes = self.get_codes()
cno = [jj for jj in range(len(codes)) if not codes[jj].pattern()]
self.delcodes(cno)
def get_codes(self) -> list[AnalyzerOpcode, ...]:
"""Return list of opcodes
"""
return self.codes[self.cp]
def merge(self, other: 'Analyzer', pos: int = None):
"""Merge in another analyzer
:param other: The analyzer to be merged in.
:param pos: The position at which to merge in (optional)
"""
oc = self.codes[self.cp]
if pos is None:
pos = len(oc)
self.cp += 1
self.codes = self.codes[:self.cp]
self.codes.append(oc[:pos] + other.get_codes() + oc[pos:])
def dup(self) -> list[AnalyzerOpcode, ...]:
"""Duplicate current codes
:returns: The duplicate
"""
codes = [c.copy() for c in self.codes[self.cp]]
self.cp += 1
self.codes = self.codes[:self.cp]
self.codes.append(codes)
return codes
def move(self, src: int, to: int):
"""Move code within list
:param src: Position of code pattern in list
:param to: New position of code pattern
"""
if src < 0 or src >= len(self):
return
codes = self.dup()
c = codes.pop(src)
codes.insert(max(0, to), c)
def save(self, name, filename):
"""Save analyzer as pyton code
:param name: Variable name or analyzer
:param filename: File to write to
"""
with open(filename, "w") as file:
codes = self.codes[self.cp]
file.write("import ocparse\n\n")
file.write("{} = ocparse.Analyzer([\n".format(name))
file.write(",\n".join([" (\'{}\', \'{}\')".format(c.lstr(),
c.desc) for c in codes]))
file.write("\n])")