Exemple #1
0
        private Token ExtractStringToken()
        {
            int line = source.LineNumber;
            int pos = source.Position;

            StringBuilder lexeme = new StringBuilder();
            StringBuilder value = new StringBuilder();

            char current = source.GetNextChar(); // consume initial quote
            lexeme.Append('\'');

            // deal with string characters
            do
            {
                // replace any whitespace character with a blank
                if (Char.IsWhiteSpace(current))
                {
                    current = ' ';
                }

                if (current != '\'' && current != Source.END_OF_FILE)
                {
                    lexeme.Append(current);
                    value.Append(current);
                    current = source.GetNextChar(); // again consume
                }

                // quote ? each pair of adjacent quotes represent a single quote.
                if (current == '\'')
                {
                    while (current == '\'' && source.PeekNextChar() == '\'')
                    {
                        lexeme.Append("''");
                        value.Append('\'');
                        current = source.GetNextChar();
                        current = source.GetNextChar(); // consume both quotes
                    }
                }
            } while(current != '\'' && current != Source.END_OF_FILE);

            TokenBuilder builder = new TokenBuilder();
            if (current == '\'')
            {
                source.GetNextChar(); // consume final quote
                lexeme.Append('\'');
                builder.CreateTokenWithType(TokenType.STRING).WithValue(value.ToString()).WithLexeme(lexeme.ToString());
            } else
            {
                builder.CreateTokenWithType(TokenType.ERROR).WithValue(ErrorCode.UNEXPECTED_EOF);
            }
            return builder.AtPosition(pos).AtLine(line).Build();
        }