parser
parser copied to clipboard
Parser.int accepts input starting with `e`
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
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
e10as validInt. But before failing it advances in the input. Since it's not backtrackable by default, youroneOfdoes not try the other possibilities. (edited)
ccapndave [6:00 PM] Hmm I see
mdevlamynck [6:01 PM] So it's more the fact that
intis acts asbacktrackablewith 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
backtrackablejust in case