Пример #1
0
        private string OnFunction(string line)
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder args = new StringBuilder();

            Symbol s = new Symbol();

            s.EnclosingType = _className;
            s.Kind = Symbol.FUNCTION;

            string[] tokenss = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string token in tokenss)
            {
                switch (_state)
                {
                    case State.COMMENT:
                        sb.Append(token);
                        continue;
                    default:
                        break;
                }

                if (token == "public")
                {
                    s.Scope = Symbol.PUBLIC;
                }
                else if (token == "protected")
                {
                    s.Scope = Symbol.PROTECTED;
                }
                else if (token == "private")
                {
                    s.Scope = Symbol.PRIVATE;
                }
                else if (token == "function")
                {
                    _state = State.FUNCDEF;
                }
                else if (token == "const")
                {
                    s.Modifier = Symbol.CONST;
                }
                else if (token == "static")
                {
                    s.Modifier = Symbol.STATIC;
                }
                else if (token == "override")
                {
                    s.Modifier = Symbol.OVERRIDE;
                }
                else if (token == "set")
                {
                    _state = State.SET;
                    s.Kind = Symbol.SETTER;
                }
                else if (token == "get")
                {
                    _state = State.GET;
                    s.Kind = Symbol.GETTER;
                }
                else if (token == "=")
                {
                    _state = State.ASSIGNMENT;
                }
                else if (token == "//" || token == "/*")
                {
                    _state = State.COMMENT;
                    sb.Append(token);
                }
                else
                {
                    string[] functokens;
                    string[] argtokens;
                    switch (_state)
                    {
                        case State.ASSIGNMENT:
                            string[] assigntokens = token.Split(new char[] { ' ', '\t', '=', ';' }, StringSplitOptions.RemoveEmptyEntries);
                            s.DefaultValue = assigntokens[0];
                            sb.AppendFormat("{0} {1} = {2};", s.Type, s.Name, s.DefaultValue);
                            _state = State.START;
                            break;
                        case State.FUNCDEF:
                            functokens = token.Split(new char[] { '(' }, StringSplitOptions.RemoveEmptyEntries);
                            s.Name = functokens[0];
                            if (s.Name == _className)
                            {
                                s.Constructor = true;
                            }
                            _fname = functokens[0];
                            if (functokens[1].StartsWith(")"))
                            {
                                string[] rettype = functokens[1].Split(new char[] { ')',':' }, StringSplitOptions.RemoveEmptyEntries);
                                s.Arguments = doArguments("");
                                if (rettype.Length > 0)
                                {
                                    s.ReturnType = TypeMapping.Convert(rettype[0].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[0]); ; ;
                                }
                                else
                                {
                                    s.ReturnType = "";
                                }
                                _state = State.START;
                            }
                            else
                            {
                                argtokens = functokens[1].Split(new char[] { ')' }, StringSplitOptions.RemoveEmptyEntries);
                                if (argtokens.Length == 0)
                                {
                                    s.Arguments = doArguments("");
                                    s.ReturnType = "";
                                }
                                else if (argtokens.Length == 1)
                                {
                                    s.Arguments = doArguments(argtokens[0]);
                                    s.ReturnType = "";
                                }
                                else if (argtokens.Length == 2)
                                {
                                    s.Arguments = doArguments(argtokens[0]);
                                    s.ReturnType = TypeMapping.Convert(argtokens[1].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[0]); ;
                                }
                                _state = State.ARGSDEF;
                            }
                            break;

                        case State.ARGSDEF:
                            args.Append(token);
                            break;

                        case State.SET:
                            functokens = token.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                            s.Name = functokens[0];
                            s.Arguments = doArguments(functokens[1]);
                            s.ReturnType = TypeMapping.Convert(functokens[2].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[0]);
                            _state = State.ARGSDEF;
                            break;

                        case State.GET:
                            functokens = token.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                            s.Name = functokens[0];
                            s.ReturnType = TypeMapping.Convert(functokens[1].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[0]);
                            sb.AppendFormat("{0} {1}::{2}()", s.ReturnType, s.EnclosingType, s.Name);
                            sb.Append(" {");
                            _state = State.START;
                            break;
                    }
                }
            }

            switch (_state)
            {
                case State.ASSIGNMENT:
                    sb.AppendFormat("{0} {1};", s.Type, s.Name);
                    _state = State.START;
                    break;
                case State.COMMENT:
                    _state = State.START;
                    break;
                case State.ARGSDEF:
                    switch (s.Kind)
                    {
                        case Symbol.FUNCTION:
                            sb.AppendFormat("{0} {1}::{2}({3})", s.ReturnType, s.EnclosingType, s.Name, RewriteArgs(s.Arguments));
                            sb.Append(" {");
                            break;
                        case Symbol.SETTER:
                            sb.AppendFormat("{0} {1}::{2}({3})", s.ReturnType, s.EnclosingType, s.Name, RewriteArgs(s.Arguments));
                            sb.Append(" {");
                            break;
                    }
                    _state = State.START;
                    break;
            }

            _symtab.Add(s);

            line = sb.ToString();

            return line;
        }
Пример #2
0
        private string OnVarKeyword(string line)
        {
            _symbolModifier = -1;
            _symbolStorage = -1;

            StringBuilder sb = new StringBuilder();

            Symbol s = new Symbol();
            s.Kind = Symbol.VARIABLE;

            if (_inFunction)
            {
                s.EnclosingType = _fname;
            }
            else
            {
                s.EnclosingType = _className;
            }

                string[] tokens = line.Split(new char[] { ' ','\t','=',';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string token in tokens)
                {
                    switch (_state)
                    {
                        case State.COMMENT:
                            sb.Append(token);
                            continue;
                        default:
                            break;
                    }

                    if (token == "public")
                    {
                        s.Scope = Symbol.PUBLIC;
                    }
                    else if (token == "protected")
                    {
                        s.Scope = Symbol.PROTECTED;
                    }
                    else if (token == "private")
                    {
                        s.Scope = Symbol.PRIVATE;
                    }
                    else if (token == "var")
                    {
                        _state = State.VARDEF;
                    }
                    else if (token == "const")
                    {
                        s.Modifier = Symbol.CONST;
                    }
                    else if (token == "new")
                    {
                        s.Storage = Symbol.ALLOCATED;
                        _state = State.ALLOCATING;
                    }
                    else if (token == "static")
                    {
                        s.Storage = Symbol.STATIC;
                    }
                    else if (token == "=")
                    {
                        _state = State.ASSIGNMENT;
                    }
                    else if (token == "//" || token == "/*")
                    {
                        _state = State.COMMENT;
                        sb.Append(token);
                    }
                    else
                    {
                        switch (_state)
                        {
                            case State.ASSIGNMENT:
                                string[] assigntokens = token.Split(new char[] { ' ', '\t', '=',';' }, StringSplitOptions.RemoveEmptyEntries);
                                s.DefaultValue = assigntokens[0];
                                sb.AppendFormat("{0} {1} = {2};", s.Type, s.Name, s.DefaultValue);
                                _state = State.START;
                                break;
                            case State.VARDEF:
                                string[] vartokens = token.Split(new char[] { ' ', '\t', ':' }, StringSplitOptions.RemoveEmptyEntries);
                                s.Name = vartokens[0];
                                s.Type = TypeMapping.Convert(vartokens[1]);
                                _state = State.ASSIGNMENT;
                                break;
                            case State.ALLOCATING:
                                sb.AppendFormat("{0} *{1} = new {2}", s.Type, s.Name, token);
                                s.Allocator = Symbol.ALLOCATED;
                                _state = State.START;
                                break;
                            case State.START:
                                sb.AppendFormat("{0}", token);
                                break;
                        }
                    }
                }

                switch (_state)
                {
                    case State.ASSIGNMENT:
                        sb.AppendFormat("{0} {1};", s.Type, s.Name);
                        break;
                    case State.COMMENT:
                        _state = State.START;
                        break;
                    case State.START:
                        if (!sb.ToString().EndsWith(";"))
                        {
                            sb.Append(";");
                        }
                        break;
                }

                line = sb.ToString();

                //_meta.Source = line;

                _symtab.Add(s);

                return line;
        }
Пример #3
0
        private string OnClass(string line)
        {
            StringBuilder sb = new StringBuilder();

            Symbol s = new Symbol();

            s.Kind = Symbol.CLASS;

            string[] tokenss = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string token in tokenss)
            {
                switch (_state)
                {
                    case State.COMMENT:
                        sb.Append(token);
                        continue;
                    default:
                        break;
                }
                if (token == "public")
                {
                    s.Scope = Symbol.PUBLIC;
                }
                else if(token == "protected")
                {
                    s.Scope = Symbol.PROTECTED;
                }
                else if (token == "private")
                {
                    s.Scope = Symbol.PRIVATE;
                }
                else if (token == "final")
                {
                    s.Modifier = Symbol.FINAL;
                }
                else if (token == "class")
                {
                    _state = State.CLASSDEF;
                }
                else if (token == "extends")
                {
                    _state = State.EXTENDS;
                }
                else if (token == "implements")
                {
                    _state = State.IMPLEMENTS;
                }
                else
                {
                    string[] classtokens;
                    //@string[] argtokens;
                    switch (_state)
                    {
                        case State.CLASSDEF:
                            classtokens = token.Split(new char[] { '(' }, StringSplitOptions.RemoveEmptyEntries);
                            s.Name = classtokens[0];
                            _className = classtokens[0];
                            sb.AppendFormat("class {0}", classtokens[0]);
                            break;
                        case State.EXTENDS:
                            classtokens = token.Split(new char[] { '(' }, StringSplitOptions.RemoveEmptyEntries);
                            sb.AppendFormat(" : public {0}", classtokens[0]);
                            s.ExtendedType = classtokens[0];
                            _state = State.START;
                            break;
                        case State.IMPLEMENTS:
                            classtokens = token.Split(new char[] { '(' }, StringSplitOptions.RemoveEmptyEntries);
                            sb.AppendFormat(", {0}", classtokens[0]);
                            s.ImplementedType = classtokens[0];
                            _state = State.START;
                            break;
                    }
                }
            }

            switch (_state)
            {
                case State.START:
                    sb.Append(" {");
                    break;
            }

            _symtab.Add(s);

            line = sb.ToString();

            _meta.Source = line;

            return line;
        }
Пример #4
0
        private string OnStatement(string line)
        {
            if (_inComment)
            {
                return line;
            }

            StringBuilder sb = new StringBuilder();

            Symbol s = new Symbol();

            string[] tokenss = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string token in tokenss)
            {
                switch (_state)
                {
                    case State.STMTDEF:
                        _state = State.START;
                        string str = OnMethodCall(token);
                        sb.Append(str);
                        continue;
                    case State.NEWDEF:
                        _state = State.CALLDEF;
                        sb.Append("new ");
                        break;
                    case State.ASSIGNMENT:
                        _state = State.START;
                        sb.Append(" = ");
                        break;
                    case State.QUOTEDEF:
                        sb.Append(token);
                        continue;
                    default:
                        break;
                }

                if (token == "new")
                {
                    _state = State.NEWDEF;
                }
                else if (token.Contains('"'))
                {
                    sb.Append(token);
                    _state = State.QUOTEDEF;
                }
                else if (token == "=")
                {
                    _state = State.ASSIGNMENT;
                }
                else
                {
                    switch (_state)
                    {
                        case State.CALLDEF:
                            string[] argtokens;
                            string[] functokens = token.Split(new char[] { '(' }, StringSplitOptions.RemoveEmptyEntries);

                            if (functokens[1].StartsWith(")"))
                            {
                                sb.Append(token);
                            }
                            else
                            {
                                Dictionary<string, string> args = null;
                                argtokens = functokens[1].Split(new char[] { ')' }, StringSplitOptions.RemoveEmptyEntries);
                                if (argtokens.Length == 0)
                                {
                                    args = doArguments("");
                                }
                                else if (argtokens.Length == 1)
                                {
                                    args = doArguments(argtokens[0]);
                                }
                                else if (argtokens.Length == 2)
                                {
                                    args = doArguments(argtokens[0]);
                                }
                                sb.AppendFormat("{0}({1});", functokens[0], RewriteArgs(args));
                            }
                            break;
                        case State.START:
                            //@string str = OnMethodCall(token);
                            sb.Append(token);
                            break;
                    }
                }
            }

            return sb.ToString();
        }
Пример #5
0
        private string ParseVar(string line)
        {
            _symbolModifier = -1;
            _symbolStorage = -1;

            if (!_inComment && !_inFunction)
            {
                int colon = line.IndexOf(":");

                string variable = "";
                string type = "";

                char[] anyOf = new char[] { ' ', '\t', ';' };
                int ws;

                int kVar = line.IndexOf("var");
                int kConst = line.IndexOf("const");
                int kStatic = line.IndexOf("static");

                Symbol s = new Symbol();

                if (kVar != -1)
                {
                    kVar += 3;
                    variable = line.Substring(kVar, colon - kVar);
                    ws = line.IndexOfAny(anyOf, colon);
                    if (ws < 0)
                    {
                        type = line.Substring(colon + 1, colon - 1);
                    }
                    else
                    {
                        type = line.Substring(colon + 1, (ws - colon) - 1);
                    }
                    line = line.Replace(type+" ", "");
                    line = line.Replace("var", "");
                }
                else if (kConst != -1)
                {
                    kConst += 5;
                    variable = line.Substring(kConst, colon - kConst);
                    ws = line.IndexOfAny(anyOf, colon);
                    type = line.Substring(colon + 1, (ws - colon) - 1);
                    line = line.Replace(type, "");
                    line = line.Replace("const", "const " + type);
                }
                else if (kStatic != -1)
                {
                    kStatic += 6;
                    variable = line.Substring(kStatic, colon - kStatic);
                    ws = line.IndexOfAny(anyOf, colon);
                    type = line.Substring(colon + 1, (ws - colon) - 1);
                    line = line.Replace(type, "");
                    line = line.Replace("static", "static " + type);
                }

                s.Name = variable;
                s.Type = type;
                s.EnclosingName = _className;

                int kEquals = line.IndexOf('=');
                int kSemi = line.IndexOf(';');

                if (kEquals != -1)
                {
                    if (kSemi != -1)
                    {
                        s.DefaultValue = line.Substring(kEquals + 1, kSemi - kEquals - 1);
                    }
                    else
                    {
                        // array initializer
                        if (line.IndexOf('[') != -1)
                        {
                            line = line.Replace('[', '{');
                            s.DefaultValue = line.Substring(kEquals + 1);
                            if (s.Type == "Array")
                            {
                                _state = STATE.ARRAY_INITALIZER;
                            }
                        }
                        else
                        {
                            s.DefaultValue = line.Substring(kEquals + 1);
                        }
                    }
                }
                else
                {
                    s.DefaultValue = null;
                }

                line = line.Replace(":", " ");

                if (line.Contains("public"))
                {
                    line = line.Replace("public", "");
                    line = line.Trim();
                    _meta.ProtectedField = type + " " + variable;
                    _symbolScope = PUBLIC;
                    s.Scope = Symbol.PUBLIC;
                }
                else if (line.Contains("protected"))
                {
                    line = line.Replace("protected", "");
                    line = line.Trim();
                    _meta.ProtectedField = type + " " + variable;
                    _symbolScope = PROTECTED;
                    s.Scope = Symbol.PROTECTED;
                }
                else if (line.Contains("private"))
                {
                    line = line.Replace("private", "");
                    line = line.Trim();
                    _meta.PrivateField = type + " " + variable;
                    _symbolScope = PRIVATE;
                    s.Scope = Symbol.PRIVATE;
                }

                if (line.Contains("static"))
                {
                    line = line.Replace("static", "");
                    line = line.Replace(type + " ", _className + "::");
                    _symbolStorage = STATIC;
                    s.Storage = Symbol.STATIC;
                }

                if (line.Contains("const"))
                {
                    line = line.Replace("const", "");
                    _symbolModifier = CONST;
                    s.Modifier = Symbol.CONST;
                }

                line = line.Trim();

                if (line.Contains("new"))
                {
                    s.Allocator = Symbol.ALLOCATED;
                }

                if (line.Contains("="))
                {
                    _meta.Initializer = line;
                    _meta.Source = line;
                }
                else if (_symbolStorage == STATIC)
                {
                    _meta.Static = line;
                }
                else if (_symbolScope == PUBLIC)
                {
                    _meta.PublicField = type + " " + variable;
                }
                else if (_symbolScope == PROTECTED)
                {
                    _meta.ProtectedField = type + " " + variable;
                }
                else if (_symbolScope == PRIVATE)
                {
                    _meta.PrivateField = type + " " + variable;
                }
                else
                {
                    _meta.Source = line;
                }

                _symtab.Add(s);
            }
            else
            {
                if (_inFunction)
                {
                    int colon = line.IndexOf(":");

                    string variable = "";
                    string type = "";

                    char[] anyOf = new char[] { ' ', '\t', ';' };
                    int ws;

                    int kVar = line.IndexOf("var");

                    Symbol s = new Symbol();

                    if (kVar != -1)
                    {
                        kVar += 3;
                        variable = line.Substring(kVar, colon - kVar);
                        ws = line.IndexOfAny(anyOf, colon);
                        if (ws < 0)
                        {
                            type = line.Substring(colon + 1, colon - 1);
                        }
                        else
                        {
                            type = line.Substring(colon + 1, (ws - colon) - 1);
                        }
                        line = line.Replace(":"+type, "");
                        line = line.Replace(variable, "");
                        line = line.Replace("var", type + "*"+variable);
                    }

                    s.Name = variable;
                    s.Type = type;
                    s.EnclosingName = _fname;

                    if (line.Contains("new"))
                    {
                        s.Allocator = Symbol.ALLOCATED;
                    }

                    _meta.Source = line;

                    _symtab.Add(s);
                }
            }
            return line;
        }
Пример #6
0
        private string OnFunction(string line)
        {
            if (!_inComment)
            {
                _inFunction = true;

                int kOpenParen = line.IndexOf('(');
                int kCloseParen = line.IndexOf(')');
                int kOpenCurly = line.IndexOf('{');
                if (kOpenCurly > 0)
                {
                    _brace += 1;
                }
                int colon = line.IndexOf(":", kCloseParen);

                Symbol s = new Symbol();

                s.Kind = Symbol.FUNCTION;

                string argList;
                if (line[kOpenParen + 1] == ')')
                {
                    argList = doArguments("");
                }
                else
                {
                    string old = line.Substring(kOpenParen + 1, (kCloseParen - kOpenParen) - 1);
                    argList = doArguments(old);
                    line = line.Replace(old, argList);
                }

                s.ArgList = argList;

                int kFunc = line.IndexOf("function") + 8;
                string delim = " \t;";
                char[] anyOf = delim.ToCharArray();

                int index = (colon == -1) ? kCloseParen : colon;
                kCloseParen = line.IndexOf(')');
                colon = line.IndexOf(":", kCloseParen);
                int ws = line.IndexOfAny(anyOf, kCloseParen);

                _fname = line.Substring(kFunc, kOpenParen - kFunc).Trim();
                if (line.Contains("set"))
                {
                    line = line.Replace("set ", "");
                    _fname = _fname.Replace("set ", "");
                    _setter = true;
                    s.Accessor = Symbol.SETTER;
                }
                else if (line.Contains("get"))
                {
                    line = line.Replace("get ", "");
                    _fname = _fname.Replace("get ", "");
                    _getter = true;
                    s.Accessor = Symbol.GETTER;
                }

                s.Name = _fname;

                string rettype = "";

                kCloseParen = line.IndexOf(')');
                colon = line.IndexOf(":", kCloseParen);
                ws = line.IndexOfAny(anyOf, kCloseParen);

                if (colon != -1)
                {
                    int length = ws - colon;
                    if (ws > 0)
                    {
                        rettype = line.Substring(colon + 1, length);
                    }
                    else
                    {
                        rettype = line.Substring(colon + 1);
                    }
                    line = line.Replace(rettype, "");
                    line = line.Replace(":", " ");
                }

                s.ReturnType = rettype;

                if (_setter)
                {
                    // TODO: Use a command line switch to control prefix
                    line = line.Replace("function ", rettype + _className + "::set_");
                }
                else if (_getter)
                {
                    // TODO: Use a command line switch to control prefix
                    line = line.Replace("function ", rettype + _className + "::get_");
                }
                else
                {
                    line = line.Replace("function ", rettype + _className + "::");
                }

                if (line.Contains("public"))
                {
                    line = line.Replace("public", "");
                    line = line.Trim();
                    _meta.Source = line;
                    line = line.Replace(_className + "::", "");
                    kCloseParen = line.IndexOf(")");
                    ws = line.IndexOfAny(WS, kCloseParen);
                    if (ws > 0)
                    {
                        _fname = line.Substring(0, ws);
                    }
                    else
                    {
                        _fname = line.Substring(0);
                    }
                    if (_fname.Contains(_className+"("))
                    {
                        _meta.Constructor = _fname;
                        _addDTOR = true;
                    }
                    else
                    {
                        _meta.PublicMember = _fname;
                    }

                    s.Scope = Symbol.PUBLIC;
                }
                else if (line.Contains("private"))
                {
                    line = line.Replace("private", "");
                    line = line.Trim();
                    _meta.Source = line;
                    line = line.Replace(_className + "::", "");
                    kCloseParen = line.IndexOf(")");
                    ws = line.IndexOfAny(WS, kCloseParen);
                    _fname = line.Substring(0, ws);
                    _meta.PrivateMember = _fname;

                    s.Scope = Symbol.PRIVATE;
                }
                else if (line.Contains("protected"))
                {
                    line = line.Replace("protected", "");
                    line = line.Trim();
                    _meta.Source = line;
                    line = line.Replace(_className + "::", "");
                    kCloseParen = line.IndexOf(")");
                    ws = line.IndexOfAny(WS, kCloseParen);
                    _fname = line.Substring(0, ws);
                    _meta.ProtectedMember = _fname;

                    s.Scope = Symbol.PROTECTED;
                }
            }
            else
            {
                _meta.Source = line;
            }
            return line;
        }
Пример #7
0
        private string ParseVar(string line)
        {
            _symbolModifier = -1;
            _symbolStorage = -1;

            if (!_inComment && !_inFunction)
            {
                StringBuilder sb = new StringBuilder();

                Symbol s = new Symbol();

                s.EnclosingType = _className;

                string[] tokens = line.Split(new char[] { ' ','\t' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string token in tokens)
                {
                    if (token == "public")
                    {
                        s.Scope = Symbol.PUBLIC;
                    }
                    else if (token == "protected")
                    {
                        s.Scope = Symbol.PROTECTED;
                    }
                    else if (token == "private")
                    {
                        s.Scope = Symbol.PRIVATE;
                    }
                    else if (token == "var")
                    {
                        _state = State.VARDEF;
                    }
                    else if (token == "const")
                    {
                        s.Modifier = Symbol.CONST;
                    }
                    else if (token == "new")
                    {
                        s.Storage = Symbol.ALLOCATED;
                        _state = State.ALLOCATING;
                    }
                    else if (token == "=")
                    {
                        _state = State.ASSIGNMENT;
                    }
                    else
                    {
                        switch (_state)
                        {
                            case State.ASSIGNMENT:
                                string[] assigntokens = token.Split(new char[] { ' ', '\t', '=',';' }, StringSplitOptions.RemoveEmptyEntries);
                                s.DefaultValue = assigntokens[0];
                                sb.Append(s.DefaultValue);
                                break;
                            case State.VARDEF:
                                string[] vartokens = token.Split(new char[] { ' ', '\t', ':' }, StringSplitOptions.RemoveEmptyEntries);
                                s.Name = vartokens[0];
                                s.Type = vartokens[1];
                                break;
                            case State.ALLOCATING:
                                sb.AppendFormat("= {0};", s.Type);
                                break;
                        }
                        _state = State.START;
                    }
                }

                int colon = line.IndexOf(":");

                string variable = "";
                string type = "";

                char[] anyOf = new char[] { ' ', '\t', ';' };
                int ws;

                int kVar = line.IndexOf("var");
                int kConst = line.IndexOf("const");
                int kStatic = line.IndexOf("static");

                if (kVar != -1)
                {
                    kVar += 3;
                    variable = line.Substring(kVar, colon - kVar);
                    ws = line.IndexOfAny(anyOf, colon);
                    if (ws < 0)
                    {
                        type = line.Substring(colon + 1, colon - 1);
                    }
                    else
                    {
                        type = line.Substring(colon + 1, (ws - colon) - 1);
                    }
                    line = line.Replace(type+" ", "");
                    line = line.Replace("var", "");
                }
                else if (kConst != -1)
                {
                    kConst += 5;
                    variable = line.Substring(kConst, colon - kConst);
                    ws = line.IndexOfAny(anyOf, colon);
                    type = line.Substring(colon + 1, (ws - colon) - 1);
                    line = line.Replace(type, "");
                    line = line.Replace("const", "const " + type);
                }
                else if (kStatic != -1)
                {
                    kStatic += 6;
                    variable = line.Substring(kStatic, colon - kStatic);
                    ws = line.IndexOfAny(anyOf, colon);
                    type = line.Substring(colon + 1, (ws - colon) - 1);
                    line = line.Replace(type, "");
                    line = line.Replace("static", "static " + type);
                }

                s.Name = variable;
                s.Type = type;
                s.EnclosingType = _className;

                int kEquals = Match(line, '=');
                int kSemi = Match(line, ';');

                if (kEquals != -1)
                {
                    if (kSemi != -1)
                    {
                        s.DefaultValue = line.Substring(kEquals + 1, kSemi - kEquals - 1);
                    }
                    else
                    {
                        // array initializer
                        if (line.IndexOf('[') != -1)
                        {
                            line = line.Replace('[', '{');
                            s.DefaultValue = line.Substring(kEquals + 1);
                            if (s.Type == "Array")
                            {
                                _state = State.ARRAY_INITALIZER;
                            }
                        }
                        else
                        {
                            s.DefaultValue = line.Substring(kEquals + 1);
                        }
                    }
                }
                else
                {
                    s.DefaultValue = null;
                }

                line = line.Replace(":", " ");

                if (line.Contains("public"))
                {
                    line = line.Replace("public", "");
                    line = line.Trim();
                    _meta.ProtectedField = type + " " + variable;
                    _symbolScope = PUBLIC;
                    s.Scope = Symbol.PUBLIC;
                }
                else if (line.Contains("protected"))
                {
                    line = line.Replace("protected", "");
                    line = line.Trim();
                    _meta.ProtectedField = type + " " + variable;
                    _symbolScope = PROTECTED;
                    s.Scope = Symbol.PROTECTED;
                }
                else if (line.Contains("private"))
                {
                    line = line.Replace("private", "");
                    line = line.Trim();
                    _meta.PrivateField = type + " " + variable;
                    _symbolScope = PRIVATE;
                    s.Scope = Symbol.PRIVATE;
                }

                if (line.Contains("static"))
                {
                    line = line.Replace("static", "");
                    line = line.Replace(type + " ", _className + "::");
                    _symbolStorage = STATIC;
                    s.Storage = Symbol.STATIC;
                }

                if (line.Contains("const"))
                {
                    line = line.Replace("const", "");
                    _symbolModifier = CONST;
                    s.Modifier = Symbol.CONST;
                }

                line = line.Trim();

                if (line.Contains("new"))
                {
                    s.Allocator = Symbol.ALLOCATED;
                }

                if (line.Contains("="))
                {
                    _meta.Initializer = line;
                    _meta.Source = line;
                }
                else if (_symbolStorage == STATIC)
                {
                    _meta.Static = line;
                }
                else if (_symbolScope == PUBLIC)
                {
                    _meta.PublicField = type + " " + variable;
                }
                else if (_symbolScope == PROTECTED)
                {
                    _meta.ProtectedField = type + " " + variable;
                }
                else if (_symbolScope == PRIVATE)
                {
                    _meta.PrivateField = type + " " + variable;
                }
                else
                {
                    _meta.Source = line;
                }

                _symtab.Add(s);
            }
            else
            {
                if (_inFunction)
                {
                    int colon = line.IndexOf(":");

                    string variable = "";
                    string type = "";

                    char[] anyOf = new char[] { ' ', '\t', ';' };
                    int ws;

                    int kVar = line.IndexOf("var");

                    Symbol s = new Symbol();

                    if (kVar != -1)
                    {
                        kVar += 3;
                        variable = line.Substring(kVar, colon - kVar);
                        ws = line.IndexOfAny(anyOf, colon);
                        if (ws < 0)
                        {
                            type = line.Substring(colon + 1, colon - 1);
                        }
                        else
                        {
                            type = line.Substring(colon + 1, (ws - colon) - 1);
                        }
                        line = line.Replace(":"+type, "");
                        line = line.Replace(variable, "");
                        line = line.Replace("var", type + "*"+variable);
                    }

                    s.Name = variable;
                    s.Type = type;
                    s.EnclosingType = _fname;

                    if (line.Contains("new"))
                    {
                        s.Allocator = Symbol.ALLOCATED;
                    }

                    _meta.Source = line;

                    _symtab.Add(s);
                }
            }
            return line;
        }
Пример #8
0
        private string OnFunction(string line)
        {
            if (!_inComment)
            {
                _inFunction = true;

                switch (_state)
                {
                    case State.FUNCTION_ARGLIST:
                        _sb.Append(line);
                        if (line.IndexOf(')') != -1)
                        {
                            line = _sb.ToString();
                            _state = State.START;
                        }
                        else
                        {
                            return line;
                        }
                        break;
                }

                int kOpenParen = line.IndexOf('(');
                int kCloseParen = line.IndexOf(')');
                int kOpenCurly = line.IndexOf('{');
                if (kOpenCurly > 0)
                {
                    _brace += 1;
                }
                int colon = 0;
                if (kCloseParen != -1)
                {
                    colon = line.IndexOf(":", kCloseParen);
                    _state = State.START;
                }
                else
                {
                    _state = State.FUNCTION_ARGLIST;
                    return _sb.Append(line).ToString();
                }

                Symbol s = new Symbol();

                s.Kind = Symbol.FUNCTION;

                Dictionary<string, string> argList;
                if (line[kOpenParen + 1] == ')')
                {
                    argList = doArguments("");
                }
                else
                {
                    string asargs;
                    if (kCloseParen != -1)
                    {
                        asargs = line.Substring(kOpenParen + 1, (kCloseParen - kOpenParen) - 1);
                    }
                    else
                    {
                        asargs = line.Substring(kOpenParen + 1);
                    }
                    argList = doArguments(asargs);
                    line = RewriteArgs(line, asargs, argList);
                }

                s.Arguments = argList;

                int kFunc = line.IndexOf("function") + 8;
                string delim = " \t;";
                char[] anyOf = delim.ToCharArray();

                int index = (colon == -1) ? kCloseParen : colon;
                kCloseParen = line.IndexOf(')');
                int ws = -1;
                if (kCloseParen != -1)
                {
                    colon = line.IndexOf(":", kCloseParen);
                    ws = line.IndexOfAny(anyOf, kCloseParen);
                }

                _fname = line.Substring(kFunc, kOpenParen - kFunc).Trim();
                if (line.Contains("set"))
                {
                    line = line.Replace("set ", "");
                    _fname = _fname.Replace("set ", "");
                    _setter = true;
                    s.Accessor = Symbol.SETTER;
                }
                else if (line.Contains("get"))
                {
                    line = line.Replace("get ", "");
                    _fname = _fname.Replace("get ", "");
                    _getter = true;
                    s.Accessor = Symbol.GETTER;
                }

                s.Name = _fname;

                string rettype = "";

                kCloseParen = line.IndexOf(')');
                colon = -1;
                if (kCloseParen != -1)
                {
                    colon = line.IndexOf(":", kCloseParen);
                    ws = line.IndexOfAny(anyOf, kCloseParen);
                }

                if (colon != -1)
                {
                    int length = ws - colon;
                    if (ws > 0)
                    {
                        rettype = line.Substring(colon + 1, length);
                    }
                    else
                    {
                        rettype = line.Substring(colon + 1);
                    }
                    line = line.Replace(rettype, "");
                    line = line.Replace(":", " ");
                }

                s.ReturnType = rettype;

                if (_setter)
                {
                    // TODO: Use a command line switch to control prefix
                    line = line.Replace("function ", rettype + _className + "::set_");
                }
                else if (_getter)
                {
                    // TODO: Use a command line switch to control prefix
                    line = line.Replace("function ", rettype + _className + "::get_");
                }
                else
                {
                    line = line.Replace("function ", rettype + _className + "::");
                }

                string[] tokens = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string token in tokens)
                {
                    if (token == "public")
                    {
                        line = line.Replace("public ", "");
                        line = line.Trim();
                        if (_fname == _className)
                        {
                            _meta.Constructor = _fname;
                            _addDTOR = true;
                        }
                        else
                        {
                            _meta.PublicMember = _fname;
                        }

                        s.Scope = Symbol.PUBLIC;
                    }
                    else if (token == "protected")
                    {
                        line = line.Replace("protected ", "");
                        line = line.Trim();
                        _meta.ProtectedMember = _fname;

                        s.Scope = Symbol.PROTECTED;
                    }
                    else if (token == "private")
                    {
                        line = line.Replace("private ", "");
                        line = line.Trim();
                        _meta.PrivateMember = _fname;

                        s.Scope = Symbol.PRIVATE;
                    }
                    else if (token == "override")
                    {
                        line = line.Replace("override ", "virtual ");
                        line = line.Trim();
                        _meta.VirtualMember = _fname;

                        s.Virtual = true;
                    }
                }

                _meta.Source = line;

                _symtab.Add(s);
            }
            else
            {
                _meta.Source = line;
            }
            return line;
        }