聚合列表到字典

本文关键字:字典 列表 | 更新日期: 2023-09-27 18:03:32

我正在尝试创建一个Sprache解析器,其中输入的一部分应被解析为字典

input=some/fixed/stuff;and=a;list=of;arbitrary=key;value=pairs

and=a;list=of;arbitrary=key;value=pairs部分应该以Dictionary<string,string>结束。
这里是

    public static Parser<string> Key = Parse.CharExcept('=').Many().Text();
    public static Parser<string> Value = Parse.CharExcept(';').Many().Text();
    public static Parser<KeyValuePair<string, string>> ParameterTuple =
        from key in Key
        from eq in Parse.Char('=')
        from value in Value
        select new KeyValuePair<string, string>(key, value);

和扩展方法

    public static IEnumerable<T> Cons<T>(this T head, IEnumerable<T> rest)
    {
        yield return head;
        foreach (var item in rest)
            yield return item;
    }
    public static Parser<IEnumerable<T>> ListDelimitedBy<T>(this Parser<T> parser, char delimiter)
    {
        return
            from head in parser
            from tail in Parse.Char(delimiter).Then(_ => parser).Many()
            select head.Cons(tail);
    }

(从示例中复制)

then I try

public static Parser<IEnumerable<KVP>> KeyValuePairList = KVPair.ListDelimitedBy(';'); // KVP is just an alias for KeyValuePair<string,string>

现在我被如何写

之类的东西卡住了
public static Parser<???> Configuration =
        from fixedstuff in FixedStuff
        from kvps in Parse.Char(';').Then(_ => KeyValuePairList)
        select new ???(fixedstuff, MakeDictionaryFrom(kvps))

之类的
如何将任意key=value[;]对解析到字典中?

聚合列表到字典

用IEnumerable创建字典其实很简单,只要确保包含System.Linq。这真的只是查询是否正确并理解它的问题,在您的情况下,这是实际的字符串解析,所以我把它留给您。下面的代码构建一个字典,您只需要提供字符串和解析。

//If the parse is simple it can be done inline with the dictionary creation
private string GenerateKey(string fullString)
{
   //parse key from original string and return
}
//If the parse is simple it can be done inline with the dictionary creation
private string GenerateValue(string fullString)
{
     //Parse Values from your original string and return
}

private void UsageMethod(IEnumerable<fullString> sprachs)
{
      var dictionary = sprachs.ToDictionary(
                              fString => GenerateKey(fString), //The Key
                              fString => GenerateValue(fString) //The Value
                       );
      //Now you can use Dicitonary as it is a IDictionary<string,string>
      // so it too can be overriden an extended if need be
}

你是这个意思吗?

public static Parser<string> Key = Parse.CharExcept('=').Many().Text();
public static Parser<string> Value = Parse.CharExcept(';').Many().Text();
public static Parser<KeyValuePair<string, string>> ParameterTuple =
    from key in Key
    from eq in Parse.Char('=')
    from value in Value
    select new KeyValuePair<string, string>(key, value);
public static Parser<string> FixedStuff = Parse.String("input=some/fixed/stuff").Text();
public static Parser<Config> Configuration =
    from fixedStuff in FixedStuff
    from _ in Parse.Char(';')
    from kvps in ParameterTuple.DelimitedBy(Parse.Char(';'))
    select new Config(fixedStuff, kvps.ToDictionary(x => x.Key, x => x.Value));
public class Config
{
    public Config(string fixedStuff, IReadOnlyDictionary<string, string> dict)
    {
        FixedStuff = fixedStuff;
        Dict = dict;
    }
    public string FixedStuff { get; }
    public IReadOnlyDictionary<string, string> Dict { get; }
}
[Fact]
public void ParseIt()
{
    Configuration.Parse("input=some/fixed/stuff;and=a;list=of;arbitrary=key;value=pairs");
}