diff --git a/00_Common/dotnet/Games.Common/IO/TokenReader.cs b/00_Common/dotnet/Games.Common/IO/TokenReader.cs index 3219c459..04e7ad41 100644 --- a/00_Common/dotnet/Games.Common/IO/TokenReader.cs +++ b/00_Common/dotnet/Games.Common/IO/TokenReader.cs @@ -1,10 +1,13 @@ using System; using System.Collections.Generic; - +using System.Linq; using static Games.Common.IO.Strings; namespace Games.Common.IO; +/// +/// Reads from input and assembles a given number of values, or tokens, possibly over a number of input lines. +/// internal class TokenReader { private readonly TextIO _io; @@ -16,9 +19,26 @@ internal class TokenReader _isTokenValid = isTokenValid ?? (t => true); } + /// + /// Creates a which reads string tokens. + /// + /// A instance. + /// The new instance. public static TokenReader ForStrings(TextIO io) => new(io, t => true); + + /// + /// Creates a which reads tokens and validates that they can be parsed as numbers. + /// + /// A instance. + /// The new instance. public static TokenReader ForNumbers(TextIO io) => new(io, t => t.IsNumber); + /// + /// Reads valid tokens from one or more input lines and builds a list with the required quantity. + /// + /// The string used to prompt the user for input. + /// The number of tokens required. + /// The sequence of tokens read. public IEnumerable ReadTokens(string prompt, uint quantityNeeded) { if (quantityNeeded == 0) @@ -39,6 +59,12 @@ internal class TokenReader return tokens; } + /// + /// Reads a line of tokens, up to , and rejects the line if any are invalid. + /// + /// The string used to prompt the user for input. + /// The maximum number of tokens to read. + /// The sequence of tokens read. private IEnumerable ReadValidTokens(string prompt, uint maxCount) { while (true) @@ -62,6 +88,12 @@ internal class TokenReader } } + /// + /// Lazily reads up to tokens from an input line. + /// + /// The string used to prompt the user for input. + /// The maximum number of tokens to read. + /// private IEnumerable ReadLineOfTokens(string prompt, uint maxCount) { var tokenCount = 0; diff --git a/00_Common/dotnet/Games.Common/IO/Tokenizer.cs b/00_Common/dotnet/Games.Common/IO/Tokenizer.cs index fdaf829e..dc6a4d79 100644 --- a/00_Common/dotnet/Games.Common/IO/Tokenizer.cs +++ b/00_Common/dotnet/Games.Common/IO/Tokenizer.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; namespace Games.Common.IO; +/// +/// A simple state machine which parses tokens from a line of input. +/// internal class Tokenizer { private const char Quote = '"';