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

Handle number exponents with signs in filter #80

Merged
merged 1 commit into from
Jun 26, 2023

Conversation

baodrate
Copy link
Contributor

1e-1 and 1e+1 in filters are now parsed correctly.

closes #79

`1e-1` and `1e+1` in filters are now parsed correctly
@baodrate
Copy link
Contributor Author

first time working with chumsky (or rust, really), so apologies if it correctly appears like I don't know what I"m doing.

I also noticed that:

  • jq allows .5 in both filters and JSON even though it's not a valid number according to JSON spec
    • Actually found this in some .jq files I had. But I prefer jaq's stricter JSON parsing so not sure it's worth it to implement this for only filter parsing and breaking consistency between the two syntaxes
  • jaq preserves 2e3 literally when it's in the input JSON (jq produces 2000), but when 2e3 in the filter produces 2000.0

@01mf02
Copy link
Owner

01mf02 commented Jun 26, 2023

That's looking good --- congratulations for having figured this out without previous Rust and chumsky experience!

  • jaq preserves 2e3 literally when it's in the input JSON (jq produces 2000), but when 2e3 in the filter produces 2000.0

This is on purpose. jaq tries to keep JSON input faithfully, because its contents might be unknown to the user, whereas jaq optimises the filter, because it is supposed to be known to the user. If we would not do this, then a simple filter like . + 1.0 would convert 1.0 from a string representation to a floating-point representation for every single input value, which would have a massive impact on performance.

@01mf02 01mf02 merged commit a280379 into 01mf02:main Jun 26, 2023
@baodrate
Copy link
Contributor Author

That's looking good --- congratulations for having figured this out without previous Rust and chumsky experience!

😄

jaq tries to keep JSON input faithfully, because its contents might be unknown to the user, whereas jaq optimises the filter, because it is supposed to be known to the user

That's reasonable. I was more-so pointing out that 2e3 gets transformed into 2000.0 where I would assume an int (2e3 -> 2*10^3 -> 2*1000 -> 2000). not a big deal though because you probably want a float if you're working with scientific notation (see relevant discussion), and actually handling this would complicate the parsing beyond the current isize.from_str()

@baodrate baodrate deleted the signed-exp branch July 7, 2023 10:14
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

Successfully merging this pull request may close these issues.

negative exponent in numeric literals
2 participants