Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Parsing (Mike Houghton)
2. Re: Parsing (Francesco Ariis)
3. Re: Parsing (Mike Houghton)
4. Re: Parsing (Francesco Ariis)
5. Re: Parsing (Mike Houghton)
----------------------------------------------------------------------
Message: 1
Date: Sun, 6 Mar 2016 09:26:17 +0000
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Parsing
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
Hi, I?m using Parsec to parse structured text and have some problems with
mandatory and optional text entries and the order in which they occur.
For example
module{
name = some string
source = ?
dest = ?..
bundle = ?
bundle_dest = ?
zip_name = ?.
}
In the above text the name and source are mandatory and all others are
optional. I?m not quite sure how to handle the optional parts once I?ve parsed
name and source.
Should I enforce the rule that if dest does appear then it must be first in the
list?
In this situation how should I make use of the various Parsec functions such
as choice, option, optionMaybe, lookAhead etc.
Thanks
Mike
------------------------------
Message: 2
Date: Sun, 6 Mar 2016 10:30:19 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Parsing
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
On Sun, Mar 06, 2016 at 09:26:17AM +0000, Mike Houghton wrote:
> Hi, I?m using Parsec to parse structured text and have some problems
> with mandatory and optional text entries and the order in which they
> occur.
> For example:
>
> module{
>
> name = some string
> source = ?
>
> dest = ?..
> bundle = ?
> bundle_dest = ?
> zip_name = ?.
>
> }
Hello Mike,
I would personally keep it simple. Parse the (item, value) list
and after that check if it is well formed (i.e. contains 'name' and
'source' values). If not, call `parserFail` with an appropriate
message.
------------------------------
Message: 3
Date: Sun, 6 Mar 2016 09:54:34 +0000
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Parsing
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hi Francesco,
Quick response! Thanks.
I see, so would it reduce to something like?
many itemValue
and originally I was thinking the data structure that it would parse into would
be
data Module = Module {? some record structure?}
but now it would be roughly like?
type Entry = (String, String)
data Module = Module [Entry]
Thanks
> On 6 Mar 2016, at 09:30, Francesco Ariis <[email protected]> wrote:
>
> On Sun, Mar 06, 2016 at 09:26:17AM +0000, Mike Houghton wrote:
>> Hi, I?m using Parsec to parse structured text and have some problems
>> with mandatory and optional text entries and the order in which they
>> occur.
>> For example:
>>
>> module{
>>
>> name = some string
>> source = ?
>>
>> dest = ?..
>> bundle = ?
>> bundle_dest = ?
>> zip_name = ?.
>>
>> }
>
> Hello Mike,
> I would personally keep it simple. Parse the (item, value) list
> and after that check if it is well formed (i.e. contains 'name' and
> 'source' values). If not, call `parserFail` with an appropriate
> message.
> _______________________________________________
> Beginners mailing list
> [email protected] <mailto:[email protected]>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20160306/e7952ddf/attachment-0001.html>
------------------------------
Message: 4
Date: Sun, 6 Mar 2016 11:17:19 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Parsing
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
On Sun, Mar 06, 2016 at 09:54:34AM +0000, Mike Houghton wrote:
> Hi Francesco,
> Quick response! Thanks.
>
> I see, so would it reduce to something like?
> many itemValue
>
> and originally I was thinking the data structure that it would parse
> into would be
>
> data Module = Module {? some record structure?}
>
> but now it would be roughly like?
>
> type Entry = (String, String)
>
> data Module = Module [Entry]
>
> Thanks
Yes, it would lead to some kind of (YourType, String) association list.
If you are more interested in a datatype with records I see two
ways of achieving it:
a. a function `[(YrType, String)] -> RecordsData` (not so
pretty but doable, also you can check for well-formedness here)
(Using a sum type YrType is in my opinion better than plain
Strings as it catches some more errors at compile time).
b. directly via parsing, using `optionMaybe` and glue.
Depending on how your input is structured this may or may not be
more hairy (can name and source appear after an optional tag?
What about duplicated tags? etc.).
In its simplest form you can use a succinct applicative-style,
but the castle crumbles if want more.
See which fits better (I suspect a.), play with it and report
back; parsing has never been an elegant business!
------------------------------
Message: 5
Date: Sun, 6 Mar 2016 10:48:11 +0000
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Parsing
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
Hi Francesco,
This is really helpful - thank you.
I?m neutral about using a record - it is just how I?d done other simple
parsers before.
Using a list of tuples seems to simplify the parsing but then the ?hit? comes
on converting to a record.
However, this has made me question the necessity of a record, I suspect the
processing subsequent to parsing can be better achieved by
mapping over the list.
Thanks
> On 6 Mar 2016, at 10:17, Francesco Ariis <[email protected]> wrote:
>
> On Sun, Mar 06, 2016 at 09:54:34AM +0000, Mike Houghton wrote:
>> Hi Francesco,
>> Quick response! Thanks.
>>
>> I see, so would it reduce to something like?
>> many itemValue
>>
>> and originally I was thinking the data structure that it would parse
>> into would be
>>
>> data Module = Module {? some record structure?}
>>
>> but now it would be roughly like?
>>
>> type Entry = (String, String)
>>
>> data Module = Module [Entry]
>>
>> Thanks
>
> Yes, it would lead to some kind of (YourType, String) association list.
> If you are more interested in a datatype with records I see two
> ways of achieving it:
>
> a. a function `[(YrType, String)] -> RecordsData` (not so
> pretty but doable, also you can check for well-formedness here)
> (Using a sum type YrType is in my opinion better than plain
> Strings as it catches some more errors at compile time).
>
> b. directly via parsing, using `optionMaybe` and glue.
> Depending on how your input is structured this may or may not be
> more hairy (can name and source appear after an optional tag?
> What about duplicated tags? etc.).
> In its simplest form you can use a succinct applicative-style,
> but the castle crumbles if want more.
>
>
> See which fits better (I suspect a.), play with it and report
> back; parsing has never been an elegant business!
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 93, Issue 4
****************************************