Skip to content
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

Float parser comitting to leading 'e' #44

Open
j-maas opened this issue Apr 14, 2020 · 3 comments
Open

Float parser comitting to leading 'e' #44

j-maas opened this issue Apr 14, 2020 · 3 comments

Comments

@j-maas
Copy link

j-maas commented Apr 14, 2020

Similar to #28, the float parser comitts when it encounters an 'e', making it impossible to have a parser that reads either a float or some text:

floatOrText =
    oneOf
        [ float |> map (\n -> "Number: " ++ String.fromFloat n)
        , chompUntilEndOr "\n" |> getChompedString
    ]

run floatOrText "not starting with e" --> Ok "not starting with e"
run floatOrText "1e10" --> Ok "Number: 10000000000"
run floatOrText "e should be text" --> Err: ExpectingFloat

(Try it on Ellie: https://ellie-app.com/8yp6MxtzSsna1)

@j-maas
Copy link
Author

j-maas commented May 5, 2020

As a workaround, one can define their own float parser:

{-| The built-in float parser has a bug with leading 'e'.
See <https://github.com/elm/parser/issues/44>
-}
parseFloat : Parser Float
parseFloat =
    {- By making it backtrackable, even if the input start with an 'e',
       we will be able to try out other alternatives instead of getting
       stuck on it as an invalid number.
    -}
    Parser.backtrackable <| Parser.float ExpectingFloat InvalidNumber

@AlienKevin
Copy link

int parser has the same problem of committing the leading 'e'

AlienKevin added a commit to AlienKevin/lambda-calculus-untyped that referenced this issue May 21, 2020
AlienKevin added a commit to AlienKevin/lambda-calculus-untyped that referenced this issue May 21, 2020
@dwayne
Copy link

dwayne commented May 4, 2022

I ran in to this same problem:

term : Parser Expr
term =
  P.oneOf
    [ P.succeed Number
        |= number
    , P.succeed EParam
        |= param
    ]

Given,

Parser.run term "E"

it returns

Err [{ col = 2, problem = ExpectingInt, row = 1 }]

instead of

Ok (EParam "E")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants