Parsec issue

Lennart Kolmodin kolmodin at dtek.chalmers.se
Tue Sep 11 05:42:27 EDT 2007


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



More information about the Libraries mailing list