Пример #1
0
        private static bool TryTakeStringLiteral(StringKeeper stringKeeper, out StringKeeper newStringKeeper, out Token token)
        {
            if (stringKeeper.Next == '\'')
            {
                var start = stringKeeper.Take();
                var work  = new StringKeeper(start);
                while (!work.IsEmpty)
                {
                    if (work.IsNext("\'\'"))
                    {
                        work = work.Take(2);
                    }
                    else
                    {
                        if (work.Next == '\'')
                        {
                            var literalValue = work.Difference(start).Replace("''", "'");

                            token           = new Token(TokenType.StringLiteral, literalValue);
                            work            = work.Take();
                            newStringKeeper = work;

                            if (!IsTerminator(work))
                            {
                                do
                                {
                                    work = work.Take();
                                } while (!work.IsEmpty && !work.IsWhitespace);

                                token.TokenType = TokenType.Error;
                                return(true);
                            }

                            return(true);
                        }
                        else
                        {
                            work = work.Take();
                        }
                    }
                }

                newStringKeeper = work;
                token           = new Token(TokenType.Error, work.Difference(stringKeeper));
                return(true);
            }

            newStringKeeper = stringKeeper;
            token           = null;
            return(false);
        }
Пример #2
0
        private static bool TryTakeIdentifier(StringKeeper stringKeeper, out StringKeeper newStringKeeper, out Token token)
        {
            var work = new StringKeeper(stringKeeper);

            if (CanStartIdentifier(work.Next))
            {
                work = work.Take();
                while (ValidIdentifierChar(work.Next))
                {
                    work = work.Take();
                }

                if (work.IsEmpty || work.IsWhitespace)
                {
                    var difference = work.Difference(stringKeeper);
                    newStringKeeper = work;
                    token           = new Token(TokenType.Identifier, difference);
                    return(true);
                }
            }

            newStringKeeper = stringKeeper;
            token           = null;
            return(false);
        }
Пример #3
0
        private static bool TryTakeNumeric(StringKeeper stringKeeper, out StringKeeper newStringKeeper, out Token token)
        {
            if (char.IsDigit(stringKeeper.Next))
            {
                var invalidNumber = false;
                var work          = new StringKeeper(stringKeeper);
                var doubleValue   = false;
                do
                {
                    work = work.Take();
                    if (!work.IsEmpty && work.Next == '.' && !doubleValue)
                    {
                        doubleValue = true;
                        work        = work.Take();

                        if (!char.IsDigit(work.Next))
                        {
                            invalidNumber = true;
                        }
                    }
                } while (!work.IsEmpty && char.IsDigit(work.Next));

                var value   = work.Difference(stringKeeper);
                var longInt = false;
                if (!work.IsEmpty && work.Next == 'L')
                {
                    longInt = true;
                    work    = work.Take();
                }

                if (!IsTerminator(work))
                {
                    do
                    {
                        work = work.Take();
                    } while (!work.IsWhitespace && !work.IsEmpty);

                    token           = new Token(TokenType.Error, work.Difference(stringKeeper));
                    newStringKeeper = work;
                    return(true);
                }

                TokenType tokenType;
                if (invalidNumber || (longInt && doubleValue))
                {
                    tokenType = TokenType.Error;
                }
                else
                {
                    if (doubleValue)
                    {
                        tokenType = TokenType.DoubleLiteral;
                    }
                    else
                    {
                        tokenType = longInt ? TokenType.LongLiteral : TokenType.IntLiteral;
                    }
                }

                token           = new Token(tokenType, value);
                newStringKeeper = work;
                return(true);
            }

            newStringKeeper = stringKeeper;
            token           = null;
            return(false);
        }