V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dimingxuan
V2EX  ›  Python

新人求修改.c 函数注释头思路

  •  
  •   dimingxuan · 2015-07-02 14:52:58 +08:00 via iPad · 2713 次点击
    这是一个创建于 3192 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现在有大量的.c文件需要批量修改函数头,类似的格式为
    /* xxxxxxxx
    xxxxxxxxx
    xxxxxxxx
    @warning xxxxxx
    */
    Xxxx 函数名(xxxxx)
    {
    Xxxxxxxx
    }

    函数头中可能包含/* xxx */,@warning也不一定存在。想在函数头末尾添加类似 @html_文件名_函数名 这样的字段。新人刚学python,想用正则,但貌似正则无法支持 /* */的嵌套,搜到网上有人说用pyparsing可以实现,getting started文档比较简单,没看明白怎么使用。ide里面都能区分注释和函数名,所以想请教一下这个问题的处理思路和方法,是否有现成的方式
    9 条回复    2015-07-03 12:14:28 +08:00
    leavic
        1
    leavic  
       2015-07-02 16:00:55 +08:00   ❤️ 1
    不是很清楚你所谓的函数头是哪里,给个具体的修改例子看看。
    lilydjwg
        2
    lilydjwg  
       2015-07-02 16:10:42 +08:00   ❤️ 1
    这个正则引擎 https://pypi.python.org/pypi/regex 支持嵌套,不过我不觉得用它比 pyparsing 更简单。
    dimingxuan
        3
    dimingxuan  
    OP
       2015-07-02 16:32:54 +08:00 via iPad
    @leavic
    大概每个c文件里都是这样的
    /*
    * @fn fun_add
    * @brief xxx
    .......
    * @warning xx
    */
    int fun_add()
    {
    .....
    }
    函数头都是在函数定义上方,每个c文件里都包含许多函数。
    现在需要改成

    /*
    * @fn fun_add
    * @brief xxx
    .......
    * @warning xx
    @html_文件名_fun_add
    */
    int fun_add()
    {
    .....
    }
    dimingxuan
        4
    dimingxuan  
    OP
       2015-07-02 16:37:03 +08:00 via iPad
    @lilydjwg 您的意思是用pyparsing解决问题?
    lilydjwg
        5
    lilydjwg  
       2015-07-02 17:25:18 +08:00
    @dimingxuan 嗯。pyparsing 比起正则来好学多了~
    mhycy
        6
    mhycy  
       2015-07-02 17:29:41 +08:00   ❤️ 1
    目测意思就是: 解析注释,并在注释中插入文档地址
    /* */会产生嵌套么?记忆中如果有嵌套的话C的编译器是不能正常处理的
    leavic
        7
    leavic  
       2015-07-02 20:07:41 +08:00   ❤️ 1
    这,一行行读进来,读到“*/ ”不就是注释的结尾了?再往前面插入一行没什么难度吧。
    asxalex
        8
    asxalex  
       2015-07-03 09:55:06 +08:00   ❤️ 1
    for i in *.c
    do
    awk '{
    if ($2 == "@warning")
    {print $0 "\n" " @html_'$i'_" func_name}
    else if ($2 == "@fn")
    {func_name = $3}
    else
    {print}
    }' $i
    done
    dimingxuan
        9
    dimingxuan  
    OP
       2015-07-03 12:14:28 +08:00 via iPad
    @asxalex 多谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4271 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:13 · PVG 18:13 · LAX 03:13 · JFK 06:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.