Add API for user defined grmtools section entries in GrammarAST - #667
Add API for user defined grmtools section entries in GrammarAST#667ratmice wants to merge 14 commits into
Conversation
| pub fn grmtools_section_value_for_crate( | ||
| &mut self, | ||
| crate_name: &str, | ||
| key_name: &str, |
There was a problem hiding this comment.
I was kind of undecided whether this should do the format!({crate_name}.{key_name}), or
just take the key as a single string, including the crate name.
I just picked one, randomly based on the unused takes the crate name as a separate parameter,
but no strong opinions.
| let build_env = src_env | ||
| .build_env(ParserBuildEnvArgs::new().mod_name(Some("test_module"))) | ||
| .unwrap(); | ||
| build_env |
There was a problem hiding this comment.
This may still need some work, on how to expose this check in a way that will work with downstream crates that want to use the code_generator. But this stuff is all private still anyways.
The thing to note is that this check still happens in CTParserBuilder so code_generator is not automatically checking for unused keys as a byproduct of code generation.
Hence having to reimplement the checks in these test cases as calls to build_env.check_unused...
There was a problem hiding this comment.
Regardless of whether downstream crates automatically get checking for unused keys,
they can now perform the check themselves to obtain the same results as CTParserBuilder?
There was a problem hiding this comment.
Having thought about it, I'm in the coping stage where it feels like there isn't much the code generator can do about this. It doesn't know at what time all the header values have been resolved, since it gives mutable access to the header via PaserBuildEnv::header_mut(), currently the check_unused_header_keys_for_crate function is also on PaserBuildEnv.
Edit: Oops, disregard the paragraph below. I was looking at the lrlex::codegen, in lrpar::codegen we actually need to mutate the header/mark keys as used after the call to code_generator().
The reason we need it after is because of the inspect_rt callback which we use to implement test_files, it builds a RTParserBuilder, and marks the test_files entry as used.
It is worth noting though that test_files is explicitly marked as experimental in the book here.
Edit2: Given the above perhaps we code do the checking in ParserCodegen::generate it takes a ParserBuildEnv and returns a Result. This provides a place to do the check, but still has some open questions about Header<Location> vs Header<Span>, and which header to perform the check on during codegen.
It looks like it could work if we actually made this a private function, and called it from ParserBuildEnv::code_generator. That is the next point after all header values should have been marked used. Presumably we'd need some way perhaps in ParserBuildEnvArgs to pass it a list of crates to check.
|
It took me a while thinking about how to handle that there are multiple header instances across various crates. I came to the conclusion that it isn't actually a problem, because these The key to this working is that each of these crates is going to be marking and checking the keys for the crates they know in the header they'll check, so we don't really have much of an issue with a key being marked used in one But I don't imagine that is either an obvious as either problem or solution. There's really two ways this could be a problem:
The second case actually will currently fail as we're only calling mark_used in the Edit: I'll work on a fix for this second case, and feel like we can just accept that we won't do the first? So i'm wondering if we just perform the checks e.g. during codegen, and allow inconsistent results if a user tries to call |
|
Marking as ready, because I think I've covered all the issues I can think of as best I can. |
|
I've tried a couple of times to write a sort of high level comment about design issues, but it keeps turning into a textbook/great american novel. This was mostly talking about duplication between e.g. There is something to say though about the mechanisms provided by this patch, which is they currently aren't exposing any of this or making it usable from within In nimbleparse_lsp v2, we're basically calling One of the unforseen problems, which I guess I am encountering now, and didn't anticipate is that the intent was for the public API to kind of not expose the It hadn't really occurred to me how/whether we plan on exposing this via |
|
I'm going to go ahead and mark most of my waffling review comments as resolved for now. It just feels like they're probably more distracting than helpful at this point. |
| /// If the entry is found it marks the key as `used`, for the purposes of `unused_header_keys_for_crate`. | ||
| pub fn header_value_for_crate( | ||
| &mut self, | ||
| crate_name: &str, |
There was a problem hiding this comment.
Dumb question: do we even need to force two variables (crate_name and key_name) on the user? Would key: &str make sense alone?
There was a problem hiding this comment.
We don't, I made the same comment in a review above too, for a different function.
I kind of flipped a coin, and did separate crate names and keys, due to the separate crate name elsewhere
like check_unused_... where the key name isn't passed in at all.
It didn't seem like a strong argument then, happy to change it.
There was a problem hiding this comment.
I think that if we use a single string, we get a nicer API in #667 (comment) too? Warning: I might be wrong.
| let unused_keys = self.header.unused(); | ||
| /// Returns an error if any unused keys specified in a `%grmtools` directive that begin with a | ||
| /// `crate_name.` prefix for `crate_name` value are found. If the `crate_name` is None returns | ||
| /// an error if any unused keys with no crate prefix specified are found. |
There was a problem hiding this comment.
I wonder if instead of crate_name we have prefix, where "" means "match all" and any non-empty string implicitly ends with . (we don't want "x" to match "xy.z")?
There was a problem hiding this comment.
I'm not exactly certain how to interpret the suggestion,
One thing to note is that the current behavior, None is not actually "match all", but "match anything without any crate prefix".
Are you saying we should have
Some("")should match all keys.Noneshould match keys with no prefix.Some("foo")should match "foo.*"
Or did you just mean to replace the option with just the empty string?
There was a problem hiding this comment.
Ah! OK, so I think we had this discussion recently (or at least I'm hallucinating something similar). I think intuition approach is to say "everything has a prefix; for legacy keys, we add the prefix "grmtools" (or "lrpar" or ...)". So "match" then becomes "match a prefix". So match("") matches everything; match("grmools") matches grmtools' keys, and so on. Again, warning: I haven't thought deeply about this and this might not be the best API.
There was a problem hiding this comment.
Ahh, maybe we need to handle unknown unprefixed keys elsewhere then.
The way it currently works, unprefixed keys can squeek by, because the prefix is only added for known keys.
So if you have a key like unknown: "foo", it can end up in the header because it doesn't match any "grmtools", "lrpar", or "lrlex" keys.
Perhaps that should be an error when we try to insert the key into the Header instead of handling it at this point where they key just happens to end up not being used by anything.
This is kind of a "lazy" way to handle that error of an unknown key with no crate prefix.
There was a problem hiding this comment.
Aha, yes, I think unprefixed-keys-that-aren't-recognised-legacy-keys are probably best banned. Forcing namespacing on users seems like a good idea (at least right now).
There was a problem hiding this comment.
Okay, so the next question is do we actually want/need the match-all behavior?
I guess I see how it is useful for users to call on their own grammars to ensure all their keys are valid.
I had been looking at it from the perspective of grmtools, and nimbleparse_lsp, where there
maybe crate specific keys that these crates can't know about. So they can't perform the checks
for external crates.
I suppose that end-user checking is enough motiviation to add it.
There was a problem hiding this comment.
I think the "match all" behaviour falls out of the hat, so unless we ban it, it Just Works (TM). I think it's useful to have it, at least from grmtools internal purposes? But we could start without it (i.e. ban it), and only unban it later if we find someone has a real use case for it: I'm fine with doing that.
There was a problem hiding this comment.
I think there is a use for it, as an end-user you can typo the crate name which will get by the per-crate checks. So if you do the match-all, it'll catch that. But it's only really possible to do if you are certain all the keys have been used.
For instance if you want to add that check to build.rs in a grammar that contains nimbleparse_lsp keys, you'll need to mark them as used yourself (or I can provide a helper function).
That is to say this key checking becomes somewhat of a distributed problem.
This is a second attempt at exposing querying of entries defined by downstream crates stored in the
%grmtoolssection.The first attempt was #665, this attempt is extended to allow crates to query for unused keys defined within their namespace. And is overall simpler due to being based on the new
header::Valuetype work done in #666 .