我試圖創建一個Sprache解析器,其中輸入的部分應該被解析成一個字典名單彙總到字典
input=some/fixed/stuff;and=a;list=of;arbitrary=key;value=pairs
的and=a;list=of;arbitrary=key;value=pairs
部分應該在字典<字符串結束,串>。
爲此,我已經
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);
}
(從例子中複製)
然後我試圖
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))
或類似的東西。
我該如何解析字典中的任意鍵=值[;]對?