コード例 #1
0
        /// Function - FunctionLength
        /// <summary>
        /// gets the function line and buffer returns the function length.
        /// </summary>
        /// <param name="sr"> Buffer type MyStream.</param>
        /// <param name="codeLine"> Code line type string</param>
        /// <returns> returns the function length type int.</returns>
        public static int FunctionLength(MyStream sr, string codeLine)
        {
            int   count   = 0;
            uint  curPos  = sr.Pos;
            Stack myStack = new Stack();

            codeLine = sr.ReadLine();
            myStack.Push(codeLine);
            bool found = false;

            while ((codeLine != null && myStack.Count > 0))
            {
                count++;
                codeLine = sr.ReadLine();
                if (codeLine.IndexOf("{") != NOT_FOUND_STRING)
                {
                    myStack.Push(codeLine);
                }
                if (codeLine.IndexOf("}") != NOT_FOUND_STRING)
                {
                    myStack.Pop();
                }
            }
            if (myStack.Count == 0)
            {
                found = true;
            }
            count = count - 1;
            myStack.Clear();
            //returns the buffer to the start of the function.
            sr.Seek(curPos);
            return(count);
        }
コード例 #2
0
        /// Function - skipDocumentation
        /// <summary>
        /// skips the documentation in c file in a buffer.
        /// </summary>
        /// <param name="sr"> buffer type MyStream</param>
        /// <param name="codeLine"> string </param>
        /// <returns> returns the amount of rows the documentation was.</returns>
        public static int skipDocumentation(MyStream sr, string codeLine)
        {
            int  count = 0;
            uint pos   = sr.Pos;

            if (codeLine.IndexOf("//") != GeneralConsts.NOT_FOUND_STRING)
            {
                while ((codeLine.IndexOf("//") != GeneralConsts.NOT_FOUND_STRING))
                {
                    pos = sr.Pos;
                    count++;
                    codeLine = sr.ReadLine();
                }
                sr.Seek(pos);
                count--;
            }
            if (codeLine.IndexOf("/*") != GeneralConsts.NOT_FOUND_STRING)
            {
                while (!(codeLine.IndexOf("*/") != GeneralConsts.NOT_FOUND_STRING))
                {
                    count++;
                    codeLine = sr.ReadLine();
                }
            }
            return(count);
        }
コード例 #3
0
        /// Function - FunctionCode
        /// <summary>
        /// function gets a buffer and a refference to the code line and returns all the function code.
        /// in the scope.
        /// </summary>
        /// <param name="sr"> buffer type MyStream.</param>
        /// <param name="codeLine"> code line type string.</param>
        /// <returns> returns the whole function code. </returns>
        public static string FunctionCode(MyStream sr, ref string codeLine)
        {
            uint   curPos         = sr.Pos;
            int    functionLength = 0;
            string finalCode      = GeneralConsts.EMPTY_STRING;
            Stack  myStack        = new Stack();

            codeLine = sr.ReadLine();
            myStack.Push(codeLine);
            while ((codeLine != null && myStack.Count > 0))
            {
                codeLine   = sr.ReadLine();
                finalCode += codeLine + GeneralConsts.NEW_LINE;
                functionLength++;
                if (OpenBlockPattern.IsMatch(codeLine))
                {
                    myStack.Push(codeLine);
                }
                if (CloseBlockPattern.IsMatch(codeLine))
                {
                    myStack.Pop();
                }
            }
            myStack.Clear();
            return(finalCode);
        }
コード例 #4
0
        public static int FunctionLength(MyStream sr, string s)
        {
            int   count   = 0;
            uint  curPos  = sr.Pos;
            Stack myStack = new Stack();

            s = sr.ReadLine();
            myStack.Push(s);
            bool found = false;

            while ((s != null && myStack.Count > 0))
            {
                count++;
                s = sr.ReadLine();
                if (s.IndexOf("{") != -1)
                {
                    myStack.Push(s);
                }
                if (s.IndexOf("}") != -1)
                {
                    myStack.Pop();
                }
            }
            if (myStack.Count == 0)
            {
                found = true;
            }
            count = count - 1;
            myStack.Clear();
            sr.Seek(curPos);
            return(count);
        }
コード例 #5
0
        public static bool NextScopeLength(MyStream sr, ref string s, ref int count)
        {
            Stack myStack = new Stack();

            s = sr.ReadLine();
            myStack.Push(s);
            bool found = false;

            while ((s != null && myStack.Count > 0))
            {
                count++;
                s = sr.ReadLine();
                if (s.IndexOf("{") != -1)
                {
                    myStack.Push(s);
                }
                if (s.IndexOf("}") != -1)
                {
                    myStack.Pop();
                }
            }
            if (myStack.Count == 0)
            {
                found = true;
            }
            count = count - 1;
            myStack.Clear();
            return(found);
        }
コード例 #6
0
        public static string FunctionCode(MyStream sr, ref string s)
        {
            uint   curPos         = sr.Pos;
            int    functionLength = 0;
            string finalCode      = "";
            Stack  myStack        = new Stack();

            s = sr.ReadLine();
            myStack.Push(s);
            while ((s != null && myStack.Count > 0))
            {
                s          = sr.ReadLine();
                finalCode += s + "\n\r";
                functionLength++;
                if (OpenBlockPattern.IsMatch(s))
                {
                    myStack.Push(s);
                }
                if (CloseBlockPattern.IsMatch(s))
                {
                    myStack.Pop();
                }
                //here will be where i will store the function code.
            }
            myStack.Clear();
            return(finalCode);
        }
コード例 #7
0
        /// Function - findFunction
        /// <summary>
        /// find the next function in the code and returns the function line.
        /// </summary>
        /// <param name="sr"> Buffer type MyStream.</param>
        /// <param name="pattern"> Regex Pattern for the function.</param>
        /// <returns></returns>
        public static string findFunction(MyStream sr, Regex pattern)
        {
            string codeLine = sr.ReadLine();

            while ((!pattern.IsMatch(codeLine)) && ((codeLine = sr.ReadLine()) != null))
            {
                ;
            }
            return(codeLine);
        }
コード例 #8
0
        public static string findFunction(MyStream sr, Regex pattern)
        {
            bool   found = false;
            string s     = sr.ReadLine();

            while ((!pattern.IsMatch(s)) && ((s = sr.ReadLine()) != null))
            {
                ;
            }
            return(s);
        }
コード例 #9
0
        /// Function - SyntaxCheck
        /// <summary>
        /// that function uses the Function "ChecksInSyntaxCheck" if that is in a scope
        /// or outside a scope according to the situation.
        /// </summary>
        /// <param name="path"> The path of the c code type string.</param>
        /// <param name="keywords"> keywords type Hashtable that conatins the code keywords.</param>
        public static bool SyntaxCheck(string path, ArrayList globalVariable, Hashtable keywords, Dictionary <string, ArrayList> funcVariables, int threadNumber)
        {
            MyStream sr = null;

            try
            {
                sr = new MyStream(path, System.Text.Encoding.UTF8);
            }
            catch (Exception e)
            {
                Server.ConnectionServer.CloseConnection(threadNumber, FILE_NOT_FOUND, GeneralConsts.ERROR);
            }
            if (sr != null)
            {
                //in order to delete struct keywords when they come in a function at the end of the function.
                ArrayList parameters     = new ArrayList();
                ArrayList blocksAndNames = new ArrayList();
                ArrayList variables      = new ArrayList();
                //adds an ArrayList inside blocksAndNames ArrayList for the action outside the scopes.
                blocksAndNames.Add(new ArrayList());
                string codeLine;
                int    scopeLength  = 0;
                string lastFuncLine = "";
                while ((codeLine = sr.ReadLine()) != null && !CompileError)
                {
                    scopeLength = 0;
                    //handling the scopes.
                    if (OpenBlockPattern.IsMatch(codeLine))
                    {
                        NextScopeLength(sr, ref codeLine, ref scopeLength, true);
                        ChecksInSyntaxCheck(path, sr, codeLine, true, keywords, threadNumber, variables, globalVariable, blocksAndNames, parameters, scopeLength + 1);
                        parameters.Clear();
                    }
                    // if there is a function it saves its parameters.
                    else if (FunctionPatternInC.IsMatch(codeLine))
                    {
                        parameters.AddRange(GeneralRestApiServerMethods.FindParameters(codeLine));
                        if (lastFuncLine != "")
                        {
                            funcVariables.Add(lastFuncLine, new ArrayList(variables));
                            variables.Clear();
                        }
                        lastFuncLine = codeLine;
                    }
                    //handling outside the scopes.
                    else
                    {
                        ChecksInSyntaxCheck(path, sr, codeLine, false, keywords, threadNumber, variables, globalVariable, blocksAndNames);
                    }
                }
                if (lastFuncLine != "")
                {
                    funcVariables.Add(lastFuncLine, new ArrayList(variables));
                    variables.Clear();
                }
            }
            return(CompileError);
        }
コード例 #10
0
        //the checks that are being mad in syntax Check are being written here.
        public static void ChecksInSyntaxCheck(MyStream sr, ref string s, bool IsFunction, int functionLength = 1)
        {
            ArrayList tempStructInFunc = new ArrayList();
            string    temp;
            int       loopCount;

            char[]    cutChars = { '*', '&' };
            int       pos      = 0;
            bool      found;
            int       i, j;
            ArrayList results = new ArrayList();

            for (i = 0; i < functionLength; i++)
            {
                if (IsFunction)
                {
                    s = sr.ReadLine();
                }
                pos   = 0;
                found = true;
                if (StructPattern.IsMatch(s) || TypedefOneLine.IsMatch(s))
                {
                    results = AddStructNames(sr, s);
                    tempStructInFunc.Add(s);
                }
                if (VariableDecleration.IsMatch(s) && !(s.IndexOf("typedef") != -1))
                {
                    loopCount = KeywordsAmountOnVariableDeclaration(s);
                    for (j = 0; j < loopCount; j++)
                    {
                        found = found && CheckIfStringInHash(keywords, s.Substring(pos, s.Substring(pos, s.Length - pos).IndexOf(' ')).Trim(cutChars));
                        pos   = s.IndexOf(' ', pos + 1) + 1;
                    }
                    if (loopCount == 0)
                    {
                        found = found && CheckIfStringInHash(keywords, s.Substring(pos, s.Substring(pos, s.Length - pos).IndexOf(' ')).Trim(cutChars));
                    }
                    if (s.IndexOf("struct") != -1)
                    {
                        pos   = s.IndexOf("struct");
                        temp  = s.Substring(pos, s.IndexOf(" ", pos + 7) - pos);
                        found = CheckIfStringInHash(keywords, temp.Trim(cutChars));
                    }
                }
                if (!found)
                {
                    Console.WriteLine(s.Trim() + " is written wrong (bad keyword usage).");
                }
            }
            if (IsFunction)
            {
                for (i = 0; i < results.Count; i++)
                {
                    keywords.Remove(results[i]);
                }
            }
        }
コード例 #11
0
        /// Function - AddToArrayListFromFile
        /// <summary>
        /// Adds from a file the names that split by "split by" in the path "path"
        /// </summary>
        /// <param name="path"> path of the c code type string.</param>
        /// <param name="a"> ArrayList </param>
        /// <param name="splitBy"> What to split by type string.</param>
        public static void AddToArrayListFromFile(string path, ArrayList a, string splitBy)
        {
            MyStream sr = new MyStream(path, System.Text.Encoding.UTF8);
            string   s  = sr.ReadLine();

            string [] temp = Regex.Split(s, splitBy);
            foreach (string i in temp)
            {
                a.Add(i);
            }
            sr.Close();
        }
コード例 #12
0
        /// Function - NextScopeLength
        /// <summary>
        ///  in order to find function length or struct length or "next scope" this function can be used.
        /// </summary>
        /// <param name="sr"> type MyStream buffer for the file. </param>
        /// <param name="codeLine"> refference of the current code line type string. </param>
        /// <param name="count"> refference of the length of the scope type int. </param>
        /// <param name="Seek"> bool type parameter for returning the buffer to where it started from
        ///                     or to keep the buffer where it is after the scope ends. </param>
        /// <returns></returns>
        public static bool NextScopeLength(MyStream sr, ref string codeLine, ref int count, bool Seek)
        {
            //stack to count the blocks.
            Stack myStack = new Stack();
            //saving the current position of the buffer.
            uint   curPos    = sr.Pos;
            string ScopeName = new string(codeLine.ToCharArray());

            codeLine = sr.ReadLine();
            myStack.Push(codeLine);
            bool found = false;

            while ((codeLine != null && myStack.Count > 0))
            {
                count++;
                codeLine = sr.ReadLine();
                if (codeLine.IndexOf("{") != GeneralConsts.NOT_FOUND_STRING)
                {
                    myStack.Push(codeLine);
                }
                if (codeLine.IndexOf("}") != GeneralConsts.NOT_FOUND_STRING)
                {
                    myStack.Pop();
                }
            }
            if (myStack.Count == 0)
            {
                found = true;
            }
            count = count - 1;
            //checking the bool for seeking.
            if (Seek)
            {
                sr.Seek(curPos);
                codeLine = ScopeName;
            }
            myStack.Clear();
            return(found);
        }
コード例 #13
0
        public static string findDocumentation(MyStream sr, uint documentation, string firstLineDocumentation, uint functionPos)
        {
            string documetationString = firstLineDocumentation + "\n\r";

            sr.Seek(documentation);
            string s = sr.ReadLine();

            documetationString += s + "\n\r";
            if (!(firstLineDocumentation.IndexOf("//") != -1) && !(firstLineDocumentation.IndexOf("/*") != -1))
            {
                documetationString = "No documentation for this function";
            }
            if ((firstLineDocumentation.IndexOf("/*") != -1))
            {
                while (!(s.IndexOf("*/") != -1))
                {
                    s = sr.ReadLine();
                    documetationString += s + "\n\r";
                }
            }
            sr.Seek(functionPos);
            return(documetationString);
        }
コード例 #14
0
        public static void AddToHashFromFile(string path, Hashtable a, string splitBy)
        {
            MyStream sr   = new MyStream(path, System.Text.Encoding.UTF8);
            string   temp = sr.ReadLine();

            string[]    tempArr = Regex.Split(temp, splitBy);
            ICollection keys    = keywords.Keys;

            for (int i = 0; i < tempArr.Length; i++)
            {
                a.Add(CreateMD5(tempArr[i]), tempArr[i]);
            }
            sr.Close();
        }
コード例 #15
0
        /// Function - FindDocumentation
        /// <summary>
        /// Finds the documentation of a function.
        /// </summary>
        /// <param name="sr"> Buffer type MyStream.</param>
        /// <param name="documentation"> Position of the first documentation line type uint.</param>
        /// <param name="firstLineDocumentation"> First documentation line type string.</param>
        /// <param name="functionPos"> Position of the function type uint.</param>
        /// <returns> returns the documentation of the function included.</returns>
        public static string FindDocumentation(MyStream sr, uint documentation, string firstLineDocumentation, uint functionPos)
        {
            string documetationString = firstLineDocumentation + GeneralConsts.NEW_LINE;

            sr.Seek(documentation);
            string codeLine = sr.ReadLine();

            documetationString += codeLine + GeneralConsts.NEW_LINE;
            if (!(firstLineDocumentation.IndexOf("//") != NOT_FOUND_STRING) && !(firstLineDocumentation.IndexOf("/*") != NOT_FOUND_STRING))
            {
                documetationString = GeneralConsts.EMPTY_STRING;
            }
            if ((firstLineDocumentation.IndexOf("/*") != NOT_FOUND_STRING))
            {
                while (!(codeLine.IndexOf("*/") != NOT_FOUND_STRING))
                {
                    codeLine            = sr.ReadLine();
                    documetationString += codeLine + GeneralConsts.NEW_LINE;
                }
            }
            sr.Seek(functionPos);
            return(documetationString);
        }
コード例 #16
0
        /// Function - SearchPattern
        /// <summary>
        ///
        /// </summary>
        /// <param name="Pattern"> The pattern that it search type Regex.</param>
        /// <param name="returnSize"> size of the return code.</param>
        /// <param name="filePath"> the path of the file type string.</param>
        /// <returns> An array of strings that contains all of the code that matches the patterns.</returns>
        public static string [] SearchPattern(Regex Pattern, string returnSize, string filePath)
        {
            ArrayList results        = new ArrayList();
            MyStream  sr             = new MyStream(filePath, System.Text.Encoding.UTF8);
            uint      pos            = sr.Pos;
            Stack     s              = new Stack();
            string    blockLine      = "";
            bool      modelStopWhile = false;
            string    codeLine;

            while ((codeLine = sr.ReadLine()) != null && !modelStopWhile)
            {
                if (codeLine.IndexOf("{") != GeneralConsts.NOT_FOUND_STRING)
                {
                    pos       = sr.Pos;
                    blockLine = codeLine;
                    s.Push(codeLine);
                }
                if (codeLine.IndexOf("}") != GeneralConsts.NOT_FOUND_STRING)
                {
                    s.Pop();
                }
                if (Pattern.IsMatch(codeLine))
                {
                    if (returnSize == "model")
                    {
                        modelStopWhile = true;
                        sr.Seek(0);
                        results.Add(sr.ReadToEnd());
                    }
                    else if (returnSize == "scope")
                    {
                        if (s.Count == 0)
                        {
                            pos = 0;
                        }
                        if (!results.Contains(ReadAllScope(sr, pos, blockLine)))
                        {
                            results.Add(ReadAllScope(sr, pos, blockLine));
                        }
                    }
                    else if (returnSize == "line")
                    {
                        results.Add(codeLine);
                    }
                }
            }
            string[] finalResult = (string[])results.ToArray(typeof(string));
            return(finalResult);
        }
コード例 #17
0
        public static void CheckUninitializeVariableType(string path)
        {
            MyStream sr = new MyStream(path, System.Text.Encoding.UTF8);
            string   s;
            bool     endLoop = false;

            string[] structNames;
            while ((s = sr.ReadLine()) != null)
            {
                if (StructPattern.IsMatch(s) || TypedefOneLine.IsMatch(s))
                {
                    AddStructNames(sr, s);
                }
            }
            sr.Close();
        }
コード例 #18
0
        /// Function - TakePatternFromFile
        /// <summary>
        /// Search for the pattern in the patterns file and returns it.
        /// </summary>
        /// <param name="pattern"> name of the pattern the tool requested.</param>
        /// <returns> pattern type string.</returns>
        public static string TakePatternFromFile(string pattern)
        {
            bool     found = false;
            MyStream sr    = new MyStream(patternFilePath, System.Text.Encoding.UTF8);
            string   line;
            string   final_pattern = GeneralConsts.EMPTY_STRING;

            string[] split = { ",,," };
            while ((line = sr.ReadLine()) != null && !found)
            {
                if (line.Split(split, System.StringSplitOptions.None)[0] == pattern)
                {
                    final_pattern = line.Split(split, System.StringSplitOptions.None)[1];
                    found         = true;
                }
            }
            return(final_pattern);
        }
コード例 #19
0
        public static void SyntaxCheck(string path)
        {
            MyStream sr = new MyStream(path, System.Text.Encoding.UTF8);
            //in order to delete struct keywords when they come in a function at the end of the function.
            string s;
            int    function_Length = 0;

            while ((s = sr.ReadLine()) != null)
            {
                if (FunctionPatternInC.IsMatch(s))
                {
                    function_Length = FunctionLength(sr, s);
                    ChecksInSyntaxCheck(sr, ref s, true, function_Length);
                }
                else
                {
                    ChecksInSyntaxCheck(sr, ref s, false);
                }
            }
        }
コード例 #20
0
        /// Function - AddToHashFromFile
        /// <summary>
        /// Adds from a file splited by "splitBy" parameter to the Hashtable fromt he path.
        /// </summary>
        /// <param name="path"> The path for the file.</param>
        /// <param name="a"> Hashtable to store the keywords.</param>
        /// <param name="splitBy"> String that the file needs to split by.</param>
        public static void AddToHashFromFile(string path, Hashtable a, string splitBy)
        {
            MyStream sr = null;

            try
            {
                sr = new MyStream(path, System.Text.Encoding.UTF8);
            }
            catch (Exception e)
            {
                MainProgram.AddToLogString(path, e.ToString());
            }
            string line = sr.ReadLine();

            string[]    keysArr = Regex.Split(line, splitBy);
            ICollection keys    = a.Keys;

            for (int i = 0; i < keysArr.Length; i++)
            {
                a.Add(CreateMD5(keysArr[i]), keysArr[i]);
            }
            sr.Close();
        }
コード例 #21
0
        /// Fubnction - CreateFunctionsJsonFile
        /// <summary>
        /// function is creating the json file for the "Function" GET request.
        /// </summary>
        /// <param name="path"> path of the file that is being checked.</param>
        /// <param name="pattern"></param>
        /// <param name="variables"> Dictionary that every key on him is the function LINE and every value is an arrayList
        /// type "ParameterType" of all of his variables.
        /// </param>
        /// <param name="final_json"> the final big json.</param>
        static void CreateFunctionsJsonFile(string path, Regex pattern, Dictionary <string, ArrayList> variables, Dictionary <string, Dictionary <string, Object> > final_json)
        {
            string codeLine = GeneralConsts.EMPTY_STRING;
            string fName;

            string[] temp;
            string   returnType             = GeneralConsts.EMPTY_STRING;
            bool     exitFlag               = false;
            string   firstLineDocumentation = GeneralConsts.EMPTY_STRING;
            uint     curPos;
            Object   tempDict    = new Dictionary <string, FunctionInfoJson>();
            MyStream sr          = new MyStream(path, System.Text.Encoding.UTF8);
            uint     documentPos = sr.Pos;

            while (codeLine != null)
            {
                //saves the last documentation.
                while (!exitFlag && !FunctionPatternInC.IsMatch(codeLine))
                {
                    if (codeLine != null)
                    {
                        codeLine = sr.ReadLine();
                    }
                    firstLineDocumentation = GeneralConsts.EMPTY_STRING;
                    if (codeLine == null)
                    {
                        exitFlag = true;
                        break;
                    }
                    if (codeLine.IndexOf("//") != NOT_FOUND_STRING)
                    {
                        documentPos            = sr.Pos;
                        firstLineDocumentation = codeLine;
                    }
                    while ((codeLine.IndexOf("//") != NOT_FOUND_STRING))
                    {
                        if (codeLine != null)
                        {
                            codeLine = sr.ReadLine();
                        }
                    }
                    if ((codeLine.IndexOf("/*") != NOT_FOUND_STRING))
                    {
                        documentPos            = sr.Pos;
                        firstLineDocumentation = codeLine;
                        while (!(codeLine.IndexOf("*/") != NOT_FOUND_STRING))
                        {
                            if (codeLine != null)
                            {
                                codeLine = sr.ReadLine();
                            }
                        }
                        if ((codeLine.IndexOf("*/") != NOT_FOUND_STRING))
                        {
                            if (codeLine != null)
                            {
                                codeLine = sr.ReadLine();
                            }
                        }
                    }
                    if (codeLine == null)
                    {
                        exitFlag = true;
                    }
                }
                if (codeLine == null)
                {
                    exitFlag = true;
                }
                if (!exitFlag)
                {
                    fName = codeLine;
                    if (fName != null)
                    {
                        temp = Regex.Split(fName, @"\*|\s");
                        if (fName.IndexOf("static") != NOT_FOUND_STRING)
                        {
                            returnType = takeSecondNotNullString(temp);
                        }
                        else
                        {
                            returnType = temp[0];
                        }

                        returnType = returnType.Trim();
                        //enter function to where i store it.
                        Object tempStorage = new FunctionInfoJson();
                        GeneralCompilerFunctions.NextScopeLength(sr, ref codeLine, ref ((FunctionInfoJson)tempStorage).codeLength, true);
                        ((FunctionInfoJson)tempStorage).content    = FunctionCode(sr, ref codeLine);
                        ((FunctionInfoJson)tempStorage).parameters = FindParameters(fName);
                        ((FunctionInfoJson)tempStorage).returnType = returnType;
                        ((FunctionInfoJson)tempStorage).variables  = FindVariables(variables[fName]);
                        curPos = sr.Pos;
                        ((FunctionInfoJson)tempStorage).documentation = FindDocumentation(sr, documentPos, firstLineDocumentation, curPos);
                        ((Dictionary <string, FunctionInfoJson>)tempDict).Add(fName, (FunctionInfoJson)tempStorage);
                    }
                    else
                    {
                        exitFlag = true;
                    }
                }
                //add it to where i store the function code.
            }
            //Serialize.
            Dictionary <string, Object> tempOuterDict = new Dictionary <string, Object>();

            tempOuterDict.Add("function", tempDict);
            final_json.Add(path, tempOuterDict);
            sr.Close();
        }
コード例 #22
0
        //path to the code;
        /// Function - PreprocessorActions
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"> The path for the C code.</param>
        /// <param name="threadNumber"> thread number is a parameter to make sure when the main file is in.
        ///                             number 0 means its the main file any other number means it  is currently
        ///                             reads a file that is not the main. (Import).</param>
        /// <param name="keywords"> Hashtable to store the keywords.</param>
        /// <param name="includes"> Hashtable to store the includes.</param>
        /// <param name="defines"> Dictionary to store the defines . (key - new keyword, value - old Definition)</param>
        /// <param name="pathes"> Paths for all the places where the imports might be.</param>
        static void PreprocessorActions(string path, int threadNumber, Hashtable keywords, Hashtable includes, Dictionary <string, string> defines, string [] pathes)
        {
            bool     endLoop = false;
            MyStream sr      = null;

            //try to open the buffer.
            try
            {
                sr = new MyStream(path, System.Text.Encoding.UTF8);
            }
            catch (Exception e)
            {
                MainProgram.AddToLogString(path, String.Format("{0} Second exception caught.", e.ToString()));
                Server.ConnectionServer.CloseConnection(threadNumber, FILE_NOT_FOUND, GeneralConsts.ERROR);
                endLoop = true;
            }

            string codeLine;
            string firstNewVariableWord;
            string secondNewVariableWord;

            string[] newkeyWords;
            string   newKeyword;
            string   defineOriginalWord;

            while (!endLoop && (codeLine = sr.ReadLine()) != null)
            {
                if (DefineDecleration.IsMatch(codeLine))
                {
                    //getting both of the names in two variables.
                    firstNewVariableWord = codeLine.Substring(codeLine.IndexOf(' '), codeLine.Length - codeLine.IndexOf(' '));
                    firstNewVariableWord = firstNewVariableWord.Trim();
                    firstNewVariableWord = firstNewVariableWord.Substring(firstNewVariableWord.IndexOf(GeneralConsts.SPACEBAR), firstNewVariableWord.Length - firstNewVariableWord.IndexOf(GeneralConsts.SPACEBAR));
                    firstNewVariableWord = firstNewVariableWord.Trim(CharsToTrim);
                    //old definition.
                    defineOriginalWord = firstNewVariableWord;

                    if (firstNewVariableWord.IndexOf(GeneralConsts.SPACEBAR) != GeneralConsts.NOT_FOUND_STRING)
                    {
                        //checks if the definition exists.
                        if (keywords.ContainsKey(CreateMD5(firstNewVariableWord)))
                        {
                            //new keyword
                            //takes the part after the first space.
                            newKeyword = Regex.Split(codeLine, GeneralConsts.SPACEBAR)[1];
                            newKeyword = newKeyword.Trim();
                            //make sure that the keyword isn't already existed.
                            if (!keywords.ContainsKey(CreateMD5(newKeyword)))
                            {
                                //adds her if not
                                keywords.Add(CreateMD5(newKeyword), newKeyword);
                                //adds the new definition.
                                defines.Add(newKeyword, defineOriginalWord);
                                //types the dont mind the variable are being ingored. for an example static : so if
                                //the type for example is static and i define a new static definition it will add it to
                                //the ignoreVariablesType.
                                if (ignoreVarialbesType.Contains(defineOriginalWord))
                                {
                                    ignoreVarialbesType.Add(newKeyword);
                                }
                            }
                        }
                        else
                        {
                            //splits when there are 2 types that you define for an example "unsinged int"
                            //so what this section is doing is checking if both of the types exist.
                            newkeyWords           = Regex.Split(firstNewVariableWord, GeneralConsts.SPACEBAR);
                            secondNewVariableWord = newkeyWords[1];
                            firstNewVariableWord  = newkeyWords[0];
                            //checks both types.
                            if (CheckIfStringInHash(keywords, firstNewVariableWord) && CheckIfStringInHash(keywords, secondNewVariableWord))
                            {
                                newKeyword = Regex.Split(codeLine, GeneralConsts.SPACEBAR)[1];
                                newKeyword = newKeyword.Trim();
                                //creates the keywords if they dont exist.
                                if (!keywords.ContainsKey(CreateMD5(newKeyword)))
                                {
                                    keywords.Add(CreateMD5(newKeyword), newKeyword);
                                    defines.Add(newKeyword, defineOriginalWord);
                                    MainProgram.AddToLogString(path, "new Keywords :" + newkeyWords[0]);
                                }
                            }
                        }
                    }
                    else
                    {
                        //if there is only one type in the old definition.
                        if (CheckIfStringInHash(keywords, firstNewVariableWord))
                        {
                            newKeyword = Regex.Split(codeLine, GeneralConsts.SPACEBAR)[1];
                            newKeyword = newKeyword.Trim();
                            if (!keywords.ContainsKey(CreateMD5(newKeyword)))
                            {
                                keywords.Add(CreateMD5(newKeyword), newKeyword);
                                defines.Add(newKeyword, defineOriginalWord);
                                MainProgram.AddToLogString(path, "new : " + newKeyword);
                            }
                        }
                    }
                }
                //Handling almost the same patterns as the syntaxCheck function.
                if (StructPattern.IsMatch(codeLine) && threadNumber != 0)
                {
                    AddStructNames(sr, codeLine, keywords);
                }
                else if (TypedefOneLine.IsMatch(codeLine) && threadNumber != 0)
                {
                    AddStructNames(sr, codeLine, keywords);
                }
                //if the code line is an include it creates a thread and enters to the defines , structs and more to
                //the Hashtables and Dictionaries.
                else if (IncludeTrianglesPattern.IsMatch(codeLine) || IncludeRegularPattern.IsMatch(codeLine))
                {
                    string currentPath = GeneralConsts.EMPTY_STRING;
                    string result;
                    if (codeLine.IndexOf("<") != -1 && codeLine.IndexOf(">") != -1)
                    {
                        result = CutBetween2Strings(codeLine, "<", ">");
                    }
                    else
                    {
                        result = CutBetween2Strings(codeLine, "\"", "\"");
                    }
                    MainProgram.AddToLogString(path, result);
                    //only enters an include if it didnt already included him.
                    if (!includes.Contains(CreateMD5(result)))
                    {
                        includes.Add(CreateMD5(result), result);
                        Thread preprocessorThread;
                        //if the include includes a path inside of it.
                        if (result.IndexOf("\\") != -1)
                        {
                            //opens the thread (thread number +1).
                            preprocessorThread = new Thread(() => PreprocessorActions(result, threadNumber + 1, keywords, includes, defines, pathes));
                        }
                        //if it does not include an exact path.
                        else
                        {
                            //runs on the pathes that the import files might be in.
                            for (int i = 0; i < pathes.Length; i++)
                            {
                                //checks if the file exists in one of those folders.
                                if (File.Exists(pathes[i] + "\\" + result))
                                {
                                    currentPath = pathes[i];
                                    break;
                                }
                            }
                            //creats a thread.
                            preprocessorThread = new Thread(() => PreprocessorActions(currentPath + "\\" + result, threadNumber + 1, keywords, includes, defines, pathes));
                        }
                        preprocessorThread.Start();
                        preprocessorThread.Join(GeneralConsts.TIMEOUT_JOIN);
                        MainProgram.AddToLogString(path, "thread " + threadNumber + "stopped");
                    }
                }
            }
            if (sr != null)
            {
                sr.Close();
            }
        }
コード例 #23
0
        public static string findAllFunctionNamesAndCode(string path, Regex pattern)
        {
            string s = "";
            string fName;

            string[] temp;
            string   returnType = "";
            bool     exitFlag   = false;
            bool     found;
            string   firstLineDocumentation = "";
            uint     curPos;
            Dictionary <string, FunctionInfoJson> tempDict = new Dictionary <string, FunctionInfoJson>();
            MyStream sr          = new MyStream(path, System.Text.Encoding.UTF8);
            uint     documentPos = sr.Pos;

            while (s != null)
            {
                while (!exitFlag && !FunctionPatternInC.IsMatch(s))
                {
                    if (s != null)
                    {
                        s = sr.ReadLine();
                    }
                    firstLineDocumentation = "";
                    if (s == null)
                    {
                        exitFlag = true;
                    }
                    if (s.IndexOf("//") != -1)
                    {
                        documentPos            = sr.Pos;
                        firstLineDocumentation = s;
                    }
                    while ((s.IndexOf("//") != -1))
                    {
                        if (s != null)
                        {
                            s = sr.ReadLine();
                        }
                    }
                    if ((s.IndexOf("/*") != -1))
                    {
                        documentPos            = sr.Pos;
                        firstLineDocumentation = s;
                        while (!(s.IndexOf("*/") != -1))
                        {
                            if (s != null)
                            {
                                s = sr.ReadLine();
                            }
                        }
                        if ((s.IndexOf("*/") != -1))
                        {
                            if (s != null)
                            {
                                s = sr.ReadLine();
                            }
                        }
                    }
                    if (s == null)
                    {
                        exitFlag = true;
                    }
                }
                if (s == null)
                {
                    exitFlag = true;
                }
                if (!exitFlag)
                {
                    fName = s;
                    if (fName != null)
                    {
                        temp = Regex.Split(fName, @"\*|\s");
                        if (fName.IndexOf("static") != -1)
                        {
                            returnType = takeSecondNotNullString(temp);
                        }
                        else
                        {
                            returnType = temp[0];
                        }

                        returnType = returnType.Trim();
                        //enter function to where i store it.
                        FunctionInfoJson tempStorage = new FunctionInfoJson();
                        tempStorage.content    = FunctionCode(sr, ref s);
                        tempStorage.parameters = findParameters2(fName);
                        tempStorage.returnType = returnType;
                        curPos = sr.Pos;
                        tempStorage.documentation = findDocumentation(sr, documentPos, firstLineDocumentation, curPos);
                        tempDict.Add(fName, tempStorage);
                    }
                    else
                    {
                        exitFlag = true;
                    }
                }


                //add it to where i store the function code.
            }
            string finalJson = JsonConvert.SerializeObject(tempDict);

            sr.Close();
            return(finalJson);
        }
コード例 #24
0
        /// Function - ChecksInSyntaxCheck
        /// <summary>
        /// this function take cares of the whole syntax check of the program.
        /// it uses the functions mentioned in the documentations before.
        /// it take cares scopes and no scopes with the parameter IsScope type bool.
        /// </summary>
        /// <param name="path"> the path of the file.</param>
        /// <param name="globalVariables"> variables that all file have.</param>
        /// <param name="threadNumber"> the number of the current thread running.</param>
        /// <param name="variables"> all of the variables that the scope knows.</param>
        /// <param name="sr"> buffer type MyStream.</param>
        /// <param name="codeLine"> code line type string.</param>
        /// <param name="IsScope"> bool type IsScope.</param>
        /// <param name="keywords"> keywords type Hashtable that conatins the code keywords.</param>
        /// <param name="blocksAndNames"> blocksAndNames type ArrayList that conatins the code variables in the scope.</param>
        /// <param name="parameters"> parameters type ArrayList conatins the function parameters.</param>
        /// <param name="functionLength"> scopeLength type int default is 0 if the code line is outside any scopes.</param>
        static void ChecksInSyntaxCheck(string path, MyStream sr, string codeLine, bool IsScope, Hashtable keywords, int threadNumber, ArrayList variables, ArrayList globalVariables, ArrayList blocksAndNames, ArrayList parameters = null, int functionLength = 0)
        {
            //adds the parameters of the function to the current ArrayList of variables.
            if (parameters != null)
            {
                blocksAndNames.Add(new ArrayList());
                ((ArrayList)blocksAndNames[1]).AddRange(parameters);
            }
            if (StructPattern.IsMatch(codeLine))
            {
                //Add struct keywords to the keywords Hashtable.
                AddStructNames(sr, codeLine, keywords);
            }
            if (codeLine.Trim(GeneralConsts.TAB_SPACE).IndexOf("{") != GeneralConsts.NOT_FOUND_STRING)
            {
                codeLine = sr.ReadLine();
            }
            //how to convert to array list
            bool      keywordCheck        = true;
            bool      DifferentTypesCheck = true;
            int       pos = 0;
            int       i;
            ArrayList keywordResults = new ArrayList();

            for (i = 0; i < functionLength + 1; i++)
            {
                if (codeLine.Trim(GeneralConsts.TAB_SPACE) == GeneralConsts.EMPTY_STRING)
                {
                    codeLine = sr.ReadLine();
                }
                //take cares to all of those situations.
                if (StructPattern.IsMatch(codeLine) || TypedefOneLine.IsMatch(codeLine))
                {
                    keywordResults = AddStructNames(sr, codeLine, keywords);
                }
                if (VariableDecleration.IsMatch(codeLine) && !(codeLine.IndexOf("typedef") != GeneralConsts.NOT_FOUND_STRING))
                {
                    keywordCheck = VariableDeclarationHandler(ref codeLine, ref pos, keywords, threadNumber, variables, globalVariables, blocksAndNames, IsScope, sr);
                }
                else if (VariableEquation.IsMatch(codeLine))
                {
                    DifferentTypesCheck = VariableEquationHandler(sr, codeLine, blocksAndNames, threadNumber);
                }
                codeLine = codeLine.Trim();
                //checks if any of the error bools is on.
                if (!keywordCheck)
                {
                    string error = (codeLine + " keyword does not exist. row : " + sr.curRow);
                    try
                    {
                        MainProgram.AddToLogString(path, sr.curRow.ToString());
                    }
                    catch (Exception e)
                    {
                        MainProgram.AddToLogString(path, e.ToString());
                    }
                    MainProgram.AddToLogString(path, error);
                    Server.ConnectionServer.CloseConnection(threadNumber, error, GeneralConsts.ERROR);
                    CompileError = true;
                }
                if (!DifferentTypesCheck)
                {
                    string error = (codeLine + " types of both variables are different in row : " + sr.curRow);
                    Server.ConnectionServer.CloseConnection(threadNumber, error, GeneralConsts.ERROR);
                    CompileError = true;
                }
                pos = 0;
                //resets the error bools.
                keywordCheck = DifferentTypesCheck = true;
                if (codeLine.IndexOf("//") != GeneralConsts.NOT_FOUND_STRING || codeLine.IndexOf("/*") != GeneralConsts.NOT_FOUND_STRING)
                {
                    //skips documentation if needed.
                    i += skipDocumentation(sr, codeLine);
                }
                //adds a new ArrayList inside the keywordsAndNames ArrayList for the scope that has started.
                if (OpenBlockPattern.IsMatch(codeLine))
                {
                    blocksAndNames.Add(new ArrayList());
                }
                if (CloseBlockPattern.IsMatch(codeLine))
                {
                    try
                    {
                        //close the last scope that just closed.
                        blocksAndNames.RemoveAt(blocksAndNames.Count - 1);
                    }
                    catch (Exception e)
                    {
                        //bad scoping causes the function to remove from an ArrayList something while its already 0.
                        Server.ConnectionServer.CloseConnection(threadNumber, "bad scoping in function in row " + sr.curRow, GeneralConsts.ERROR);
                        CompileError = true;
                    }
                }
                //if the code line is in a scope or if its not the last line in the scope continute to the next line.
                if (IsScope && i != functionLength)
                {
                    codeLine = sr.ReadLine();
                }
            }
            //if that was a scope it removes all the keywords of the scope.
            if (IsScope)
            {
                for (i = 0; i < keywordResults.Count; i++)
                {
                    keywords.Remove(keywordResults[i]);
                }
            }
        }
コード例 #25
0
        //path to the code;
        public static void PreprocessorActions(string path, int threadNumber)
        {
            bool     endLoop = false;
            MyStream sr      = null;

            try
            {
                sr = new MyStream(path, System.Text.Encoding.UTF8);
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Second exception caught.", e);
                endLoop = true;
            }

            string s;
            string firstNewVariableWord;
            string secondNewVariableWord;

            string[] newkeyWords;
            string   newKeyword;

            char[] charsToTrim = { '\t', ' ', '*', '&' };
            while (!endLoop && (s = sr.ReadLine()) != null)
            {
                if (DefineDecleration.IsMatch(s))
                {
                    firstNewVariableWord = s.Substring(s.IndexOf(' '), s.Length - s.IndexOf(' '));
                    firstNewVariableWord = firstNewVariableWord.Trim();
                    firstNewVariableWord = firstNewVariableWord.Substring(firstNewVariableWord.IndexOf(' '), firstNewVariableWord.Length - firstNewVariableWord.IndexOf(' '));
                    firstNewVariableWord = firstNewVariableWord.Trim();
                    firstNewVariableWord = firstNewVariableWord.Trim(charsToTrim);
                    if (firstNewVariableWord.IndexOf(" ") != -1)
                    {
                        newkeyWords           = Regex.Split(firstNewVariableWord, " ");
                        secondNewVariableWord = newkeyWords[1];
                        firstNewVariableWord  = newkeyWords[0];
                        if (CheckIfStringInHash(keywords, firstNewVariableWord) && CheckIfStringInHash(keywords, secondNewVariableWord))
                        {
                            newKeyword = Regex.Split(s, " ")[1];
                            newKeyword = newKeyword.Trim();
                            if (!keywords.ContainsKey(CreateMD5(newKeyword)))
                            {
                                keywords.Add(CreateMD5(newKeyword), newKeyword);
                            }
                        }
                    }
                    else
                    {
                        if (CheckIfStringInHash(keywords, firstNewVariableWord))
                        {
                            newKeyword = Regex.Split(s, " ")[1];
                            newKeyword = newKeyword.Trim();
                            if (!keywords.ContainsKey(CreateMD5(newKeyword)))
                            {
                                keywords.Add(CreateMD5(newKeyword), newKeyword);
                            }
                        }
                    }
                }
                if (StructPattern.IsMatch(s) && threadNumber != 0)
                {
                    AddStructNames(sr, s);
                }
                else if (TypedefOneLine.IsMatch(s) && threadNumber != 0)
                {
                    AddStructNames(sr, s);
                }
                else if (IncludeTrianglesPattern.IsMatch(s) || IncludeRegularPattern.IsMatch(s))
                {
                    string CurrentPath;
                    string result;
                    if (s.IndexOf("<") != -1 && s.IndexOf(">") != -1)
                    {
                        result      = CutBetween2Strings(s, "<", ">");
                        CurrentPath = librariesPath;
                    }
                    else
                    {
                        result      = CutBetween2Strings(s, "\"", "\"");
                        CurrentPath = projectPath;
                    }
                    Console.WriteLine(result);
                    if (!includes.Contains(CreateMD5(result)))
                    {
                        includes.Add(CreateMD5(result), result);
                        Thread thread;
                        if (result.IndexOf("\\") != -1)
                        {
                            thread = new Thread(() => PreprocessorActions(result, threadNumber + 1));
                        }
                        else
                        {
                            thread = new Thread(() => PreprocessorActions(CurrentPath + "\\" + result, threadNumber + 1));
                        }
                        thread.Start();
                        thread.Join();
                        Console.WriteLine("thread " + threadNumber + "stopped");
                    }
                }
            }
            if (sr != null)
            {
                sr.Close();
            }
        }