Example #1
0
        public Boolean GetIdentifiers(string txt)
        {
            LexicalConstants.Identifier       id = new LexicalConstants.Identifier();
            LexicalConstants.IdentifierDelims delims = new LexicalConstants.IdentifierDelims();
            Boolean hastoken = false, valID = false, isvalID = true;
            Tokens  t = new Tokens();

            id.id.AddRange(id.delim_lowlet);
            id.id.AddRange(id.delim_caplet);
            id.id.AddRange(id.delim_undscr);
            id.id.AddRange(id.delim_digit);

            int ictr = 0;

            foreach (char c in id.delim_caplet)
            {
                if (txt.ElementAt(ictr) == c)
                {
                    valID = true;
                }
            }

            if (valID)
            {
                isvalID = true;
                while (isvalID)
                {
                    isvalID = false;
                    foreach (char n in id.id)
                    {
                        if ((txt.Length - 1) > ictr)
                        {
                            if (txt.ElementAt(ictr + 1) == n)
                            {
                                ictr++;
                                isvalID = true;
                            }
                        }
                    }
                    if (ictr > 18)
                    {
                        valID = false;
                    }
                }

                if (valID)
                {
                    foreach (char delim in delims.delim_end)
                    {
                        if ((txt.Length - 1) > ictr)
                        {
                            if (txt.ElementAt(ictr + 1) == delim)
                            {
                                hastoken = true;
                                break;
                            }
                        }
                    }
                }

                if (hastoken)
                {
                    valid++;
                    t.setTokens("id");
                    t.setLexemes(txt.Substring(0, (ictr + 1)));
                    t.setAttributes("identifier" + idnum);
                    token.Add(t);
                    idnum++;
                }

                ctr = ictr + 1;
            }
            return(hastoken);
        }
Example #2
0
        public Boolean GetLiterals(string txt)
        {
            LexicalConstants.LiteralsDelims ld = new LexicalConstants.LiteralsDelims();
            LexicalConstants.Literals       l  = new LexicalConstants.Literals();
            Tokens      t = new Tokens();
            List <char> delims = new List <char>();
            Boolean     hastoken = false, validtxt = false;
            string      literal = "";

            state = 0;
            int lctr = 0;

            if (txt.ElementAt(lctr) == '"')
            {
                state = 1;
            }
            else if (txt.ElementAt(lctr) == '\'')
            {
                state = 2;
            }
            else
            {
                foreach (char num in l.nums)
                {
                    if (txt.ElementAt(lctr) == num)
                    {
                        state = 3;
                    }
                }
            }

            if (state != 0)
            {
                switch (state)
                {
                case 1:
                case 2:
                    delims = ld.delim_txt;
                    //String Literal Analyzer
                    if (state == 1)
                    {
                        if (txt.Length != 1)
                        {
                            while ((txt.Length - 1) > lctr && !(txt[lctr + 1] == '"') && !(txt[lctr + 1] == '\n'))
                            {
                                literal += txt[lctr].ToString();
                                lctr++;
                            }

                            if ((txt.Length - 1) == lctr && (txt[lctr] != '"'))
                            {
                                hastoken = false;
                            }
                            else
                            {
                                if (!(lctr == 1 && txt[lctr] == '\\'))
                                {
                                    validtxt = true;
                                    lctr++;
                                    foreach (char c in delims)
                                    {
                                        if ((txt.Length - 1) >= (lctr + 1))
                                        {
                                            if (txt[lctr + 1] == c)
                                            {
                                                hastoken = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                                if (hastoken && validtxt)
                                {
                                    valid++;
                                    t.setTokens("stringlit");
                                    t.setLexemes(txt.Substring(0, (lctr + 1)));
                                    t.setAttributes("stringlit");
                                    token.Add(t);
                                    ctr = lctr + 1;
                                }
                                else if (!validtxt)
                                {
                                    ctr      = lctr + 2;
                                    hastoken = false;
                                }
                            }
                        }
                    }

                    //Character Literal Analyzer
                    else
                    {
                        if (txt.Length != 1)
                        {
                            while ((txt.Length - 1) > lctr && !(txt[lctr + 1] == '\'') && !(txt[lctr + 1] == '\n'))
                            {
                                literal += txt[lctr].ToString();
                                lctr++;
                            }
                            if (lctr >= 3)
                            {
                                hastoken = false;
                                ctr      = lctr + 2;
                                if (ctr > txt.Length)
                                {
                                    ctr = txt.Length;
                                }
                            }
                            else
                            {
                                if ((txt[1] == '\\' && lctr == 2) || (lctr == 1 && txt[1] != '\\') || lctr == 0)
                                {
                                    validtxt = true;
                                }
                                else
                                {
                                    validtxt = false;
                                    hastoken = false;
                                    ctr      = lctr + 2;
                                    if (ctr > txt.Length)
                                    {
                                        ctr = txt.Length;
                                    }
                                }
                                if (validtxt)
                                {
                                    if ((txt.Length - 1) >= (lctr + 1) && txt[lctr + 1] == '\'')
                                    {
                                        lctr++;
                                        foreach (char c in delims)
                                        {
                                            if ((txt.Length - 1) >= (lctr + 1))
                                            {
                                                if (txt[lctr + 1] == c)
                                                {
                                                    hastoken = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (hastoken)
                                    {
                                        valid++;
                                        t.setTokens("charlit");
                                        t.setLexemes(txt.Substring(0, (lctr + 1)));
                                        t.setAttributes("charlit");
                                        token.Add(t);
                                        ctr = lctr + 1;
                                    }
                                    else
                                    {
                                        ctr = lctr + 1;
                                        if (ctr > txt.Length)
                                        {
                                            ctr = lctr;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;

                case 3:
                    LexicalConstants.Identifier id = new LexicalConstants.Identifier();
                    delims = ld.delim_num;
                    Boolean     isNumNext = true, hasnum = true, hasid = false;
                    List <char> num = new List <char> {
                        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
                    };

                    id.id.AddRange(id.delim_caplet);
                    id.id.AddRange(id.delim_lowlet);

                    //If Negative
                    if (txt.ElementAt(lctr) == '~')
                    {
                        hasnum = false;
                        foreach (char n in num)
                        {
                            if ((txt.Length - 1) > lctr)
                            {
                                if (txt.ElementAt(lctr + 1) == n)
                                {
                                    hasnum = true;
                                    lctr++;
                                }
                            }
                        }
                    }

                    if (hasnum)
                    {
                        while (isNumNext)
                        {
                            isNumNext = false;
                            foreach (char n in num)
                            {
                                if ((txt.Length - 1) > lctr)
                                {
                                    if (txt.ElementAt(lctr + 1) == n)
                                    {
                                        lctr++;
                                        isNumNext = true;
                                    }
                                }
                            }
                        }

                        Boolean isDouble = false;
                        if ((txt.Length - 1) > lctr)
                        {
                            if (txt.ElementAt(lctr + 1) == '.')
                            {
                                if ((txt.Length - 1) > lctr + 1)
                                {
                                    foreach (char n in num)
                                    {
                                        if (txt.ElementAt(lctr + 2) == n)
                                        {
                                            isDouble = true;
                                        }
                                    }
                                }
                            }
                        }

                        //Double Literal Analyzer
                        if (isDouble)
                        {
                            lctr++;
                            isNumNext = true;
                            while (isNumNext)
                            {
                                isNumNext = false;
                                foreach (char n in num)
                                {
                                    if ((txt.Length - 1) > lctr)
                                    {
                                        if (txt.ElementAt(lctr + 1) == n)
                                        {
                                            lctr++;
                                            isNumNext = true;
                                        }
                                    }
                                }
                            }

                            foreach (char delim in delims)
                            {
                                if ((txt.Length - 1) > lctr)
                                {
                                    if (txt.ElementAt(lctr + 1) == delim)
                                    {
                                        hastoken = true;
                                        break;
                                    }
                                }
                            }

                            if (hastoken)
                            {
                                valid++;
                                t.setTokens("doublelit");
                                t.setLexemes(txt.Substring(0, (lctr + 1)));
                                t.setAttributes("doublelit");
                                token.Add(t);
                            }
                            else
                            {
                                foreach (char c in id.id)
                                {
                                    if ((txt.Length - 1) > lctr)
                                    {
                                        if (txt.ElementAt(lctr + 1) == c)
                                        {
                                            hasid = true;
                                        }
                                    }
                                }
                            }

                            if (!hasid)
                            {
                                ctr = lctr + 1;
                            }
                        }
                        //Integer Literal Analyzer
                        else
                        {
                            foreach (char delim in delims)
                            {
                                if ((txt.Length - 1) > lctr)
                                {
                                    if (txt.ElementAt(lctr + 1) == delim)
                                    {
                                        hastoken = true;
                                        break;
                                    }
                                }
                            }

                            if (hastoken)
                            {
                                valid++;
                                t.setTokens("intlit");
                                t.setLexemes(txt.Substring(0, (lctr + 1)));
                                t.setAttributes("intlit");
                                token.Add(t);
                            }
                            else
                            {
                                foreach (char c in id.id)
                                {
                                    if ((txt.Length - 1) > lctr)
                                    {
                                        if (txt.ElementAt(lctr + 1) == c)
                                        {
                                            lctr++;
                                            hasid = true;
                                        }
                                    }
                                }
                            }
                            if (!hasid)
                            {
                                ctr = lctr + 1;
                            }
                        }
                    }
                    break;
                }
            }
            return(hastoken);
        }