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

Parser.int accepts input starting with e #25

Open
ccapndave opened this issue Dec 19, 2018 · 1 comment
Open

Parser.int accepts input starting with e #25

ccapndave opened this issue Dec 19, 2018 · 1 comment

Comments

@ccapndave
Copy link

I'm not exactly sure what is going on, or what is being accepted, but here is an example of Parser.int eating input starting with the letter e (although it doesn't succeed, it needs to be backtracked in order for a following parser to succeed).

https://ellie-app.com/4cD7gfngmNVa1

module Main exposing (main)

import Browser
import Html exposing (Html, br, button, div, text)
import Parser exposing (..)
import Set


{-| An Ellie example showing some parsing madness
-}
type Expr
    = Int Int
    | Func String String


main =
    div
        []
        [ text "This is what we expect: "
        , run expr """a("arg")""" |> Debug.toString |> text
        , br [] []
        , text "And this: "
        , run expr """b("something")""" |> Debug.toString |> text
        , br [] []
        , text "And this too: "
        , run expr """myFuncName("stuff")""" |> Debug.toString |> text
        , br [] []
        , br [] []
        , text "But, if the function name start with an `e`:"
        , run expr """e("what the devil?")""" |> Debug.toString |> text
        , br [] []
        , text "Another example:"
        , run expr """exerciseScoreToInt("blahblah")""" |> Debug.toString |> text
        , br [] []
        , br [] []
        , text "This doesn't happen with any other letter, and if you take away the `int` parse then it works."
        ]


expr : Parser Expr
expr =
    oneOf
        [ backtrackable int |> map Int
        , func
        ]


func : Parser Expr
func =
    succeed Func
        |= backtrackable
            (variable
                { start = Char.isLower
                , inner = Char.isAlphaNum
                , reserved = Set.empty
                }
            )
        |. backtrackable (symbol "(")
        |. symbol "\""
        |= getChompedString (chompWhile (\c -> c /= '"'))
        |. symbol "\""
        |. symbol ")"
        |. end
@ccapndave
Copy link
Author

ccapndave commented Dec 19, 2018

Comments from the chat in Slack:

mdevlamynck [5:54 PM]
https://github.com/elm/parser/blob/1.1.0/src/Parser/Advanced.elm#L734
It's trying to parse exponent apparently.

mdevlamynck [5:59 PM]
Well actually, it's a bit tricky. The parser is correct in that it does not accept things like e10 as valid Int. But before failing it advances in the input. Since it's not backtrackable by default, your oneOf does not try the other possibilities. (edited)

ccapndave [6:00 PM]
Hmm
I see

mdevlamynck [6:01 PM]
So it's more the fact that int is acts as backtrackable with certain input that is weird.

ccapndave [6:01 PM]
At the very least there should be docs about it
No-one is going to add backtrackable just in case

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

1 participant