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

Better error message when parsing Float32 fails #43469

Closed
jonas-schulze opened this issue Dec 18, 2021 · 3 comments
Closed

Better error message when parsing Float32 fails #43469

jonas-schulze opened this issue Dec 18, 2021 · 3 comments
Labels
parser Language parsing and surface syntax

Comments

@jonas-schulze
Copy link
Contributor

Recently, I was trying to scale a variable f and subtract x from it, 2*f - x. Without * and spaces, that leads to a malformed expression error:

julia> mirror(f, x) = x > f ? (2f - x) : x
mirror (generic function with 1 method)

julia> mirror(f, x) = x > f ? (2*f-x) : x
mirror (generic function with 1 method)

julia> mirror(f, x) = x > f ? (2f-x) : x
ERROR: syntax: malformed expression
Stacktrace:
 [1] top-level scope
   @ none:1

It took me a while to figure out that Julia was trying to parse this as a Float32 where f seperates mantissa from exponent, e.g. 2f-3. A better error message would have helped me a lot, e.g. cannot parse expression as Float32. Then, the docstring of Float32 would reveal the use of f that I didn't have in mind.

Would it be reasonable to parse 2f-X as 2f - X if X is anything but a number?
This would make 2f-x behave as expected.


Side note: if there is a variable f around, this leads to some interesting situations:

julia> f = 10
10

julia> 2f + 1
21

julia> 2*f+1
21

julia> 2f+1
20.0f0

julia> 2f+1f # (2f+1)*f
200.0f0
@ViralBShah ViralBShah added the parser Language parsing and surface syntax label Jul 19, 2023
@ViralBShah
Copy link
Member

Much nicer with the new Julia parser thanks to @c42f.

julia> mirror(f, x) = x > f ? (2f-x) : x
       ERROR: syntax: malformed expression
       Stacktrace:
        [1] top-level scope
          @ none:1
ERROR: ParseError:
# Error @ REPL[3]:1:25
mirror(f, x) = x > f ? (2f-x) : x
#                       └─┘ ── invalid numeric constant
# Error @ REPL[3]:2:25
mirror(f, x) = x > f ? (2f-x) : x
ERROR: syntax: malformed expression
#                       └─────────┘ ── extra tokens after end of expression
# Error @ REPL[3]:3:12
ERROR: syntax: malformed expression
#          ┌
Stacktrace:
 [1] top-level scope
┘ ── line break after `:` in range expression
# Error @ REPL[3]:4:5
Stacktrace:
 [1] top-level scope
#   └──────────────┘ ── extra tokens after end of expression
# Error @ REPL[3]:5:5
 [1] top-level scope
   @ none:1
#   ╙ ── whitespace is not allowed here
Stacktrace:
 [1] top-level scope
   @ none:1

@jonas-schulze
Copy link
Contributor Author

Nice. 🚀

Will this be part of Julia 1.10?

@oscardssmith
Copy link
Member

already in 1.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

3 participants