Mercurial > hg > CbC > old > CbC_scripts
view meta_connector/meta_connector_name.py @ 8:ae45108716a2
fix usage
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 14 Sep 2015 13:13:17 +0900 |
parents | 4b015ace1eb4 |
children | 185d545a9a8e |
line wrap: on
line source
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys import argparse import re # parse arguments and return arguments list. def get_args(): parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, description="""\ Parse meta connect syntax. Default output is stdout. sample) __code code0(struct Data data1, ...){ goto code1(data1, data2, ...); \t| \tV __code code0(struct Context* context, struct Data data1, ...){ goto meta_code1(context, data1, data2, ..., Code1); """) parser.add_argument('input_file',\ nargs=None,\ type=str,\ help='input file path.') parser.add_argument('-o', dest='output',\ nargs=1,\ type=str,\ metavar='<file>',\ help='write output to <file>') return parser.parse_args() # parse input file and create meta connection list def parse_meta_syntax(lines,file): comment_out = False target_cs = False caller_name = '' isMetaOrStub = False for i,l in enumerate(lines): regexed_l = re.search(r"[a-zA-Z0-9_]+ *\(",l) # get caller code segment name if re.search(r"^ *__code",l) is not None: caller_name = regexed_l.group(0).rstrip('(') if re.search(r"^ *meta_*|stub$",caller_name) is not None: isMetaOrStub = True else: isMetaOrStub = False file.write(l) elif not isMetaOrStub and regexed_l is not None and re.search(r"^ *goto",l): callee_name = regexed_l.group(0).rstrip('(') if meta_search(lines, callee_name): file.write("/*-- connected by script */\n") file.write('// '+l) file.write("goto {0:s}(context, {1:s}, {2:s});\n".format('meta_'+caller_name,\ l.split('(')[1].rsplit(')')[0],\ callee_name.capitalize())) else: file.write(l) else: file.write(l) # search meta code segment. # Find it : __code meta_'name' def meta_search(lines, name): for l in lines: if re.search(r"^ *__code +meta_{0:s}\(".format(name),l) is not None: return True return False def main(): args = get_args() output = sys.stdout try: f = open(args.input_file,'r') except IOError: print("cannot open file %s" % input_file) if args.output is not None: try: output = open(args.output[0],'w') except IOError: print("cannot open file %s" % args.output) lines = f.readlines() connect_list = parse_meta_syntax(lines, output) main()