Skip to content

Make codegen unstable pub - #672

Open
ratmice wants to merge 7 commits into
softdevteam:masterfrom
ratmice:codegen_pub
Open

ratmice wants to merge 7 commits into
softdevteam:masterfrom
ratmice:codegen_pub

Conversation

@ratmice

@ratmice ratmice commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

This is a first attempt at my remaining todo list.

It tries to make codegen pub, and remove the usage of hidden API in nimbleparse.
It does so by usage of the codegen process and selectively unhiding some API we've worked towards making nicer over this development cycle (the header::Value structure and header errors).

The actual errors that are going to be emitted here will very likely need some work,
But I did do a basic smoke test run of nimbleparse with some erroneous input.

The usage of codegen might seem weird, nimbleparse doesn't actually generating any code...
We kind of use it as a "PreBuilder" for RTParserBuilder which does a couple of things

  1. Defaulting values like %grmtools{yacckind: ...} by parsing the %grmtools section.
  2. Building and obtaining the resulting AST
  3. Checking header keys for validity

As an example of step 3, we can add an invalid key to one of the examples

%grmtools{
    cfgrammar.foo: "There really is no cfgrammar.foo key",
}

$ cargo run --bin nimbleparse -- ./lrpar/examples/calc_parsetree/src/calc.{l,y}

[Error] in ./lrpar/examples/calc_parsetree/src/calc.y
4|     cfgrammar.foo: "There really is no cfgrammar.foo key",
       ^^^^^^^^^^^^^ Unused keys in %grmtools section: cfgrammar.foo

I had hoped to before making this stuff pub, get a version of nimbleparse_lsp ready to test with.
But that was before I had noticed the hidden API usage in nimbleparse. In this regard, there isn't
much that could go wrong in nimbleparse_lsp that wouldn't also run afoul of nimbleparse itself.

I would say that the codegen error handling is not totally pretty here.
I should probably go through CTParserBuilder and double check it's error handling,
to see if I missed any nicer mechanisms. Despite that, it does seem to come out as a net negative lines of code (prior to adding documentation) 👏

@ratmice
ratmice marked this pull request as draft November 29, 2026 12:12
@ratmice

ratmice commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Besides a more exhaustive check of the errors, this probably also needs a read through the rustdoc output.
Because of that I guess that I should mark it as draft.

At a first glance, a lot of the codegen docs are in fact missing.

Comment thread lrpar/src/lib/codegen.rs Resolved
@ratmice

ratmice commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

So I think that is a initial pass over everything, I'll probably leave it draft for a day or two and mull over whether or not I might have forgotten anything and if nothing comes up mark it ready for review.

Comment thread cfgrammar/src/lib/header.rs Resolved
Comment thread cfgrammar/src/lib/header.rs Resolved
@ltratt

ltratt commented Nov 30, 2026

Copy link
Copy Markdown
Member

One thing I haven't fully thought through: because this exposes a lot of previously-internal APIs, I wonder if we might want to document this API as being partly unstable? Some parts seem rock solid to me, and others I haven't really thought about if I'm honest! It might be that the whole kaboodle is good as-is: I'm happy to hear your thoughts!

@ratmice

ratmice commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

I don't really have a problem doing that, I recall we actually have features for that.
it looks like cfgrammar doesn't yet have an unstable_api feature like lrpar/lrlex. But it wouldn't be hard to add one.

It would mean that nimbleparse has to enable that feature, which seems fine (since it is a binary).
I would be hesitant to enable that feature on a library, I think the library I tend to release for nimbleparse_lsp for the extra user defined keys from build.rs wouldn't need to use any of the features here. It just needs the API we added recently to from ASTWithValidityInfo for user defined keys.

My feeling would be that it would be good to make a stabilization issue, and put it in a comment,
asking people to report their experiences good/bad/neutral, and any changes they would make.
Then use that as guidance on whether to fully stabilize anything.

I think the place i'm least happy with the API is around the error handling,
that the errors own Spanned errors, but not all variants themselves are spanned.
It might be nice to have some flexibility to change around error variants more easily without repercussions.

@ratmice

ratmice commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

I think it might require some acrobatics to continue using the API from lrpar/lrlex without enabling the unstable_api feature. Likely a separate #[doc(hidden)] new constructor differentiating internal API from unstable api that requires a feature to use it.

Is that the kind of thing you were thinking about, or something more informal?

@ltratt

ltratt commented Nov 30, 2026

Copy link
Copy Markdown
Member

Is that the kind of thing you were thinking about, or something more informal?

I haven't thought it through very carefully! FWIW I don't have a problem with the grmtools crates using an unstable API between themselves while we wait to see how it goes. If you think that might be a good compromise, it would work for me, but you're deeper into the details here than I am!

@ratmice

ratmice commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

I'll do some playing around with using the unstable_api feature locally and see how it goes.

@ltratt

ltratt commented Nov 30, 2026

Copy link
Copy Markdown
Member

Thanks!

Comment thread nimbleparse/src/main.rs Resolved
Comment thread cfgrammar/src/lib/mod.rs Resolved
@ratmice ratmice changed the title Make codegen pub Make codegen unstable pub Nov 30, 2026
@ltratt

ltratt commented Nov 30, 2026

Copy link
Copy Markdown
Member

I think this one is squashable and mergeable?

Comment thread lrlex/src/lib/mod.rs Outdated
/// This value should only be used within grmtools.
/// External users should *always* use the `unstable_api::UnstableApi` value.
#[doc(hidden)]
pub const HIDDEN_UNSTABLE_API: crate::unstable::UnstableApi = crate::unstable::UnstableApi;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This const value, and type alias should be removed to keep it in line with the unstable module of other crates. This was kind of an experiment that didn't work out, but didn't get removed from lrlex.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Should be fixed in 4a6b52a

@ratmice
ratmice marked this pull request as ready for review December 1, 2026 07:13
@ratmice

ratmice commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator Author

Besides that one little bit of accidental code that needed to be removed, I had to rustfmt.

With that I squashed it down into a few sensible commits, and marked it ready for review.

@ratmice

ratmice commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

I guess, I did want to ask, let me just quote your previous message

One thing I haven't fully thought through: because this exposes a lot of previously-internal APIs, I wonder if we might want to document this API as being partly unstable? Some parts seem rock solid to me, and others I haven't really thought about if I'm honest! It might be that the whole kaboodle is good as-is: I'm happy to hear your thoughts!

I'm curious which parts you've thought less about vs the parts you thought seemed rock solid.

I guess my list of things that give me some hesitation with this api is mostly just the error types.
The second thing I don't know how to solve without linear types.

  1. It would be nice if all the errors implemented Spanned but currently they can't because not all the variants have spans.
  2. There is some error checking during the codegen process, but because of the way rusts affine types work,
    a types can be used 0 or more times. There is no guarantee that codegen proceeds all the way to the finish()
    method. So potentially you can run half-way through the code generation process and grab the AST.

The second is that the error cannot be checked until you've produced the AST, but by that point you can waltz off with the AST into the sunset. I'm not super worried about this second, as this probably is never going to be a commonly used end-user API. So it seems like enough to know you should call finish() even if you could waltz off with the AST half-way through without ever calling finish(). Or that the solution is just "don't do that", it seems pretty hard to prevent without having the API jump through hoops.

@ltratt

ltratt commented Dec 1, 2026

Copy link
Copy Markdown
Member

So potentially you can run half-way through the code generation process and grab the AST.

I guess the only way to solve this would be to rethink the API a bit. IIRC the problem in a sense is that generate is &self but finish is self. Off hand I'm not sure if I remember why we have that split: I guess because one doesn't always want to call generate?

@ratmice

ratmice commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator Author

So potentially you can run half-way through the code generation process and grab the AST.

I guess the only way to solve this would be to rethink the API a bit. IIRC the problem in a sense is that generate is &self but finish is self. Off hand I'm not sure if I remember why we have that split: I guess because one doesn't always want to call generate?

Yep, exactly like what nimbleparse is doing, it doesn't call generate (eventually we could e.g. dump generated source to stdout if that would be useful), rather it wants to take ownership of the YaccGrammar, and pass that on to RTParserBuilder.

I think one way we might approach fixing it is that we remove the ability to get an ASTWithValidityInfo from
ParserBuildEnv, and have a function in ParserCodegen which given a &ParserBuildEnv returns an &ASTWithValidityInfo. It's actually ParserBuildGen::code_generator() where these "late" errors
occur during the call to self.check_unused_grmtools_header_values()?;

So that might ensure that these errors are checked before people can run off with the ast.
(Note: a guess, untested -- under the assumption we don't need the AST before the call to codegen(), I can't see any reason why it wouldn't work though)

@ratmice

ratmice commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

I played around with the above, and did get it working, besides moving the ast_with_validity_info method to codegen, I also had to add a warnings() method to build_env. That seems to be the only reason we're ever
grabbing the AST early, so we can handle all the warnings and errors at the same time.

Previously you could get an `ASTWithValidityInfo` before calling
`ParserBuildEnv::code_generator` where errors in the `ast.grmtools_section`
 were checked.

This patch delays the ability to obtain an `ASTWithValidityInfo` until later.
Add a `warnings()` method to `ParserBuildEnv` that returns the ast's warnings,
so that you can still handle warnings alongside errors.
@ratmice

ratmice commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

I went ahead and pushed that as 0fea24b so you could take a look.

Comment thread lrpar/src/lib/codegen.rs Outdated
let mut unrecognized_keys = Vec::new();
unrecognized_keys.extend(
self.ast_with_validity_info()
self.ast_with_validity_info

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

An alternate (perhaps better) fix might be to move this check so it happens earlier.
It looks like the place to do that would be in ParserSrcEnv::build_env().

Then we could leave ParserBuildEnv::ast_with_validity_info() as it was.

@ratmice ratmice Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

In d5d4b6f I went ahead and moved this error check so it happens earlier and reverted some of the patch that moved the AST getter to happen late.

In doing so I noticed one spot we were printing errors without spans, so I added that case to the match.

}
Box::<dyn Error>::from(ErrorString(out))
}
ref e @ ParserSrcEnvError::GrmtoolsSectionUnusedKeys(ref errs) => {

@ratmice ratmice Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It would be nice if we could collapse these 3 match case branches for error printing down to two cases, one for spanned errors, and one for non-spanned errors.

  1. This error doesn't currently implement the Spanned trait
  2. We'd need ParserSrcEnvError to have something that returns impl Into<Option<dyn Spanned>> for ParserSrcEnvError something like an impl Iterator<Item=dyn Spanned + Error>

That might be best left for a future cleanup?

This branch has not been deployed

No deployments
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