public IEnumerable <ICommand> Parse(string statement) { if (statement == null) { throw new ArgumentException(""); } IRule command = null; IRule current = null; List <IArgumentRule> options = new List <IArgumentRule>(); var tokens = statement.Tokenize(); var commandWord = tokens.ElementAt(0); if (!_syntax.ContainsWord(commandWord)) { throw new ArgumentException(commandWord); } command = _syntax.FindRule(commandWord); current = command; options.AddRange(command.OptionalArgs); if (!IsEnoughArguments(current, commandWord, tokens)) { throw new ArgumentNotEnoughException(commandWord); } yield return(new Command(commandWord, action: command.Action)); foreach (var word in tokens.Skip(1)) { if (current.RequiredArgs.ContainsWord(word)) { current = current.RequiredArgs.FindRule(word); options.AddRange(current.OptionalArgs); if (!IsEnoughArguments(current, word, tokens)) { throw new ArgumentNotEnoughException(word); } yield return(new Command(word, action: current.Action)); continue; } var wordSplited = word.SplitIntoKeyValue(); var option = options.FindRule(wordSplited.Key); if (!option.IsNullOrEmpty()) { if (!IsEnoughArguments(current, word, tokens)) { throw new ArgumentNotEnoughException(current.Name); } yield return(new Command(wordSplited.Key, wordSplited.Value, option.Action)); continue; } throw new ArgumentException(word); } }
public IEnumerable <ICandidate> GenerateCandidates(string sentence) { Contract.Ensures(Contract.Result <IEnumerable <ICandidate> >() != null); if (sentence == null) { return(Empty); } var commandTokens = sentence.Tokenize(); var command = commandTokens.ElementAt(0); var args = commandTokens.Skip(1); if (_syntax.ContainsWord(command)) { return(GetArgumentCandidates(command, args).OrderBy(x => x.Name)); } else { if (commandTokens.Count() > 1) { return(Empty); } return(GetCommandCandidates(command).OrderBy(x => x.Name)); } }