Example #1
0
        public string check(string T)
        {
            T = T.ToLower();
            string type = "Error";

            if (Classes.ContainsKey(T))
            {
                type = Classes[T].ToString();
            }
            else
            {
                int i = 0;
                if (ScannerClassification.is_letter(T[i]))
                {
                    while (i < T.Length && (ScannerClassification.is_letter(T[i]) || ScannerClassification.is_digit(T[i])))
                    {
                        i++;
                    }
                    if (i == T.Length)
                    {
                        type = Token_Class.Identifier.ToString();
                    }
                    else
                    {
                        i = 0;
                    }
                }
                else if (ScannerClassification.is_digit(T[i]))
                {
                    int dots = 0;
                    while (i < T.Length && (ScannerClassification.is_digit(T[i]) || (T[i] == '.' && dots == 0)))
                    {
                        if (T[i] == '.')
                        {
                            dots++;
                        }
                        ++i;
                    }
                    if (dots < 2 && i == T.Length)
                    {
                        type = Token_Class.constant.ToString();
                    }
                }
                else if (i <= T.Length - 2 && ScannerClassification.is_two_operator(T[i], T[i + 1]))
                {
                    if (T[i] == '/' && T[i + 1] == '/')
                    {
                        type = Token_Class.comment.ToString();
                    }
                    else if (T[i] == '/' && T[i + 1] == '*')
                    {
                        if (T[T.Length - 2] == '*' && T[T.Length - 1] == '/')
                        {
                            type = Token_Class.comment.ToString();
                        }
                        else
                        {
                            type = Token_Class.Error.ToString();
                        }
                    }
                    else
                    {
                        type = Token_Class.two_operator.ToString();
                    }
                }
                else if (ScannerClassification.is_one_operator(T[i]))
                {
                    //type = Type.Operator.ToString();
                    if (T[i] == '"')
                    {
                        if (T[T.Length - 1] == '"')
                        {
                            type = Token_Class.String.ToString();
                        }
                    }
                    else
                    {
                        type = Token_Class.Error.ToString();
                    }
                }
            }
            return(type);
        }
Example #2
0
        public List <String> scan(String source_code)
        {
            NewLine.Add(-1);
            List <String> tokens = new List <String>();

            for (int i = 0; i < source_code.Length; i++)
            {
                int    cur = i;
                String lex = "";

                if (ScannerClassification.is_letter(source_code[cur]))
                {
                    while (cur < source_code.Length && (ScannerClassification.is_letter(source_code[cur]) || ScannerClassification.is_digit(source_code[cur])))
                    {
                        lex += source_code[cur];
                        cur++;
                    }
                    tokens.Add(lex);
                    i = cur - 1;
                }
                else if (ScannerClassification.is_digit(source_code[cur]))
                {
                    int dots = 0;
                    while (cur < source_code.Length && (ScannerClassification.is_digit(source_code[cur]) || source_code[cur] == '.' || ScannerClassification.is_letter(source_code[cur])))
                    {
                        if (source_code[cur] == '.')
                        {
                            dots++;
                        }
                        lex += source_code[cur];
                        cur++;
                    }
                    tokens.Add(lex);
                    i = cur - 1;
                }
                else if (cur != source_code.Length - 1 && ScannerClassification.is_two_operator(source_code[cur], source_code[cur + 1]))
                {
                    lex += source_code[cur];
                    lex += source_code[cur + 1];

                    if (source_code[cur] == '/' && source_code[cur + 1] == '/')
                    {
                        cur = cur + 2;
                        while (cur < source_code.Length && source_code[cur] != '\n')
                        {
                            lex += source_code[cur];
                            ++cur;
                            //  MessageBox.Show(lex.ToString());
                        }
                    }
                    else if (source_code[cur] == '/' && source_code[cur + 1] == '*')
                    {
                        cur = cur + 2;
                        while (cur < source_code.Length - 1 && source_code[cur] != '*' && source_code[cur + 1] != '/')
                        {
                            lex += source_code[cur];
                            cur += 1;
                            //  MessageBox.Show(lex.ToString());
                        }

                        if (cur < source_code.Length - 1 && source_code[cur] == '*' && source_code[cur + 1] == '/')
                        {
                            lex += source_code[cur];
                            lex += source_code[cur + 1];
                            cur++;
                        }
                    }
                    else
                    {
                        cur++;
                    }
                    // MessageBox.Show(lex.ToString());
                    i = cur;
                    tokens.Add(lex);
                }
                else if (ScannerClassification.is_one_operator(source_code[cur]))
                {
                    lex += source_code[cur];


                    if (source_code[cur] == '"')
                    {
                        //string tmp ="";
                        //  tmp += source_code[cur];
                        ++cur;
                        while (cur < source_code.Length && source_code[cur] != '"' && source_code[cur] != '\n' && source_code[cur] != ';')
                        {
                            lex += source_code[cur];
                            ++cur;
                        }
                        if (source_code[cur] == '"')
                        {
                            lex += source_code[cur];
                        }
                    }
                    i = cur;
                    tokens.Add(lex);
                }
                else
                {
                    cur = i;
                    if (source_code[cur] == ' ' || source_code[cur] == '\r' || source_code[cur] == '\n')
                    {
                        if (source_code[cur] == '\n')
                        {
                            NewLine.Add(tokens.Count - 1);
                        }
                        continue;
                    }
                    //  else tokens.Add(source_code[cur].ToString());
                    //  MessageBox.Show(source_code.Length.ToString());

                    while (cur < source_code.Length && source_code[cur] != ' ' && source_code[cur] != '\r' && source_code[cur] != '\n')
                    {
                        lex += source_code[cur]; ++cur;
                    }
                    tokens.Add(lex);
                    i = cur - 1;
                }
            }
            NewLine.Add(tokens.Count - 1);
            return(tokens);
        }