0

I have a js file which with comments in jsdoc style:

/****************************************************************************
 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;

Now I want to extract all the variables and functions with jsdoc comment using python regexp.

For the example above the segments I want to extract are:

segment 1:

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

segment 2:

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

segment 3:

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

segment 4:

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

segment 5:

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

segment 6:

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

As you can see what I want is to extract all the variables, instance functions, class functions which has a jsdoc style comment and make a list kind like:

variables:

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

instance functions:

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

class functions:

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

I tried to parse class functions of the file using:

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

and instance functions:

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

but failed ...

Any suggestion will be appreciated, thanks :)

Updated:

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

The pattern works great except when there is empty line in between the comment such as:

/**1
2

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

});
1
  • Copyright (c) 2010-2012 cocos2d-x.org Commented Apr 4, 2014 at 9:32

1 Answer 1

1

You can not match bracket expressions with regular expressions. They need context-free expressions.

You can match the first line after the comment or anything until a ; is there.

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;

Is the best that I could get. In the last line you can see that empty lines in between are also welcome.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for you help, it works great except that when there is an empty line in between the comment part, the match will fail ~ see the update of the question ~
Modified the regex accordingly. Have alook at the difference.
Thanks for the quick reply and sorry for my bad example (the ex is updated) ~ there is some text in between the comment along with the empty lines ~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.