I'm using Sprache to parse a section of a file that looks like this:
OneThing=Foo
AnotherThing=Bar
YetAnotherThing=Baz
All three lines are mandatory but they can appear in any order. I have parsers for the individual lines, that look like this:
public static readonly Parser<string> OneThing = (
from open in Parse.String("OneThing=")
from rest in Parse.AnyChar.Except(Parse.LineTerminator).Many().Text()
from newLine in Parse.LineEnd
select rest
);
And I combine them to parse the whole section, like this:
public static readonly Parser<MyClass> Section = (
from oneThing in SectionGrammar.OneThing
from anaotherThing in SectionGrammar.AnotherThing
from yetAnotherThing in SectionGrammar.YetAnotherThing
select new MyClass(oneThing, anotherThing, yetAnotherThing)
);
But this only works if the lines appear in the order OneThing, AnotherThing, YetAnotherThing. How can I change this to allow the lines to appear in any order but still enforce that each line should appear once?
Any help much appreciated! Thanks
I'm quite naive with Sprache but one perhaps verbose way is to select a tuple of each option for each line then filter the tuple array in your final select. Something like:
But it feels like there should be a better way. The above also isn't very scalable if you'd said to me that there were 20 lines that had unique parsing and could be in different orders.