コード例 #1
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);
        }
コード例 #2
0
        /// <summary>
        /// method responsible for validating the user written code retrieved from the texteditor control of the coding form,
        /// splits the code by Environment.Newline to be checked line by line,
        /// if the line is a comment it is ignored but counter variables are still incremented,
        /// checkForMultipleCommands() method is called to check whether multiple lines are declared in one line,
        /// splits the linestring by word, i.e., by ' ' spaces,
        /// checks whether the word is a valid syntax command,
        /// if command is a type of value type command adds it to value commands list,
        /// if command is a type of block command, calls the method reponsible for the validation of block commands,
        /// generate error if the word is not a valid syntax command.
        /// call methods responsible for block and comment validity and invalid syntax errors
        /// </summary>
        public void validateCode()
        {
            int lineNumber = 1;

            ErrorPOSCounters.indexStartFORPos = 0;

            foreach (string lineString in code.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
            {
                string[] wordsinLine = lineString.Split(' ');

                if (lineString[0].Equals(GeneratedLists.acceptedWords[0][0]))
                {
                    lineNumber++;
                    ErrorPOSCounters.indexStartFORPos = ErrorPOSCounters.indexStartFORPos + lineString.Length;
                    continue;
                }

                checkForMultipleCommands(wordsinLine, lineNumber, lineString);

                foreach (string word in wordsinLine)
                {
                    if (word.Equals(""))
                    {
                        continue;
                    }

                    Counters.valid = false;

                    foreach (string acceptedWord in GeneratedLists.acceptedWords)
                    {
                        if (word.Equals(acceptedWord))
                        {
                            Counters.valid = true;

                            if (GeneratedLists.valueTypeCommands.Contains(word))
                            {
                                valueCmdList.Add(new ValueTypeCommand(Counters.valueTypeGenerator.Id, checkErrorPos(word), word, lineNumber, lineString));
                                Counters.valueTypeGenerator.increment();
                            }

                            if (GeneratedLists.BlockCommands.Contains(word))
                            {
                                checkBlockValidity(word, lineNumber, lineString);
                            }
                        }
                    }
                    if (!Counters.valid)
                    {
                        //TODO Fix the error messages
                        InvalidSyntaxErrorMessage msg = new InvalidSyntaxErrorMessage(checkErrorPos(word), word, fileName, lineNumber, lineString);
                        msg.generateErrorMsg();
                        GeneratedLists.errorMessages.Add(msg);
                    }
                }

                ErrorPOSCounters.indexStartFORPos = ErrorPOSCounters.indexStartFORPos + lineString.Length;

                lineNumber++;
            }

            checkBlocknCommentValidity();
            checkParameters();
            foreach (ErrorMessage msg in GeneratedLists.errorMessages)
            {
                checkInvalidSyntax();
            }
        }