|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import re |
| 4 | +import sys |
| 5 | +from typing import Generator |
| 6 | + |
| 7 | +BINDING_DIR = 'native/bindings/java/capstone' |
| 8 | +ENTRYPOINT_FILE = f'{BINDING_DIR}/Capstone.java' |
| 9 | + |
| 10 | +def get_const(start_markup, end_markup) -> Generator[None, str, list]: |
| 11 | + re_const = re.compile(r'(\w+\s+= .+);') |
| 12 | + re_space = re.compile(r'\s+') |
| 13 | + |
| 14 | + while True: |
| 15 | + line = yield |
| 16 | + |
| 17 | + if start_markup in line: |
| 18 | + break |
| 19 | + |
| 20 | + result = [] |
| 21 | + while True: |
| 22 | + line = yield |
| 23 | + |
| 24 | + if not line: |
| 25 | + result.append(None) |
| 26 | + |
| 27 | + if end_markup in line: |
| 28 | + break |
| 29 | + |
| 30 | + if not line.startswith('public'): |
| 31 | + continue |
| 32 | + |
| 33 | + match = re_const.search(line) |
| 34 | + if not match: |
| 35 | + print("Warning: No const found for '{}'".format(line), file=sys.stderr) |
| 36 | + continue |
| 37 | + |
| 38 | + result.append(re_space.sub(' ', match.group(1))) |
| 39 | + |
| 40 | + return result |
| 41 | + |
| 42 | +def process_file(file_path, visitor): |
| 43 | + with open(file_path) as f: |
| 44 | + next(visitor) |
| 45 | + |
| 46 | + for line in f: |
| 47 | + try: |
| 48 | + visitor.send(line.strip()) |
| 49 | + except StopIteration as e: |
| 50 | + return e.value |
| 51 | + |
| 52 | +def handle_get_const(): |
| 53 | + def _c(line): |
| 54 | + if not line: |
| 55 | + return '' |
| 56 | + |
| 57 | + return f'export const {line}' |
| 58 | + |
| 59 | + result = process_file(ENTRYPOINT_FILE, get_const('Capstone API version', 'NativeStruct')) |
| 60 | + result = [ |
| 61 | + '// AUTO-GENERATED FILE, DO NOT EDIT', |
| 62 | + '/* eslint-disable */', |
| 63 | + '', |
| 64 | + *map(_c, result), |
| 65 | + ] |
| 66 | + |
| 67 | + return result |
| 68 | + |
| 69 | +def main(): |
| 70 | + if len(sys.argv) == 1: |
| 71 | + print('Missing action') |
| 72 | + exit(1) |
| 73 | + |
| 74 | + action = sys.argv[1] |
| 75 | + |
| 76 | + if action == "const": |
| 77 | + result = handle_get_const() |
| 78 | + else: |
| 79 | + print('Invalid action') |
| 80 | + exit(1) |
| 81 | + |
| 82 | + print(*result, sep='\n') |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + main() |
0 commit comments