parsec get line number of lexems

hello, I am experimenting with parsec. I am building a very simple language. I have a (almost) wholy funcrionning language : syntactic check, AST building and byte code génération. Now i want to improve error checking (type checking more precisely). this should be done on the AST basis. but in the ast i lose the line number information for precise error message. so my question is : is ther a way to get the line numbers at parsers level ? i would then store it in the ast for semantic check purpose thanks for your answers Olivier

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:get... (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

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:get...
(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.

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
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:get...
(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.

thanks for your answer, looks like what I am looking for. Will try and tell Le lundi 18 juillet 2016 16:50:36 UTC+2, Patrick Chilton a écrit :
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
javascript:> 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:get...
(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.

So I did it and it works exactly as expected. thanks again Patrick Le lundi 18 juillet 2016 16:58:49 UTC+2, Olivier Duhart a écrit :
thanks for your answer, looks like what I am looking for. Will try and tell
Le lundi 18 juillet 2016 16:50:36 UTC+2, Patrick Chilton a écrit :
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
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:get...
(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.
participants (3)
-
Francesco Ariis
-
Olivier Duhart
-
Patrick Chilton