Skip to content

Add line/column info to nimbleparses printing of HeaderError. #564

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

Merged
merged 2 commits into from
May 16, 2025

Conversation

ratmice
Copy link
Collaborator

@ratmice ratmice commented May 11, 2025

In the process replace some usage of Option with OnceCell.

It also restores some spanned error printing that was lost in commit 277ae03 when Location was in YaccGrammarError.

This is a follow up to PR #561 in that PR the errors when run through nimbleparse looked like:

Error(s) parsing `%grmtools` section:
 Unxpected token: '*', perhaps this is a glob, in which case it requires string quoting.

After this patch they include source context information,

[Error] parsing the `%grmtools` section: in foo.y
    3|     test_files: *.test,
                        Unxpected token: '*', perhaps this is a glob, in which case it requires string quoting.
[Error] parsing the `%grmtools` section: in bar.y
    3|     test_files: input*.test,
                            ^ Unxpected token: '*', perhaps this is a glob, in which case it requires string quoting.

@ratmice
Copy link
Collaborator Author

ratmice commented May 11, 2025

One thing I notice in the output is that the first instance doesn't have a ^ caret and I'm not certain why, but it will be a few days before I can investigate it.

I am also wondering whether you think it is worth trying larger follow up where we try moving this diagnostics formatting code to a support library (or just making it a hidden module in cfgrammar), and use them from within CTParserBuilder, CTLexerBuilder and nimbleparse, I believe all my other failed attempts have been to just make API so people can use whatever error printing library they want within their build.rs.

This would be a different approach where since it doesn't try to add any API where people can customize error printing, but just shares the nimbleparse error printing code between the aformentioned things?

@ltratt
Copy link
Member

ltratt commented May 12, 2025

I am also wondering whether you think it is worth trying larger follow up where we try moving this diagnostics formatting code to a support library (or just making it a hidden module in cfgrammar)

My gut reaction is to first see what we can break into a separate module within nimbleparse; and, depending on how that goes, maybe we can then put that in another crate. Warning: this might not be a good idea on my part!

@ratmice
Copy link
Collaborator Author

ratmice commented May 12, 2025

My gut reaction is to first see what we can break into a separate module within nimbleparse; and, depending on how that goes, maybe we can then put that in another crate. Warning: this might not be a good idea on my part!

The diagnostics.rs stuff is basically already is it's own module, but the problem/need to move it outside nimbleparse is more so that it can be used within CTParserBuilder and pub so that it can be used within CTLexerBuilder that was the motiviation behind proposing that it be #[doc(hidden)].

It seemed like something I haven't tried yet at least

@ltratt
Copy link
Member

ltratt commented May 15, 2025

Please squash.

ratmice added 2 commits May 15, 2025 16:14
`occupied_entry()` is documented as being for a specific use case of
mutating the flags for the *key*, using `occupied_entry()` with `insert`
runs afoul of this by assuming it can also return a value previously associated
with the key.

This is a case where the header's valueless key API doesn't coincide very
well with the existing `std::collections` entry API.  In a sense `Header`
can be occupied with a key *without* any associated value.

While for the `std::collections` entry API the existence of a key implies
the existence of a value.

This fixes the problematic usage of the API, but it would be good to revisit
the non_standard `occupied_entry()` function, and try to make another API that
is more misuse resistent such as perhaps `key_entry` which allows you to modify
the flags associated with a key, but doesn't assume the existence of a value.
Move lazy initialisation to using `OnceCell` inside the formatter.

Restore some spanned error printing that was lost in commit
277ae03 when `Location` was still
within `YaccGrammarError`.
@ratmice ratmice force-pushed the spanned_header_errors branch from c6605c4 to 6912702 Compare May 15, 2025 23:15
@ratmice
Copy link
Collaborator Author

ratmice commented May 15, 2025

Squashed, but I left a long commit message for be35407 describing that the API is somewhat prone to misuse and perhaps some future changes that could be made to avoid this kind of error in the future.

I'm not sure it is imperative that we fix that immediately, considering it's hidden API and the only other usage of the API looks fine.

@@ -141,12 +141,12 @@ fn main() {
};
let entry = match header.entry("yacckind".to_string()) {
Entry::Occupied(_) => unreachable!("Header should be empty"),
Entry::Vacant(v) => v.occupied_entry(),
Entry::Vacant(v) => v,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think rather than what I discussed in the commit message, of adding a different key_entry, it's best to just implement the mark_required and set_merge_behavior functions for VacantEntry directly, perhaps mark_used too but I can't think of a reason to mark a vacant entry used.

I still think it's best to do as a separate patch, but let me know if you feel otherwise.

Copy link
Member

Choose a reason for hiding this comment

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

Doing it in another PR is fine!

@ltratt ltratt added this pull request to the merge queue May 16, 2025
Merged via the queue into softdevteam:master with commit 361316d May 16, 2025
2 checks passed
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.

2 participants