Skip to content
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

Conversation

AntonyBlakey
Copy link
Contributor

No description provided.

Copy link

changeset-bot bot commented Apr 9, 2024

⚠️ No Changeset found

Latest commit: 64f8ab0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@AntonyBlakey AntonyBlakey force-pushed the AntonyBlakey/generify-cst-cursor-query branch from 6ed022d to c855d7e Compare April 9, 2024 09:43
@AntonyBlakey AntonyBlakey marked this pull request as ready for review April 9, 2024 09:47
@AntonyBlakey AntonyBlakey requested a review from a team as a code owner April 9, 2024 09:47
@AntonyBlakey AntonyBlakey self-assigned this Apr 9, 2024
@AntonyBlakey AntonyBlakey force-pushed the AntonyBlakey/generify-cst-cursor-query branch 2 times, most recently from fd40ef7 to 7d67a4d Compare April 9, 2024 11:39
crates/metaslang/cst/Cargo.toml Outdated Show resolved Hide resolved
crates/codegen/parser/runtime/src/mod_for_destination.rs Outdated Show resolved Hide resolved
crates/codegen/parser/runtime/src/mod_for_destination.rs Outdated Show resolved Hide resolved

mod metaslang_cst {
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)] // But why?
pub struct KindTypes;
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

let text_len = children.iter().map(|node| node.text_len()).sum();

Self::Rule(Rc::new(RuleNode {
Self::Rule(Rc::new(NonTerminalNode::<T> {
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but done after f2f

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 }))
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as above, so below

crates/codegen/parser/runtime/src/lib.rs Outdated Show resolved Hide resolved
+ std::fmt::Display
+ std::fmt::Debug
+ serde::Serialize
+ for<'a> std::convert::TryFrom<&'a str, Error = strum::ParseError>
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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>;
}

Copy link
Collaborator

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.

Copy link
Contributor Author

@AntonyBlakey AntonyBlakey May 13, 2024

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@AntonyBlakey AntonyBlakey force-pushed the AntonyBlakey/generify-cst-cursor-query branch from a0e5324 to d5a687b Compare April 25, 2024 11:48
@AntonyBlakey AntonyBlakey force-pushed the AntonyBlakey/generify-cst-cursor-query branch 2 times, most recently from 2784082 to f2ffeb6 Compare May 14, 2024 08:04
@AntonyBlakey AntonyBlakey force-pushed the AntonyBlakey/generify-cst-cursor-query branch from f2ffeb6 to aa12506 Compare May 15, 2024 07:25
@AntonyBlakey AntonyBlakey force-pushed the AntonyBlakey/generify-cst-cursor-query branch from aa12506 to 64f8ab0 Compare May 15, 2024 07:41
Copy link
Collaborator

@OmarTawfik OmarTawfik left a 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.

OmarTawfik added a commit to OmarTawfik-forks/slang that referenced this pull request May 16, 2024
This rebases NomicFoundation#930 after applying recent infra changes.
OmarTawfik added a commit to OmarTawfik-forks/slang that referenced this pull request May 16, 2024
This rebases NomicFoundation#930 after applying recent infra changes.
OmarTawfik added a commit to OmarTawfik-forks/slang that referenced this pull request May 16, 2024
This rebases NomicFoundation#930 after applying recent infra changes.
github-merge-queue bot pushed a commit that referenced this pull request May 17, 2024
This rebases #930 after applying recent infra changes.
auto-merge was automatically disabled May 17, 2024 08:50

Pull request was closed

@AntonyBlakey AntonyBlakey deleted the AntonyBlakey/generify-cst-cursor-query branch May 22, 2024 17:31
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.

3 participants