Beispiel #1
0
 def parse():
     nonlocal ptr
     tag = data[ptr]
     ptr += 1
     if tag == 0:
         value, offset = parse_int(data[ptr:])
         ptr += offset
         return {'int': str(value)}
     elif tag == 1:
         value, offset = parse_array(data[ptr:])
         ptr += offset
         return {'string': value.decode()}
     elif tag == 2:
         return parse_list()
     elif tag == 3:
         return parse_prim_expr(args_len=0, annots=False)
     elif tag == 4:
         return parse_prim_expr(args_len=0, annots=True)
     elif tag == 5:
         return parse_prim_expr(args_len=1, annots=False)
     elif tag == 6:
         return parse_prim_expr(args_len=1, annots=True)
     elif tag == 7:
         return parse_prim_expr(args_len=2, annots=False)
     elif tag == 8:
         return parse_prim_expr(args_len=2, annots=True)
     elif tag == 9:
         return parse_prim_expr(args_len=3, annots=True)
     elif tag == 10:
         value, offset = parse_array(data[ptr:])
         ptr += offset
         return {'bytes': value.hex()}
     else:
         assert False, f'unkonwn tag {tag} at position {ptr}'
Beispiel #2
0
 def parse_list():
     nonlocal ptr
     _, offset = parse_array(data[ptr:])
     end, res = ptr + offset, []
     ptr += 4
     while ptr < end:
         res.append(parse())
     assert ptr == end, f'out of array boundaries'
     return res
Beispiel #3
0
    def parse_prim_expr(args_len=0, annots=False):
        nonlocal ptr
        prim_tag = data[ptr]
        ptr += 1
        expr = {'prim': prim_int[prim_tag]}

        if 0 < args_len < 3:
            expr['args'] = [parse() for _ in range(args_len)]
        elif args_len == 3:
            expr['args'] = parse_list()
        else:
            assert args_len == 0, f'unexpected args len {args_len}'

        if annots:
            value, offset = parse_array(data[ptr:])
            ptr += offset
            if len(value) > 0:
                expr['annots'] = value.decode().split(' ')

        return expr