Ejemplo n.º 1
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]);
                }
            }
        }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
0
        /// Function - SyncServer
        /// <summary>
        /// Creation of the rest api server.
        /// </summary>
        /// <param name="filePath"> Path for the code file.</param>
        /// <param name="includes"> Hashtable for all of the includes in the code.</param>
        /// <param name="defines"> Dictionary that stores all of the defines in the code.</param>
        public SyncServer()
        {
            var listener = new HttpListener();

            //add prefixes.
            listener.Prefixes.Add("http://localhost:8081/");
            listener.Prefixes.Add("http://127.0.0.1:8081/");
            //start listening.
            listener.Start();

            while (ConnectionServer.GetCloseAllBool() == false)
            {
                try
                {
                    //if gets connection.
                    var context = listener.GetContext(); //Block until a connection comes in
                    context.Response.StatusCode  = 200;
                    context.Response.SendChunked = true;
                    context.Response.ContentType = "application/json";
                    string dataJson = GeneralConsts.EMPTY_STRING;
                    Dictionary <string, Dictionary <string, object> > final_json = MainProgram.GetFinalJson();
                    string filePath          = context.Request.QueryString["filePath"];
                    bool   not_found_pattern = false;
                    bool   not_found         = false;
                    char[] trimChars         = { '/', ' ' };
                    int    totalTime         = 0;
                    string path = GeneralConsts.EMPTY_STRING;
                    Regex  r;
                    //All GET commands.
                    if (context.Request.HttpMethod == "GET")
                    {
                        if (context.Request.QueryString["pattern"] != null)
                        {
                            MainProgram.AddToLogString(filePath, context.Request.QueryString["pattern"]);
                            MainProgram.AddToLogString(filePath, context.Request.QueryString["returnSize"]);
                            r = new Regex(context.Request.QueryString["pattern"]);
                            string    returnSize = context.Request.QueryString["returnSize"];
                            string [] result     = GeneralRestApiServerMethods.SearchPattern(r, returnSize, filePath);
                            dataJson = JsonConvert.SerializeObject(result);
                            MainProgram.AddToLogString(filePath, dataJson);
                        }
                        else if (context.Request.QueryString["readyPattern"] != null)
                        {
                            MainProgram.AddToLogString(filePath, context.Request.QueryString["readyPattern"]);
                            MainProgram.AddToLogString(filePath, context.Request.QueryString["returnSize"]);
                            if (GeneralRestApiServerMethods.TakePatternFromFile(context.Request.QueryString["readyPattern"]) == GeneralConsts.EMPTY_STRING)
                            {
                                not_found_pattern = true;
                            }
                            else
                            {
                                r = new Regex(GeneralRestApiServerMethods.TakePatternFromFile(context.Request.QueryString["readyPattern"]));
                                string   returnSize = context.Request.QueryString["returnSize"];
                                string[] result     = GeneralRestApiServerMethods.SearchPattern(r, returnSize, filePath);
                                dataJson = JsonConvert.SerializeObject(result);
                            }
                        }
                        else
                        {
                            path = context.Request.RawUrl;
                            path = path.Trim(trimChars);
                            path = path.Split('?')[0];
                            MainProgram.AddToLogString(filePath, path);
                            //switch case for get commands.
                            switch (path)
                            {
                            case "functions":
                                dataJson = JsonConvert.SerializeObject(final_json[filePath]["function"]);
                                MainProgram.AddToLogString(filePath, dataJson);
                                break;

                            case "codeInfo":
                                dataJson = JsonConvert.SerializeObject(final_json[filePath]["codeInfo"]);
                                MainProgram.AddToLogString(filePath, dataJson);
                                break;

                            default:
                                break;
                            }
                        }
                        byte [] bytes;
                        if (not_found)
                        {
                            bytes = Encoding.UTF8.GetBytes(NOT_FOUND_500);
                            context.Response.StatusCode = 500;
                        }
                        else
                        {
                            if (dataJson.Length < 0)
                            {
                                bytes = Encoding.UTF8.GetBytes(NOT_FOUND_404);
                                context.Response.StatusCode = 404;
                            }
                            else
                            {
                                bytes = Encoding.UTF8.GetBytes(dataJson);
                            }
                        }

                        Stream OutputStream = context.Response.OutputStream;
                        //sends the message back.
                        OutputStream.Write(bytes, 0, bytes.Length);
                        //Close connection.
                        OutputStream.Close();
                    }
                }


                catch (Exception)
                {
                    // Client disconnected or some other error - ignored for this example
                }
            }
        }