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

Syntax errors for string and int #7952

Merged
merged 10 commits into from Feb 13, 2023
Merged

Conversation

bobhy
Copy link
Contributor

@bobhy bobhy commented Feb 3, 2023

Description

Added a few syntax errors in ints and strings, changed parser to stop and show that error rather than continue trying to parse those tokens as some other shape. However, I don't see how to push this direction much further, and most of the classic confusing errors can't be changed.

Flagged as WIP for the moment, but passes all checks and works better than current release:

  1. I have yet to figure out how to make these errors refer back to the book, as I see some other errors do.
  2. How to give syntax error when malformed int is first token in line? Currently parsed as external command, user gets confusing error message.
  3. Would like to be more strict with decimal int literals (lacking, e.g, `0x' prefix). Need to tinker more with the order of parse shape calls, currently, float is tried after int, so '1.4' has to be passed.

(Description of your pull request goes here. Provide examples and/or screenshots if your changes affect the user experience.)

"\z"
Error: 
   ╭─[entry #3:1:1]
 1 │ "\z"
   ·  ─┬─
   ·   ╰── Syntax error in string, unrecognized character after escape '\'.
   ╰────

Canonic presentation of a syntax error.

"  \u{01ffbogus}"
Error: 
  × Invalid syntax
   ╭─[entry #2:1:1]
 1 │ "  \u{01ffbogus}"
   ·    ───────┬──────
   ·           ╰── Syntax error in string, expecting 1 to 6 hex digits in unicode escape '\u{X...}', max value 10FFFF.
   ╰────

Malformed unicode escape in string, flagged as error.
String parse can be opinionated, it's the last shape tried.

〉0x22bogus
Error: nu::shell::external_command (link)
  × External command failed
   ╭─[entry #4:1:1]
1 │ 0x22bogus
   · ────┬────
   ·     ╰── executable was not found
   ╰────
  help: No such file or directory (os error 2)

A correct number in first token would be evaluated, but an incorrect one is treated as external command? Confusing to users.

〉0 + 0x22bogus
Error: 
  × Invalid syntax
   ╭─[entry #5:1:1]
1 │ 0 + 0x22bogus
   ·     ────┬────
   ·         ╰── Syntax error in int, invalid digits in radix 16 int.
   ╰────

Can give syntax error if token is unambiguously int literal. e.g has 0b or 0x prefix, could not be a float.

〉0 + 098bogus
Error: nu::parser::unsupported_operation (link)

  × Types mismatched for operation.
   ╭─[entry #6:1:1]
 1 │ 0 + 098bogus
   · ┬ ┬ ────┬───
   · │ │     ╰── string
   · │ ╰── doesn't support these values.
   · ╰── int
   ╰────
  help: Change int or string to be the right types and try again.

But decimal literal (no prefix) can't be too strict. Parser is going to try float later. So '1.4' must be passed.

User-Facing Changes

First and foremost, more specific error messages for typos in string and int literals. Probably improves interactive user experience.

But a script that was causing and then checking for specific error might notice a different error message.

(List of all changes that impact the user experience here. This helps us keep track of breaking changes.)

Tests + Formatting

Added (positive and negative unit tests in cargo test -p nu-parser. Didn't add integration tests.

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace to check that all tests pass

After Submitting

If your PR had any user-facing changes, update the documentation after the PR is merged, if necessary. This will help us keep the docs up to date.

@bobhy bobhy marked this pull request as draft February 3, 2023 06:01
@fdncred
Copy link
Collaborator

fdncred commented Feb 3, 2023

Thanks for this PR. I'm very excited about making error messages better. I think this is a killer feature of nushell that we should continue to invest in making better.

One thing that helps drive home your changes is to have before and after examples in the PR. I just did "\z" to see what it was before and wow, that new message is so much more clear.

@bobhy bobhy marked this pull request as ready for review February 3, 2023 15:10
@bobhy
Copy link
Contributor Author

bobhy commented Feb 3, 2023

De nada! I'd like to be able to do more (than the current nothing) on the decimal literal int, and, as mentioned above, on the literal int as first position in the expression. And I have an idea it'd like to experiment with to enable syntax errors in many of the other leaf node terms. But I won't have much time this coming week to polish them. If you think there's value in the PR as is, I'll submit it for review and let you merge it, then come back for phase 2 later on. I can update the docs and release notes for this PR now.

@sholderbach
Copy link
Member

Thanks for tackling the parser beast and getting our error messages closer to our promises!

Great to see more helpful messages for the constraints on the literal/escape syntax.

One general concern I have that might not be touched by this PR is that we shouldn't necessarily try to turn Type errors into Syntax errors. I can see that creating supportive "did-you-mean" error messages when parsing the code anyways is often more straightforward when you can include a parsing of the unhappy path for enriched suggestions. But if we were to encode too many semantics of the type system at parse time, we could potentially have many places that define type semantics with a higher chance of breakage. (The comment around the literal parse order, seems to speak of past traumatic experiences there)

@bobhy
Copy link
Contributor Author

bobhy commented Feb 6, 2023 via email

@fdncred
Copy link
Collaborator

fdncred commented Feb 9, 2023

@bobhy Do you think this PR is ready to land?

@bobhy
Copy link
Contributor Author

bobhy commented Feb 9, 2023

I will wrap this up in the next few hours and confirm then. Sorry for the radio silence, family visiting!

@fdncred
Copy link
Collaborator

fdncred commented Feb 9, 2023

No worries at all. Just wanted to check in on ya. Take your time.

@bobhy
Copy link
Contributor Author

bobhy commented Feb 10, 2023

Assuming CI checks pass, this PR is ready for review. It does add syntax errors for hex and octal literals, but does nothing for decimal or float literals. I could not shoehorn those into the parser code without getting way too clever.
I'll open a new issue to discuss potential parser changes, and kick it off with the kinds of errrors I'd like to be able to catch.

Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for getting our literals into a much better shape!

Preamble: You are probably now more of an expert on our parser than me. So take my comments with a grain of salt.

The only thing jumping out to me were your fixme comments, wondering i we are in a good enough place with them or if they are out of date.
Else I am maybe a bit pedantic about the naming of our error variants as they might be used a bit randomly in some places already (more a problem with ShellError)

Wondering if we should the unrelated Cargo.lock version bump as that might cause churn if we want to revert something.

crates/nu-parser/src/parser.rs Show resolved Hide resolved
crates/nu-parser/src/errors.rs Outdated Show resolved Hide resolved
"filesize".into(),
"non-filesize unit".into(),
Some(ParseError::Expected(
"filesize with valid units".into(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an error variant where we could display the valid units as a help text? Or will this ParseError::Expected always be hidden through a later parse?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::Expected will be hidden in ::Any shape, can't say about other contexts. I had to change the variant to make parse_filesize() play nice in the ::Any shape. If it issues any kind of terminating (and therefore user-visible) error, it'll mask what might be a valid bare string, like an executable name. For example:

〉7day
1wk
--------------------------------------------------------------
〉7da
Error: nu::shell::external_command (link)

  × External command failed
   ╭─[entry #181:1:1]
 1 │ 7da
   · ─┬─
   ·  ╰── executable was not found
   ╰────
  help: No such file or directory (os error 2)

but if the token were 7zip, you would not want parse_duration() issuing a terminating error. So we have to figure out how to stop calling many of the entity parsers on what might be the name of an executable.

crates/nu-parser/src/parser.rs Show resolved Hide resolved
crates/nu-parser/tests/test_parser.rs Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
@bobhy
Copy link
Contributor Author

bobhy commented Feb 10, 2023 via email

sholderbach added a commit to sholderbach/nushell that referenced this pull request Feb 12, 2023
This disables automatic detection of `#[bench]` and other benchmarks
within the crates. Our benchmarks should all live in `benches`

This fixes a problem with criterion flags and should also reduce the
build requirements for `cargo bench` a bit

Taken from nushell#7952

See: https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options
sholderbach added a commit to bobhy/nushell that referenced this pull request Feb 12, 2023
@codecov
Copy link

codecov bot commented Feb 12, 2023

Codecov Report

Merging #7952 (7cc3913) into main (208ffdc) will decrease coverage by 0.01%.
The diff coverage is 73.13%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7952      +/-   ##
==========================================
- Coverage   54.26%   54.25%   -0.01%     
==========================================
  Files         608      608              
  Lines       98967    98985      +18     
==========================================
+ Hits        53701    53708       +7     
- Misses      45266    45277      +11     
Impacted Files Coverage Δ
crates/nu-parser/src/errors.rs 1.31% <0.00%> (-0.02%) ⬇️
crates/nu-parser/src/parser.rs 74.42% <74.24%> (-0.14%) ⬇️
crates/nu-color-config/src/style_computer.rs 80.76% <0.00%> (-0.55%) ⬇️
crates/nu-protocol/src/value/mod.rs 47.97% <0.00%> (+0.07%) ⬆️

sholderbach added a commit that referenced this pull request Feb 12, 2023
# Description

This disables automatic detection of `#[bench]` and other benchmarks
within the crates. Our benchmarks should all live in `benches`

This fixes a problem with criterion flags and should also reduce the
build requirements for `cargo bench` a bit

Taken from #7952

See:
https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options


# User-Facing Changes

None
@sholderbach
Copy link
Member

AH you force pushed over my commit to avoid a merge conflict.

Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's give this a go!

@sholderbach sholderbach added this pull request to the merge queue Feb 13, 2023
Merged via the queue into nushell:main with commit 007916c Feb 13, 2023
@bobhy bobhy deleted the invalid_syntax_error branch April 7, 2023 12:42
bobhy added a commit to bobhy/nushell that referenced this pull request Oct 22, 2023
* Release notes for `0.76`

Please add your important new features and breaking changes to the release notes by committing to/opening a PR against the `release-notes-0.76` branch. Thank you!

* Add breaking change for plugin signature (nushell#775)

* add breaking change

* Update blog/2023-02-21-nushell_0_76.md

---------

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>

* add some info on debugging commands

* release notes for nushell#7952 (nushell#777)

* release notes for nushell#7952

* Fix html tags that broke CI

* more debug notes

* Add `profile` note and screenshot (nushell#778)

* add ast to debug commands section

* add breaking change (nushell#790)

* Remove example stuff

Don't let the lorem ipsum loose

* added more breaking changes notes

* trim down error message documentation in blog post

* Add description of some commands

* Do some polishing. sequence multiplication

* Screenshot help of a plugin

* Add section on nu plugin

* Add section on background work and full log

* Executive summary

* Details to "mul"

---------

Co-authored-by: WindSoilder <WindSoilder@outlook.com>
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
Co-authored-by: Bob Hyman <bob.hyman@gmail.com>
Co-authored-by: Jakub Žádník <kubouch@gmail.com>
Co-authored-by: Reilly Wood <reilly.wood@icloud.com>
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.

None yet

3 participants