view meta_connector/meta_connector.py @ 1:4a172166e6c3

add meta connector script
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Wed, 29 Jul 2015 00:13:12 +0900
parents
children 81da5b2c3382
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()
    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):
    connect_set = []
    for i,l in enumerate(lines):
        if l.find("meta_connect") >= 0:
            offset = 0
            connect_l = lines[i+offset].split('(')[1].strip()

            while connect_l.find(')') < 0:
                if (connect_l.find(':') >= 0):
                    connect_set.append(re.split(':|,',connect_l))
                offset = offset + 1
                connect_l = lines[i+offset].strip()

            if connect_l.find(':') >= 0:
                connect_set.append(connect_l.split(')')[0].split(':'))
    return connect_set
            
def connect_meta(lines, list, file):
    comment_out = False
    target_cs = False
    meta_index = 0
    for l in lines:
        if l.find("meta_connect") >= 0:
            comment_out = True
            print('/* -- meta connector --')
        
        for meta_index, meta in enumerate(list):
            if re.search(r" *__code +{0:s} *\(".format(meta[1].strip()),l) is not None:
                target_cs = True
                break

        if target_cs:
            # skip commnet outed line.
            if re.search(r" *//+ *goto",l) is not None:
                pass
            # connect to meta code segment
            elif re.search(r" *goto +\w+\(",l) is not None:
                print("/*-- connected by script */")
                print("// "+l, end='')
                ll = re.split(r"\(|\)", l)
                print("goto {0:s}(context, {1:s}, {2:s});".format(list[meta_index][0],\
                                                                  ll[1],\
                                                                  re.split(" +",ll[0].strip())[1].capitalize()))
                print("/* connected by script --*/")
                continue
            if l.find('}') >= 0:
                target_cs = False

        print(l, end='')

        if comment_out and l.find(')') >= 0:
            comment_out = False
            print('-- meta connector --*/')

def main():
    args = get_args()
    try:
        f = open(args.input_file,'r')
    except IOError:
        print("cannot read file %s" % input_file)
    lines = f.readlines()
    connect_list = parse_meta_syntax(lines)
    connect_meta(lines, connect_list, args.output)
    
main()