Exemplo n.º 1
0
        /// <summary>
        /// Skip over whitespace and comments, then return the first char of the next token.
        /// (This may be '\0' if the end of the token stream is reached.)
        /// </summary>
        /// <returns>The first char of the next token.</returns>
        public char GetCharSkippingTrivia()
        {
            var inLineComment = false;

            while (true)
            {
                var c = _reader.PeekChar();

                // Report end of token stream no matter what.
                if (c == '\0')
                {
                    return(c);
                }

                // Report (and skip over) a file separator no matter what.
                if (c == CharExtensions.FileSeparator)
                {
                    return(_reader.GetChar());
                }

                // Parse \r or \n or \r\n as a newline.
                var isNewLine = false;
                if (c == '\r')
                {
                    _reader.GetChar();
                    c         = _reader.PeekChar();
                    isNewLine = true;
                }
                if (c == '\n')
                {
                    _reader.GetChar();
                    isNewLine = true;
                }
                if (isNewLine)
                {
                    inLineComment = false;
                    continue;
                }

                // Skip over non-newline whitespace.
                // While in a line comment, skip over anything that isn't a newline.
                if (c.IsWhitespace() || inLineComment)
                {
                    _reader.GetChar();
                    continue;
                }

                // This character starts the next token, unless it's the start of a line comment.
                TokenStart = _reader.CurrentSpan();
                c          = _reader.GetChar();
                if (c == '/' && _reader.PeekChar() == '/')
                {
                    _reader.GetChar();
                    inLineComment = true;
                    continue;
                }
                return(c);
            }
        }