Skip to content

Parse numbers as strings #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Nixfmt/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Data.Char (isAlpha)
import Data.Foldable (toList)
import Data.Functor (($>))
import Data.Maybe (fromMaybe, mapMaybe, maybeToList)
import Data.Text (Text)
import Data.Text (Text, pack)
import qualified Data.Text as Text
import Data.Void (Void)
import Nixfmt.Lexer (lexeme, takeTrivia, whole)
Expand Down Expand Up @@ -71,8 +71,7 @@ import Text.Megaparsec (
try,
(<|>),
)
import Text.Megaparsec.Char (char)
import qualified Text.Megaparsec.Char.Lexer as L (decimal)
import Text.Megaparsec.Char (char, digitChar)
import Prelude hiding (String)

-- HELPER FUNCTIONS
Expand Down Expand Up @@ -110,7 +109,7 @@ reserved t =
-- VALUES

integer :: Parser (Ann Token)
integer = ann Integer L.decimal
integer = ann Integer (pack <$> some digitChar)

float :: Parser (Ann Token)
float = ann Float floatParse
Expand Down
7 changes: 3 additions & 4 deletions src/Nixfmt/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ import Control.Monad.State.Strict (StateT)
import Data.Bifunctor (first)
import Data.Foldable (toList)
import Data.Function (on)
import Data.Int (Int64)
import Data.List.NonEmpty as NonEmpty
import Data.Maybe (maybeToList)
import Data.Text (Text, pack)
import Data.Text (Text)
import Data.Void (Void)
import Text.Megaparsec (Pos)
import qualified Text.Megaparsec as MP (ParseErrorBundle, Parsec, pos1)
Expand Down Expand Up @@ -488,7 +487,7 @@ instance (LanguageElement a) => LanguageElement (NonEmpty a) where
mapAllTokens f = NonEmpty.map (mapAllTokens f)

data Token
= Integer Int64
= Integer Text
| Float Text
| Identifier Text
| EnvPath Text
Expand Down Expand Up @@ -588,7 +587,7 @@ operators =

tokenText :: Token -> Text
tokenText (Identifier i) = i
tokenText (Integer i) = pack (show i)
tokenText (Integer i) = i
tokenText (Float f) = f
tokenText (EnvPath p) = "<" <> p <> ">"
tokenText KAssert = "assert"
Expand Down
8 changes: 6 additions & 2 deletions test/correct/numbers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
(-1)
0
1
# 01
# 0000000000000000000000000000000000000000000001
# https://github.com/NixOS/nixfmt/issues/292
01
0000000000000000000000000000000000000000000001
9223372036854775807
55555555555555555555555555555
(-9223372036854775808)
9223372036854775808

.1
0.1
Expand Down