zoukankan      html  css  js  c++  java
  • python写的调试器

    生成调试文件的源代码
    #
    !/usr/bin/python
    #
    _*_coding:utf8_*_
    import os
    import sys
    import re
    """行匹配(必须是对应的符号),例{},(),[]等,head表示前半部分,tail表示后半部分"""
    def match(head, tail,fileName):
    try:
    f = open(fileName)
    lineSum = 0
    line = f.readline()
    while line:
    summary = 0
    lineSum += 1
    s = line
    biaoji=0
    i = 0
    sign = 0
    note = 0
    while i < len(s):
    """若当前字符为“#”,表示后面文字为注释:无需再进行遍历"""
    if s[i] == '#':
    break;

    """处理“”“的注释:note表示记录”“”的个数,奇数个时表示后面文字是注释。偶数个时表示后面文字是有效代码"""
    if i < len(s)-2 and s[i] == '\"' and s[i+1] == '\"' and s[i+2] == '\"':
    note += 1
    i = i + 1
    if note % 2 == 1:
    i += 1
    continue

    if s[i] == head:
    summary += 1
    elif s[i] == tail:
    summary -= 1
    if summary < 0:
    sign = 1
    print "匹配错误:%d 行 中 %s 与 %s 不匹配,缺少 %s" % (lineSum, head, tail, head)
    f.close()
    return 0

    """记录最后一个有效字符(去除:空格、换行符、退隔键等)"""
    if s[i] != ' ' and s[i] != '\b' and s[i] != '\t' and s[i] != '\n':
    biaoji = s[i]

    """当最后一个有效字符为"换行键"的时候,则需要遍历下一行,进行循环判断"""
    if i == len(s) - 1 and biaoji == '\\':
    line = f.readline()
    if line :
    lineSum += 1
    s = line
    i = 0
    else:
    print "续行符\\下一行无内容"
    f.close()
    return 0
    i += 1
    if summary > 0:
    print "匹配错误:%d 行 中 %s 与 %s 不匹配,缺少 %s" % (lineSum, head, tail, tail)
    f.close()
    return 0
    line = f.readline()
    f.close()
    return 1
    except Exception, e:
    print e
    ######生成调试文件#########
    def ReadSourceFile():
    #获得需要调试的文件名并判断是否存在
    try :
    fileName = raw_input("请输入需要调试的文件名:")
    while os.path.isfile(fileName) == False:
    fileName = raw_input("请输文件名不存在请重新输入:")
    except:
    os.system("clear")
    print "程序出错!即将退出"
    #结束当前的进程后面的代码都不执行
    sys.exit()

    ###调用函数检查语法匹配
    if (match("(",")",fileName) ==0):
    sys.exit()
    if (match("{","}",fileName) ==0):
    sys.exit()
    if (match("[","]",fileName) ==0):
    sys.exit()

    #以写模式打开调试文件
    writeDebugFile = open ("debugFile.py","w")
    writeDebugFile.write("#!/usr/bin/python\n")
    writeDebugFile.write("#_*_coding:utf8_*_\n")
    writeDebugFile.write("POINTS = [ ]\n")
    writeDebugFile.write("ERRPOINT = [ ]\n")
    writeDebugFile.write('SRC ="'+fileName+'"\n')
    #以读的模式打开系统文件,将系统文件中的信息插入到调试文件中
    sysFile=open("sys.py","r")
    while True:
    sysLine=sysFile.readline()
    if (len(sysLine)!=0):
    writeDebugFile.write(sysLine) #插入系统文件信息
    else :
    break
    sysFile.close() #关闭系统文件
    #以读模式打开源程序
    srcFile=file(fileName)
    pattern1=re.compile("^[ \t]*#")
    pattern2=re.compile("^[ \t]*$")
    pat = re.compile("^\t$") #tab匹配
    pat1= re.compile("^ $") #匹配空格
    pat2= re.compile("^[ \t]*else")#匹配关键字
    lineCount = 0 #用于统计源文件的行数
    while True:
    j=0
    lineCount=lineCount + 1
    line=srcFile.readline()
    if len(line)==0:
    break
    else :
    #删除注释,空行
    if pattern1.match(line)==None and pattern2.match(line)==None :
    #非空行和注释,则统计空格的个数一个tab等于8个空格
    for i in line :
    if (pat.match(i)!=None):
    j=j+8
    elif (pat1.match(i)!=None):
    j=j+1
    else:
    break
    #判断本行是不是else,eslf,except,如果是着缩进一个tab
    if (pat2.match(line)!=None):
    j= j + 8
    #将源文件信息和调试文件信息写到调试文件中
    blankString=" " * j + 'debug_wait("'+fileName+'",'+str(lineCount)+")"+"\n";
    writeDebugFile.write(blankString)
    writeDebugFile.write(line)
    else :
    ERRPOINT.append(lineCount)
    srcFile.close()#关闭源程序
    writeDebugFile.close()#关闭调试文件
    ERRPOINT = []
    ReadSourceFile()
    os.system("chmod +x debugFile.py")
    os.system("python debugFile.py")

    系统函数

    import re
    def canBePoint():
    #以读模式打开源程序
    srcFile=file(SRC)
    pattern1=re.compile("^[ \t]*#")
    pattern2=re.compile("^[ \t]*$")
    lineCount = 0 #用于统计源文件的行数
    while True:
    j=0
    lineCount=lineCount + 1
    line=srcFile.readline()
    if len(line)==0:
    break
    else :
    #删除注释,空行
    if pattern1.match(line)==None and pattern2.match(line)==None :
    pass
    else :
    ERRPOINT.append(lineCount)
    print ERRPOINT
    def set_point():
    while True:
    point = raw_input('请输入断点:--->')
    try:
    k = int(point)
    except ValueError:
    print '输入不合法,请重新输入'
    continue
    if int(point) == 0:
    break
    else:
    flag = 0
    for i in range(len(ERRPOINT)):
    if int(point) == ERRPOINT[i]:
    flag = 1
    if flag == 0:
    POINTS.append( [SRC,int(point)] )
    else:
    print '该点不能设为断点!'
    continue

    def is_break(SRC,line):
    for i in range(0,len(POINTS)):
    if str(SRC) == POINTS[i][0] and line == POINTS[i][1]:
    return True
    return False

    def debug_wait(SRC,line):
    #try:
    #global name
    #print name
    #except NameError:
    #print '还没有这个变量'
    l = line + 1
    if is_break(SRC,l):
    print ' '
    print '当前为第%d行,下一句为段点' % (line)
    while True:
    command = raw_input('输入操作--->')
    print ' '
    try:
    k = int(command)
    except ValueError:
    print '输入不合发,请重新输入!'
    continue
    if int(command) == 1:
    flag = 0
    #print 1
    nextl = l + 1
    global POINTS
    for i in range(0,len(POINTS)):
    if nextl == POINTS[i][1]:
    flag = 1
    if flag == 0:
    POINTS.append( [SRC,nextl] )
    #print POINTS
    break
    elif int(command) == 2:
    print ' 》》程序将执行到下一个断点'
    break
    elif int(command) == 3:
    #print 3
    POINTS = [ ]
    break
    else:
    print '没有这项操作,重新输入!'
    continue
    canBePoint()
    print '————————————————————————————————'
    print ' *请输入断点* '
    print '————————————————————————————————'
    set_point()
    #print POINTS

    print '————————————————————————————————'
    print ' *操作提示* '
    print ''
    print ' 1,单步调试 '
    print ' 2,运行到下一个断点 '
    print ' 3,运行程序 '
    print '————————————————————————————————'
    #!/usr/bin/python
    #
    _*_coding:utf8_*_

    def p_t():
    print '调试6'
    print '调试7'
    print '调试8'
    key = raw_input('SRC--请输入key:')
    print '调试1'
    print '调试2'
    print '调试3'
    p_t()
    print '调试4'
    print '调试5'
    if key == 'test':
    print '好的'
    else:
    print '不好'




  • 相关阅读:
    Abp Hangfire 占用 PostgreSql 连接数的另类解决方案
    Git 仓库中文件大小写重命名实践(Windows 环境)
    C# WinForm 文件夹选择控件 folderBrowserDialog 的应用实例
    使用 C# 修改文件创建时间(图片也可修改)
    ABP 在 EntityFramework 中使用扩展批量更新时的异常
    Visual Studio之“生成事件”实践
    博客专家纪念
    深入浅出-应用服务
    如何管理 .NET Core 工具
    深入浅出-可定制仓储设计
  • 原文地址:https://www.cnblogs.com/wuxi/p/2284731.html
Copyright © 2011-2022 走看看