Two questions regarding Alex/Parsec

Hello Haskellers, As an experiment I'm writing a parser for MediaWiki language. I'm using the Alex lexer and Parsec. I have novice two questions. (I've tried using Happy but I was unable to get a grammar file to compile, which must be because I have no real experience using Yacc and the like. I've also attempted to write an ad-hoc recursive descent parser, but that become too complex to be fun very quickly.) == SourcePos and AlexPosn == My first question is about Parsec's SourcePos. I have a definition like: type LexToken = (AlexPosn, Token) type MWP a = GenParser LexToken () a toSourcePos :: AlexPosn -> SourcePos toSourcePos (AlexPn _ line col) = undefined mwtok :: (Token -> Maybe a) -> MWP a mwtok test = token showToken posToken posTest where showToken = show . snd posToken = toSourcePos . fst posTest = test . snd The definition of toSourcePos isn't 100% satisfactory. The constructor SourcePos is hidden, so I cannot simply use (SourcePos line col). Is there a way to convert an AlexPosn value to a SourcePos? == Data Constructors as Parameters and Tags == Secondly, I frequently encounter the pattern where I try to match a token, return Just some result if it matches, and Nothing otherwise. So I tried to fix this pattern in a nice reusable function. onTok0 :: Token -> a -> MWP a onTok0 t x = mwtok (\tok -> if tok `isTok` t then Just x else Nothing) This seems to work, but I'd also like a definition for data constructors taking one parameter: onTok1 :: (a -> Token) -> (a -> b) -> MWP b onTok1 t f = mwtok (\tok -> case tok of (t n) = Just $ f n _ = Nothing) This doesn't work, the pattern match (t n) fails, which is understandable: n is used as a parameter to t and a matched result, which looks like a paradox. t is a function (constructor) but also a tag. Is it possible to pass a tag as an argument? Or is there another way to extract this pattern? Regards, Niels

nielsadb:
Hello Haskellers,
As an experiment I'm writing a parser for MediaWiki language. I'm using the Alex lexer and Parsec. I have novice two questions.
Just a quick question, did you try using the pandoc markdown parser? http://hackage.haskell.org/packages/archive/pandoc/0.46/doc/html/Text-Pandoc... It'd be useful to know if that was enough for printing and generation of wiki content. Though being GPLd, it'd also be nice to have a BSD-ish licensed wiki parser/pretty printer. -- Don

On Tue, 2008-06-24 at 14:08 -0700, Don Stewart wrote:
Just a quick question, did you try using the pandoc markdown parser?
http://hackage.haskell.org/packages/archive/pandoc/0.46/doc/html/Text-Pandoc...
It'd be useful to know if that was enough for printing and generation of wiki content. Though being GPLd, it'd also be nice to have a BSD-ish licensed wiki parser/pretty printer.
No, I did not. In fact I didn't know about this project, but it looks very interesting. I'll definitely go and look at the code. One of the reasons I started worked on a MediaWikiParser was because I wanted to use the Haskell language and some of its tools rather than just read about them. I'm not really interested in making a clone of something that already exists just for the sake of ideology (although personally I'm much more fond of BSD-style licenses than of GPL). On a side note: are there any lexer/parser generators out there I should be aware of? As mentioned in my original post I've tried using Happy, but I was unable to understand its very concise error message 'parE'. It would be really nice to have a tool that generates an AST automatically (e.g. something like ANTLR). Regards, Niels

nielsadb:
On Tue, 2008-06-24 at 14:08 -0700, Don Stewart wrote:
Just a quick question, did you try using the pandoc markdown parser?
http://hackage.haskell.org/packages/archive/pandoc/0.46/doc/html/Text-Pandoc...
It'd be useful to know if that was enough for printing and generation of wiki content. Though being GPLd, it'd also be nice to have a BSD-ish licensed wiki parser/pretty printer.
No, I did not. In fact I didn't know about this project, but it looks very interesting. I'll definitely go and look at the code.
One of the reasons I started worked on a MediaWikiParser was because I wanted to use the Haskell language and some of its tools rather than just read about them. I'm not really interested in making a clone of something that already exists just for the sake of ideology (although personally I'm much more fond of BSD-style licenses than of GPL).
Personally, I'd find a MediaWiki parser/pretty printer quite useful!
On a side note: are there any lexer/parser generators out there I should be aware of? As mentioned in my original post I've tried using Happy, but I was unable to understand its very concise error message 'parE'. It would be really nice to have a tool that generates an AST automatically (e.g. something like ANTLR).
There's lots, but probably Alex (lexer) and Happy (parser) or Parsec are the most widely used. -- Don

On Wed, 2008-06-25 at 10:46 -0700, Don Stewart wrote:
One of the reasons I started worked on a MediaWikiParser was because I wanted to use the Haskell language and some of its tools rather than just read about them. I'm not really interested in making a clone of something that already exists just for the sake of ideology (although personally I'm much more fond of BSD-style licenses than of GPL).
Personally, I'd find a MediaWiki parser/pretty printer quite useful!
Oh yes, don't yet me wrong, this isn't just a study project for me; I really want to have a working parser. I'm just saying that if there was such a parser for Haskell already under the GPL I wouldn't be bothered rewriting it just so I could license it under MIT/BSD.
On a side note: are there any lexer/parser generators out there I should be aware of? As mentioned in my original post I've tried using Happy, but I was unable to understand its very concise error message 'parE'. It would be really nice to have a tool that generates an AST automatically (e.g. something like ANTLR).
There's lots, but probably Alex (lexer) and Happy (parser) or Parsec are the most widely used.
Okay, in that case I'll stick to Alex + Parsec, it's quite comfortable so far. By the way, does anyone know the answer to my original question? Especially the first one (Alex' AlexPosn vs. Parsec's SourcePos) is worrying me somewhat. Regards, Niels

On Wed, 2008-06-25 at 20:17 +0200, Niels Aan de Brugh wrote:
By the way, does anyone know the answer to my original question? Especially the first one (Alex' AlexPosn vs. Parsec's SourcePos) is worrying me somewhat.
To answer my own question: as it turns out I was using an old version of Parsec (2.1.0, the one that comes with Ubuntu Linux). A newer version of Parsec (3.0.0) adds a newPos function in Text.Parsec.Pos that trivially constructs a new SourcePos. Regards, Niels
participants (2)
-
Don Stewart
-
Niels Aan de Brugh