正则式代码转换程序,用于cocos2d-x不同版本之间程序移植。

前端之家收集整理的这篇文章主要介绍了正则式代码转换程序,用于cocos2d-x不同版本之间程序移植。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如果开发环境没有反射功能代码改名的工作量不小,下面代码实现了批量替换变量名称修改可用于代码转换的许多情况。

本文中用于cocos2d-x不同版本之间程序移植。

代码主要功能:将注释和字符串排除在外,其他内容匹配变量名,用字典进行替换。点击打开链接

#!/usr/bin/env python
import os
import re
import sys
import codecs
#############################
dic={}
jiacc=["spriteWithFile","actionWithDuration","menuWithItem","itemWithLabel","transitionWithDuration","spriteWithFile","labelWithString","node"]#,"Object","Sprite","Point","Scene","Node","MoveTo","Director","Application","Size","Touch","Event"]
dic["CCMutableArray"]="CCArray"
dic["CGFloat"]="float"
dic["ccTime"]="float"
for one in jiacc:
    dic[one]="create"
##############################
pattern = re.compile(r"""   (//[^\r\n]*) # match a single line comment
                            | (/\*.*?\*/)      # match a multi line comment
                            | ("[^"]*")        # match a string literal
                            | ([a-zA-Z_][a-zA-Z_0-9]*) #identifier
                        """,re.X | re.S)
def translateValue(old):
    new=dic.get(old)
    if new!=None:
        return new
    return old
def func(match):
    if match.group(1) or match.group(2) or match.group(3):
        return match.group()
    if match.group(4):
        return  translateValue(match.group())#
        #raw_input()
    else:
      return match.group()
def translateStr(source):
  return re.sub(pattern,func,source)
def translateFile(inputFileName):
  s=codecs.open(inputFileName,"r","utf-8").read()
  return translateStr(s)
def treatfile(inputFileName):
  print inputFileName
  #bak
  cmd="cp %s %s" %(inputFileName,inputFileName+".bak")
  os.system(cmd)
  #translate
  fc=translateFile(inputFileName)
  #output
  f=codecs.open(inputFileName,"w","utf-8")
  f.write(fc)
  f.close()
#dir##################
def mylistdir(p,f):
    a=os.listdir(p)
    fs=myfind(a,f)
    return(fs)
def myfind(l,p):
    lr=[];
    #print p
    p1=p.replace(".",r"\.")
    p2=p1.replace("*",".*")
    p2=p2+"$"
    for a in l:
        #print a
        if  re.search(p2,a,re.IGNORECASE)==None :
           pass
           #print "pass"
        else:
           lr.append(a)
       #print "append"
    return lr
def translateDir(path):#
    files=mylistdir(path,"*.cpp")
    files2=mylistdir(path,"*.h")
    for f in files2:
        files.append(f)
    for f in  files:
        fn=path+"/"+f
        treatfile(fn)
if __name__=="__main__":
    translateDir(sys.argv[1])
原文链接:/regex/362037.html

猜你在找的正则表达式相关文章