Beispiel #1
0
        /// <summary>
        /// creates a method to check parameters of the syntax commands,
        /// since parameters of every syntax command can be different, checks the type of command before validating,
        /// checks the parameter of every value type command detected in the code according to its definition,
        /// after validation of value type commands is done, same is done for block commands detected in the code,
        /// validation is done by splitting the line string of command by ' ' spaces and then by ','
        /// parameters returned by splitting the second part of the string is then checked for integer value or variable declaration,
        /// for if command, the parameter is checked for a valid conditional statement
        /// </summary>
        public void checkParameters()
        {
            foreach (ValueTypeCommand valueCmd in valueCmdList)
            {
                bool valid = true;

                if (valueCmd.name.Equals("number"))
                {
                    string message = "";

                    var words = valueCmd.lineString.Split(new[] { ' ' }, 2);

                    if (words.Length < 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        var restWords = words[1];
                        restWords = restWords.Replace("  ", String.Empty);

                        string[] assignmentPart = restWords.Split('=');

                        if (assignmentPart.Length < 2 || assignmentPart.Length > 2 || assignmentPart[0].Equals(""))
                        {
                            valid = false;
                        }
                        else
                        {
                            if (!int.TryParse(assignmentPart[1], out int n))
                            {
                                valid = false;
                            }
                        }

                        foreach (Variable vr in GeneratedLists.variables)
                        {
                            if (vr.Name.Equals(assignmentPart[0].Trim()))
                            {
                                valid   = false;
                                message = "Variable with name '" + assignmentPart[0] + "' already declared in this context";
                            }
                        }

                        if (valid)
                        {
                            GeneratedLists.variables.Add(new Variable(assignmentPart[0].Trim(), "Number", valueCmd.line, assignmentPart[1].Trim()));
                        }
                        else
                        //TODO Filename
                        {
                            InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(valueCmd.index, valueCmd.name, fileName, valueCmd.line, valueCmd.lineString);
                            if (message.Equals(""))
                            {
                                error.generateErrorMsg();
                            }
                            else
                            {
                                error.generateErrorMsg(message);
                            }
                            GeneratedLists.errorMessages.Add(error);
                        }
                    }
                }

                else if (valueCmd.name.Equals("pen"))
                {
                    valid = true;

                    var words = valueCmd.lineString.Split(new[] { ' ' }, 2);


                    if (words.Length < 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        var restWords = words[1];

                        if (restWords.Equals(""))
                        {
                            valid = false;
                        }
                        else
                        {
                            if (restWords.Contains(','))
                            {
                                string[] paramsPart = restWords.Split(',');
                                valid = int.TryParse(paramsPart[1], out int res);
                            }
                        }
                    }
                    if (!valid)
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(valueCmd.index, valueCmd.name, fileName, valueCmd.line, valueCmd.lineString);
                        error.generateErrorMsg();
                        GeneratedLists.errorMessages.Add(error);
                    }
                }

                else if (valueCmd.name.Equals("polygon"))
                {
                    valid = true;
                    string message = "";

                    var words = valueCmd.lineString.Split(new[] { ' ' }, 2);

                    if (words.Length < 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        var restWords = words[1];
                        restWords = restWords.Replace(" ", String.Empty);

                        string[] paramsPart = restWords.Split(',');

                        if (paramsPart.Length < 4)
                        {
                            valid = false;
                        }
                        else
                        {
                            (valid, message) = checkforParamsSplit(paramsPart);
                        }
                    }
                    if (!valid)
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(valueCmd.index, valueCmd.name, fileName, valueCmd.line, valueCmd.lineString);
                        if (message.Equals(""))
                        {
                            error.generateErrorMsg();
                        }
                        else
                        {
                            error.generateErrorMsg(message);
                        }

                        GeneratedLists.errorMessages.Add(error);
                    }
                }

                else if (valueCmd.name.Equals("bezier"))
                {
                    valid = true;
                    string message = "";

                    var words = valueCmd.lineString.Split(new[] { ' ' }, 2);

                    if (words.Length < 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        var restWords = words[1];
                        restWords = restWords.Replace(" ", String.Empty);

                        string[] paramsPart = restWords.Split(',');

                        if (paramsPart.Length < 8)
                        {
                            valid = false;
                        }
                        else
                        {
                            (valid, message) = checkforParamsSplit(paramsPart);
                        }
                    }
                    if (!valid)
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(valueCmd.index, valueCmd.name, fileName, valueCmd.line, valueCmd.lineString);
                        if (message.Equals(""))
                        {
                            error.generateErrorMsg();
                        }
                        else
                        {
                            error.generateErrorMsg(message);
                        }

                        GeneratedLists.errorMessages.Add(error);
                    }
                }

                else if (valueCmd.name.Equals("triangle"))
                {
                    valid = true;
                    string message = "";

                    var words = valueCmd.lineString.Split(new[] { ' ' }, 2);

                    if (words.Length < 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        var restWords = words[1];
                        restWords = restWords.Replace(" ", String.Empty);

                        string[] paramsPart = restWords.Split(',');

                        if (paramsPart.Length < 6 || paramsPart.Length > 6)
                        {
                            valid = false;
                        }
                        else
                        {
                            (valid, message) = checkforParamsSplit(paramsPart);
                        }
                    }
                    if (!valid)
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(valueCmd.index, valueCmd.name, fileName, valueCmd.line, valueCmd.lineString);
                        if (message.Equals(""))
                        {
                            error.generateErrorMsg();
                        }
                        else
                        {
                            error.generateErrorMsg(message);
                        }

                        GeneratedLists.errorMessages.Add(error);
                    }
                }

                else if (GeneratedLists.TwoParameterCommands.Contains(valueCmd.name))
                {
                    valid = true;
                    string message = "";

                    var words = valueCmd.lineString.Split(new[] { ' ' }, 2);

                    if (words.Length < 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        var restWords = words[1];
                        restWords = restWords.Replace(" ", String.Empty);

                        string[] paramsPart = restWords.Split(',');

                        if (paramsPart.Length < 2 || paramsPart.Length > 2)
                        {
                            valid = false;
                        }
                        else
                        {
                            (valid, message) = checkforParamsSplit(paramsPart);
                        }
                    }

                    if (!valid)
                    //TODO Filename
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(valueCmd.index, valueCmd.name, fileName, valueCmd.line, valueCmd.lineString);
                        if (message.Equals(""))
                        {
                            error.generateErrorMsg();
                        }
                        else
                        {
                            error.generateErrorMsg(message);
                        }

                        GeneratedLists.errorMessages.Add(error);
                    }
                }

                else if (GeneratedLists.FourParameterCommands.Contains(valueCmd.name))
                {
                    valid = true;
                    string message = "";

                    var words = valueCmd.lineString.Split(new[] { ' ' }, 2);

                    if (words.Length < 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        var restWords = words[1];
                        restWords = restWords.Replace(" ", String.Empty);

                        string[] paramsPart = restWords.Split(',');

                        if (paramsPart.Length < 4 || paramsPart.Length > 4)
                        {
                            valid = false;
                        }
                        else
                        {
                            (valid, message) = checkforParamsSplit(paramsPart);
                        }
                    }


                    if (!valid)
                    //TODO Filename
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(valueCmd.index, valueCmd.name, fileName, valueCmd.line, valueCmd.lineString);
                        if (message.Equals(""))
                        {
                            error.generateErrorMsg();
                        }
                        else
                        {
                            error.generateErrorMsg(message);
                        }
                        GeneratedLists.errorMessages.Add(error);
                    }
                }

                else if (GeneratedLists.SingleParameterCommands.Contains(valueCmd.name))
                {
                    valid = true;
                    string message = "";

                    string[] words = valueCmd.lineString.Split(' ');

                    if (words.Length > 2 || words.Length < 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        if (!int.TryParse(words[1], out int sp))
                        {
                            valid   = false;
                            message = "Invalid parameters. Couldn't find the variable '" + words[1] + "' declared in the context";
                            foreach (Variable vr in GeneratedLists.variables)
                            {
                                if (words[1].Equals(vr.Name))
                                {
                                    valid = true;
                                }
                            }
                        }
                    }

                    if (!valid)
                    //TODO Filename
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(valueCmd.index, valueCmd.name, fileName, valueCmd.line, valueCmd.lineString);
                        if (message.Equals(""))
                        {
                            error.generateErrorMsg();
                        }
                        else
                        {
                            error.generateErrorMsg(message);
                        }

                        GeneratedLists.errorMessages.Add(error);
                    }
                }

                else if (valueCmd.name.Equals("repeat"))
                {
                    valid = true;
                    string msg = "";

                    string[] words = valueCmd.lineString.Split(' ');

                    if (words.Length > 6 || words.Length < 4)
                    {
                        valid = false;
                    }
                    else
                    {
                        if (!int.TryParse(words[1], out int num))
                        {
                            valid = false;
                            msg   = "Invalid parameters. Couldn't find the variable '" + words[1] + "' declared in the context";
                            foreach (Variable vr in GeneratedLists.variables)
                            {
                                if (words[1].Equals(vr.Name))
                                {
                                    valid = true;
                                }
                            }
                        }

                        if (words[2].ToLower().Equals("circle"))
                        {
                            valid = false;

                            if (words.Length == 4)
                            {
                                foreach (Variable vr in GeneratedLists.variables)
                                {
                                    if (words[3].StartsWith(vr.Name))
                                    {
                                        foreach (string s in GeneratedLists.Operators)
                                        {
                                            if (words[3].Contains(s))
                                            {
                                                string[] trimWords = words[3].Split(char.Parse(s));
                                                if (trimWords[0].Equals(vr.Name))
                                                {
                                                    valid = true;
                                                    List <string> abc = trimWords.ToList();
                                                    words = abc.ToArray();
                                                }
                                                (valid, msg) = checkforParamsSplit(words);
                                                if (valid)
                                                {
                                                    GeneratedLists.repeatDictionary.Add(vr, int.Parse(trimWords[1]));
                                                }

                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            else
                            {
                                string restWords = "";
                                for (int i = 3; i < words.Length; i++)
                                {
                                    restWords += words[i];
                                }

                                Console.WriteLine(restWords);

                                restWords = restWords.Replace(" ", string.Empty);

                                foreach (Variable vr in GeneratedLists.variables)
                                {
                                    if (restWords.StartsWith(vr.Name))
                                    {
                                        foreach (string s in GeneratedLists.Operators)
                                        {
                                            if (restWords.Contains(s))
                                            {
                                                string[] trimWords = restWords.Split(char.Parse(s));
                                                if (trimWords[0].Equals(vr.Name))
                                                {
                                                    valid = true;
                                                    List <string> abc = trimWords.ToList();
                                                    words = abc.ToArray();
                                                }
                                                (valid, msg) = checkforParamsSplit(words);

                                                if (valid)
                                                {
                                                    GeneratedLists.repeatDictionary.Add(vr, int.Parse(trimWords[1]));
                                                }

                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (!valid)
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(valueCmd.index, valueCmd.name, fileName, valueCmd.line, valueCmd.lineString);
                        if (msg.Equals(""))
                        {
                            error.generateErrorMsg();
                        }
                        else
                        {
                            error.generateErrorMsg(msg);
                        }

                        GeneratedLists.errorMessages.Add(error);
                    }
                }
            }

            foreach (BlockCommand bc in blockList)
            {
                bool   valid   = true;
                string message = "";

                if (bc.name.Equals("loop"))
                {
                    string[] words = bc.lineString.Split(' ');

                    Console.WriteLine(words.Length);

                    if (words.Length < 2 || words.Length > 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        if (!int.TryParse(words[1], out int ab))
                        {
                            valid = false;

                            message = "Invalid parameters. Couldn't find the variable '" + words[1] + "' declared in the context";
                            foreach (Variable vr in GeneratedLists.variables)
                            {
                                if (words[1].Equals(vr.Name))
                                {
                                    valid = true;
                                }
                            }
                        }
                    }

                    if (!valid)
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(bc.index, bc.name, fileName, bc.line, bc.lineString);
                        error.generateErrorMsg();
                        GeneratedLists.errorMessages.Add(error);
                    }
                }

                if (bc.name.Equals("if"))
                {
                    string[] words = bc.lineString.Split(new[] { ' ' }, 2);

                    if (words.Length < 2)
                    {
                        valid = false;
                    }
                    else
                    {
                        var restWords = words[1];

                        restWords = restWords.Replace(" ", String.Empty);

                        if (!restWords.Contains('='))
                        {
                            valid = false;
                        }
                        else
                        {
                            string[] paramsPart = restWords.Split(GeneratedLists.Operators.ToArray(), StringSplitOptions.None);

                            if (paramsPart.Length < 2)
                            {
                                valid = false;
                            }
                            else
                            {
                                (valid, message) = checkforParamsSplit(paramsPart);
                            }
                        }
                    }

                    if (!valid)
                    {
                        InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(bc.index, bc.name, fileName, bc.line, bc.lineString);
                        if (message.Equals(""))
                        {
                            error.generateErrorMsg();
                        }
                        else
                        {
                            error.generateErrorMsg(message);
                        }

                        GeneratedLists.errorMessages.Add(error);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// creates method to check whether the erors identified as syntax commands are really errors or not,
        /// creates a temp list to store the errors that should be ignored,
        /// creates another list to store errors in the error list which are identified by this method,
        /// ignores the commands in the same line as repeat command as its parameters,
        /// ignores the commands in the same line as block and value type commands,
        /// makes sure that the variables declared are not counted as invalid syntax errors,
        /// if the variables parameters is not valid, creates an invalid parameter errro and adds it to the list,
        /// removes the ignored list of errors from the GeneratedLists object
        /// </summary>
        public void checkInvalidSyntax()
        {
            List <ErrorMessage> tempList = new List <ErrorMessage>();
            List <ErrorMessage> toAdd    = new List <ErrorMessage>();

            foreach (ErrorMessage message in GeneratedLists.errorMessages)
            {
                if (message.lineString.StartsWith("repeat") && !message.GetType().ToString().Equals("Draw.GUI.InvalidParameterErrorMessage"))
                {
                    tempList.Add(message);

                    goto End;
                }
                else if (message.lineString.StartsWith("repeat") && message.word.Equals("circle"))
                {
                    tempList.Add(message);

                    goto End;
                }

                if (message.GetType().ToString().Equals("Draw.GUI.InvalidSyntaxErrorMessage") || message.GetType().ToString().Equals("Draw.GUI.MultipleCommandsErrorMessage"))
                {
                    //For block parameter checking
                    foreach (BlockCommand block in blockList)
                    {
                        if (block.mapTo != null)
                        {
                            //if(message.index > block.index && message.index < block.mapToIndex)
                            //{
                            //    Console.WriteLine("Error word: " + message.word + "AND YES");
                            //    tempList.Add(message);
                            //    goto End;
                            //}

                            if (message.line == block.line)
                            {
                                tempList.Add(message);
                                goto End;
                            }
                        }
                    }

                    //For value types parameter checking
                    foreach (ValueTypeCommand value in valueCmdList)
                    {
                        if (message.line == value.line)
                        {
                            tempList.Add(message);
                            goto End;
                        }
                    }


                    foreach (Variable vr in GeneratedLists.variables)
                    {
                        bool   valid    = false;
                        string errorMsg = "";

                        InvalidSyntaxErrorMessage msg = (InvalidSyntaxErrorMessage)message;
                        if (msg.lineString.StartsWith(vr.Name))
                        {
                            tempList.Add(message);
                            string line = msg.lineString;
                            line = line.Replace(" ", string.Empty);

                            foreach (string s in GeneratedLists.Operators)
                            {
                                if (line.Contains(s))
                                {
                                    string[] words = line.Split(char.Parse(s));
                                    if (words[0].Equals(vr.Name))
                                    {
                                        valid = true;
                                        List <string> abc = words.ToList();
                                        abc.Remove(words[0]);
                                        words = abc.ToArray();
                                    }
                                    (valid, errorMsg) = checkforParamsSplit(words);

                                    break;
                                }
                            }

                            if (!valid)
                            {
                                InvalidParameterErrorMessage error = new InvalidParameterErrorMessage(message.index, vr.Name, fileName, vr.Line, message.lineString);
                                if (errorMsg.Equals(""))
                                {
                                    error.generateErrorMsg();
                                }
                                else
                                {
                                    error.generateErrorMsg(errorMsg);
                                }

                                toAdd.Add(error);
                            }

                            break;
                        }
                    }
                }
            }

End:
            GeneratedLists.errorMessages = GeneratedLists.errorMessages.Except(tempList).ToList();
            GeneratedLists.errorMessages.AddRange(toAdd);
        }