Skip to content

Commit

Permalink
wip: Use derivative to skip bounds on a type marker
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Apr 22, 2024
1 parent 7d67a4d commit a034f8a
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 32 deletions.
53 changes: 38 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ clap = { version = "4.5.4", features = ["derive", "wrap_help"] }
clap_complete = { version = "4.5.1" }
console = { version = "0.15.8" }
derive-new = { version = "0.6.0" }
derivative = { version = "2.2.0" }
ignore = { version = "0.4.22" }
enum_dispatch = { version = "0.3.12" }
indexmap = { version = "2.2.3", features = ["serde"] }
Expand Down
3 changes: 1 addition & 2 deletions crates/codegen/parser/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ pub mod napi_interface;
// }

mod metaslang_cst {
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)] // But why?
pub struct KindTypes;
pub enum KindTypes {}
impl metaslang_cst::KindTypes for KindTypes {
type NonTerminalKind = crate::kinds::RuleKind;
type TerminalKind = crate::kinds::TokenKind;
Expand Down
3 changes: 1 addition & 2 deletions crates/codegen/parser/runtime/src/mod_for_destination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ pub mod queries;
pub mod napi_interface;

mod metaslang_cst {
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)] // But why?
pub struct KindTypes;
pub enum KindTypes {}
impl metaslang_cst::KindTypes for KindTypes {
type NonTerminalKind = crate::kinds::RuleKind;
type TerminalKind = crate::kinds::TokenKind;
Expand Down
1 change: 1 addition & 0 deletions crates/metaslang/cst/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description = "Trees, cursors, queries"

[dependencies]
enum_dispatch = { workspace = true }
derivative = { workspace = true }
nom = { workspace = true }
serde = { workspace = true }
strum = { workspace = true }
23 changes: 19 additions & 4 deletions crates/metaslang/cst/src/cst.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
use std::rc::Rc;

use derivative::Derivative;
use serde::Serialize;

use crate::cursor::Cursor;
use crate::text_index::TextIndex;
use crate::{KindTypes, TerminalKind as _};

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[derive(Clone, Serialize, Derivative)]
#[derivative(Debug(bound = ""), PartialEq(bound = ""), Eq(bound = ""))]
pub struct TerminalNode<T: KindTypes> {
pub kind: T::TerminalKind,
pub text: String,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[derive(Clone, Serialize, Derivative)]
#[derivative(PartialEq(bound = ""), Eq(bound = ""), Debug(bound = ""))]
pub struct NonTerminalNode<T: KindTypes> {
pub kind: T::NonTerminalKind,
pub text_len: TextIndex,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub children: Vec<LabeledNode<T>>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[derive(Derivative, Serialize)]
#[derivative(
Debug(bound = ""),
Clone(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
pub enum Node<T: KindTypes> {
Rule(Rc<NonTerminalNode<T>>),
Token(Rc<TerminalNode<T>>),
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[derive(Serialize, Derivative)]
#[derivative(
Debug(bound = ""),
Clone(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
pub struct LabeledNode<T: KindTypes> {
pub label: Option<T::EdgeKind>,
pub node: Node<T>,
Expand Down
5 changes: 4 additions & 1 deletion crates/metaslang/cst/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use std::rc::Rc;

use derivative::Derivative;

use crate::cst::{LabeledNode, Node, NonTerminalNode};
use crate::text_index::{TextIndex, TextRange};
use crate::KindTypes;
Expand All @@ -18,7 +20,8 @@ struct PathAncestor<T: KindTypes> {
/// A cursor that can traverse a CST.
///
/// Nodes are visited in a DFS pre-order traversal.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Derivative, Debug, PartialEq, Eq)]
#[derivative(Clone(bound = ""))]
pub struct Cursor<T: KindTypes> {
/// The parent path of this cursor
parent: Option<Rc<PathAncestor<T>>>,
Expand Down
2 changes: 1 addition & 1 deletion crates/metaslang/cst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait TerminalKind: Kind {
pub trait NonTerminalKind: Kind {}
pub trait EdgeKind: Kind {}

pub trait KindTypes: Clone + PartialEq {
pub trait KindTypes {
type NonTerminalKind: NonTerminalKind;
type TerminalKind: TerminalKind;
type EdgeKind: EdgeKind;
Expand Down
11 changes: 8 additions & 3 deletions crates/metaslang/cst/src/query/model.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::fmt;
use std::rc::Rc;

use derivative::Derivative;

// This crate is copied to another crate, so all imports should be relative
use crate::KindTypes;

#[derive(Clone)]
#[derive(Derivative)]
#[derivative(Clone(bound = ""))]
pub struct Query<T: KindTypes>(pub(super) Matcher<T>);

impl<T: KindTypes> Query<T> {
Expand All @@ -19,7 +22,8 @@ impl<T: KindTypes> fmt::Display for Query<T> {
}
}

#[derive(Clone)]
#[derive(Derivative)]
#[derivative(Clone(bound = ""))]
pub(super) enum Matcher<T: KindTypes> {
Binding(Rc<BindingMatcher<T>>),
Node(Rc<NodeMatcher<T>>),
Expand Down Expand Up @@ -86,7 +90,8 @@ impl<T: KindTypes> fmt::Display for Matcher<T> {
}
}

#[derive(Copy, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, Derivative)]
#[derivative(PartialEq(bound = ""), Eq(bound = ""))]
pub(super) enum Kind<T: KindTypes> {
Rule(T::NonTerminalKind),
Token(T::TerminalKind),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a034f8a

Please sign in to comment.