Ejemplo n.º 1
0
        public string LexicalAnalysis(string input, List <string> origin)
        {
            string temp     = "";
            string response = "";
            //int cont = 1;

            List <string> lines = input.Split('\n').ToList();
            //List<string> lines = Regex.Split(input, "\r\n").ToList();
            bool gettinStrings = false;
            bool gettinInteger = false;
            bool concatNext    = true;
            bool gettinDouble  = false;
            bool finish        = false;

            foreach (string item in lines)
            {
                int cont = origin.FindIndex(x => x.Contains(item.Replace("\r", ""))) + 1;
                for (int i = 0; i < item.Length; i++)
                {
                    if (Char.IsWhiteSpace(item[i]))
                    {
                        if (temp != "")
                        {
                            if (!gettinStrings)
                            {
                                response += Analysis(item, temp, cont, cont == lines.Count);
                                temp      = "";
                            }
                            else
                            {
                                temp += item[i];
                            }
                        }
                    }
                    else if (operators.Contains(item[i].ToString()))
                    {
                        if (temp != "")
                        {
                            if (gettinStrings) // if a operator is between '"' is not really an operator
                            {
                                temp += item[i];
                            }
                            else if (gettinInteger && item[i] == '.') // if a '.' is after a number, is a double var
                            {
                                gettinInteger = false;
                                gettinDouble  = true;
                                temp         += item[i];
                            }
                            else if (gettinDouble && (item[i] == '+' || item[i] == '-')) // exponential double
                            {
                                temp += item[i];
                            }
                            else
                            {
                                response += Analysis(item, temp, cont, cont == lines.Count);
                                temp      = "";
                                i--;
                            }
                        }
                        else
                        {
                            //check if the next char is operator
                            if (item.Length > 1)
                            {
                                try
                                {
                                    if (item[i].ToString() + item[i + 1].ToString() == "*/")
                                    {
                                        response += FormatUnpairedComment(item, (item[i].ToString() + item[i + 1].ToString()), cont, i);
                                        i++;
                                    }
                                    else if (item[i].ToString() + item[i + 1].ToString() == "/*")
                                    {
                                        response += FormatEOF(item, (item[i].ToString() + item[i + 1].ToString()), cont);
                                        finish    = true;
                                        break;
                                    }
                                    else if (operators.Contains((item[i].ToString() + item[i + 1].ToString())))
                                    {
                                        response += FormatOperator(item, (item[i].ToString() + item[i + 1].ToString()), cont, i);
                                        i++;
                                    }
                                    else
                                    {
                                        response += FormatOperator(item, item[i].ToString(), cont, i);
                                    }
                                }
                                catch (Exception) // operator is at the end of line
                                {
                                    response += FormatOperator(item, item[i].ToString(), cont, i);
                                }
                            }
                            else
                            {
                                response += FormatOperator(item, item[i].ToString(), cont, i);
                            }
                        }
                    }
                    else
                    {
                        List <char> hexDigits = new List <char>()
                        {
                            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
                        };
                        if (item[i] == '"' && !gettinStrings)
                        {
                            gettinStrings = true;
                        }
                        else if (item[i] == '"' && gettinStrings)
                        {
                            gettinStrings = false;
                        }
                        else if (int.TryParse(item[i].ToString(), out int x) && !gettinStrings && temp == "")
                        {
                            try
                            {
                                if (x == 0 && (item[i + 1] == 'x' || item[i + 1] == 'X')) // posibility to being an hex value
                                {
                                    temp         += item[i];
                                    temp         += item[i + 1];
                                    i            += 2;
                                    gettinInteger = false;
                                    while (i < item.Length)
                                    {
                                        if (hexDigits.Contains(item[i]))
                                        {
                                            temp += item[i];
                                            i++;
                                        }
                                        else
                                        {
                                            i--;
                                            concatNext = false;
                                            break;
                                        }
                                    }
                                    if (i == item.Length)
                                    {
                                        concatNext = false;
                                    }
                                    response += Analysis(item, temp, cont, cont == lines.Count);
                                    temp      = "";
                                }
                                else
                                {
                                    gettinInteger = true;
                                }
                            }
                            catch (Exception)
                            {
                                gettinInteger = true;
                            }
                        }
                        else if (gettinInteger)
                        {
                            if (!int.TryParse(item[i].ToString(), out int y))
                            {
                                gettinInteger = false;
                                if (temp != "")
                                {
                                    response += Analysis(item, temp, cont, cont == lines.Count);
                                    temp      = "";
                                }
                            }
                        }
                        //else if (gettinDouble && (item[i] != 'e' && item[i] != 'E'))
                        //{
                        //    gettinDouble = false;
                        //    response += Analysis(item, temp, cont);
                        //    temp = "";
                        //    i--;
                        //    concatNext = false;
                        //}

                        if (concatNext)
                        {
                            if (RegularExpressions.Validate(item[i].ToString()) || operators.Contains(item[i].ToString()) || gettinStrings)
                            {
                                temp += item[i];
                            }
                            else
                            {
                                response += FormatIdentifier(item, item[i].ToString(), cont);
                            }
                        }
                        else
                        {
                            concatNext = true;
                        }
                    }
                }

                if (finish)
                {
                    break;
                }

                if (temp != "") // No analyzed item
                {
                    response += Analysis(item, temp, cont, cont == lines.Count);
                    temp      = "";
                }

                //cont++;
                semantic.Add(new KeyValuePair <string, string>("", ""));
            }

            return(response);
        }