Re: uu-parsinglib pKeyword

pToken [] = pSucceed [] pToken (x:xs) = (:) <$> pSym x <*> pToken xs pKeyword_Float = pToken "Float" etc Doaitse PS: this function has been defined in the module Text.ParserCombinators.UU.Derived On 28 okt 2009, at 17:39, Ozgur wrote:
Hi everybody,
I am using the uu-parsinglib to parse a structured language and map the results to some proper data structures. Thanks to Prof Doaitse Swierstra (and other authors if any), it is fun to write a parser using this library.
I've been sending private mails to Doaitse about my questions, who kindly gives nice replies everytime. But this time I thought I can ask my question to the community, and give everyone the chance to benefit from the answers.
[After the intro, here comes my real question]
I am trying to capture the following pattern.
pKeyword_Int = ( \ _ _ _ -> "int" ) <$> pSym 'i' <*> pSym 'n' <*> pSym 't' pKeyword_Float = ( \ _ _ _ _ _ -> "float" ) <$> pSym 'f' <*> pSym 'l' <*> pSym 'o' <*> pSym 'a' <*> pSym 't'
As you can see there is an obvious pattern if you try to capture a "keyword". If there were a function called pKeyword taking a string as an argument and producing the necessary parser, things would be easier.
What I mean is,
pKeyword_Int = pKeyword "int" pKeyword_Float = pKeyword "float"
I tried to create this pKeyword function myself but I couldn't manage to do it.
I can feel that, one can simply add a "<* pReturn []" to the ends of every parser and write a recursion with this base condition.
Any suggestions?
PS: Actually I'm a little bit uncomfortable since there may be such a function in the library already :)

One of the parser combinator libraries for Clean had the (<:>)
combinator that captures the idiom (sorry!) in Doaitse's version.
Defined applicatively it would be:
(<:>) :: Applicative f => f a -> f [a] -> f [a]
(<:>) p1 p2 = (:) <$> p1 <*> p2
so pToken would be
pToken [] = pSucced []
pToken (x:xs) = pSym x <:> pToken xs
Admittedly this is not as succinct as
pToken = traverse pSym
... but I have used it from time to time.
Regards,
Stephen
2009/10/28 Ross Paterson
On Wed, Oct 28, 2009 at 06:07:49PM +0100, S. Doaitse Swierstra wrote:
pToken [] = pSucceed [] pToken (x:xs) = (:) <$> pSym x <*> pToken xs
pKeyword_Float = pToken "Float"
pToken = traverse pSym _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Even though it was nice to see how it can be implemented, I'll be using the
"PS" version :)
I was really close in implementing it myself.
Anyway thanks for the quick reply.
2009/10/28 S. Doaitse Swierstra
pToken [] = pSucceed [] pToken (x:xs) = (:) <$> pSym x <*> pToken xs
pKeyword_Float = pToken "Float" etc
Doaitse
PS: this function has been defined in the module Text.ParserCombinators.UU.Derived
On 28 okt 2009, at 17:39, Ozgur wrote:
Hi everybody,
I am using the uu-parsinglib to parse a structured language and map the results to some proper data structures. Thanks to Prof Doaitse Swierstra (and other authors if any), it is fun to write a parser using this library.
I've been sending private mails to Doaitse about my questions, who kindly gives nice replies everytime. But this time I thought I can ask my question to the community, and give everyone the chance to benefit from the answers.
[After the intro, here comes my real question]
I am trying to capture the following pattern.
pKeyword_Int = ( \ _ _ _ -> "int" ) <$> pSym 'i' <*> pSym 'n' <*> pSym 't' pKeyword_Float = ( \ _ _ _ _ _ -> "float" ) <$> pSym 'f' <*> pSym 'l' <*> pSym 'o' <*> pSym 'a' <*> pSym 't'
As you can see there is an obvious pattern if you try to capture a "keyword". If there were a function called pKeyword taking a string as an argument and producing the necessary parser, things would be easier.
What I mean is,
pKeyword_Int = pKeyword "int" pKeyword_Float = pKeyword "float"
I tried to create this pKeyword function myself but I couldn't manage to do it.
I can feel that, one can simply add a "<* pReturn []" to the ends of every parser and write a recursion with this base condition.
Any suggestions?
PS: Actually I'm a little bit uncomfortable since there may be such a function in the library already :)
-- Ozgur Akgun

And of course thanks for all the other replies and versions.
This mailing list is *really* active!
2009/10/28 Ozgur Akgun
Even though it was nice to see how it can be implemented, I'll be using the "PS" version :) I was really close in implementing it myself.
Anyway thanks for the quick reply.
2009/10/28 S. Doaitse Swierstra
pToken [] = pSucceed []
pToken (x:xs) = (:) <$> pSym x <*> pToken xs
pKeyword_Float = pToken "Float" etc
Doaitse
PS: this function has been defined in the module Text.ParserCombinators.UU.Derived
On 28 okt 2009, at 17:39, Ozgur wrote:
Hi everybody,
I am using the uu-parsinglib to parse a structured language and map the results to some proper data structures. Thanks to Prof Doaitse Swierstra (and other authors if any), it is fun to write a parser using this library.
I've been sending private mails to Doaitse about my questions, who kindly gives nice replies everytime. But this time I thought I can ask my question to the community, and give everyone the chance to benefit from the answers.
[After the intro, here comes my real question]
I am trying to capture the following pattern.
pKeyword_Int = ( \ _ _ _ -> "int" ) <$> pSym 'i' <*> pSym 'n' <*> pSym 't' pKeyword_Float = ( \ _ _ _ _ _ -> "float" ) <$> pSym 'f' <*> pSym 'l' <*> pSym 'o' <*> pSym 'a' <*> pSym 't'
As you can see there is an obvious pattern if you try to capture a "keyword". If there were a function called pKeyword taking a string as an argument and producing the necessary parser, things would be easier.
What I mean is,
pKeyword_Int = pKeyword "int" pKeyword_Float = pKeyword "float"
I tried to create this pKeyword function myself but I couldn't manage to do it.
I can feel that, one can simply add a "<* pReturn []" to the ends of every parser and write a recursion with this base condition.
Any suggestions?
PS: Actually I'm a little bit uncomfortable since there may be such a function in the library already :)
-- Ozgur Akgun
-- Ozgur Akgun
participants (4)
-
Ozgur Akgun
-
Ross Paterson
-
S. Doaitse Swierstra
-
Stephen Tetley