Пример #1
0
        private static FunctionDB ParseScriptDump(string data)
        {
            FunctionDB parsedDB = new FunctionDB();
            //Parse the outputdata

            string[] Text = data.Split('\n');

            Parsing_E Parsing = Parsing_E.None;

            Function currentFunction = null;
            ConstantGroup currentConstants = null;

            string previousLine = "";

            foreach (string line in Text)
            {
                //remove all the [    Vscript   ]: text
                string l = line.Remove(0, line.IndexOf(':') + 1);
                l = l.Trim();

                //Three - means description, and the start of a new function definition
                if (l.StartsWith("---"))
                {
                    if(Parsing == Parsing_E.Enum)
                    {
                        //Commit the enum
                        //commit the changes
                        parsedDB.Constants.Add(currentConstants);

                        Console.WriteLine("Parsed " + currentConstants.EnumName);

                        currentConstants = null;

                        Parsing = Parsing_E.None;
                    }

                    if (Parsing != Parsing_E.None)
                    {
                        throw new Exception("Error: Found a new definition while already writing a function");
                    }

                    l = l.Replace("---", "").Trim();

                    if (l.StartsWith("[["))
                    {
                        currentFunction = new Function();

                        //Remove the comment lines
                        l = l.Replace("[[", "");
                        l = l.Replace(" ]]", "").Trim();

                        if (l.Contains(' ')) l = l.Substring(l.IndexOf(' ') + 1).Trim(); //If there is a space, that means we have a description.  Remove the function name
                                                                                         //from it and just use it
                        else l = ""; //If the above fails, the description only contains the function name.  Ignore it.

                        //Add this line as the current functions description
                        currentFunction.FunctionDescription = l;

                        Parsing = Parsing_E.Function;
                    }
                    else if(l.StartsWith("Enum"))
                    {
                        currentConstants = new ConstantGroup();

                        l = l.Replace("Enum", "").Trim();
                        currentConstants.EnumName = l;

                        Parsing = Parsing_E.Enum;
                    }

                }
                else if (Parsing == Parsing_E.Function && l.StartsWith("--"))
                {
                    if (currentFunction == null)
                    {
                        throw new Exception("Error: Parse a function when no function created");
                    }

                    //Possible cases
                    //@return type - Return value
                    //@param Name Type - Param name and type.  Can be multiple

                    l = l.Replace("--", "").Trim(); //Remove the comment stuff

                    if (l.StartsWith("@return"))
                    {
                        currentFunction.ReturnType = l.Replace("@return", "").Trim();
                    }
                    if (l.StartsWith("@param"))
                    {
                        string paramdat = l.Replace("@param", "").Trim();
                        string[] d = paramdat.Split(' ');
                        Param p = new Param();
                        p.Name = d[0];
                        p.Type = d[1];

                        currentFunction.Params.Add(p);
                    }

                }
                else if (Parsing == Parsing_E.Function && l.StartsWith("function"))
                {
                    if (currentFunction == null)
                    {
                        throw new Exception("Error: Trying to add on to a function without a function created");

                    }

                    //Pattern: function [ClassName:]FunctionName( param ...) end

                    //We want to pull out a class name and a function name name out of this

                    currentFunction.Example = l;

                    l = l.Replace("function", "").Trim();

                    if (l.Contains(":"))
                    {
                        //We have a classname
                        l = l.Substring(0, l.IndexOf("("));
                        var s = l.Split(':');
                        currentFunction.Class = s[0];
                        currentFunction.FunctionName = s[1];

                        //Check to see if the class exists
                        ClassType c = parsedDB.Classes.FirstOrDefault(x => x.ClassName == currentFunction.Class);
                        if (c == null)
                        {
                            //Create the class if it doesn't exist
                            c = new ClassType();
                            c.ClassName = currentFunction.Class;
                            parsedDB.Classes.Add(c);
                        }
                    }
                    else
                    {
                        l = l.Substring(0, l.IndexOf("("));
                        currentFunction.FunctionName = l;
                        currentFunction.Class = "Global";
                    }

                }
                else if(Parsing == Parsing_E.Enum && !string.IsNullOrEmpty(l)) //Modifer entry
                {
                    if(currentConstants == null)
                    {
                        throw new Exception("Tried to parse constant but group was null");
                    }

                    string[] Words = l.Split(' ');
                    ConstantEntry ce = new ConstantEntry()
                    {
                        Name = Words[0],
                        Value = Words[2]
                    };
                    if (l.Contains("--"))
                    {
                        string comment = l.Substring(l.IndexOf("--") + 2).Trim();
                        ce.Description = comment;
                    }

                    currentConstants.Entries.Add(ce);

                }
                else if (string.IsNullOrEmpty(l))
                {
                    if(previousLine == line && Parsing == Parsing_E.None)
                    {
                        continue; //Skip multiple blank lines
                    }
                    if (Parsing == Parsing_E.None)
                    {
                        throw new Exception("Tried to commit something when nothing was being worked on");
                    }

                    if(Parsing == Parsing_E.Function)
                    {
                        //Commit the changes
                        parsedDB.Functions.Add(currentFunction);

                        Console.WriteLine("Parsed " + currentFunction.GetQualifiedName());

                        currentFunction = null;
                    }
                    if(Parsing == Parsing_E.Enum)
                    {
                        //commit the changes
                        parsedDB.Constants.Add(currentConstants);

                        Console.WriteLine("Parsed " + currentConstants.EnumName);

                        currentConstants = null;
                    }

                    Parsing = Parsing_E.None;
                }

                previousLine = line;
            }

            return parsedDB;
        }
Пример #2
0
        public object Clone()
        {
            ConstantGroup cg = new ConstantGroup()
            {
                EnumName = this.EnumName
            };

            foreach(ConstantEntry entry in Entries)
            {
                cg.Entries.Add(entry.Clone() as ConstantEntry);
            }

            return cg;
        }