-
Notifications
You must be signed in to change notification settings - Fork 18
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
Generify CST/Cursor/Query #930
Generify CST/Cursor/Query #930
Conversation
|
6ed022d
to
c855d7e
Compare
fd40ef7
to
7d67a4d
Compare
|
||
mod metaslang_cst { | ||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)] // But why? | ||
pub struct KindTypes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever we want to use types as markers, it's slightly better to use an uninhabitable enum pub enum KindTypes {}
as this makes it impossible to construct the value (by mistake), but can still be used in the generic argument position.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
crates/metaslang/cst/src/cst.rs
Outdated
let text_len = children.iter().map(|node| node.text_len()).sum(); | ||
|
||
Self::Rule(Rc::new(RuleNode { | ||
Self::Rule(Rc::new(NonTerminalNode::<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost all of the types can be easily inferred in the return position; specifying turbofish ::<T>
here is, I feel, adding unnecessary visual noise that distract slightly from the actual logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is that 'almost all' comment. I prefer a more regular, obvious form that doesn't require the reader to be aware of just when the type can be inferred. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but done after f2f
crates/metaslang/cst/src/cst.rs
Outdated
pub fn token(kind: TokenKind, text: String) -> Self { | ||
Self::Token(Rc::new(TokenNode { kind, text })) | ||
pub fn token(kind: T::TerminalKind, text: String) -> Self { | ||
Self::Token(Rc::new(TerminalNode::<T> { kind, text })) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto here and in all of the similar places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as above, so below
+ std::fmt::Display | ||
+ std::fmt::Debug | ||
+ serde::Serialize | ||
+ for<'a> std::convert::TryFrom<&'a str, Error = strum::ParseError> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of relying on strum
in our public API, and forcing users to use it, I wonder if it is possible to use the standard std::str::FromStr
instead, and keep strum
as an internal implementation detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has to be TryFrom
because it is a partial function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FromStr
also returns a Result<>
:
https://doc.rust-lang.org/std/str/trait.FromStr.html
pub trait FromStr: Sized {
type Err;
fn from_str(s: &str) -> Result<Self, Self::Err>;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and is also typically used for many other rust types via "value".parse()
.
For us, we can use #[derive(strum::EnumString)]
which auto-derives std::str::FromStr
on the enum, but that is an implementation detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem is that the Err
associated type in FromStr
needs to implement std::fmt::Debug
, but it isn't possible to constrain the associated type using stable Rust.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either you or @Xanewok may have a solution - the only one I have propagates the constraint on the associated type all through the codebase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. What do you think about simply using String
as the error type for FromStr
? this also has the advantage of providing an actual error message, instead of the (only) Unit variant strum::ParseError::VariantNotFound
. The API will then be idiomatic, without exposing/depending on the highly-specific strum
.
Not blocking though if it is not feasible. Just a minor suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that, but no joy, because the specification of the Err
type still causes difficulties. My preference is to use an associated type constraint, as soon as it is stabilised. It is a significant improvement to the type system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take quick look if we can simplify this.
a0e5324
to
d5a687b
Compare
b090afe
to
2784082
Compare
2784082
to
f2ffeb6
Compare
crates/codegen/language/tests/src/fail/parsing/duplicate_map_key/test.stderr
Outdated
Show resolved
Hide resolved
f2ffeb6
to
aa12506
Compare
aa12506
to
64f8ab0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great. I will rebase this on the latest main
and send you an update!
Thanks.
This rebases NomicFoundation#930 after applying recent infra changes.
This rebases NomicFoundation#930 after applying recent infra changes.
This rebases NomicFoundation#930 after applying recent infra changes.
This rebases #930 after applying recent infra changes.
Pull request was closed
No description provided.