Skip to content

Text.Read.Lex: valid integer lexes are missed

Summary

Observed when writing a parser for a data format where integers (never decimal numbers) could be separated by one or more '.' characters:

λ> reads "1." :: [(Int,String)]
[(1,".")]    -- expected
λ> reads "1..1" :: [(Int,String)]
[(1,"..1")]  -- expected
λ> reads "1.1" :: [(Int,String)]
[]           -- where is my int?  I expected [(1,".1")]

Environment

Observed on GHC 9.2 and 9.4, on x86-64. But it seems likely that all supported GHC versions and platforms are affected.

Additional info

Text.Read.Lex.lex seems to greedily commit to a "decimal" lex if possible, and ignores the valid "int part only" lex. See https://hackage.haskell.org/package/base-4.17.0.0/docs/src/Text.Read.Lex.html#lexDecNumber.

λ> readP_to_S Text.Read.Lex.lex "1.1"
[(Number (MkDecimal [1] (Just [1]) Nothing),"")]

Conversion of this Number lexeme to an Int then fails, as expected.

This issue seems related to #8924.

Edited by Fraser Tweedale
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information