-
Notifications
You must be signed in to change notification settings - Fork 46
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
int parser fails on trailing decimal separator #14
Comments
I suspect this is not a bug, but it's definitely a legitimate concern. IP address parsing provides a concrete example of how the Potential solutionNote: Someone more skilled than I may know how to change digitChain : Parser (Maybe Int)
digitChain =
getChompedString (chompWhile Char.isDigit)
|> Parser.map String.toInt Demo: https://ellie-app.com/4hJzJjnPQ3Ma1 Of course, even this isn't general purpose. As hinted at in the docs, you may still need to make choices about how to handle Some thoughts on why this isn't a bugHere's the underlying implementation of From Advanced.elm int : x -> x -> Parser c x Int
int expecting invalid =
number
{ int = Ok identity
, hex = Err invalid
, octal = Err invalid
, binary = Err invalid
, float = Err invalid
, invalid = invalid
, expecting = expecting
} Here we see that the I'm pretty sure this means that the digits-followed-by-a-dot-that's-not-a-decimal problem can't be solved without a fundamental change in both architecture and behavior for all of the parsers in the number family. I'm also pretty sure that such a shift would go against the library's core goals of simplicity and speed. |
@clozach Also you are completely wrong about current parser following core goals - actually it's exact opposite as it does NOT follow first two of the three core goals:
|
Hey @malaire. Sounds like this is causing you some frustration. I'm tempted to mount an argument that I'm not completely wrong, but I'm too familiar with Evan's thoughts on building community to expect any joy from that direction. But if you'll concede that I'm only partially wrong, well…I'd be surprised if that were not the case! 😉 If you're currently stuck trying to resolve something that Or, if you're primarily interested in proving a point (more power to you), how about implementing your own number parsing functions and posting them as a package so the whole community can benefit? If neither of those appeal, how about writing up an experience report? I'd love to learn more about what it is you were trying to do, and what you experienced when |
As one of the individual bitten by this (I was the Discourse thread author whom @malaire and others assisted), allow me to mount an argument for why this is an unexpected behaviour. Expectation based on DocumentationIn the Parser library, there is the
The implication is that a parser ought to succeed greedily by consuming until the last character that would not produce a parse error. If the user expects an explicit end, one can use the This would allow type alias IntegerAndMantissa =
{ integer: Int
, mantissa: Int
}
splitIntegerAndMantissa : Parser IntegerAndMantissa
splitIntegerAndMantissa =
succeed IntegerAndMantissa
|= int
|. symbol "."
|= int
main = Html.text <| Debug.toString (run splitIntegerAndMantissa "42.56") Without the ability to use or If the user did not want the int to be followed by a period, then perhaps: succeed identity
|= int
|. end
-- succeeds with the input "42" but fails with "42." Potential Solution at the Library levelChanging the default behaviour of My solution eschews changing the current behaviour, and instead introduce a new function: The behaviour as described in the expectation above will be mapped onto this new functions, leaving the current The idea is that in cases where we want to just parse the raw number in a greedy fashion, ignoring interpretations of the period for decimal point (which isn't even universal, to begin with!), we can use The expected behaviour for SummaryConcepts such as Introducing a new function |
Even this
Therefore even if the current
This one will only accept digits, including leading zeroes, without consuming more characters. One alternative could be to write a very configurable function in a user package, maybe something like |
This library contains operators. Its API can not be replicated by third-party programmers. |
Hi,
I have found that the
int
parser fails on the input"42."
. I would have expected thatint
parses the"42"
, and leaves"."
to be consumed later on.Here is an Ellie demonstrating the problem: https://ellie-app.com/3gJS4VwRJrfa1
If this is not a bug, I would propose to add documentation describing that behavior and how to work around it.
The text was updated successfully, but these errors were encountered: