File tree Expand file tree Collapse file tree 2 files changed +43
-7
lines changed Expand file tree Collapse file tree 2 files changed +43
-7
lines changed Original file line number Diff line number Diff line change @@ -16,17 +16,18 @@ type Format
1616
1717type Outcome
1818 = Integer { format: Format, value : Int }
19+ | FloatingPoint Float
1920
2021
2122type Error
2223 = NotANumber
2324 | LeadingZero
25+ | TerminatingDot
2426
2527
2628parser : Parser c Error Outcome
2729parser =
28- Parser.succeed {}
29- |> Parser.skip (Parser.chompIf (\c -> c == '-' || Char.isDigit c) NotANumber)
30+ Parser.chompIf (\c -> c == '-' || Char.isDigit c) NotANumber
3031 |> Parser.skip (Parser.chompWhile Char.isDigit)
3132 |> Parser.getChompedString
3233 |> Parser.andThen
@@ -40,9 +41,27 @@ parser =
4041 Parser.problem NotANumber
4142
4243 Just num ->
43- { format = Decimal
44- , value = num
45- }
46- |> Integer
47- |> Parser.succeed
44+ Parser.oneOf
45+ [ Parser.chompIf (\c -> c == '.') NotANumber
46+ |> Parser.skip (Parser.chompWhile Char.isDigit)
47+ |> Parser.getChompedString
48+ |> Parser.andThen
49+ (\postDot ->
50+ if postDot == "." then
51+ Parser.problem TerminatingDot
52+
53+ else
54+ when String.toFloat (str ++ postDot) is
55+ Nothing ->
56+ Parser.problem NotANumber
57+
58+ Just float ->
59+ Parser.succeed (FloatingPoint float)
60+ )
61+ , Parser.succeed <|
62+ Integer
63+ { format = Decimal
64+ , value = num
65+ }
66+ ]
4867 )
Original file line number Diff line number Diff line change @@ -24,6 +24,23 @@ tests =
2424 run "-012"
2525 |> expectErr PN.LeadingZero
2626 ]
27+ , describe "Floats"
28+ [ fuzz (Fuzz.floatRange 0 32000) "Can parse regular floats" <| \float ->
29+ run (String.fromFloat float)
30+ |> Result.map
31+ (\outcome ->
32+ when outcome is
33+ PN.Integer { value } ->
34+ PN.FloatingPoint (toFloat value)
35+
36+ _ ->
37+ outcome
38+ )
39+ |> Expect.equal (Ok (PN.FloatingPoint float))
40+ , test "Requires a number after ." <| \{} ->
41+ run "0."
42+ |> expectErr PN.TerminatingDot
43+ ]
2744 ]
2845
2946
You can’t perform that action at this time.
0 commit comments