[JavaScript] Objective-C parameter list syntax Conversion Tool. Can be converted to UML or C ++ syntax for drawing UML

Source: Internet
Author: User

Author: zyl910

I. background

Currently, many commonly used UML tools do not support objective-C, which is inconvenient to draw UML. You must manually convert the objective-C syntax parameter list to the UML or C ++ syntax. It is laborious and error-prone.
So I want to write a tool that automatically converts the parameter list of objective-C syntax to UML or C ++ syntax.

What language is used for development?
For cross-platform purposes, I decided to use JavaScript to compile the tool.
It is only a small tool for string analysis and generation. Javascript is sufficient.

Ii. Functional Design

The interface is as follows --

Basic usage --
1. Fill in the objective-C method declaration in the "Source Parameter List;
2. Click "convert". The conversion result is displayed in the UML and C ++ text boxes;
3. Copy the Conversion Result of UML and C ++ text box to the clipboard and paste it into the UML tool.

Auxiliary Functions --
1. There is a "Paste" button next to "Source Parameter List. Click it to post the text in the clipboard to the "Source Parameter List" text box.
2. There is an "automatic" check box next to the "Switch" button. If the check box is selected, the conversion is automatically performed when the "Source Parameter List" text box is modified.
3. There is a copy button next to "UML" and "c ++. Click it to copy the content of the text box to the clipboard.
4. There is a "paste conversion and copy" button next to "UML" and "c ++. Click it, and the following functions will be executed: "post the text in the clipboard to the Source parameter list", "convert", and "copy the content of the text box to the Clipboard.

Iii. key code

The key code of this tool is in the paramsfromobjc function, and the objective-C parameter list string is parsed using the state machine.

The Code is as follows --

// Class to store function parameters.
function FunctionParam (paramname, paramtype) {
    this.paramname = paramname;
    this.paramtype = paramtype;
}

// Convert Objective-C parameter list code string to FunctionParam array.
function paramsFromObjc (s) {
    // == const
    // State of the state machine.
    var STATE_DEFAULT = 0; // default. It becomes STATE_WILLPARAMTYPE when it encounters ':'.
    var STATE_WILLPARAMTYPE = 1; // expected parameter type. STATE_PARAMTYPE when it encounters '('.
    var STATE_PARAMTYPE = 2; // parameter type. STATE_WILLPARAMNAME when it encounters ')'.
    var STATE_WILLPARAMNAME = 3; // Expected parameter name. It becomes STATE_PARAMNAME when a non-blank is encountered.
    var STATE_PARAMNAME = 4; // parameter name. Submit when non-English / number / underscore is encountered, and change back to STATE_DEFAULT.
    // == var
    var rt = new Array ();
    var i;
    do {
        if (! s) break;
        var len = s.length;
        if (! len) break;
        // Use state machine to parse strings
        var state = STATE_DEFAULT;
        var paramname;
        var paramtype;
        for (i = 0; i <len; ++ i) {
            // alert (s [i]);
            var ch = s [i];
            switch (state) {
                case STATE_DEFAULT:
                {
                    if (':' == ch) {
                        paramname = "";
                        paramtype = "";
                        state = STATE_WILLPARAMTYPE;
                    }
                }
                break;
                case STATE_WILLPARAMTYPE:
                {
                    if ('(' == ch) {
                        state = STATE_PARAMTYPE;
                    }
                }
                break;
                case STATE_PARAMTYPE:
                {
                    if (')' == ch) {
                        state = STATE_WILLPARAMNAME;
                    }
                    else {
                        paramtype = paramtype + ch;
                    }
                }
                break;
                case STATE_WILLPARAMNAME:
                {
                    if ('' == ch || '\ t' == ch || '\ r' == ch || '\ n' == ch) {
                    }
                    else {
                        paramname = ch;
                        state = STATE_PARAMNAME;
                    }
                }
                break;
                case STATE_PARAMNAME:
                {
                    if (('_' == ch) || ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z')) {
                        paramname = paramname + ch;
                    }
                    else {
                        // Submit.
                        // alert (paramtype + "" + paramname);
                        var p = new FunctionParam (paramname, paramtype);
                        rt.push (p);
                        // Restore state.
                        state = STATE_DEFAULT;
                    }
                }
                break;
            }
        }
        // Complete the last item.
        if (STATE_PARAMNAME == state) {
            // Submit.
            // alert (paramtype + "" + paramname);
            var p = new FunctionParam (paramname, paramtype);
            rt.push (p);
            // Restore state.
            state = STATE_DEFAULT;
        }
    } while (0);
    return rt;
} 

 

 

4. online tools


 

Download source code --
Http://files.cnblogs.com/zyl910/convparam_objc.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.