Skip to content

Lrpar diagnostics #567

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Conversation

ratmice
Copy link
Collaborator

@ratmice ratmice commented May 19, 2025

This patch moves the diagnostics.rs from nimbleparse, to lrpar, so it can be used in CTParserBuilder.
It modifies the YaccGrammarError and CTConflictsError and some portions of the HeaderError error printing to inline the source snippets in the error messages.

There is a couple of things to note:

  • I haven't done a great job of manually going through and looking at the output of each error yet.
  • I wasn't terribly fond of CTConflictsError changes here, I'll explain the hurdles I ran into below.

CTConflictsError{stable} is used via downcast_ref in the testsuite, and the SpannedDiagnosticsFormatter only borrows the string, owned by build() which causes lifetime problems with trying to put the SDF into the CTConflictsError with the source string and the formatter, thus it gets pre-formatted into a String and embed that in the CTConflictsError.

Further, since the source string contains temp filenames when run during the testsuite, it is difficult to do some form of string comparison with what we expect the error to output.

Thus the somewhat weird implementation where we also include the otherwise unused stable, and jump through clippy hoops to keep the existing tests working, without a string comparison replacement. Otherwise I probably would likely have just used ErrorString, and removed CTConflictsError.

@ratmice ratmice marked this pull request as draft May 19, 2025 04:04
@ratmice
Copy link
Collaborator Author

ratmice commented May 19, 2025

I was thinking about it and, while I anticipate not having any issues with CTLexerBuilder, it still isn't exactly trivial (in places where we need both lex and yacc sources),
so probably worth knowing what we're going to run into end up with there before making any decisions on this.

@ratmice
Copy link
Collaborator Author

ratmice commented May 19, 2025

I did the easy part of CTLexerBuilder, what is missing is the missing from lexer and missing from parser,
corresponding to the following nimbleparse formatting code https://github.com/softdevteam/grmtools/blob/master/nimbleparse/src/main.rs#L312-L351

This doesn't appear to be something that we can even achieve via CTParserBuilder callbacks, because this happens after CTParserBuilder::build() which takes self by value.

I'm uncertain, but wondering if it will be enough to add the yacc sources to CTParser in the build() return value.
I think that code in nimbleparse also needs the &YaccGrammar which is only present with conflicts unfortunately.

My hope is that perhaps that can be moved to it's own field outside of conflicts, and returned unconditionally.
I think those two changes will allow parity of errors between nimbleparse and the ctbuilder implementations.
If that is the case it seems like the only semver breakage is regarding CTParser::conflicts() which would need changing but is documented as an unstable interface that may change at any time.

@@ -1547,6 +1528,10 @@ where
}
}

fn indent(s: &str, indent: &str) -> String {
Copy link
Member

Choose a reason for hiding this comment

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

Let's document this one. I suspect we should also reorder the parameters (I misread the calls above!).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is it okay if I reorder the parameters in a separate PR?

Since this function is copy/paste from nimbleparse I'd want to update that as well, and I fear updating all the callers over seems like it might make these patches a bit more messy.

Copy link
Member

Choose a reason for hiding this comment

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

Is it okay if I reorder the parameters in a separate PR?

Works for me!

}

impl PushLine for String {
fn push_line<S: AsRef<str>>(&mut self, s: S) {
Copy link
Member

Choose a reason for hiding this comment

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

How about we call this pushln to mirror println? Also, a quick docstring on the trait to make that clear would be great.

@ltratt
Copy link
Member

ltratt commented May 19, 2025

A couple of small comments but I think we can easily get this in: I appreciate that there are more rough edges we could smooth off, but this already feels like an improvement.

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