Ejemplo n.º 1
0
        /// <summary>
        /// Moves to the end of quoted text and returns the text within the quotes. Discards the
        /// quote characters. Assumes the current position is at the starting quote character.
        /// </summary>
        public string ParseQuotedText()
        {
            // Get quote character
            char quote = Peek();

            // Jump to start of quoted text
            MoveAhead();
            // Parse quoted text
            StringBuilder builder = new StringBuilder();

            while (!EndOfText)
            {
                char c = Peek();
                if (c == quote)
                {
                    // End of quoted string
                    MoveAhead();
                    break;
                }
                else if (c == '\r' || c == '\n')
                {
                    // Terminate string at newline
                    Lexer.OnError(ErrorCode.NewLineInString);
                    break;
                }
                else if (c == Escape)
                {
                    // Escaped character
                    MoveAhead();
                    if (!EndOfText)
                    {
                        c = Peek();
                        if (EscapeCharacterLookup.TryGetValue(c, out char replacement))
                        {
                            builder.Append(replacement);
                        }
                        else if (c == '\r' || c == '\n')
                        {
                            // Allow newline in string if escaped
                            builder.Append(c);
                            if (c == '\r' && Peek(1) == '\n')
                            {
                                MoveAhead();
                                builder.Append(Peek());
                            }
                        }
                        else
                        {
                            // Unknown escape sequence; just include entire sequence
                            builder.Append(Escape);
                            builder.Append(c);
                        }
                    }
                }
                else
                {
                    builder.Append(c);
                }
                MoveAhead();
            }
            return(builder.ToString());
        }