-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from NixOS/fix-floats
Don't rewrite floats and don't break e+ syntax
- Loading branch information
Showing
5 changed files
with
48 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,34 @@ | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
|
||
module Nixfmt.Parser.Float (floatParse) where | ||
|
||
import "base" Control.Monad (void) | ||
import qualified "base" Data.Char as Char | ||
import "base" Data.Foldable (foldl') | ||
import "base" Data.Proxy (Proxy (..)) | ||
import "megaparsec" Text.Megaparsec ( | ||
import Data.List (singleton) | ||
import Data.Text (Text, pack) | ||
import Text.Megaparsec ( | ||
MonadParsec, | ||
Token, | ||
chunkToTokens, | ||
notFollowedBy, | ||
many, | ||
oneOf, | ||
option, | ||
takeWhile1P, | ||
satisfy, | ||
some, | ||
try, | ||
(<?>), | ||
(<|>), | ||
) | ||
import "megaparsec" Text.Megaparsec.Char (char, char', digitChar) | ||
import "megaparsec" Text.Megaparsec.Char.Lexer (decimal, signed) | ||
import "scientific" Data.Scientific (scientific, toRealFloat) | ||
import Text.Megaparsec.Char (char, digitChar) | ||
|
||
-- copied (and modified) from Text.Megaparsec.Char.Lexer | ||
data SP = SP !Integer {-# UNPACK #-} !Int | ||
|
||
floatParse :: (MonadParsec e s m, Token s ~ Char, RealFloat a) => m a | ||
floatParse = do | ||
notFollowedBy $ char '0' >> digitChar | ||
notFollowedBy (char' 'e') | ||
c' <- (decimal <?> "decimal") <|> return 0 | ||
toRealFloat | ||
<$> ( ( do | ||
SP c e' <- dotDecimal_ c' | ||
e <- option e' (try $ exponent_ e') | ||
return (scientific c e) | ||
floatParse :: (MonadParsec e s m, Token s ~ Char, Semigroup (m [Char])) => m Text | ||
floatParse = | ||
-- This mirrors https://github.com/NixOS/nix/blob/b89eca9aecc69d4dfc0f0afd9353c126eb7b5858/src/libexpr/lexer.l#L96: | ||
-- (([1-9][0-9]*\.[0-9]*)|(0?\.[0-9]+))([Ee][+-]?[0-9]+)? | ||
pack | ||
<$> ( ( try oneThroughNineStart | ||
<|> zeroDotStart | ||
) | ||
<|> (scientific c' <$> exponent_ 0) | ||
<> scientific | ||
) | ||
{-# INLINE floatParse #-} | ||
|
||
-- copied from Text.Megaparsec.Char.Lexer | ||
dotDecimal_ :: | ||
forall e s m. | ||
(MonadParsec e s m, Token s ~ Char) => | ||
Integer -> | ||
m SP | ||
dotDecimal_ c' = do | ||
void (char '.') | ||
let mkNum = foldl' step (SP c' 0) . chunkToTokens @s Proxy | ||
step (SP a e') c = | ||
SP | ||
(a * 10 + fromIntegral (Char.digitToInt c)) | ||
(e' - 1) | ||
mkNum <$> takeWhile1P (Just "digit") Char.isDigit | ||
{-# INLINE dotDecimal_ #-} | ||
|
||
-- copied from Text.Megaparsec.Char.Lexer | ||
exponent_ :: (MonadParsec e s m, Token s ~ Char) => Int -> m Int | ||
exponent_ e' = do | ||
void (char' 'e') | ||
(+ e') <$> signed (return ()) decimal | ||
{-# INLINE exponent_ #-} | ||
where | ||
oneThroughNineStart = oneThroughNine <> many digitChar <> (singleton <$> char '.') <> many digitChar | ||
zeroDotStart = option "" (singleton <$> char '0') <> (singleton <$> char '.') <> some digitChar | ||
scientific = option "" ((singleton <$> oneOf "Ee") <> option "" (singleton <$> oneOf "+-") <> some digitChar) | ||
oneThroughNine = singleton <$> satisfy (\c -> '1' <= c && c <= '9') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[ | ||
(-9223372036854775807) | ||
(-1) | ||
0 | ||
1 | ||
# 01 | ||
# 0000000000000000000000000000000000000000000001 | ||
9223372036854775807 | ||
|
||
.1 | ||
0.1 | ||
1.0 | ||
.1e0 | ||
1.0e0 | ||
1.e0 | ||
.1e-0 | ||
1.0e-0 | ||
1.e-0 | ||
.1e+0 | ||
1.0e+0 | ||
1.e+0 | ||
|
||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# https://github.com/NixOS/nixfmt/issues/122 | ||
(1 + 1) (1 + 0.4) | ||
(1 + 1) (1 + .4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# https://github.com/NixOS/nixfmt/issues/122 | ||
(1 + 1) (1 + 0.4) | ||
(1 + 1) (1 + .4) |