示例#1
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);
        }
示例#2
0
        public Boolean hasLiterals(string txt)
        {
            LexicalConstants.Literals      lit      = new LexicalConstants.Literals();
            LexicalConstants.LiteralsDelim litDelim = new LexicalConstants.LiteralsDelim();
            Tokens token = new Tokens();

            List <char> delims = new List <char>();

            string strlit = "";

            Boolean hasToken = false, validstr = false, numNext = true, decLit = false, numLit = false, isNum = true, isStr = true, isNeg = false;

            int ctr = 0, digitNum = 0, digitDec = 0;

            choice = 0;

            if (txt.ElementAt(0).Equals('"'))
            {
                choice = 1;
            }
            else
            {
                foreach (char num in lit.number)
                {
                    if (txt.ElementAt(0) == num)
                    {
                        choice = 2;
                        break;
                    }
                }
            }

            switch (choice)
            {
            case 1:
                if (!txt.Length.Equals(1))
                {
                    while ((txt.Length - 1) > ctr && !txt.ElementAt(ctr + 1).Equals('"'))
                    {
                        strlit += txt[ctr];
                        ctr++;
                    }

                    if (ctr + 1 == txt.Length)
                    {
                        isStr = false;
                    }

                    if (isStr)
                    {
                        if (txt.ElementAt(0).Equals('"') && txt.ElementAt(ctr + 1).Equals('"'))
                        {
                            ctr     += 2;
                            validstr = true;
                            foreach (char delim in litDelim.delim_str)
                            {
                                if (txt.Length >= ctr + 1)
                                {
                                    if (txt.ElementAt(ctr).Equals(delim))
                                    {
                                        hasToken = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (hasToken && validstr)
                    {
                        token.setTokens("stringlit");
                        token.setLexemes(txt.Substring(0, ctr));
                        token.setDescription("stringlit");
                        tokens.Add(token);
                    }
                    else if (!hasToken && validstr)
                    {
                        hasToken = true;
                        token.setTokens("No Delimiter");
                        token.setLexemes(txt.Substring(0, ctr));
                        token.setDescription("stringlit");
                        tokens.Add(token);
                    }
                    else if (!validstr)
                    {
                        hasToken = false;
                    }
                }
                break;

            case 2:
                if (txt.ElementAt(0) == '~')
                {
                    ctr++;
                    isNeg = true;
                    isNum = false;
                    while (numNext)
                    {
                        foreach (char no in lit.number)
                        {
                            if ((txt.Length - 1) >= ctr)
                            {
                                if (txt.ElementAt(ctr) == no)
                                {
                                    numLit  = true;
                                    numNext = true;
                                    isNum   = true;
                                    ctr++;
                                    digitNum++;
                                    break;
                                }
                                else
                                {
                                    numNext = false;
                                }
                            }
                            else
                            {
                                numNext = false;
                            }
                        }
                    }
                }
                else
                {
                    numLit = true;
                    while ((txt.Length - 1 >= ctr) && numNext)
                    {
                        foreach (char no in lit.num)
                        {
                            if ((txt.Length - 1) >= ctr)
                            {
                                if (txt.ElementAt(ctr) == no)
                                {
                                    numNext = true;
                                    isNum   = true;
                                    ctr++;
                                    digitNum++;
                                    break;
                                }
                                else
                                {
                                    numNext = false;
                                }
                            }
                        }
                    }
                }

                if ((txt.Length - 1) > ctr)
                {
                    if (txt.ElementAt(ctr) == '.')
                    {
                        numNext = true;
                        foreach (char no in lit.num)
                        {
                            if (txt.ElementAt(ctr + 1) == no)
                            {
                                numLit = false;
                                decLit = true;
                                ctr++;
                                break;
                            }
                        }
                        while ((txt.Length > ctr) && numNext)
                        {
                            foreach (char no in lit.num)
                            {
                                if (txt.ElementAt(ctr) == no)
                                {
                                    ctr++;
                                    digitDec++;
                                    numNext = true;
                                    break;
                                }
                                else
                                {
                                    numNext = false;
                                }
                            }
                        }
                    }
                }


                if (numLit && digitNum > 9)
                {
                    if ((isNeg == true) && (numLit && digitNum <= 10))
                    {
                        break;
                    }
                    hasToken = true;
                    token.setTokens("Over the limit");
                    token.setLexemes(txt.Substring(0, ctr));
                    token.setDescription("numberlit");
                    tokens.Add(token);
                }
                else if (numLit)
                {
                    foreach (char delim in litDelim.delim_num)
                    {
                        if ((txt.Length - 1) >= ctr)
                        {
                            if (txt.ElementAt(ctr) == delim)
                            {
                                hasToken = true;
                                break;
                            }
                        }
                    }
                    if (hasToken)
                    {
                        token.setTokens("numberlit");
                        token.setLexemes(txt.Substring(0, ctr));
                        token.setDescription("numberlit");
                        tokens.Add(token);
                    }
                    else
                    {
                        hasToken = true;
                        token.setTokens("No Delimiter");
                        token.setLexemes(txt.Substring(0, ctr));
                        token.setDescription("numberlit");
                        tokens.Add(token);
                    }
                }
                else if (decLit && ((digitNum > 9) || digitDec > 5))
                {
                    if ((isNeg == true) && decLit && ((digitNum <= 10) && digitDec <= 5))
                    {
                        break;
                    }
                    token.setTokens("decimallit");
                    token.setLexemes(txt.Substring(0, ctr));
                    token.setDescription("decimallit");
                    tokens.Add(token);
                }
                else if (decLit)
                {
                    foreach (char delim in litDelim.delim_dec)
                    {
                        if ((txt.Length - 1) >= ctr)
                        {
                            if (txt.ElementAt(ctr) == delim)
                            {
                                hasToken = true;
                                break;
                            }
                        }
                    }
                    if (hasToken)
                    {
                        token.setTokens("decimallit");
                        token.setLexemes(txt.Substring(0, ctr));
                        token.setDescription("decimallit");
                        tokens.Add(token);
                    }
                    else
                    {
                        hasToken = true;
                        token.setTokens("No Delimiter");
                        token.setLexemes(txt.Substring(0, ctr));
                        token.setDescription("decimallit");
                        tokens.Add(token);
                    }
                }

                break;
            }

            ctra = ctr;
            return(hasToken);
        }