-
Notifications
You must be signed in to change notification settings - Fork 28
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
Store raw string for numbers in the AST #108
Comments
Will a user want to do math with the Integer or Floatable value? If it contains a string instead of an Int or Float then we've left it up to them to parse the string and they might forget to handle edge cases like hex values. Maybe we could instead have an opaque type that contains the number and helper functions like |
Add tests to document what currently works and what doesn't. Also, remove a precision loss in the function to map to JSON values: Encode integers as strings to avoid precision loss here. The current representation in the Elm syntax parser causes a precision loss for integer literals: https://github.com/stil4m/elm-syntax/blob/3d4ee78c007ee8987b8cfe6c3ea07e5500632b6b/src/Elm/Syntax/Expression.elm#L115-L116 For discussion of this issue, see also stil4m/elm-syntax#108
Add tests to document what currently works and what doesn't. Also, remove a precision loss in the function to map to JSON values: Encode integers as strings to avoid precision loss here. The current representation in the Elm syntax parser causes a precision loss for integer literals: https://github.com/stil4m/elm-syntax/blob/3d4ee78c007ee8987b8cfe6c3ea07e5500632b6b/src/Elm/Syntax/Expression.elm#L115-L116 For discussion of this issue, see also stil4m/elm-syntax#108
I don't think people will do math on the expressions no, but I think people will potentially want to pattern match on the numbers |
Currently, if we try to parse the following code
we'll get an AST with the following node
It's not much, but there is a precision loss. If we were to write that number using the writer, we would not get the same output as the input.
My thinking is that we could keep the original "text" in the same node, like
and then the writer could re-use that original text from the AST.
elm-review
rules could also use this information for other reasons, though I can't think of any.Downside: It's possible to have different values represented by
a
andb
inInteger a b
(though that is also true currently in a way...), but now it's even easier because you can doInteger 1 "2"
, or evenInteger 1 ""
.I think the
Floatable
variant should use the same approach.I'm thinking we could remove the
Hex
variant in favor ofInteger Int String
. I think in most cases (at least inelm-review
rules), we don't care how an integer is written but we care about the value more. If we care about integers written in hex, it's reasonably easy to check whether the string starts with0x
.If Elm ever adds support for writing numbers like
10_000
, then we will need to update the parser but we won't need to change theExpression
type.(This was inspired by issue avh4/elm-format#690, where the problem seems to appear for smaller numbers than this one.)
The text was updated successfully, but these errors were encountered: