public ParserResults Parse() { var results = new ParserResults(); var scanner = new StringScanner(_source); var matched = true; while (matched && scanner.RemainingLength > 0) { var start = scanner.Position; scanner.Skip(@"^\s+"); var keyPart = scanner.Scan(@"^((?<key>-\w)[\s]?|(?<key>--.*?)(\s|$))"); var keyOffset = scanner.Position; var valuePart = scanner.Scan(@"^(?!-)(\""(?<value>.*?)\""|(?<value>.*?)(\s|$))"); matched = keyPart != null || valuePart != null; var arg = _processor.ArgumentConfiguration(keyPart == null ? _processor.DefaultArgument : keyPart.Groups["key"].Value); if(arg != null && arg.Flag && valuePart != null) { if(keyPart != null) scanner.Position -= valuePart.Length; valuePart = null; } if(arg == null) { if(keyOffset > -1) { results.UnresolvedParts.Add(_source.Substring(start, keyOffset - start).Trim()); results.UnresolvedParts.Add(_source.Substring(keyOffset, scanner.Position - keyOffset).Trim()); } else results.UnresolvedParts.Add(_source.Substring(start, scanner.Position - start).Trim()); continue; } if (arg.Flag) valuePart = null; if (!arg.TakesManyValues && results.ValuePairs.Any(v => v.Key == arg)) { results.UnresolvedParts.Add(_source.Substring(start, scanner.Position - start)); } else { results.ValuePairs.Add(new KeyValuePair<Argument, string>(arg, valuePart == null ? "True" : valuePart.Groups["value"].Value)); } } if (scanner.RemainingLength > 0) { throw new ParseException(string.Format("Invalid arguments value: {0}", _source)); } return results; }
public void IncrementsOnMatch() { var scanner = new StringScanner("foobar"); scanner.Scan("^foo"); Assert.That(scanner.Remaining, Is.EqualTo("bar")); }
public void DoesntIncrementWithoutMatch() { var scanner = new StringScanner("foobar"); scanner.Scan("^bar"); Assert.That(scanner.Remaining, Is.EqualTo("foobar")); }