
Hi dear list, while using parsec for a project I ran into this issue. Take the following program: import Text.ParserCombinators.Parsec import Text.ParserCombinators.Parsec.Language import Text.ParserCombinators.Parsec.Token main = do run "0xf" run "xf" run str = putStrLn $ str ++ " parses as: " ++ show (parse p "" str) p = hexadecimal $ makeTokenParser emptyDef ----- Running it yields: 0xf parses as: Left (line 1, column 1): unexpected "0" xf parses as: Right 15 ----- Looking at the code for hexadecimal in Parsec.Token it looks like: hexadecimal = do{ oneOf "xX"; number 16 hexDigit } clearly not handling the leading zero as the docs states: hexadecimal :: CharParser st Integer Parses a positive whole number in the hexadecimal system. The number should be prefixed with "0x" or "0X". Returns the value of the number. Same goes for 'octal' in the same module. Parsing using 'natural' works though: nat = zeroNumber <|> decimal zeroNumber = do{ char '0' ; hexadecimal <|> octal <|> decimal <|> return 0 } Also, neither hexadecimal nor octal eat the trailing whitespaces as natural, integer and float does (using lexme). Am I missing something? Cheers, Lennart K

Your observations are correct. The documentation at http://legacy.cs.uu.nl/daan/parsec.html would need to be updated by Daan (which will not happen, I'm afraid). Parsec's haddock documentation is poor. I would recommend not to use Text.ParserCombinators.Parsec.Token and Text.ParserCombinators.Parsec.Language for your own language, but only take out those bits from the sources that you need for your language and remain "portable". Cheers Christian Lennart Kolmodin wrote:
Hi dear list,
while using parsec for a project I ran into this issue. Take the following program:
import Text.ParserCombinators.Parsec import Text.ParserCombinators.Parsec.Language import Text.ParserCombinators.Parsec.Token
main = do run "0xf" run "xf"
run str = putStrLn $ str ++ " parses as: " ++ show (parse p "" str)
p = hexadecimal $ makeTokenParser emptyDef
-----
Running it yields:
0xf parses as: Left (line 1, column 1): unexpected "0" xf parses as: Right 15
-----
Looking at the code for hexadecimal in Parsec.Token it looks like: hexadecimal = do{ oneOf "xX"; number 16 hexDigit } clearly not handling the leading zero as the docs states:
hexadecimal :: CharParser st Integer Parses a positive whole number in the hexadecimal system. The number should be prefixed with "0x" or "0X". Returns the value of the number.
Same goes for 'octal' in the same module.
Parsing using 'natural' works though:
nat = zeroNumber <|> decimal
zeroNumber = do{ char '0' ; hexadecimal <|> octal <|> decimal <|> return 0 }
Also, neither hexadecimal nor octal eat the trailing whitespaces as natural, integer and float does (using lexme).
Am I missing something?
Cheers, Lennart K
participants (2)
-
Christian Maeder
-
Lennart Kolmodin