<div dir="ltr"><div><div>I think you are right, this is probably the right track. A little more googling with permutation parsers gave me this, which is also about parsing iCal using parsec:<br><a href="http://stackoverflow.com/questions/3706172/haskell-parsec-and-unordered-properties">http://stackoverflow.com/questions/3706172/haskell-parsec-and-unordered-properties</a><br>
<br></div>I&#39;ll review all this and see if that solves the problem... Thank you!<br><br></div>Emmanuel<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Dec 25, 2012 at 3:28 AM, Brent Yorgey <span dir="ltr">&lt;<a href="mailto:byorgey@seas.upenn.edu" target="_blank">byorgey@seas.upenn.edu</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Emmanuel,<br>
<br>
Sounds like you want a permutation parser, perhaps?  Check out<br>
<br>
  <a href="http://hackage.haskell.org/packages/archive/parsec/latest/doc/html/Text-Parsec-Perm.html" target="_blank">http://hackage.haskell.org/packages/archive/parsec/latest/doc/html/Text-Parsec-Perm.html</a><br>
<br>
-Brent<br>
<div><div class="h5"><br>
On Tue, Dec 25, 2012 at 12:18:37AM +0100, Emmanuel Touzery wrote:<br>
&gt; Hi,<br>
&gt;<br>
&gt;  I&#39;m trying to parse ical files but the source material doesn&#39;t matter<br>
&gt; much. First, I know there is an icalendar library on hackage, but I&#39;m<br>
&gt; trying to learn as well through this.<br>
&gt;<br>
&gt;  Now the format is really quite simple and actually I&#39;m parsing it, it<br>
&gt; works, but I don&#39;t like the code I&#39;m writing, it feels wrong and I&#39;m sure<br>
&gt; there is a better way. Actually for now I&#39;m parsing it to an array of<br>
&gt; arrays, but I want to fill a proper &quot;data&quot; structure.<br>
&gt;<br>
&gt;  For my purpose the file contains a bunch of records like this:<br>
&gt;<br>
&gt; BEGIN:VEVENT<br>
&gt; DTSTART:20121218T103000Z<br>
&gt; DTEND:20121218T120000Z<br>
&gt; [..]<br>
&gt; DESCRIPTION:<br>
&gt; [..]<br>
&gt; END:VEVENT<br>
&gt;<br>
&gt; There are a bunch of records I don&#39;t care about and also I want to parse no<br>
&gt; matter what is the order of directives (so, i want to parse also if DTEND<br>
&gt; appears before DTSTART for instance, and so on).<br>
&gt;<br>
&gt; That last part is my one problem. I can&#39;t do:<br>
&gt;<br>
&gt; parseBegin<br>
&gt; start &lt;- parseStart<br>
&gt; end &lt;- parseEnd<br>
&gt; skipRows<br>
&gt; desc &lt;- parseDesc<br>
&gt; skipRows<br>
&gt; end &lt;- parseEnd<br>
&gt; return Event { eventStart = start, eventEnd = end ...}<br>
&gt;<br>
&gt; my current working code is:<br>
&gt;<br>
&gt; parseEvent = do<br>
&gt;     parseBegin<br>
&gt;     contents &lt;- many1 $ (try startDate)<br>
&gt;             &lt;|&gt; (try endDate)<br>
&gt;             &lt;|&gt; (try description)<br>
&gt;             &lt;|&gt; unknownCalendarInfo<br>
&gt;     parseEnd<br>
&gt;     return contents<br>
&gt;<br>
&gt; But then contents of course returns an array, while I want to return only<br>
&gt; one element here.<br>
&gt;<br>
&gt; SOMEHOW what I would like is:<br>
&gt;<br>
&gt; parseEvent = do<br>
&gt;     parseBegin<br>
&gt;     contents &lt;- many1 $ (start &lt;- T.try startDate)<br>
&gt;             &lt;|&gt; (end &lt;- T.try endDate)<br>
&gt;             &lt;|&gt; (desc &lt;- T.try description)<br>
&gt;             &lt;|&gt; unknownCalendarInfo<br>
&gt;     parseEnd<br>
&gt;     return Event { eventStart = start, eventEnd = end ...}<br>
&gt;<br>
&gt;  But obviously as far as Parsec is concerned startDate could occur several<br>
&gt; times and also it&#39;s just not valid Haskell syntax.<br>
&gt;<br>
&gt;  So, any hint about this problem? Parsing multi-line records with Parsec,<br>
&gt; when I don&#39;t know the order in which the lines will appear? I mean sure I<br>
&gt; can convert my array to the proper data structure... I find which element<br>
&gt; in the array contains the start date and then which contains the end<br>
&gt; date... and build my data structure.. But I&#39;m sure something much nicer can<br>
&gt; be done... I just can&#39;t find how.<br>
&gt;<br>
&gt;  I see the author of iCalendar fixed the problem but I can&#39;t completely<br>
&gt; understand his source, it&#39;s too many things at the same time for me, I need<br>
&gt; to take this one step at a time.<br>
&gt;<br>
&gt;  Thank you!<br>
&gt;<br>
&gt; Emmanuel<br>
<br>
</div></div>&gt; _______________________________________________<br>
&gt; Beginners mailing list<br>
&gt; <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>