2014-04-04 89 views
0

我有在jsdoc風格註釋的js文件:蟒蛇正則表達式解析js文件

/**************************************************************************** 
Copyright (c) 2010-2012 cocos2d-x.org 
Copyright (c) 2008-2010 Ricardo Quesada 
Copyright (c) 2011  Zynga Inc. 

http://www.cocos2d-x.org 
****************************************************************************/ 

cc.g_NumberOfDraws = 0; 

//Possible OpenGL projections used by director 
/** 
* sets a 2D projection (orthogonal projection) 
* @constant 
* @type Number 
*/ 
cc.DIRECTOR_PROJECTION_2D = 0; 

/** 
* sets a 3D projection with a fovy=60, znear=0.5f and zfar=1500. 
* @constant 
* @type Number 
*/ 
cc.DIRECTOR_PROJECTION_3D = 1; 

//---------------------------------------------------------------------------------------------------------------------- 

/** 
* <p> 
* </p> 
* @class 
* @extends cc.Class 
*/ 
cc.Director = cc.Class.extend(/** @lends cc.Director# */{ 
    //Variables 
    _landscape:false, 
    _nextDeltaTimeZero:false, 
    /** 
    * <p> 
    * </p> 
    */ 
    popToRootScene:function() { 
     // ... 
    }, 

    /** 
    * <p> 
    * </p> 
    * @param {Number} level 
    */ 
    popToSceneStackLevel: function (level) { 
     // ... 
    } 
}); 

/** 
* returns a shared instance of the director 
* @function 
* @return {cc.Director} 
*/ 
cc.Director.getInstance = function() { 
    // ... 
}; 

/** 
* is director first run 
* @type Boolean 
*/ 
cc.firstRun = true; 

現在我想提取所有變量和函數使用正則表達式的Python評論jsdoc

對於我要提取的節段上面的例子有:

段1:

/** 
* sets a 2D projection (orthogonal projection) 
* @constant 
* @type Number 
*/ 
cc.DIRECTOR_PROJECTION_2D = 0; 

段2:

/** 
* sets a 3D projection with a fovy=60, znear=0.5f and zfar=1500. 
* @constant 
* @type Number 
*/ 
cc.DIRECTOR_PROJECTION_3D = 1; 

段3:

/** 
* <p> 
* </p> 
*/ 
popToRootScene:function() { 
    // ... 
}, 

第4部分:

/** 
* <p> 
* </p> 
* @param {Number} level 
*/ 
popToSceneStackLevel: function (level) { 
    // ... 
} 

段5:

/** 
* returns a shared instance of the director 
* @function 
* @return {cc.Director} 
*/ 
cc.Director.getInstance = function() { 
    // ... 
}; 

段6:

/** 
* is director first run 
* @type Boolean 
*/ 
cc.firstRun = true; 

正如你可以看到我想要的是提取所有變量,實例函數,類函數具有jsdoc風格評論並列出類似:

變量:

name: cc.DIRECTOR_PROJECTION_2D type: number 
name: cc.DIRECTOR_PROJECTION_3D type: number 

實例功能:

name: popToRootScene param: xxxx return: xxxx 
name: popToSceneStackLevel param: number - level return: xxxx 

類功能:

name: cc.Director.getInstance param: xxxx return: cc.Director 

我試着用解析文件的類函數:

re.findall('\s*/\*\*.*?\*/.*?function.*?};', content, re.S) 

和實例功能:

re.findall('\s*/\*\*.*?\*/.*?function.*?},', content, re.S) 

但失敗...

任何建議將讚賞,感謝:)

更新時間:

re.findall(r"(^(?P<identation> *)/\*\*.*$(\r?\n?^(?P=identation) * .*$)*\r?\n?(?P=identation) \*/\r?\n?^.*$)", content, re.M) 

模式的偉大工程時,有空行除在評論之間如:

/**1 
2 

3 
*/ 
cc.Node = cc.Class.extend(/** @lends cc.Node# */{ 

}); 
+0

版權所有(c)2010-2012 cocos2d-x.org –

回答

1

不能使用正則表達式匹配括號表達式。他們需要上下文無關表達式。

您可以匹配評論後的第一行或任何內容,直到;在那裏。

for x in re.findall(r"(^(?P<identation> *)/\*\*\s*$(\r?\n?^(?P=identation) * .*$)*\r?\n?(?P=identation) \*/\s*^.*$)", s, re.MULTILINE): 
    print("-" * 40) 
    print(x[0]) 


---------------------------------------- 
/** 
* sets a 2D projection (orthogonal projection) 
* @constant 
* @type Number 
*/ 
cc.DIRECTOR_PROJECTION_2D = 0; 
---------------------------------------- 
/** 
* sets a 3D projection with a fovy=60, znear=0.5f and zfar=1500. 
* @constant 
* @type Number 
*/ 
cc.DIRECTOR_PROJECTION_3D = 1; 
---------------------------------------- 
/** 
* <p> 
* </p> 
* @class 
* @extends cc.Class 
*/ 
cc.Director = cc.Class.extend(/** @lends cc.Director# */{ 
---------------------------------------- 
    /** 
    * <p> 
    * </p> 
    */ 
    popToRootScene:function() { 
---------------------------------------- 
    /** 
    * <p> 
    * </p> 
    * @param {Number} level 
    */ 
    popToSceneStackLevel: function (level) { 
---------------------------------------- 
/** 
* returns a shared instance of the director 
* @function 
* @return {cc.Director} 
*/ 
cc.Director.getInstance = function() { 
---------------------------------------- 
/** 
* is director first run 
* @type Boolean 
*/ 

cc.firstRun = true; 

是我能得到的最好的。在最後一行中,您可以看到中間的空行也受到歡迎。

+0

感謝您的幫助,除非在評論部分之間存在空行時,它的工作效果很好,否則匹配將失敗〜請參閱問題的更新〜 – supersuraccoon

+0

相應地修改了正則表達式。對不同點有所瞭解。 – User

+0

感謝您的快速回復,併爲我的壞例子感到遺憾(前者已更新)〜在評論和空行之間有一些文字〜 – supersuraccoon