Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            string path       = args[0];
            string outputpath = args[1] + ".py";

            DebugDK.StartStopwatch("main");
            DebugDK.SetLog(false);

            DebugDK.StartStopwatch("lexer");
            Lexer lexer = new Lexer(new FileHandler(path));

            lexer.LexAllLines();
            DebugDK.StopStopwatch("lexer");

            DebugDK.StartStopwatch("parser");

            Parser parser = new Parser(lexer);

            parser.ParseAllLines();
            parser.GiveStructInheritance();
            DebugDK.StopStopwatch("parser");
            //parser.PrintAllData();



            DebugDK.StartStopwatch("exporter");
            Exporter exporter = Exporter.FromParser(parser, outputpath);

            exporter.Export();
            DebugDK.StopStopwatch("exporter");

            DebugDK.StopStopwatch("main");
        }
Ejemplo n.º 2
0
        private void DetectField()
        {
            bool hasTypeDecl  = Token.IncludesType(GetCurrentLine(), TokenTypes.TypeDecl);
            bool hasSemiColon = Token.IncludesType(GetCurrentLine(), TokenTypes.SemiColon);

            if (hasTypeDecl && hasSemiColon)
            {
                DebugDK.Log("Detected a field in the line : '" + Token.SmashTokens(GetCurrentLine(), "") + "'");

                int    typeDeclIndex = Token.IndexOfType(GetCurrentLine(), TokenTypes.TypeDecl);
                string name          = Token.SmashTokens(Token.GetRange(GetCurrentLine(), 0, typeDeclIndex), "");

                int    semiColonIndex = Token.IndexOfType(GetCurrentLine(), TokenTypes.SemiColon);
                string type           = Token.SmashTokens(Token.GetRange(GetCurrentLine(), typeDeclIndex + 1, semiColonIndex), "");

                PField field;
                field.name = RemoveWhitespace(name);
                field.type = GetFromHashtable(RemoveWhitespace(type), ref defs);
                field.tags = GetTags();

                if (inStruct)
                {
                    fieldBuffer.Add(field);
                }

                DebugDK.Log("Extracted from line, fieldname : " + field.name + ", fieldtype: " + field.type + ", tags : " + ", adding field to fieldBuffer : " + inStruct);
            }
        }
Ejemplo n.º 3
0
        public Lexer(FileHandler fh)
        {
            DebugDK.SetPrefix("Lexer");
            DebugDK.Log("Creating lexer. \n[Lexer] Loading file.");

            fileHandler = fh;
            fh.LoadFile();

            DebugDK.Log("Creating Hashtable");
            InitHastable();
        }
Ejemplo n.º 4
0
        public void GiveStructInheritance()
        {
            ArrayList newStructs = new ArrayList();

            DebugDK.Log("Giving each struct its inheritant fields.");

            DebugDK.Log("Looping to all the structs.");
            for (int i = 0; i < structs.Count; i++)
            {
                PStruct currentStruct = (PStruct)structs[i];

                DebugDK.Log("Checking if this struct has inheritance..., struct is : " + currentStruct.name + " |" + i);
                if (currentStruct.inheritance != "")
                {
                    DebugDK.Log("Current struct has inheritance : " + currentStruct.inheritance);
                    DebugDK.Log("Looping through all the structs to find a match...");

                    // NOTE: change removebeginwhitespace function at the inheritance detection if problems with finding.
                    foreach (PStruct pStruct in structs)
                    {
                        DebugDK.Log("     Looking for match with struct " + pStruct.name);
                        if (pStruct.name == currentStruct.inheritance)
                        {
                            DebugDK.Log("     Match found. Adding fields.");
                            ArrayList newField = new ArrayList();

                            foreach (PField p in pStruct.fields)
                            {
                                newField.Add(p);
                            }
                            foreach (PField p in currentStruct.fields)
                            {
                                newField.Add(p);
                            }

                            currentStruct.fields = (PField[])newField.ToArray(typeof(PField));
                        }
                    }
                }

                newStructs.Add(currentStruct);
            }

            structs = newStructs;


            DebugDK.Log("Struct inheritance is done.");
        }
Ejemplo n.º 5
0
        public void ParseAllLines()
        {
            DebugDK.Log("Starting parser");

            while (true)
            {
                if (currentLine >= lexer.fileTokens.Length)
                {
                    break;
                }

                ParseCurrentLine();

                //Debug.Log("Parsing next line, current state is : \n      " + " tagBuffer : " + decoratorBuffer + ", inStruct : " + inStruct + ", currentLine : " + currentLine);
            }

            DebugDK.Log("Parser is done.");
        }
Ejemplo n.º 6
0
        private void DetectDecorator()
        {
            if (decoratorBuffer != "")
            {
                decoratorBuffer = "";
            }

            bool hasDeco = Token.IncludesType(GetCurrentLine(), TokenTypes.Tag);

            if (hasDeco)
            {
                DebugDK.Log("Detected a tag in the line : '" + Token.SmashTokens(GetCurrentLine(), "") + "'");

                decoratorBuffer = Token.SmashTokens(Token.GetRange(GetCurrentLine(), 1, GetCurrentLine().Length), "");

                DebugDK.Log("New tag found : '" + decoratorBuffer);
            }
        }
Ejemplo n.º 7
0
        private void DetectEndOfScope()
        {
            bool hasCloseCurly = Token.IncludesType(GetCurrentLine(), TokenTypes.CloseCurly);

            if (hasCloseCurly)
            {
                DebugDK.Log("Detected an end of scope in line : '" + Token.SmashTokens(GetCurrentLine(), "") + "'");
                DebugDK.Log("Adding a struct : " + inStruct);
                if (inStruct)
                {
                    DebugDK.Log("Adding the fieldBuffer to currentStruct.");
                    activeStruct.fields = (PField[])fieldBuffer.ToArray(typeof(PField)); // adding the fields
                    DebugDK.Log("Adding the currentStruct to structs.");
                    structs.Add(activeStruct);
                }

                inStruct = false;
            }
        }
Ejemplo n.º 8
0
        public void PrintAllData()
        {
            if (DebugDK.log)
            {
                DebugDK.Log("\nWriting out all of the resulting parser data (PStructs) : ");

                foreach (PStruct ps in structs)
                {
                    DebugDK.Log(PStruct.ToString(ps));
                }

                DebugDK.Log("Definitions : ");
                foreach (DictionaryEntry e in defs)
                {
                    DebugDK.Log(e.Key + " : " + defs[e.Key]);
                }
                DebugDK.Log("Flags : ");
                foreach (DictionaryEntry e in flags)
                {
                    DebugDK.Log(e.Key + " : " + flags[e.Key]);
                }
            }
        }
Ejemplo n.º 9
0
        private void DetectStruct()
        {
            bool hasStruct      = Token.IncludesType(GetCurrentLine(), TokenTypes.Struct);
            bool noSemiColon    = !Token.IncludesType(GetCurrentLine(), TokenTypes.SemiColon);
            bool hasInheritance = Token.IncludesType(GetCurrentLine(), TokenTypes.Inheritance);

            if ((hasStruct && noSemiColon) || hasInheritance)
            {
                DebugDK.Log("Detected a struct in the line : '" + Token.SmashTokens(GetCurrentLine(), "") + "'");

                inStruct = true;

                string inheritance = "";

                int start = Token.IndexOfType(GetCurrentLine(), TokenTypes.Struct) + 1;
                int stop;

                if (hasInheritance)
                {
                    stop        = Token.IndexOfType(GetCurrentLine(), TokenTypes.Inheritance);
                    inheritance = Token.SmashTokens(Token.GetRange(GetCurrentLine(), stop + 1, GetCurrentLine().Length), "");
                }
                else
                {
                    stop = GetCurrentLine().Length;
                }

                string name = Token.SmashTokens(Token.GetRange(GetCurrentLine(), start, stop), "");

                activeStruct.name        = RemoveWhitespace(name);
                activeStruct.inheritance = RemoveWhitespace(inheritance);
                activeStruct.tags        = GetTags();
                fieldBuffer = new ArrayList();        // reset fields

                DebugDK.Log("Extracted from line, structname : " + activeStruct.name + ", inheritance : " + activeStruct.inheritance + ", tags : ");
            }
        }
Ejemplo n.º 10
0
        public void LexAllLines()
        {
            ArrayList tokens = new ArrayList();

            DebugDK.Print("Reading and extracting tokens from file : " + fileHandler.path);
            DebugDK.Log("Lexing all lines.");

            while (true)
            {
                if (currentLine >= fileHandler.fileLines.Length)
                {
                    break;
                }

                tokens.Add(Token.RemoveBeginWhiteSpace(LexCurrentLine()));
                currentLine++;

                //Debug.Log("Lexing next line, current state is : ");
                //Debug.Log("     currentLine : " + currentLine + ", lineIndex : " + lineIndex);
            }

            fileTokens = (Token[][])tokens.ToArray(typeof(Token[]));
            DebugDK.Log("Lexer is done.");
        }
Ejemplo n.º 11
0
        public PTag[] ExtractTags(string s, bool addDefault)
        {
            ArrayList result = new ArrayList();

            DebugDK.Log("extracting tags from " + s);

            string currentTagName = "";
            PTag   currentTag;

            string argString   = "";
            bool   inArguments = false;

            string[] tagArgs = { };

            for (int i = 0; i < s.Length; i++)
            {
                char current = s[i];

                {                            // argument checking
                    if (current.Equals('(')) // entering
                    {
                        inArguments = true;
                    }

                    if (inArguments)            // adding
                    {
                        argString += current;
                    }

                    if (current.Equals(')'))    // extracting & reset
                    {
                        inArguments = false;
                        tagArgs     = ExtractArguments(argString, false);

                        DebugDK.Log("found arguments : ");
                        //Token.PrintStringArr(ref tagArgs);

                        argString = "";
                    }
                }

                if (!inArguments)
                {
                    if (!current.Equals(')') && !current.Equals(','))
                    {
                        currentTagName += current;
                    }

                    if ((current.Equals(',') || i == s.Length - 1))
                    {
                        DebugDK.Log("moving on to next tag, current name is " + currentTagName);

                        currentTag.name      = RemoveWhitespace(currentTagName);
                        currentTag.arguments = tagArgs;

                        result.Add(currentTag);

                        {
                            currentTagName = "";
                            string[] temp = { };
                            tagArgs = temp;
                        }
                    }
                }
            }


            return((PTag[])result.ToArray(typeof(PTag)));
        }
Ejemplo n.º 12
0
 public Parser(Lexer lexer)
 {
     this.lexer = lexer;
     DebugDK.SetPrefix("Parser");
 }