in src/Engines/InputParser.cs [29:70]
public CommandType GetNextCommand(string input, out ISymbol? symbol, out string commandInput, out string remainingInput)
{
if (input == null) { throw new ArgumentNullException("input"); }
symbol = null;
commandInput = input;
remainingInput = string.Empty;
var inputLines = input.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList();
if (inputLines == null || inputLines.Count == 0)
{
return CommandType.Mundane;
}
// Find the first non-whitespace line and see if it starts with a magic symbol.
bool isHelp = false;
int firstLineIndex = inputLines.FindIndex(s => !string.IsNullOrWhiteSpace(s));
if (firstLineIndex < 0 || !StartsWithMagic(inputLines[firstLineIndex], out symbol, out isHelp))
{
// No magic symbol found.
return isHelp ? CommandType.Help : CommandType.Mundane;
}
// Look through the remaining lines until we find one that
// starts with a magic symbol.
string? _commandInput = null;
for (int lineIndex = firstLineIndex + 1; lineIndex < inputLines.Count; lineIndex++)
{
if (StartsWithMagic(inputLines[lineIndex], out _, out _))
{
_commandInput = string.Join(Environment.NewLine, inputLines.SkipLast(inputLines.Count - lineIndex));
remainingInput = string.Join(Environment.NewLine, inputLines.Skip(lineIndex));
break;
}
}
// If we didn't find another magic symbol, use the full input
// as the command input.
commandInput = _commandInput ?? input;
return isHelp ? CommandType.MagicHelp : CommandType.Magic;
}