Skip to content

Commit

Permalink
correct float handling of the custom parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lue-bird authored and jfmengels committed Sep 11, 2024
1 parent 09c9b69 commit 1093a42
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/ParserFast.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,24 @@ skipFloatAfterIntegerDecimal : Int -> String -> Int
skipFloatAfterIntegerDecimal offset src =
case String.slice offset (offset + 1) src of
"." ->
skip1OrMoreDigits0To9 (offset + 1) src
let
offsetAfterDigits : Int
offsetAfterDigits =
skip1OrMoreDigits0To9 (offset + 1) src
in
if offsetAfterDigits == -1 then
-1

else
case String.slice offsetAfterDigits (offsetAfterDigits + 1) src of
"e" ->
skipAfterFloatExponentMark (offsetAfterDigits + 1) src

"E" ->
skipAfterFloatExponentMark (offsetAfterDigits + 1) src

_ ->
offsetAfterDigits

"e" ->
skipAfterFloatExponentMark (offset + 1) src
Expand Down
70 changes: 70 additions & 0 deletions tests/Elm/Parser/NumbersTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,76 @@ all =
)
|> Expect.equal
(Ok 42)
, test "float" <|
\() ->
parseToResult "2.0"
(ParserFast.floatOrIntegerDecimalOrHexadecimalMapWithRange
(\_ n -> n)
(\_ _ -> -1)
(\_ _ -> -1)
)
|> Expect.equal
(Ok 2.0)
, test "integer with negative exponent" <|
\() ->
parseToResult "2e-2"
(ParserFast.floatOrIntegerDecimalOrHexadecimalMapWithRange
(\_ n -> n)
(\_ _ -> -1)
(\_ _ -> -1)
)
|> Expect.equal
(Ok 2.0e-2)
, test "integer with negative exponent (uppercase E)" <|
\() ->
parseToResult "2E-2"
(ParserFast.floatOrIntegerDecimalOrHexadecimalMapWithRange
(\_ n -> n)
(\_ _ -> -1)
(\_ _ -> -1)
)
|> Expect.equal
(Ok 2.0e-2)
, test "integer with positive exponent" <|
\() ->
parseToResult "2e+2"
(ParserFast.floatOrIntegerDecimalOrHexadecimalMapWithRange
(\_ n -> n)
(\_ _ -> -1)
(\_ _ -> -1)
)
|> Expect.equal
(Ok 2.0e2)
, test "float with negative exponent" <|
\() ->
parseToResult "2.0e-2"
(ParserFast.floatOrIntegerDecimalOrHexadecimalMapWithRange
(\_ n -> n)
(\_ _ -> -1)
(\_ _ -> -1)
)
|> Expect.equal
(Ok 2.0e-2)
, test "float with negative exponent (uppercase E)" <|
\() ->
parseToResult "2.0E-2"
(ParserFast.floatOrIntegerDecimalOrHexadecimalMapWithRange
(\_ n -> n)
(\_ _ -> -1)
(\_ _ -> -1)
)
|> Expect.equal
(Ok 2.0e-2)
, test "float with positive exponent" <|
\() ->
parseToResult "2.0e+2"
(ParserFast.floatOrIntegerDecimalOrHexadecimalMapWithRange
(\_ n -> n)
(\_ _ -> -1)
(\_ _ -> -1)
)
|> Expect.equal
(Ok 2.0e2)

-- TODO handling overflow like elm-format / the elm compiler
-- would technically be a breaking change and maybe somewhat difficult to implement
Expand Down

0 comments on commit 1093a42

Please sign in to comment.