コード例 #1
0
        /// <summary>
        ///     Tries to tokenize the line and adds its tokens to the <paramref name="tokens" />
        /// </summary>
        /// <param name="line"></param>
        /// <param name="tokens"></param>
        /// <returns></returns>
        private bool TryTokenizeLine(LineOfFile line, Queue <Token> tokens)
        {
            List <string> tokenValues = Regex.Split(line.LineText, TokenSeparatorRegexPattern).ToList();

            tokens.Enqueue(new Token(TokenType.BeginLine, String.Format("Line number: {0}", line.LineNumber)));

            foreach (string tokenValue in tokenValues)
            {
                // Guard: ignore blank tokenValues
                if (String.IsNullOrWhiteSpace(tokenValue))
                {
                    continue;
                }

                Token?token;
                if (TryGetStringLiteralToken(tokenValue, out token))
                {
                    tokens.Enqueue(token.Value);
                    continue;
                }
                if (TryGetIntegerLiteralToken(tokenValue, out token))
                {
                    tokens.Enqueue(token.Value);
                    continue;
                }

                return(false);
            }
            tokens.Enqueue(new Token(TokenType.EndLine, String.Format("Line number: {0}", line.LineNumber)));
            return(true);
        }
コード例 #2
0
        /// <summary>
        ///     Retrieves the next applicable/valid line of the stream if one exists
        /// </summary>
        /// <param name="currentLineOfFile">The current line of the file</param>
        /// <param name="streamReader">A <see cref="StreamReader" /> for the file</param>
        /// <param name="nextLineOfFile">The next non blank/comment line of the file</param>
        /// <returns></returns>
        private bool TryReadNextLine(LineOfFile?currentLineOfFile, StreamReader streamReader,
                                     out LineOfFile?nextLineOfFile)
        {
            // set current line number
            int currentLineNumber = 0;

            if (null != currentLineOfFile)
            {
                currentLineNumber = currentLineOfFile.Value.LineNumber;
            }

            int nextLineNumber = currentLineNumber + 1;

            while (!streamReader.EndOfStream)
            {
                string nextLineText = RemoveCommentsAndTrailingWhiteSpace(streamReader.ReadLine());

                // when we find the next non empty line return it!
                if (!String.IsNullOrWhiteSpace(nextLineText))
                {
                    nextLineOfFile = new LineOfFile(nextLineNumber, nextLineText);
                    return(true);
                }

                // increment next line number
                nextLineNumber++;
            }

            nextLineOfFile = null;
            return(false);
        }