[Haskell-cafe] parsec get line number of lexems

Patrick Chilton chpatrick at gmail.com
Mon Jul 18 14:50:24 UTC 2016


Assuming you have a constructor Binary Op Pos Expr Expr, you can do:

Infix (Binary Multiply <$> getPosition <* reservedOp "*") AssocLeft

See Control.Applicative for the operators.

On Mon, Jul 18, 2016 at 4:28 PM, Olivier Duhart <olivier.duhart at gmail.com>
wrote:

> thanks for your answer, exactly what I was looking for. Now I have parsers
> like this :
>
> ifStmt :: Parser Stmt
> ifStmt =
>   do reserved "if"
>      *pos <- getPosition*
>      cond  <- expression
>      reserved "then"
>      stmt1 <- statement
>      reserved "else"
>      stmt2 <- statement
>      return $ If cond stmt1 stmt2 *pos*
>
> My AST is not fully "positioned" though as I don't get how to add position
> in expression (more precisely on operators).
> I am using the buildExpressionParser to build expression parsers so I have
>
> operators = [ [Prefix (reservedOp "-"   >> return (Neg ))          ]
>              , [Infix  (reservedOp "*"   >> return (Binary Multiply))
> AssocLeft, .....
>
> And I dont known how to tell parsec to add a SourcePos to my operators.
>
> Any suggestion please ?
>
>
>
>
> Le dimanche 17 juillet 2016 19:09:32 UTC+2, Francesco Ariis a écrit :
>>
>> On Sun, Jul 17, 2016 at 09:50:27AM -0700, Olivier Duhart wrote:
>> > is ther a way to get the line numbers at parsers level?
>>
>> Hi Olivier,
>>     maybe you are looking for
>>
>>
>> http://hackage.haskell.org/package/parsec-3.1.11/docs/Text-Parsec.html#v:getPosition
>>
>> (which is very simple and used mainly for error reporting) or you could
>> just keep track of `currLine` into a State of your choice (the 's' in
>> ParsecT s ...).
>> Does that help?
>> -F
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
>>
>>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160718/288fe393/attachment.html>


More information about the Haskell-Cafe mailing list