使用Python解析.h文件以便评论的简单方法?

如何使用Python以简单的方式解析用C编写的.h文件以获取注释和实体名称?

我们假设将内容进一步写入已经开发的word文件中.

源注释使用简单的标记样式规则进行格式化.
注释标签用于轻松区分一个实体注释与其他非文档注释.
评论可以是多行形式.每个评论都直接遵循实体定义:

//ENUM My comment bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
//     could be multi-line. Bla bla bla bla bla bla bla bla bla.
enum my_enum
{
    //EITEM My enum item 1.
    //      Just could be multi-line too.
    MY_ENUM_ITEM_1,

    //EITEM My enum item 2
    MY_ENUM_ITEM_2,
};

//STRUCT My struct
struct my_struct {

    //MEMBER struct member 1
    int m_1_;
};

//FUNC my function 1 description.
//     Could be multi-line also.
//INPUT  arg1 - first argument
//RETURN pointer to an allocated my_struct instance.
my_struct* func_1(int arg1);

作为解析的结果,应该出现代码和注释树.

如何快速完成并且不使用第三方库?

解决方法:

这是一个快速而肮脏的解决方案.它不会处理字符串中的注释,但因为这只适用于不应成为问题的头文件.

S_CODE,S_INLINE,S_MULTLINE = range (3)
f = open (sys.argv[1])
state = S_CODE
comments = ''
i = iter (lambda: f.read (1), '')
while True:
    try:
        c = i.next ()
    except StopIteration:
        break
    if state == S_CODE:
        if c == '/':
            c = i.next ()
            if c == '*':
                state = S_MULTLINE
            elif c == '/':
                state = S_INLINE
    elif state == S_INLINE:
        comments += c
        if c == '\n':
            state == S_CODE
    elif state == S_MULTLINE:
        if c == '*':
            c = i.next ()
            if c == '/':
                comments += '\n'
                state = S_CODE
            else:
                comments += '*%s' % c
        else:
            comments += c
print comments
上一篇:C迭代器源上的re2c扫描仪


下一篇:吴恩达深度学习笔记(十二)—— Batch Normalization